diff --git a/src/lib/poll-import/index.ts b/src/lib/poll-import/index.ts index 088af056..cedf2555 100644 --- a/src/lib/poll-import/index.ts +++ b/src/lib/poll-import/index.ts @@ -4,7 +4,7 @@ import * as sleep from 'sleep-promise'; import * as debugLib from 'debug'; import * as _ from 'lodash'; import * as pMap from 'p-map'; -import { PollImportResponse } from '../types'; +import { PollImportResponse, Project } from '../types'; import { getApiToken } from '../get-api-token'; const debug = debugLib('snyk:poll-import'); @@ -15,7 +15,7 @@ export async function pollImportUrl( locationUrl: string, retryCount = MAX_RETRY_COUNT, retryWaitTime = MIN_RETRY_WAIT_TIME, -): Promise { +): Promise { const apiToken = getApiToken(); debug(`Polling locationUrl=${locationUrl}`); @@ -51,7 +51,11 @@ export async function pollImportUrl( increasedRetryWaitTime, ); } - return res.body; + const projects: Project[] = []; + importStatus.logs.forEach((log) => { + projects.push(...log.projects); + }); + return projects; } catch (error) { debug('Could not complete API import:', error); const err: { @@ -65,21 +69,21 @@ export async function pollImportUrl( export async function pollImportUrls( locationUrls: string[], -): Promise { +): Promise { if (!locationUrls) { throw new Error( 'Missing required parameters. Please ensure you have provided: locationUrls.', ); } const uniqueLocationUrls = _.uniq(locationUrls); - const resArray: PollImportResponse[] = []; + const projectsArray: Project[] = []; await pMap( uniqueLocationUrls, async (locationUrl) => { try { - const res = await pollImportUrl(locationUrl); + const projects = await pollImportUrl(locationUrl); // TODO: log all succeeded into a file - resArray.push(res); + projectsArray.push(...projects); } catch (error) { // TODO: log all failed into a file debug('Failed to poll:', locationUrl); @@ -88,5 +92,5 @@ export async function pollImportUrls( { concurrency: 10 }, ); - return resArray; + return projectsArray; } diff --git a/src/lib/types.ts b/src/lib/types.ts index 0a9ab8d9..b7a05b7a 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -13,8 +13,9 @@ export interface Target { projectKey?: string; // Bitbucket Server repoSlug?: string; // Bitbucket Server id?: number; // Gitlab - owner: string; // Gitlab, GitHub, GH Enterprise, Bitbucket Cloud and Azure Repos - branch: string; // Gitlab, GitHub, GH Enterprise, Bitbucket Cloud and Azure Repos + owner?: string; // Gitlab, GitHub, GH Enterprise, Bitbucket Cloud and Azure Repos + // if not set default branch will be picked + branch?: string; // Gitlab, GitHub, GH Enterprise, Bitbucket Cloud and Azure Repos } export interface FilePath { diff --git a/src/scripts/import-projects.ts b/src/scripts/import-projects.ts index 44eed95e..fb798ea0 100644 --- a/src/scripts/import-projects.ts +++ b/src/scripts/import-projects.ts @@ -19,15 +19,6 @@ export async function ImportProjects( debug(`Loaded ${targets.length} targets to import`); //TODO: validation? const pollingUrls = await importTargets(targets); - const importJobs = await pollImportUrls(pollingUrls); - - const projects: Project[] = []; - importJobs.forEach((job) => { - job.logs.forEach(log => { - projects.push(...log.projects); - }) - }); - - + const projects = await pollImportUrls(pollingUrls); return projects; } diff --git a/test/lib/index.test.ts b/test/lib/index.test.ts index 5bbff190..c38c77ae 100644 --- a/test/lib/index.test.ts +++ b/test/lib/index.test.ts @@ -31,29 +31,14 @@ describe('Single target', () => { branch: 'master', }); expect(pollingUrl).not.toBeNull(); - const importLog = await pollImportUrl(pollingUrl); - expect(importLog).toMatchObject({ - id: expect.any(String), - status: 'complete', - created: expect.any(String), - }); - expect(importLog.logs.length > 1).toBeTruthy(); - expect(importLog.logs[0]).toMatchObject({ - name: expect.any(String), - created: expect.any(String), - status: 'complete', - projects: [ - { - projectUrl: expect.any(String), - success: true, - targetFile: expect.any(String), - }, - ], + const projects = await pollImportUrl(pollingUrl); + expect(projects[0]).toMatchObject({ + projectUrl: expect.any(String), + success: true, + targetFile: expect.any(String), }); // cleanup - importLog.logs.forEach((log) => { - discoveredProjects.push(...log.projects); - }); + discoveredProjects.push(...projects); }, 30000000); afterAll(async () => { await deleteTestProjects(discoveredProjects); @@ -93,39 +78,21 @@ describe('Multiple targets', () => { }, ]); expect(pollingUrls.length >= 1).toBeTruthy(); - const importLog = await pollImportUrls(pollingUrls); - expect(importLog[0]).toMatchObject({ - id: expect.any(String), - status: 'complete', - created: expect.any(String), - }); + const projects = await pollImportUrls(pollingUrls); // at least one job successfully finished - expect(importLog[0].logs[0]).toMatchObject({ - name: expect.any(String), - created: expect.any(String), - status: 'complete', - projects: [ - { - projectUrl: expect.any(String), - success: true, - targetFile: expect.any(String), - }, - ], + expect(projects[0]).toMatchObject({ + projectUrl: expect.any(String), + success: true, + targetFile: expect.any(String), }); - expect( - importLog[0].logs.every((job) => job.status === Status.COMPLETE), - ).toBeTruthy(); // cleanup - importLog[0].logs.forEach((log) => { - discoveredProjects.push(...log.projects); - }); + discoveredProjects.push(...projects); }, 30000000); afterAll(async () => { await deleteTestProjects(discoveredProjects); }); }); -test.todo('Import & poll in one'); test.todo('Failed import 100%'); test.todo('Only 1 import fails out of a few + logs created'); test.todo('If we stopped half way, restarted from where we left');