From 90b4930eb65ef44587d50ead442d32f1df129364 Mon Sep 17 00:00:00 2001 From: Mathilde Date: Fri, 21 Oct 2022 13:35:17 +0100 Subject: [PATCH] fix: projectList filters should be in the query --- src/lib/api/org/index.ts | 13 ++- .../generate-imported-targets-from-snyk.ts | 4 +- test/delete-files.ts | 2 +- test/lib/__snapshots__/org.test.ts.snap | 18 ++++ test/lib/org.test.ts | 92 +++++++++++++++++++ test/system/list:imported.test.ts | 1 + 6 files changed, 122 insertions(+), 8 deletions(-) diff --git a/src/lib/api/org/index.ts b/src/lib/api/org/index.ts index 92b07914..984c7504 100644 --- a/src/lib/api/org/index.ts +++ b/src/lib/api/org/index.ts @@ -166,6 +166,7 @@ interface ProjectsFilters { origin?: string; //If supplied, only projects that exactly match this origin will be returned type?: string; //If supplied, only projects that exactly match this type will be returned isMonitored?: boolean; // If set to true, only include projects which are monitored, if set to false, only include projects which are not monitored + targetId?: string; } export async function listProjects( @@ -237,13 +238,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, }); diff --git a/src/scripts/generate-imported-targets-from-snyk.ts b/src/scripts/generate-imported-targets-from-snyk.ts index 3704d35f..76c4f72a 100644 --- a/src/scripts/generate-imported-targets-from-snyk.ts +++ b/src/scripts/generate-imported-targets-from-snyk.ts @@ -116,9 +116,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) => { diff --git a/test/delete-files.ts b/test/delete-files.ts index 961bf37a..c090b266 100644 --- a/test/delete-files.ts +++ b/test/delete-files.ts @@ -8,4 +8,4 @@ export function deleteFiles(logs: string[]): void { // do nothing } }); -} +} \ No newline at end of file diff --git a/test/lib/__snapshots__/org.test.ts.snap b/test/lib/__snapshots__/org.test.ts.snap index 9e712373..3c45e221 100644 --- a/test/lib/__snapshots__/org.test.ts.snap +++ b/test/lib/__snapshots__/org.test.ts.snap @@ -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 [ diff --git a/test/lib/org.test.ts b/test/lib/org.test.ts index 0b068f8f..f1da0501 100644 --- a/test/lib/org.test.ts +++ b/test/lib/org.test.ts @@ -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', () => { diff --git a/test/system/list:imported.test.ts b/test/system/list:imported.test.ts index 0103aad6..f129785e 100644 --- a/test/system/list:imported.test.ts +++ b/test/system/list:imported.test.ts @@ -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) => {