Skip to content

Commit

Permalink
Merge pull request #391 from snyk-tech-services/feat/generate-project…
Browse files Browse the repository at this point in the history
…-diff-actions

feat: function to find repo files diff
  • Loading branch information
lili2311 authored Nov 29, 2022
2 parents 766debd + 69865a7 commit dacf067
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/scripts/sync/generate-projects-diff-actions.ts
Original file line number Diff line number Diff line change
@@ -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 };
}
67 changes: 67 additions & 0 deletions test/scripts/sync/generate-projects-diff-actions.spec.ts
Original file line number Diff line number Diff line change
@@ -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: [],
});
});
});

0 comments on commit dacf067

Please sign in to comment.