-
Notifications
You must be signed in to change notification settings - Fork 0
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 snyk#706 from snyk/feat/prune-test-graph
feat: prune graph on test if asked
- Loading branch information
Showing
6 changed files
with
77 additions
and
41 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,14 @@ | ||
import {CustomError} from './custom-error'; | ||
|
||
export class TooManyVulnPaths extends CustomError { | ||
private static ERROR_CODE: number = 413; | ||
private static ERROR_STRING_CODE: string = 'TOO_MANY_VULN_PATHS'; | ||
private static ERROR_MESSAGE: string = 'Too many vulnerable paths to process the project'; | ||
|
||
constructor() { | ||
super(TooManyVulnPaths.ERROR_MESSAGE); | ||
this.code = TooManyVulnPaths.ERROR_CODE; | ||
this.strCode = TooManyVulnPaths.ERROR_STRING_CODE; | ||
this.userMessage = TooManyVulnPaths.ERROR_MESSAGE; | ||
} | ||
} |
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,44 @@ | ||
import * as _debug from 'debug'; | ||
import { DepGraph, legacy } from '@snyk/dep-graph'; | ||
|
||
import { DepTree } from './types'; | ||
import * as config from './config'; | ||
import { TooManyVulnPaths } from './errors'; | ||
import { SupportedPackageManagers } from './package-managers'; | ||
|
||
const debug = _debug('snyk:prune'); | ||
|
||
const {depTreeToGraph, graphToDepTree} = legacy; | ||
|
||
export function countPathsToGraphRoot(graph: DepGraph): number { | ||
return graph.getPkgs().reduce((acc, pkg) => acc + graph.countPathsToRoot(pkg), 0); | ||
} | ||
|
||
export async function pruneGraph( | ||
depGraph: DepGraph, | ||
packageManager: SupportedPackageManagers, | ||
): Promise<DepGraph> { | ||
try { | ||
// Arbitrary threshold for maximum number of elements in the tree | ||
const threshold = config.PRUNE_DEPS_THRESHOLD; | ||
const prunedTree = (await graphToDepTree( | ||
depGraph, | ||
packageManager, | ||
{ deduplicateWithinTopLevelDeps: true }, | ||
)) as DepTree; | ||
|
||
const prunedGraph = await depTreeToGraph(prunedTree, packageManager); | ||
const count = countPathsToGraphRoot(prunedGraph); | ||
debug('prunedPathsCount: ' + count); | ||
|
||
if (count < threshold) { | ||
return prunedGraph; | ||
} | ||
|
||
debug('Too many vulnerable paths to process the project'); | ||
throw new TooManyVulnPaths(); | ||
} catch (e) { | ||
debug('Failed to prune the graph, returning original: ' + e); | ||
return depGraph; | ||
} | ||
} |
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