Skip to content

Commit

Permalink
feat: move prune logic in separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
dkontorovskyy committed Aug 5, 2019
1 parent f0e51f2 commit 842ef7f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 41 deletions.
41 changes: 2 additions & 39 deletions src/lib/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import { SingleDepRootResult, DepTree, MonitorMeta, MonitorResult } from './type
import * as projectMetadata from './project-metadata';
import * as path from 'path';
import {MonitorError, ConnectionTimeoutError} from './errors';
import { SupportedPackageManagers, GRAPH_SUPPORTED_PACKAGE_MANAGERS } from './package-managers';
import { countPathsToGraphRoot, pruneGraph } from './prune';
import { GRAPH_SUPPORTED_PACKAGE_MANAGERS } from './package-managers';

const debug = Debug('snyk');

Expand Down Expand Up @@ -332,44 +333,6 @@ export async function monitorGraph(
});
}

export function countPathsToGraphRoot(graph: depGraphLib.DepGraph): number {
return graph
.getPkgs()
.map((pkg) => graph.countPathsToRoot(pkg))
.reduce((acc, pathsToRoot) => acc + pathsToRoot, 0);
}

export async function pruneGraph(
depGraph: depGraphLib.DepGraph,
packageManager: SupportedPackageManagers,
): Promise<depGraphLib.DepGraph> {
try {
const threshold = config.PRUNE_DEPS_THRESHOLD; // Arbitrary threshold for maximum number of elements in the tree
const prunedTree: DepTree = (await depGraphLib.legacy.graphToDepTree(
depGraph,
packageManager,
{ deduplicateWithinTopLevelDeps: true },
)) as DepTree;

const prunedGraph = await depGraphLib.legacy.depTreeToGraph(
prunedTree,
packageManager,
);
const count = countPathsToGraphRoot(prunedGraph);
debug('prunedPathsCount: ' + count);

if (count < threshold) {
return prunedGraph;
}
debug('Too many vulnerable paths to monitor the project');
// TODO: create a custom error for this
throw new Error('Too many vulnerable paths to monitor the project');
} catch (e) {
debug('Failed to prune the graph, returning original:' + e);
return depGraph;
}
}

function pluckPolicies(pkg) {
if (!pkg) {
return null;
Expand Down
43 changes: 43 additions & 0 deletions src/lib/prune.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as _debug from 'debug';
import { DepGraph, legacy } from '@snyk/dep-graph';

import { DepTree } from './types';
import * as config from './config';
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 Error('Too many vulnerable paths to process the project');
} catch (e) {
debug('Failed to prune the graph, returning original: ' + e);
return depGraph;
}
}
4 changes: 2 additions & 2 deletions src/lib/snyk-test/run-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
} from '../errors';
import { maybePrintDeps } from '../print-deps';
import { SupportedPackageManagers } from '../package-managers';
import { countPathsToGraphRoot, pruneGraph } from '../monitor';
import { countPathsToGraphRoot, pruneGraph } from '../prune';

// tslint:disable-next-line:no-var-requires
const debug = require('debug')('snyk');
Expand Down Expand Up @@ -346,7 +346,7 @@ async function assembleLocalPayloads(root, options: Options & TestOptions): Prom

analytics.add('prePrunedPathsCount', prePruneDepCount);
const postPruneDepCount = countPathsToGraphRoot(depGraph);
debug('post prunedPathsCount: ' + prePruneDepCount);
debug('post prunedPathsCount: ' + postPruneDepCount);
analytics.add('postPrunedPathsCount', postPruneDepCount);
}
body.depGraph = depGraph;
Expand Down

0 comments on commit 842ef7f

Please sign in to comment.