Skip to content

Commit

Permalink
Merge pull request #396 from snyk-tech-services/chore/refactor-get-su…
Browse files Browse the repository at this point in the history
…pported-types

chore: refactor `getSCMSupportedManifests`
  • Loading branch information
lili2311 authored Dec 7, 2022
2 parents 513097e + fff7fa0 commit afea991
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/lib/api/org/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ function convertToSnykProject(projectData: RESTProjectData[]): SnykProject[] {
origin: project.attributes.origin,
name: project.attributes.name,
type: project.attributes.type,
status: project.attributes.status,
};
projects.push(projectTmp);
}
Expand Down
10 changes: 5 additions & 5 deletions src/lib/supported-project-types/supported-manifests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export const OPEN_SOURCE_PACKAGE_MANAGERS: {
},
};

export const DOCKER: {
export const CONTAINER: {
[projectType: string]: {
manifestFiles: string[];
isSupported: boolean;
Expand Down Expand Up @@ -131,18 +131,18 @@ export const CLOUD_CONFIGS: {
};

export function getSCMSupportedManifests(
manifestTypes?: string[],
manifestTypes: string[] = [],
orgEntitlements: SnykProductEntitlement[] = [],
): string[] {
const typesWithSCMSupport = Object.entries({
...OPEN_SOURCE_PACKAGE_MANAGERS,
...CLOUD_CONFIGS,
...DOCKER,
...CONTAINER,
}).filter(([, config]) => config.isSupported);

const manifestFiles = typesWithSCMSupport.reduce(
(manifests, [name, config]) => {
if (manifestTypes && !manifestTypes.includes(name)) {
if (manifestTypes.length > 0 && !manifestTypes.includes(name)) {
return manifests;
}

Expand All @@ -167,7 +167,7 @@ export function getSCMSupportedProjectTypes(
const typesWithSCMSupport = Object.entries({
...OPEN_SOURCE_PACKAGE_MANAGERS,
...CLOUD_CONFIGS,
...DOCKER,
...CONTAINER,
}).filter(([, config]) => config.isSupported);

for (const [name, entry] of typesWithSCMSupport) {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export interface SnykProject {
origin: string;
type: string;
branch: string | null;
status: 'inactive' | 'active';
}

export interface Org {
Expand Down Expand Up @@ -149,7 +150,7 @@ export interface RESTProjectsAttributes {
lifecycle: string;
name: string;
origin: string;
status: string;
status: 'inactive' | 'active';
tags: unknown;
targetReference: string | null;
type: string;
Expand Down
1 change: 1 addition & 0 deletions test/lib/__snapshots__/org.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Object {
"id": "331ede0a-de94-456f-b788-166caeca58bf",
"name": "snyk/goof",
"origin": "github",
"status": "active",
"type": "maven",
},
],
Expand Down
42 changes: 42 additions & 0 deletions test/lib/supported-project-types/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,49 @@ test('SCM supported manifest files for importing & auto discovery', async () =>
]).sort(),
).toEqual(supported.sort());
});
test('SCM supported manifest files for importing & auto discovery', async () => {
const supported = [
'package.json',
'Gemfile.lock',
'yarn.lock',
'pom.xml',
'build.gradle',
'build.sbt',
'*req*.txt',
'requirements/*.txt',
'Gopkg.lock',
'vendor.json',
'go.mod',
'packages.config',
'*.csproj',
'*.fsproj',
'*.vbproj',
'project.json',
'project.assets.json',
'*.targets',
'*.props',
'packages*.lock.json',
'global.json',
'composer.lock',
'Podfile',
'*[dD][oO][cC][kK][eE][rR][fF][iI][lL][eE]*',
'*Dockerfile*',
'templates/*.yaml',
'templates/*.yml',
'Chart.yaml',
'*.yaml',
'*.yml',
'*.json',
'*.tf',
];

expect(
getSCMSupportedManifests(
[],
['infrastructureAsCode', 'dockerfileFromScm'],
).sort(),
).toEqual(supported.sort());
});
test('SCM supported manifest files for specific project types', async () => {
expect(getSCMSupportedManifests(['rubygems'])).toEqual(['Gemfile.lock']);
expect(getSCMSupportedManifests(['npm', 'yarn'])).toEqual([
Expand Down
20 changes: 20 additions & 0 deletions test/scripts/sync/clone-and-analyze.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('cloneAndAnalyze', () => {
origin: 'github',
type: 'npm',
branch: 'master',
status: 'active',
},
{
name: 'snyk-fixtures/monorepo-simple:no-vulns-bundler-app/Gemfile.lock',
Expand All @@ -43,6 +44,7 @@ describe('cloneAndAnalyze', () => {
origin: 'github',
type: 'rubygems',
branch: 'master',
status: 'active',
},
{
name: 'snyk-fixtures/monorepo-simple:npm-project/package.json',
Expand All @@ -51,6 +53,7 @@ describe('cloneAndAnalyze', () => {
origin: 'github',
type: 'rubygems',
branch: 'master',
status: 'active',
},
{
name: 'snyk-fixtures/monorepo-simple:npm-project-with-policy/package.json',
Expand All @@ -59,6 +62,7 @@ describe('cloneAndAnalyze', () => {
origin: 'github',
type: 'rubygems',
branch: 'master',
status: 'active',
},
];

Expand Down Expand Up @@ -93,6 +97,7 @@ describe('cloneAndAnalyze', () => {
origin: 'github',
type: 'npm',
branch: 'master',
status: 'active',
},
{
name: 'snyk-fixtures/monorepo-simple:no-vulns-bundler-app/Gemfile.lock',
Expand All @@ -101,6 +106,7 @@ describe('cloneAndAnalyze', () => {
origin: 'github',
type: 'rubygems',
branch: 'master',
status: 'active',
},
{
name: 'snyk-fixtures/monorepo-simple:npm-project/package.json',
Expand All @@ -109,6 +115,7 @@ describe('cloneAndAnalyze', () => {
origin: 'github',
type: 'rubygems',
branch: 'master',
status: 'active',
},
{
name: 'snyk-fixtures/monorepo-simple:npm-project-with-policy/package.json',
Expand All @@ -117,6 +124,7 @@ describe('cloneAndAnalyze', () => {
origin: 'github',
type: 'rubygems',
branch: 'master',
status: 'active',
},
];

Expand Down Expand Up @@ -155,6 +163,7 @@ describe('cloneAndAnalyze', () => {
origin: 'github',
type: 'npm',
branch: 'master',
status: 'active',
},
{
name: 'snyk-fixtures/monorepo-simple:no-vulns-bundler-app/Gemfile.lock',
Expand All @@ -163,6 +172,7 @@ describe('cloneAndAnalyze', () => {
origin: 'github',
type: 'rubygems',
branch: 'master',
status: 'active',
},
// Should find this file needs bringing in
// {
Expand All @@ -172,6 +182,7 @@ describe('cloneAndAnalyze', () => {
// origin: 'github',
// type: 'rubygems',
// branch: 'master',
// status: 'active'
// },
{
name: 'snyk-fixtures/monorepo-simple:npm-project-with-policy/package.json',
Expand All @@ -180,6 +191,7 @@ describe('cloneAndAnalyze', () => {
origin: 'github',
type: 'npm',
branch: 'master',
status: 'active',
},
// should be detected as no longer present, and needs de-activating
{
Expand All @@ -189,6 +201,7 @@ describe('cloneAndAnalyze', () => {
origin: 'github',
type: 'npm',
branch: 'master',
status: 'active',
},
];

Expand Down Expand Up @@ -219,6 +232,7 @@ describe('cloneAndAnalyze', () => {
origin: 'github',
type: 'npm',
branch: 'master',
status: 'active',
},
],
});
Expand All @@ -233,6 +247,7 @@ describe('cloneAndAnalyze', () => {
origin: 'github',
type: 'npm',
branch: 'master',
status: 'active',
},
{
name: 'snyk-fixtures/monorepo-simple:Gemfile.lock',
Expand All @@ -241,6 +256,7 @@ describe('cloneAndAnalyze', () => {
origin: 'github',
type: 'rubygems',
branch: 'master',
status: 'active',
},
{
name: 'snyk-fixtures/monorepo-simple:bundler-app/Gemfile.lock',
Expand All @@ -249,6 +265,7 @@ describe('cloneAndAnalyze', () => {
origin: 'github',
type: 'rubygems',
branch: 'master',
status: 'active',
},
{
name: 'snyk-fixtures/monorepo-simple:no-vulns-bundler-app/Gemfile.lock',
Expand All @@ -257,6 +274,7 @@ describe('cloneAndAnalyze', () => {
origin: 'github',
type: 'rubygems',
branch: 'master',
status: 'active',
},
{
name: 'snyk-fixtures/monorepo-simple:npm-project/package.json',
Expand All @@ -265,6 +283,7 @@ describe('cloneAndAnalyze', () => {
origin: 'github',
type: 'rubygems',
branch: 'master',
status: 'active',
},
{
name: 'snyk-fixtures/monorepo-simple:npm-project-with-policy/package.json',
Expand All @@ -273,6 +292,7 @@ describe('cloneAndAnalyze', () => {
origin: 'github',
type: 'rubygems',
branch: 'master',
status: 'active',
},
];

Expand Down
15 changes: 12 additions & 3 deletions test/scripts/sync/generate-projects-diff-actions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { generateProjectDiffActions } from '../../../src/scripts/sync/generate-projects-diff-actions';
import type { SnykProject } from '../../../src/lib/types';
import {
DOCKER,
CONTAINER,
OPEN_SOURCE_PACKAGE_MANAGERS,
} from '../../../src/lib/supported-project-types/supported-manifests';

Expand All @@ -16,6 +16,7 @@ describe('generateProjectDiffActions', () => {
origin: 'github',
type: 'npm',
branch: 'master',
status: 'active',
},
{
name: 'snyk/goof:src/Dockerfile',
Expand All @@ -24,13 +25,14 @@ describe('generateProjectDiffActions', () => {
origin: 'github',
type: 'dockerfile',
branch: 'master',
status: 'active',
},
];
// Act
const res = await generateProjectDiffActions(
['package.json', 'path/to/build.gradle', 'src/Dockerfile'],
projects,
[...Object.keys(OPEN_SOURCE_PACKAGE_MANAGERS), ...Object.keys(DOCKER)],
[...Object.keys(OPEN_SOURCE_PACKAGE_MANAGERS), ...Object.keys(CONTAINER)],
);

// Assert
Expand All @@ -44,6 +46,7 @@ describe('generateProjectDiffActions', () => {
origin: 'github',
type: 'npm',
branch: 'master',
status: 'active',
},
],
});
Expand All @@ -58,6 +61,7 @@ describe('generateProjectDiffActions', () => {
origin: 'github',
type: 'npm',
branch: 'master',
status: 'active',
},
];
// Act
Expand All @@ -80,6 +84,7 @@ describe('generateProjectDiffActions', () => {
origin: 'github',
type: 'npm',
branch: 'master',
status: 'active',
},
{
name: 'snyk/goof:Dockerfile',
Expand All @@ -88,6 +93,7 @@ describe('generateProjectDiffActions', () => {
origin: 'github',
type: 'dockerfile',
branch: 'master',
status: 'active',
},
];
// Act
Expand All @@ -109,6 +115,7 @@ describe('generateProjectDiffActions', () => {
origin: 'github',
type: 'npm',
branch: 'master',
status: 'active',
},
{
name: 'snyk/goof:Dockerfile',
Expand All @@ -117,12 +124,13 @@ describe('generateProjectDiffActions', () => {
origin: 'github',
type: 'dockerfile',
branch: 'master',
status: 'active',
},
];
// Act
const res = await generateProjectDiffActions(['package.json'], projects, [
...Object.keys(OPEN_SOURCE_PACKAGE_MANAGERS),
...Object.keys(DOCKER),
...Object.keys(CONTAINER),
]);

// Assert
Expand All @@ -136,6 +144,7 @@ describe('generateProjectDiffActions', () => {
origin: 'github',
type: 'dockerfile',
branch: 'master',
status: 'active',
},
],
});
Expand Down
Loading

0 comments on commit afea991

Please sign in to comment.