Skip to content

Commit

Permalink
Merge pull request #393 from snyk-tech-services/feat/clone-and-analyze
Browse files Browse the repository at this point in the history
feat: clones & analyses repo for changes vs monitored projects
  • Loading branch information
lili2311 authored Dec 6, 2022
2 parents 2d82acc + f70a76a commit 686a6b5
Show file tree
Hide file tree
Showing 6 changed files with 549 additions and 28 deletions.
12 changes: 8 additions & 4 deletions src/lib/supported-project-types/supported-manifests.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
export type SnykProductEntitlement =
| 'dockerfileFromScm'
| 'infrastructureAsCode';

export const PACKAGE_MANAGERS: {
[projectType: string]: {
manifestFiles: string[];
isSupported: boolean;
entitlement?: string;
entitlement?: SnykProductEntitlement;
};
} = {
npm: {
Expand Down Expand Up @@ -86,7 +90,7 @@ export const PACKAGE_MANAGERS: {
'*[dD][oO][cC][kK][eE][rR][fF][iI][lL][eE]*',
'*Dockerfile*',
],
entitlement: 'dockerfileFromScm', // TODO: use API to check https://snyk.docs.apiary.io/#reference/entitlements/a-specific-entitlement-by-organization/get-an-organization's-entitlement-value
entitlement: 'dockerfileFromScm',
},
hex: {
manifestFiles: ['mix.exs'],
Expand All @@ -98,7 +102,7 @@ export const CLOUD_CONFIGS: {
[projectType: string]: {
manifestFiles: string[];
isSupported: boolean;
entitlement?: string;
entitlement?: SnykProductEntitlement;
};
} = {
helmconfig: {
Expand All @@ -120,7 +124,7 @@ export const CLOUD_CONFIGS: {

export function getSCMSupportedManifests(
manifestTypes?: string[],
orgEntitlements: string[] = [],
orgEntitlements: SnykProductEntitlement[] = [],
): string[] {
const typesWithSCMSupport = Object.entries({
...PACKAGE_MANAGERS,
Expand Down
77 changes: 77 additions & 0 deletions src/scripts/sync/clone-and-analyze.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import * as debugLib from 'debug';
import * as fs from 'fs';
import * as path from 'path';

import { find, getSCMSupportedManifests, gitClone } from '../../lib';
import type { SnykProductEntitlement } from '../../lib/supported-project-types/supported-manifests';
import type {
RepoMetaData,
SnykProject,
SupportedIntegrationTypesUpdateProject,
} from '../../lib/types';
import { generateProjectDiffActions } from './generate-projects-diff-actions';

const debug = debugLib('snyk:clone-and-analyze');

const defaultExclusionGlobs = [
'fixtures',
'tests',
'__tests__',
'test',
'__test__',
'ci',
'node_modules',
'bower_components',
'.git',
];
export async function cloneAndAnalyze(
integrationType: SupportedIntegrationTypesUpdateProject,
repoMetadata: RepoMetaData,
snykMonitoredProjects: SnykProject[],
exclusionGlobs: string[] = [],
entitlements: SnykProductEntitlement[] = [
'dockerfileFromScm',
'infrastructureAsCode',
],
manifestTypes?: string[],
): Promise<{
import: string[];
deactivate: SnykProject[];
}> {
const { success, repoPath, gitResponse } = await gitClone(
integrationType,
repoMetadata,
);
debug('Clone response', { success, repoPath, gitResponse });

if (!success) {
throw new Error(gitResponse);
}

if (!repoPath) {
throw new Error('No location returned for clones repo to analyze');
}
const { files } = await find(
repoPath,
[...defaultExclusionGlobs, ...exclusionGlobs],
// TODO: when possible switch to check entitlements via API automatically for an org
// right now the product entitlements are not exposed via API so user has to provide which products
// they are using
getSCMSupportedManifests(manifestTypes, entitlements),
6,
);
const relativeFileNames = files.map((f) => path.relative(repoPath, f));
debug(
`Detected ${files.length} files in ${repoMetadata.cloneUrl}: ${files.join(
',',
)}`,
);

fs.rmdirSync(repoPath, { recursive: true });

return generateProjectDiffActions(
relativeFileNames,
snykMonitoredProjects,
manifestTypes && manifestTypes.length > 0,
);
}
6 changes: 5 additions & 1 deletion src/scripts/sync/generate-projects-diff-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { SnykProject } from '../../lib/types';
export function generateProjectDiffActions(
repoManifests: string[],
snykMonitoredProjects: SnykProject[],
skipDeactivating = false,
): {
import: string[];
deactivate: SnykProject[];
Expand All @@ -29,5 +30,8 @@ export function generateProjectDiffActions(
}
}

return { import: filesToImport, deactivate };
return {
import: filesToImport,
deactivate: skipDeactivating ? [] : deactivate,
};
}
Loading

0 comments on commit 686a6b5

Please sign in to comment.