UNPKG

snyk-gradle-plugin

Version:
95 lines 3.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.findChildren = exports.buildGraph = void 0; const dep_graph_1 = require("@snyk/dep-graph"); async function buildGraph(gradleGraph, rootPkgName, projectVersion, verbose, coordinateMap) { const pkgManager = { name: 'gradle' }; const isEmptyGraph = !gradleGraph || Object.keys(gradleGraph).length === 0; const depGraphBuilder = new dep_graph_1.DepGraphBuilder(pkgManager, { name: rootPkgName, version: projectVersion || '0.0.0', }); if (isEmptyGraph) { return depGraphBuilder.build(); } const visitedMap = {}; const queue = []; queue.push(...findChildren('root-node', [], gradleGraph)); // queue direct dependencies // breadth first search while (queue.length > 0) { const item = queue.shift(); if (!item) continue; let { id, parentId } = item; const { ancestry } = item; // take a copy as id maybe mutated below and we need this id when finding childing in GradleGraph const gradleGraphId = `${id}`; const node = gradleGraph[id]; if (!node) continue; let { name = 'unknown', version = 'unknown' } = node; let pkgIdProvenance = undefined; if (coordinateMap) { if (coordinateMap[id]) { id = coordinateMap[id]; const [newName, newVersion] = id.split('@'); if (name !== newName || version !== newVersion) { pkgIdProvenance = `${name}@${version}`; // record pkg id provenance if re coordinated } name = newName; version = newVersion; } if (coordinateMap[parentId]) { parentId = coordinateMap[parentId]; } } const visited = visitedMap[id]; if (!verbose && visited) { const prunedId = id + ':pruned'; depGraphBuilder.addPkgNode({ name, version }, prunedId, createNodeInfo(pkgIdProvenance, 'true')); depGraphBuilder.connectDep(parentId, prunedId); continue; // don't queue any more children } if (verbose && ancestry.includes(id)) { const prunedId = id + ':pruned'; depGraphBuilder.addPkgNode(visited, prunedId, createNodeInfo(pkgIdProvenance, 'cyclic')); depGraphBuilder.connectDep(parentId, prunedId); continue; // don't queue any more children } if (verbose && visited) { // use visited node when omitted dependencies found (verbose) depGraphBuilder.addPkgNode(visited, id, createNodeInfo(pkgIdProvenance)); depGraphBuilder.connectDep(parentId, id); } else { depGraphBuilder.addPkgNode({ name, version }, id, createNodeInfo(pkgIdProvenance)); depGraphBuilder.connectDep(parentId, id); visitedMap[id] = { name, version }; } // Remember to push updated ancestry here queue.push(...findChildren(gradleGraphId, [...ancestry, id], gradleGraph)); // queue children } return depGraphBuilder.build(); } exports.buildGraph = buildGraph; function createNodeInfo(pkgIdProvenance, pruned) { const labels = {}; if (pruned) labels.pruned = pruned; if (pkgIdProvenance) labels.pkgIdProvenance = pkgIdProvenance; return Object.keys(labels).length ? { labels } : undefined; } function findChildren(parentId, ancestry, gradleGraph) { var _a; const result = []; for (const id of Object.keys(gradleGraph)) { const node = gradleGraph[id]; if ((_a = node === null || node === void 0 ? void 0 : node.parentIds) === null || _a === void 0 ? void 0 : _a.includes(parentId)) { result.push({ id, ancestry, parentId }); } } return result; } exports.findChildren = findChildren; //# sourceMappingURL=graph.js.map