You've already forked sap-jenkins-library
							
							
				mirror of
				https://github.com/SAP/jenkins-library.git
				synced 2025-10-30 23:57:50 +02:00 
			
		
		
		
	detectExecuteScan: fix project version limiting api call (#3754)
* fix project version limiting issue * add tests for detectExecute
This commit is contained in:
		| @@ -58,7 +58,7 @@ func newBlackduckMockSystem(config detectExecuteScanOptions) blackduckSystem { | ||||
| 		responseBodyForURL: map[string]string{ | ||||
| 			"https://my.blackduck.system/api/tokens/authenticate":                                                                               authContent, | ||||
| 			"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest":                                                                   projectContent, | ||||
| 			"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions":                                            projectVersionContent, | ||||
| 			"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions?limit=100&offset=0":                         projectVersionContent, | ||||
| 			"https://my.blackduck.system/api/projects/5ca86e11/versions/a6c94786/components?limit=999&offset=0":                                 componentsContent, | ||||
| 			"https://my.blackduck.system/api/projects/5ca86e11/versions/a6c94786/vunlerable-bom-components?limit=999&offset=0":                  vulnerabilitiesContent, | ||||
| 			"https://my.blackduck.system/api/projects/5ca86e11/versions/a6c94786/components?filter=policyCategory%3Alicense&limit=999&offset=0": componentsContent, | ||||
|   | ||||
| @@ -225,7 +225,9 @@ func (b *Client) GetProjectVersion(projectName, projectVersion string) (*Project | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	respBody, err := b.sendRequest("GET", versionPath, map[string]string{}, nil, headers) | ||||
| 	//While sending a request to 'versions', get all 100 versions from that project by setting limit=100 | ||||
| 	//More than 100 project versions is currently not supported/recommended by Blackduck | ||||
| 	respBody, err := b.sendRequest("GET", versionPath, map[string]string{"offset": "0", "limit": "100"}, nil, headers) | ||||
| 	if err != nil { | ||||
| 		return nil, errors.Wrapf(err, "failed to get project version '%v:%v'", projectName, projectVersion) | ||||
| 	} | ||||
|   | ||||
| @@ -156,9 +156,9 @@ func TestGetProjectVersion(t *testing.T) { | ||||
| 	t.Run("success", func(t *testing.T) { | ||||
| 		myTestClient := httpMockClient{ | ||||
| 			responseBodyForURL: map[string]string{ | ||||
| 				"https://my.blackduck.system/api/tokens/authenticate":                                    authContent, | ||||
| 				"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest":                        projectContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions": projectVersionContent, | ||||
| 				"https://my.blackduck.system/api/tokens/authenticate":                                                       authContent, | ||||
| 				"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest":                                           projectContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions?limit=100&offset=0": projectVersionContent, | ||||
| 			}, | ||||
| 			header: map[string]http.Header{}, | ||||
| 		} | ||||
| @@ -168,7 +168,7 @@ func TestGetProjectVersion(t *testing.T) { | ||||
| 		assert.Equal(t, "1.0", projectVersion.Name) | ||||
| 		assert.Equal(t, "https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions/a6c94786-0ee6-414f-9054-90d549c69c36", projectVersion.Metadata.Href) | ||||
| 		headerExpected := http.Header{"Authorization": []string{"Bearer bearerTestToken"}, "Accept": {"application/vnd.blackducksoftware.project-detail-4+json"}} | ||||
| 		assert.Equal(t, headerExpected, myTestClient.header["https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions"]) | ||||
| 		assert.Equal(t, headerExpected, myTestClient.header["https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions?limit=100&offset=0"]) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("failure - project not found", func(t *testing.T) { | ||||
| @@ -193,7 +193,7 @@ func TestGetProjectVersion(t *testing.T) { | ||||
| 				"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest": projectContent, | ||||
| 			}, | ||||
| 			errorMessageForURL: map[string]string{ | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions": "not found", | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions?limit=100&offset=0": "not found", | ||||
| 			}, | ||||
| 			header: map[string]http.Header{}, | ||||
| 		} | ||||
| @@ -207,7 +207,7 @@ func TestGetProjectVersion(t *testing.T) { | ||||
| 			responseBodyForURL: map[string]string{ | ||||
| 				"https://my.blackduck.system/api/tokens/authenticate":             authContent, | ||||
| 				"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest": projectContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions": `{ | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions?limit=100&offset=0": `{ | ||||
| 					"totalCount": 0, | ||||
| 					"items": [] | ||||
| 				}`, | ||||
| @@ -222,9 +222,9 @@ func TestGetProjectVersion(t *testing.T) { | ||||
| 	t.Run("failure - unmarshalling", func(t *testing.T) { | ||||
| 		myTestClient := httpMockClient{ | ||||
| 			responseBodyForURL: map[string]string{ | ||||
| 				"https://my.blackduck.system/api/tokens/authenticate":                                    authContent, | ||||
| 				"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest":                        projectContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions": "", | ||||
| 				"https://my.blackduck.system/api/tokens/authenticate":                                                       authContent, | ||||
| 				"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest":                                           projectContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions?limit=100&offset=0": "", | ||||
| 			}, | ||||
| 			header: map[string]http.Header{}, | ||||
| 		} | ||||
| @@ -238,9 +238,9 @@ func TestGetVulnerabilities(t *testing.T) { | ||||
| 	t.Run("success", func(t *testing.T) { | ||||
| 		myTestClient := httpMockClient{ | ||||
| 			responseBodyForURL: map[string]string{ | ||||
| 				"https://my.blackduck.system/api/tokens/authenticate":                                    authContent, | ||||
| 				"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest":                        projectContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions": projectVersionContent, | ||||
| 				"https://my.blackduck.system/api/tokens/authenticate":                                                       authContent, | ||||
| 				"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest":                                           projectContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions?limit=100&offset=0": projectVersionContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions/a6c94786-0ee6-414f-9054-90d549c69c36/vunlerable-bom-components?limit=999&offset=0": `{ | ||||
| 					"totalCount": 1, | ||||
| 					"items": [ | ||||
| @@ -270,9 +270,9 @@ func TestGetVulnerabilities(t *testing.T) { | ||||
| 	t.Run("Success - 0 vulns", func(t *testing.T) { | ||||
| 		myTestClient := httpMockClient{ | ||||
| 			responseBodyForURL: map[string]string{ | ||||
| 				"https://my.blackduck.system/api/tokens/authenticate":                                    authContent, | ||||
| 				"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest":                        projectContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions": projectVersionContent, | ||||
| 				"https://my.blackduck.system/api/tokens/authenticate":                                                                                                                      authContent, | ||||
| 				"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest":                                                                                                          projectContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions?limit=100&offset=0":                                                                projectVersionContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions/a6c94786-0ee6-414f-9054-90d549c69c36/vunlerable-bom-components?limit=999&offset=0": `{"totalCount":0,"items":[]}`, | ||||
| 			}, | ||||
| 			header: map[string]http.Header{}, | ||||
| @@ -286,9 +286,9 @@ func TestGetVulnerabilities(t *testing.T) { | ||||
| 	t.Run("Failure - unmarshalling", func(t *testing.T) { | ||||
| 		myTestClient := httpMockClient{ | ||||
| 			responseBodyForURL: map[string]string{ | ||||
| 				"https://my.blackduck.system/api/tokens/authenticate":                                    authContent, | ||||
| 				"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest":                        projectContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions": projectVersionContent, | ||||
| 				"https://my.blackduck.system/api/tokens/authenticate":                                                                                                                      authContent, | ||||
| 				"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest":                                                                                                          projectContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions?limit=100&offset=0":                                                                projectVersionContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions/a6c94786-0ee6-414f-9054-90d549c69c36/vunlerable-bom-components?limit=999&offset=0": "", | ||||
| 			}, | ||||
| 			header: map[string]http.Header{}, | ||||
| @@ -303,9 +303,9 @@ func TestGetComponents(t *testing.T) { | ||||
| 	t.Run("success", func(t *testing.T) { | ||||
| 		myTestClient := httpMockClient{ | ||||
| 			responseBodyForURL: map[string]string{ | ||||
| 				"https://my.blackduck.system/api/tokens/authenticate":                                    authContent, | ||||
| 				"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest":                        projectContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions": projectVersionContent, | ||||
| 				"https://my.blackduck.system/api/tokens/authenticate":                                                       authContent, | ||||
| 				"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest":                                           projectContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions?limit=100&offset=0": projectVersionContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions/a6c94786-0ee6-414f-9054-90d549c69c36/components?limit=999&offset=0": `{ | ||||
| 					"totalCount": 2, | ||||
| 					"items" : [ | ||||
| @@ -330,9 +330,9 @@ func TestGetComponents(t *testing.T) { | ||||
| 	t.Run("Failure - 0 components", func(t *testing.T) { | ||||
| 		myTestClient := httpMockClient{ | ||||
| 			responseBodyForURL: map[string]string{ | ||||
| 				"https://my.blackduck.system/api/tokens/authenticate":                                    authContent, | ||||
| 				"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest":                        projectContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions": projectVersionContent, | ||||
| 				"https://my.blackduck.system/api/tokens/authenticate":                                                       authContent, | ||||
| 				"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest":                                           projectContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions?limit=100&offset=0": projectVersionContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions/a6c94786-0ee6-414f-9054-90d549c69c36/components?limit=999&offset=0": `{ | ||||
| 					"totalCount": 0, | ||||
| 					"items" : []}`, | ||||
| @@ -350,7 +350,7 @@ func TestGetComponents(t *testing.T) { | ||||
| 			responseBodyForURL: map[string]string{ | ||||
| 				"https://my.blackduck.system/api/tokens/authenticate":                                                                                                       authContent, | ||||
| 				"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest":                                                                                           projectContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions":                                                                    projectVersionContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions?limit=100&offset=0":                                                 projectVersionContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions/a6c94786-0ee6-414f-9054-90d549c69c36/components?limit=999&offset=0": "", | ||||
| 			}, | ||||
| 			header: map[string]http.Header{}, | ||||
| @@ -366,9 +366,9 @@ func TestGetComponentsWithLicensePolicyRule(t *testing.T) { | ||||
| 	t.Run("success", func(t *testing.T) { | ||||
| 		myTestClient := httpMockClient{ | ||||
| 			responseBodyForURL: map[string]string{ | ||||
| 				"https://my.blackduck.system/api/tokens/authenticate":                                    authContent, | ||||
| 				"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest":                        projectContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions": projectVersionContent, | ||||
| 				"https://my.blackduck.system/api/tokens/authenticate":                                                       authContent, | ||||
| 				"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest":                                           projectContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions?limit=100&offset=0": projectVersionContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions/a6c94786-0ee6-414f-9054-90d549c69c36/components?filter=policyCategory%3Alicense&limit=999&offset=0": `{ | ||||
| 					"totalCount": 2, | ||||
| 					"items" : [ | ||||
| @@ -396,9 +396,9 @@ func TestGetComponentsWithLicensePolicyRule(t *testing.T) { | ||||
| 	t.Run("Failure - 0 components", func(t *testing.T) { | ||||
| 		myTestClient := httpMockClient{ | ||||
| 			responseBodyForURL: map[string]string{ | ||||
| 				"https://my.blackduck.system/api/tokens/authenticate":                                    authContent, | ||||
| 				"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest":                        projectContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions": projectVersionContent, | ||||
| 				"https://my.blackduck.system/api/tokens/authenticate":                                                       authContent, | ||||
| 				"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest":                                           projectContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions?limit=100&offset=0": projectVersionContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions/a6c94786-0ee6-414f-9054-90d549c69c36/components?filter=policyCategory%3Alicense&limit=999&offset=0": `{ | ||||
| 					"totalCount": 0, | ||||
| 					"items" : []}`, | ||||
| @@ -415,9 +415,9 @@ func TestGetComponentsWithLicensePolicyRule(t *testing.T) { | ||||
| 	t.Run("Failure - unmarshalling", func(t *testing.T) { | ||||
| 		myTestClient := httpMockClient{ | ||||
| 			responseBodyForURL: map[string]string{ | ||||
| 				"https://my.blackduck.system/api/tokens/authenticate":                                    authContent, | ||||
| 				"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest":                        projectContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions": projectVersionContent, | ||||
| 				"https://my.blackduck.system/api/tokens/authenticate":                                                       authContent, | ||||
| 				"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest":                                           projectContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions?limit=100&offset=0": projectVersionContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions/a6c94786-0ee6-414f-9054-90d549c69c36/components?filter=policyCategory%3Alicense&limit=999&offset=0": "", | ||||
| 			}, | ||||
| 			header: map[string]http.Header{}, | ||||
| @@ -433,9 +433,9 @@ func TestGetPolicyStatus(t *testing.T) { | ||||
| 	t.Run("success", func(t *testing.T) { | ||||
| 		myTestClient := httpMockClient{ | ||||
| 			responseBodyForURL: map[string]string{ | ||||
| 				"https://my.blackduck.system/api/tokens/authenticate":                                    authContent, | ||||
| 				"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest":                        projectContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions": projectVersionContent, | ||||
| 				"https://my.blackduck.system/api/tokens/authenticate":                                                       authContent, | ||||
| 				"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest":                                           projectContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions?limit=100&offset=0": projectVersionContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions/a6c94786-0ee6-414f-9054-90d549c69c36/policy-status": `{ | ||||
| 					"overallStatus": "IN_VIOLATION", | ||||
| 					"componentVersionPolicyViolationDetails": { | ||||
| @@ -467,7 +467,7 @@ func TestGetPolicyStatus(t *testing.T) { | ||||
| 			responseBodyForURL: map[string]string{ | ||||
| 				"https://my.blackduck.system/api/tokens/authenticate":                                                                                       authContent, | ||||
| 				"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest":                                                                           projectContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions":                                                    projectVersionContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions?limit=100&offset=0":                                 projectVersionContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions/a6c94786-0ee6-414f-9054-90d549c69c36/policy-status": "", | ||||
| 			}, | ||||
| 			header: map[string]http.Header{}, | ||||
| @@ -483,9 +483,9 @@ func TestGetProjectVersionLink(t *testing.T) { | ||||
| 	t.Run("Success Case", func(t *testing.T) { | ||||
| 		myTestClient := httpMockClient{ | ||||
| 			responseBodyForURL: map[string]string{ | ||||
| 				"https://my.blackduck.system/api/tokens/authenticate":                                    authContent, | ||||
| 				"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest":                        projectContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions": projectVersionContent, | ||||
| 				"https://my.blackduck.system/api/tokens/authenticate":                                                       authContent, | ||||
| 				"https://my.blackduck.system/api/projects?q=name%3ASHC-PiperTest":                                           projectContent, | ||||
| 				"https://my.blackduck.system/api/projects/5ca86e11-1983-4e7b-97d4-eb1a0aeffbbf/versions?limit=100&offset=0": projectVersionContent, | ||||
| 			}, | ||||
| 			header: map[string]http.Header{}, | ||||
| 		} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user