-
Notifications
You must be signed in to change notification settings - Fork 534
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 #928 from snyk/chore/monitor-refactor
chore: refactor shared code out and split out helper functions
- Loading branch information
Showing
11 changed files
with
332 additions
and
328 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { DepTree } from '../types'; | ||
|
||
export function countTotalDependenciesInTree(depTree: DepTree): number { | ||
let count = 0; | ||
if (depTree.dependencies) { | ||
for (const name of Object.keys(depTree.dependencies)) { | ||
const dep = depTree.dependencies[name]; | ||
if (dep) { | ||
count += 1 + countTotalDependenciesInTree(dep); | ||
} | ||
} | ||
} | ||
return count; | ||
} |
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,15 @@ | ||
import { DepTree } from '../types'; | ||
|
||
export function dropEmptyDeps(depTree: DepTree) { | ||
if (depTree.dependencies) { | ||
const keys = Object.keys(depTree.dependencies); | ||
if (keys.length === 0) { | ||
delete depTree.dependencies; | ||
} else { | ||
for (const k of keys) { | ||
dropEmptyDeps(depTree.dependencies[k]); | ||
} | ||
} | ||
} | ||
return depTree; | ||
} |
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,40 @@ | ||
import { DepTree } from '../types'; | ||
|
||
interface FilteredDepTree { | ||
filteredDepTree: DepTree; | ||
missingDeps: string[]; | ||
} | ||
|
||
export function filterOutMissingDeps(depTree: DepTree): FilteredDepTree { | ||
const filteredDeps = {}; | ||
const missingDeps: string[] = []; | ||
|
||
if (!depTree.dependencies) { | ||
return { | ||
filteredDepTree: depTree, | ||
missingDeps, | ||
}; | ||
} | ||
|
||
for (const depKey of Object.keys(depTree.dependencies)) { | ||
const dep = depTree.dependencies[depKey]; | ||
if ( | ||
(dep as any).missingLockFileEntry || | ||
((dep as any).labels && (dep as any).labels.missingLockFileEntry) | ||
) { | ||
// TODO(kyegupov): add field to the type | ||
missingDeps.push(`${dep.name}@${dep.version}`); | ||
} else { | ||
filteredDeps[depKey] = dep; | ||
} | ||
} | ||
const filteredDepTree: DepTree = { | ||
...depTree, | ||
dependencies: filteredDeps, | ||
}; | ||
|
||
return { | ||
filteredDepTree, | ||
missingDeps, | ||
}; | ||
} |
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,22 @@ | ||
import * as depGraphLib from '@snyk/dep-graph'; | ||
import { DepTree } from '../types'; | ||
|
||
export async function pruneTree( | ||
tree: DepTree, | ||
packageManagerName: string, | ||
): Promise<DepTree> { | ||
// Pruning requires conversion to the graph first. | ||
// This is slow. | ||
const graph = await depGraphLib.legacy.depTreeToGraph( | ||
tree, | ||
packageManagerName, | ||
); | ||
const prunedTree: DepTree = (await depGraphLib.legacy.graphToDepTree( | ||
graph, | ||
packageManagerName, | ||
{ deduplicateWithinTopLevelDeps: true }, | ||
)) as DepTree; | ||
// Transplant pruned dependencies in the original tree (we want to keep all other fields): | ||
tree.dependencies = prunedTree.dependencies; | ||
return tree; | ||
} |
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,79 @@ | ||
import * as debugModule from 'debug'; | ||
import { legacyPlugin as pluginApi } from '@snyk/cli-interface'; | ||
|
||
import { find } from '../find-files'; | ||
import { Options, TestOptions } from '../types'; | ||
import { NoSupportedManifestsFoundError } from '../errors'; | ||
import { getMultiPluginResult } from './get-multi-plugin-result'; | ||
import { getSinglePluginResult } from './get-single-plugin-result'; | ||
import { detectPackageFile, AUTO_DETECTABLE_FILES } from '../detect'; | ||
|
||
const debug = debugModule('snyk'); | ||
|
||
// Force getDepsFromPlugin to return scannedProjects for processing | ||
export async function getDepsFromPlugin( | ||
root: string, | ||
options: Options & TestOptions, | ||
): Promise<pluginApi.MultiProjectResult> { | ||
let inspectRes: pluginApi.InspectResult; | ||
|
||
if (options.allProjects) { | ||
// auto-detect only one-level deep for now | ||
const targetFiles = await find(root, [], AUTO_DETECTABLE_FILES, 1); | ||
debug( | ||
`auto detect manifest files, found ${targetFiles.length}`, | ||
targetFiles, | ||
); | ||
if (targetFiles.length === 0) { | ||
throw NoSupportedManifestsFoundError([root]); | ||
} | ||
inspectRes = await getMultiPluginResult(root, options, targetFiles); | ||
return inspectRes; | ||
} else { | ||
// TODO: is this needed for the auto detect handling above? | ||
// don't override options.file if scanning multiple files at once | ||
if (!options.scanAllUnmanaged) { | ||
options.file = options.file || detectPackageFile(root); | ||
} | ||
if (!options.docker && !(options.file || options.packageManager)) { | ||
throw NoSupportedManifestsFoundError([...root]); | ||
} | ||
inspectRes = await getSinglePluginResult(root, options); | ||
} | ||
|
||
if (!pluginApi.isMultiResult(inspectRes)) { | ||
if (!inspectRes.package) { | ||
// something went wrong if both are not present... | ||
throw Error( | ||
`error getting dependencies from ${options.packageManager} ` + | ||
"plugin: neither 'package' nor 'scannedProjects' were found", | ||
); | ||
} | ||
if (!inspectRes.package.targetFile && inspectRes.plugin) { | ||
inspectRes.package.targetFile = inspectRes.plugin.targetFile; | ||
} | ||
// We are using "options" to store some information returned from plugin that we need to use later, | ||
// but don't want to send to Registry in the Payload. | ||
// TODO(kyegupov): decouple inspect and payload so that we don't need this hack | ||
if ( | ||
inspectRes.plugin.meta && | ||
inspectRes.plugin.meta.allSubProjectNames && | ||
inspectRes.plugin.meta.allSubProjectNames.length > 1 | ||
) { | ||
options.advertiseSubprojectsCount = | ||
inspectRes.plugin.meta.allSubProjectNames.length; | ||
} | ||
return { | ||
plugin: inspectRes.plugin, | ||
scannedProjects: [{ depTree: inspectRes.package }], | ||
}; | ||
} else { | ||
// We are using "options" to store some information returned from plugin that we need to use later, | ||
// but don't want to send to Registry in the Payload. | ||
// TODO(kyegupov): decouple inspect and payload so that we don't need this hack | ||
(options as any).projectNames = inspectRes.scannedProjects.map( | ||
(scannedProject) => scannedProject.depTree.name, | ||
); | ||
return inspectRes; | ||
} | ||
} |
Oops, something went wrong.