-
Notifications
You must be signed in to change notification settings - Fork 25
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 #159 from snyk/fix/return_depgraph
feat: return depgraph from plugin
- Loading branch information
Showing
11 changed files
with
4,139 additions
and
51 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,50 @@ | ||
import { DepGraph } from '@snyk/dep-graph'; | ||
import { depTreeToGraph, DepTree } from '@snyk/dep-graph/dist/legacy'; | ||
|
||
type PackageName = string; | ||
|
||
// This is a partially dep tree: every package exists once, all additional reference will have "true" as the value | ||
export interface PartialDepTree { | ||
name?: string; | ||
version?: string; | ||
dependencies?: Dependencies; | ||
labels?: { | ||
[key: string]: string; | ||
}; | ||
} | ||
|
||
type Dependencies = { | ||
[depName: string]: PartialDepTree | 'true'; | ||
}; | ||
|
||
export function buildDepGraph( | ||
partialDepTree: PartialDepTree | ||
): Promise<DepGraph> { | ||
const packageToDepTreeMap = new Map<PackageName, PartialDepTree>(); | ||
|
||
const queue: Dependencies[] = [partialDepTree.dependencies]; | ||
const referencesToUpdate: { key: string; dependencies: Dependencies }[] = []; | ||
while (queue.length > 0) { | ||
const dependencies = queue.pop(); | ||
if (!dependencies) continue; | ||
|
||
for (const [key, dependencyDepTree] of Object.entries(dependencies)) { | ||
if (dependencyDepTree === 'true') { | ||
referencesToUpdate.push({ key, dependencies }); | ||
} else { | ||
packageToDepTreeMap.set(key, dependencyDepTree); | ||
queue.push(dependencyDepTree.dependencies); | ||
} | ||
} | ||
} | ||
|
||
referencesToUpdate.forEach(({ key, dependencies }) => { | ||
if (!packageToDepTreeMap.get(key)) { | ||
// this should never happen | ||
throw new Error(`key ${key} not found in packageToDepTreeMap`); | ||
} | ||
dependencies[key] = packageToDepTreeMap.get(key); | ||
}); | ||
|
||
return depTreeToGraph(partialDepTree as DepTree, 'pip'); | ||
} |
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
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
Oops, something went wrong.