-
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 #393 from snyk-tech-services/feat/clone-and-analyze
feat: clones & analyses repo for changes vs monitored projects
- Loading branch information
Showing
6 changed files
with
549 additions
and
28 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
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,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, | ||
); | ||
} |
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
Oops, something went wrong.