diff --git a/src/scripts/sync/generate-projects-diff-actions.ts b/src/scripts/sync/generate-projects-diff-actions.ts new file mode 100644 index 00000000..ded81d4a --- /dev/null +++ b/src/scripts/sync/generate-projects-diff-actions.ts @@ -0,0 +1,33 @@ +import type { SnykProject } from '../../lib/types'; + +export function generateProjectDiffActions( + repoManifests: string[], + snykMonitoredProjects: SnykProject[], +): { + import: string[]; + deactivate: SnykProject[]; +} { + const filesToImport: string[] = []; + const deactivate: SnykProject[] = []; + + // any files in the repo, not in Snyk already should be + // imported + for (const manifest of repoManifests) { + const snykProjectManifests = snykMonitoredProjects.map( + (p) => p.name.split(':')[1], + ); + if (!snykProjectManifests.includes(manifest)) { + filesToImport.push(manifest); + } + } + + // any files in Snyk, not found in the repo should have the + // related project de-activated + for (const project of snykMonitoredProjects) { + if (!repoManifests.includes(project.name.split(':')[1])) { + deactivate.push(project); + } + } + + return { import: filesToImport, deactivate }; +} diff --git a/test/scripts/sync/generate-projects-diff-actions.spec.ts b/test/scripts/sync/generate-projects-diff-actions.spec.ts new file mode 100644 index 00000000..52f221a6 --- /dev/null +++ b/test/scripts/sync/generate-projects-diff-actions.spec.ts @@ -0,0 +1,67 @@ +import { generateProjectDiffActions } from '../../../src/scripts/sync/generate-projects-diff-actions'; +import type { SnykProject } from '../../../src/lib/types'; + +describe('generateProjectDiffActions', () => { + it('identifies correctly the diff between files in the repo vs monitored in Snyk', async () => { + // Arrange + const projects: SnykProject[] = [ + { + name: 'snyk/goof:todo/package.json', + id: 'af137b96-6966-46c1-826b-2e79ac49bbxx', + created: '2018-10-29T09:50:54.014Z', + origin: 'github', + type: 'npm', + branch: 'master', + }, + { + name: 'snyk/goof:src/Dockerfile', + id: 'af137b96-6966-46c1-826b-2e79ac49bbxx', + created: '2018-10-29T09:50:54.014Z', + origin: 'github', + type: 'npm', + branch: 'master', + }, + ]; + // Act + const res = await generateProjectDiffActions( + ['package.json', 'path/to/build.gradle', 'src/Dockerfile'], + projects, + ); + + // Assert + expect(res).toStrictEqual({ + import: ['package.json', 'path/to/build.gradle'], + deactivate: [ + { + name: 'snyk/goof:todo/package.json', + id: 'af137b96-6966-46c1-826b-2e79ac49bbxx', + created: '2018-10-29T09:50:54.014Z', + origin: 'github', + type: 'npm', + branch: 'master', + }, + ], + }); + }); + it('no changes needed', async () => { + // Arrange + const projects: SnykProject[] = [ + { + name: 'snyk/goof:package.json', + id: 'af137b96-6966-46c1-826b-2e79ac49bbxx', + created: '2018-10-29T09:50:54.014Z', + origin: 'github', + type: 'npm', + branch: 'master', + }, + ]; + // Act + const res = await generateProjectDiffActions(['package.json'], projects); + + // Assert + expect(res).toStrictEqual({ + import: [], + deactivate: [], + }); + }); +});