Skip to content

Commit

Permalink
Merge pull request #415 from snyk-tech-services/refactor/use-target-data
Browse files Browse the repository at this point in the history
refactor: use target data not possibly empty projects data
  • Loading branch information
lili2311 authored Dec 22, 2022
2 parents fa91dd0 + f903295 commit 1c7f57f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
24 changes: 21 additions & 3 deletions src/scripts/sync/sync-projects-per-target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import type {
} from '../../lib/types';
import { ProjectUpdateType } from '../../lib/types';
import { SupportedIntegrationTypesUpdateProject } from '../../lib/types';
import { targetGenerators } from '../generate-imported-targets-from-snyk';
import { deactivateProject, listProjects } from '../../lib';
import pMap = require('p-map');
import { cloneAndAnalyze } from './clone-and-analyze';
Expand All @@ -29,6 +28,16 @@ export function getMetaDataGenerator(
return getDefaultBranchGenerators[origin];
}

export function getTargetConverter(
origin: SupportedIntegrationTypesUpdateProject,
): (target: SnykTarget) => Target {
const getTargetConverter = {
[SupportedIntegrationTypesUpdateProject.GITHUB]: snykTargetConverter,
[SupportedIntegrationTypesUpdateProject.GHE]: snykTargetConverter,
};
return getTargetConverter[origin];
}

export async function syncProjectsForTarget(
requestManager: requestsManager,
orgId: string,
Expand Down Expand Up @@ -57,10 +66,11 @@ export async function syncProjectsForTarget(
}
debug(`Syncing projects for target ${target.attributes.displayName}`);
let targetMeta: RepoMetaData;
const origin = projects[0].origin as SupportedIntegrationTypesUpdateProject;
const origin = target.attributes
.origin as SupportedIntegrationTypesUpdateProject;
const targetData = getTargetConverter(origin)(target);

try {
const targetData = targetGenerators[origin](projects[0]);
targetMeta = await getMetaDataGenerator(origin)(targetData, host);
} catch (e) {
debug(e);
Expand Down Expand Up @@ -340,3 +350,11 @@ export async function bulkImportTargetFiles(
}
return { created, failed };
}

export function snykTargetConverter(target: SnykTarget): Target {
const [owner, name] = target.attributes.displayName.split('/');
return {
owner,
name: name,
};
}
1 change: 0 additions & 1 deletion test/scripts/sync/sync-org-projects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,6 @@ describe('updateTargets', () => {

expect(githubSpy).toBeCalledWith(
{
branch: 'master',
name: 'monorepo',
owner: 'snyk',
},
Expand Down

0 comments on commit 1c7f57f

Please sign in to comment.