Skip to content

Commit

Permalink
Merge pull request #180 from snyk-tech-services/fix/error-if-no-proje…
Browse files Browse the repository at this point in the history
…ct-ids

fix: throw err if no target ids found & no load all projs
  • Loading branch information
aarlaud authored Feb 14, 2024
2 parents 47923c3 + f05605b commit cb713ae
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,25 +392,29 @@ export class SnykApiClient implements SnykApi {
orgId: string,
projectIdsArray: string[]
): Promise<ProjectsData[]> {
const backendBaseUrl = await this.getApiUrl();
const v3Headers = this.headers;
v3Headers["Content-Type"] = "application/vnd.api+json";
const version = this.getSnykApiVersion();
const projectsForTargetUrl = `${backendBaseUrl}/rest/orgs/${orgId}/projects?ids=${projectIdsArray.join(
"%2C"
)}&limit=100&version=${version}`;
const response = await fetch(projectsForTargetUrl, {
method: "GET",
headers: v3Headers,
});
if (projectIdsArray.length > 0) {
const backendBaseUrl = await this.getApiUrl();
const v3Headers = this.headers;
v3Headers["Content-Type"] = "application/vnd.api+json";
const version = this.getSnykApiVersion();
const projectsForProjectIds = `${backendBaseUrl}/rest/orgs/${orgId}/projects?ids=${projectIdsArray.join(
"%2C"
)}&limit=100&version=${version}`;
const response = await fetch(projectsForProjectIds, {
method: "GET",
headers: v3Headers,
});

if (response.status >= 400 && response.status < 600) {
throw new Error(
`Error ${response.status} - Failed fetching Projects list snyk data`
);
if (response.status >= 400 && response.status < 600) {
throw new Error(
`Error ${response.status} - Failed fetching Projects list snyk data`
);
}
const jsonResponse = await response.json();
return jsonResponse.data as ProjectsData[];
} else {
throw new Error(`Error loading projects by Project IDs.`);
}
const jsonResponse = await response.json();
return jsonResponse.data as ProjectsData[];
}

private async getTargetId(
Expand Down

0 comments on commit cb713ae

Please sign in to comment.