Skip to content

Commit

Permalink
Merge pull request #374 from snyk-tech-services/fix/getTarget
Browse files Browse the repository at this point in the history
fix: projectList filters should be in the query
  • Loading branch information
mathild3r authored Oct 21, 2022
2 parents 804150d + f33254b commit 45c7146
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 8 deletions.
12 changes: 8 additions & 4 deletions src/lib/api/org/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,17 @@ async function getProject(
filters?: ProjectsFilters,
nextPageLink?: string,
): Promise<{ projects: SnykProject[]; next?: string }> {
const url = nextPageLink
? nextPageLink
: `/orgs/${orgId.trim()}/projects?version=2022-06-08~beta`;

const query = qs.stringify({
version: '2022-09-15~beta',
...filters,
});

const url = nextPageLink ?? `/orgs/${orgId.trim()}/projects?${query}`;

const res = await requestManager.request({
verb: 'get',
url: url,
body: JSON.stringify(filters),
useRESTApi: true,
});

Expand Down
4 changes: 1 addition & 3 deletions src/scripts/generate-imported-targets-from-snyk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ export async function generateSnykImportedTargets(
? await getAllOrgs(requestManager, groupId)
: [{ id: orgId! }];
const failedOrgs: SnykOrg[] = [];
const projectFilters = {
origin: integrationTypes.length > 1 ? undefined : integrationTypes[0],
};
const projectFilters = integrationTypes.length > 1 ? undefined: { origin: integrationTypes[0] };
await pMap(
groupOrgs,
async (org: SnykOrg) => {
Expand Down
2 changes: 1 addition & 1 deletion test/delete-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export function deleteFiles(logs: string[]): void {
// do nothing
}
});
}
}
18 changes: 18 additions & 0 deletions test/lib/__snapshots__/org.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`listProjects list the projects in a given Org with filter - mock 1`] = `
Object {
"org": Object {
"id": "74e2f385-a54f-491e-9034-76c53e72927a",
},
"projects": Array [
Object {
"branch": "master",
"created": "2021-05-29T09:50:54.014Z",
"id": "331ede0a-de94-456f-b788-166caeca58bf",
"name": "snyk/goof",
"origin": "github",
"type": "maven",
},
],
}
`;

exports[`listTargets list the targets in a given Org with filter - mock 1`] = `
Object {
"targets": Array [
Expand Down
92 changes: 92 additions & 0 deletions test/lib/org.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,98 @@ describe('listProjects', () => {
branch: expect.any(String),
});
}, 5000);
it('list the projects in a given Org with filter - mock', async () => {
const req = jest.spyOn(requestManager, 'request');

req.mockResolvedValue({
statusCode: 200,
data: {
jsonapi: { version: '1.0' },
data: [
{
attributes: {
businessCriticality: ['medium'],
created: '2021-05-29T09:50:54.014Z',
environment: ['external', 'hosted'],
lifecycle: ['production'],
name: 'snyk/goof',
origin: 'github',
status: 'active',
tags: [
{
key: 'tag-key',
value: 'tag-value',
},
],
targetReference: 'master',
type: 'maven',
},
id: '331ede0a-de94-456f-b788-166caeca58bf',
relationships: {
importingUser: {
data: {
id: 'e661d4ef-5ad5-4cef-ad16-5157cefa83f5',
type: 'org',
},
links: {
self: {
href: '/v3/orgs/e661d4ef-5ad5-4cef-ad16-5157cefa83f5',
},
},
},
org: {
data: {
id: 'e661d4ef-5ad5-4cef-ad16-5157cefa83f5',
type: 'org',
},
links: {
self: {
href: '/v3/orgs/e661d4ef-5ad5-4cef-ad16-5157cefa83f5',
},
},
},
owner: {
data: {
id: 'e661d4ef-5ad5-4cef-ad16-5157cefa83f5',
type: 'org',
},
links: {
self: {
href: '/v3/orgs/e661d4ef-5ad5-4cef-ad16-5157cefa83f5',
},
},
},
target: {
data: {
id: 'e661d4ef-5ad5-4cef-ad16-5157cefa83f5',
type: 'org',
},
links: {
self: {
href: '/v3/orgs/e661d4ef-5ad5-4cef-ad16-5157cefa83f5',
},
},
},
type: 'projects',
},
},
],
links: {},
},
});

const res = await listProjects(requestManager, ORG_ID, {
targetId: 'e661d4ef-5ad5-4cef-ad16-5157cefaxxx',
});
expect(req).toBeCalledWith({
body: undefined,
url:
'/orgs/74e2f385-a54f-491e-9034-76c53e72927a/projects?version=2022-09-15~beta&targetId=e661d4ef-5ad5-4cef-ad16-5157cefaxxx',
useRESTApi: true,
verb: 'get',
});
expect(res).toMatchSnapshot();
}, 5000);
});

describe('listTargets', () => {
Expand Down
1 change: 1 addition & 0 deletions test/system/list:imported.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ describe('`snyk-api-import list:imported <...>`', () => {
'imported-targets.log',
)}`,
);
expect(stdout.trim()).toContain("Extracted 2 unique targets from 2 projects from org 74e2f385-a54f-491e-9034-76c53e72927a");
deleteFiles([path.resolve(__dirname, IMPORT_LOG_NAME)]);
},
).on('exit', (code) => {
Expand Down

0 comments on commit 45c7146

Please sign in to comment.