-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #391 from snyk-tech-services/feat/generate-project…
…-diff-actions feat: function to find repo files diff
- Loading branch information
Showing
2 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [], | ||
}); | ||
}); | ||
}); |