Skip to content

Commit

Permalink
Merge pull request #5 from snyk-tech-services/feat/poll-returns-project
Browse files Browse the repository at this point in the history
feat: return projects
  • Loading branch information
lili2311 authored May 21, 2020
2 parents 8e0e216 + a903a78 commit e5fd0c6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 65 deletions.
20 changes: 12 additions & 8 deletions src/lib/poll-import/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -15,7 +15,7 @@ export async function pollImportUrl(
locationUrl: string,
retryCount = MAX_RETRY_COUNT,
retryWaitTime = MIN_RETRY_WAIT_TIME,
): Promise<PollImportResponse> {
): Promise<Project[]> {
const apiToken = getApiToken();
debug(`Polling locationUrl=${locationUrl}`);

Expand Down Expand Up @@ -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: {
Expand All @@ -65,21 +69,21 @@ export async function pollImportUrl(

export async function pollImportUrls(
locationUrls: string[],
): Promise<PollImportResponse[]> {
): Promise<Project[]> {
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);
Expand All @@ -88,5 +92,5 @@ export async function pollImportUrls(
{ concurrency: 10 },
);

return resArray;
return projectsArray;
}
5 changes: 3 additions & 2 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 1 addition & 10 deletions src/scripts/import-projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
57 changes: 12 additions & 45 deletions test/lib/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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');

0 comments on commit e5fd0c6

Please sign in to comment.