Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [SUP-2164] Change dependency graph root name logic #266

Merged
merged 10 commits into from
Nov 18, 2023
14 changes: 13 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,25 @@ commands:
install_sdkman:
description: Install SDKMAN
steps:
- restore_cache:
name: Restore SDKMan executable
keys:
- sdkman-cli-{{ arch }}-v1
- run:
name: Installing SDKMAN
command: |
curl -s "https://get.sdkman.io?rcupdate=false" | bash
if ! command -v sdk &> /dev/null
then
curl -s "https://get.sdkman.io?rcupdate=false" | bash
fi

echo -e '\nsource "/home/circleci/.sdkman/bin/sdkman-init.sh"' >> $BASH_ENV
source $BASH_ENV
sdk list java
- save_cache:
key: sdkman-cli-{{ arch }}-v1
paths:
- ~/.sdkman
install_gradle_unix:
description: Install gradle
parameters:
Expand Down
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ test/fixtures/**/build
npm-debug.log
.DS_Store
coverage
.idea/

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
4 changes: 2 additions & 2 deletions lib/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ interface QueueItem {

export async function buildGraph(
snykGraph: SnykGraph,
projectName: string,
rootPkgName: string,
projectVersion: string,
coordinateMap?: CoordinateMap,
) {
const pkgManager: PkgManager = { name: 'gradle' };
const isEmptyGraph = !snykGraph || Object.keys(snykGraph).length === 0;

const depGraphBuilder = new DepGraphBuilder(pkgManager, {
name: projectName,
name: rootPkgName,
version: projectVersion || '0.0.0',
});

Expand Down
20 changes: 5 additions & 15 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import * as tmp from 'tmp';
import * as pMap from 'p-map';
import * as chalk from 'chalk';
import { DepGraph } from '@snyk/dep-graph';
import debugModule = require('debug');
import { legacyCommon, legacyPlugin as api } from '@snyk/cli-interface';

import { MissingSubProjectError } from './errors';
Expand All @@ -19,6 +18,7 @@ import type {
SnykHttpClient,
} from './types';
import { getMavenPackageInfo } from './search';
import debugModule = require('debug');

type ScannedProject = legacyCommon.ScannedProject;

Expand Down Expand Up @@ -526,11 +526,7 @@ async function getAllDeps(
concurrency: 100,
});
}
return await processProjectsInExtractedJSON(
root,
extractedJSON,
coordinateMap,
);
return await processProjectsInExtractedJSON(extractedJSON, coordinateMap);
} catch (err) {
const error: Error = err;
const gradleErrorMarkers = /^\s*>\s.*$/;
Expand Down Expand Up @@ -614,7 +610,6 @@ ${chalk.red.bold(mainErrorMessage)}`;
}

export async function processProjectsInExtractedJSON(
root: string,
extractedJSON: JsonDepsScriptResult,
coordinateMap?: CoordinateMap,
) {
Expand All @@ -626,21 +621,16 @@ export async function processProjectsInExtractedJSON(
continue;
}

const invalidValues = [null, undefined, ''];
const isValidRootDir = invalidValues.indexOf(root) === -1;
const isSubProject = projectId !== defaultProjectKey;

let projectName = isValidRootDir ? path.basename(root) : defaultProject;

let rootPkgName = defaultProject;
if (isSubProject) {
projectName = isValidRootDir
? `${path.basename(root)}/${projectId}`
: `${defaultProject}/${projectId}`;
rootPkgName = `${defaultProject}/${projectId}`;
}

extractedJSON.projects[projectId].depGraph = await buildGraph(
snykGraph,
projectName,
rootPkgName,
projectVersion,
coordinateMap,
);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"lint": "eslint --color --cache '{lib,test}/**/*.{js,ts}' && prettier --check '{lib,test}/**/*.{js,ts}'",
"format": "prettier --write '{lib,test}/**/*.{js,ts}'",
"prepare": "npm run build",
"test": "tsc -p tsconfig-test.json && jest -b --maxWorkers=2 --testTimeout=150000"
"test": "tsc -p tsconfig-test.json && jest --maxWorkers=2 --testTimeout=150000"
},
"author": "snyk.io",
"license": "Apache-2.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
},
"pkgs": [
{
"id": ".@unspecified",
"id": "basic-with-deps@unspecified",
"info": {
"name": ".",
"name": "basic-with-deps",
"version": "unspecified"
}
},
Expand Down Expand Up @@ -66,7 +66,7 @@
"nodes": [
{
"nodeId": "root-node",
"pkgId": ".@unspecified",
"pkgId": "basic-with-deps@unspecified",
"deps": [
{
"nodeId": "unknown:guava-87e0fd1df874ea3cbe577702fe6f17068b790fd8@unknown"
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures-with-wrappers/basic-with-deps/dep-graph.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
},
"pkgs": [
{
"id": ".@unspecified",
"id": "basic-with-deps@unspecified",
"info": {
"name": ".",
"name": "basic-with-deps",
"version": "unspecified"
}
},
Expand Down Expand Up @@ -66,7 +66,7 @@
"nodes": [
{
"nodeId": "root-node",
"pkgId": ".@unspecified",
"pkgId": "basic-with-deps@unspecified",
"deps": [
{
"nodeId": "com.google.guava:[email protected]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
},
"pkgs": [
{
"id": ".@unspecified",
"id": "basic-with-failed-dep@unspecified",
"info": {
"name": ".",
"name": "basic-with-failed-dep",
"version": "unspecified"
}
},
Expand Down Expand Up @@ -66,7 +66,7 @@
"nodes": [
{
"nodeId": "root-node",
"pkgId": ".@unspecified",
"pkgId": "basic-with-failed-dep@unspecified",
"deps": [
{
"nodeId": "com.google.guava:[email protected]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
},
"pkgs": [
{
"id": ".@unspecified",
"id": "configuration-consistency@unspecified",
"info": {
"name": ".",
"name": "configuration-consistency",
"version": "unspecified"
}
},
Expand Down Expand Up @@ -73,7 +73,7 @@
"nodes": [
{
"nodeId": "root-node",
"pkgId": ".@unspecified",
"pkgId": "configuration-consistency@unspecified",
"deps": [
{
"nodeId": "org.codehaus.groovy:[email protected]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
},
"pkgs": [
{
"id": ".@unspecified",
"id": "custom-configuration@unspecified",
"info": {
"name": ".",
"name": "custom-configuration",
"version": "unspecified"
}
},
Expand Down Expand Up @@ -38,7 +38,7 @@
"nodes": [
{
"nodeId": "root-node",
"pkgId": ".@unspecified",
"pkgId": "custom-configuration@unspecified",
"deps": [
{
"nodeId": "org.mockito:[email protected]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
},
"pkgs": [
{
"id": ".@unspecified",
"id": "no-build-gradle-in-root@unspecified",
"info": {
"name": ".",
"name": "no-build-gradle-in-root",
"version": "unspecified"
}
}
Expand All @@ -17,7 +17,7 @@
"nodes": [
{
"nodeId": "root-node",
"pkgId": ".@unspecified",
"pkgId": "no-build-gradle-in-root@unspecified",
"deps": []
}
]
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures-with-wrappers/empty-project/dep-graph.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
},
"pkgs": [
{
"id": ".@unspecified",
"id": "basic@unspecified",
"info": {
"name": ".",
"name": "basic",
"version": "unspecified"
}
}
Expand All @@ -17,7 +17,7 @@
"nodes": [
{
"nodeId": "root-node",
"pkgId": ".@unspecified",
"pkgId": "basic@unspecified",
"deps": []
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
},
"pkgs": [
{
"id": ".@1.0.0-SNAPSHOT",
"id": "kts-basic-with-deps@1.0.0-SNAPSHOT",
"info": {
"name": ".",
"name": "kts-basic-with-deps",
"version": "1.0.0-SNAPSHOT"
}
},
Expand Down Expand Up @@ -101,7 +101,7 @@
"nodes": [
{
"nodeId": "root-node",
"pkgId": ".@1.0.0-SNAPSHOT",
"pkgId": "kts-basic-with-deps@1.0.0-SNAPSHOT",
"deps": [
{
"nodeId": "org.jetbrains.kotlin:[email protected]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
},
"pkgs": [
{
"id": ".@1.0.0-SNAPSHOT",
"id": "kts-configuration-consistency@1.0.0-SNAPSHOT",
"info": {
"name": ".",
"name": "kts-configuration-consistency",
"version": "1.0.0-SNAPSHOT"
}
},
Expand Down Expand Up @@ -101,7 +101,7 @@
"nodes": [
{
"nodeId": "root-node",
"pkgId": ".@1.0.0-SNAPSHOT",
"pkgId": "kts-configuration-consistency@1.0.0-SNAPSHOT",
"deps": [
{
"nodeId": "org.jetbrains.kotlin:[email protected]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
},
"pkgs": [
{
"id": ".@1.0.0-SNAPSHOT",
"id": "kts-strict-lock-mode@1.0.0-SNAPSHOT",
"info": {
"name": ".",
"name": "kts-strict-lock-mode",
"version": "1.0.0-SNAPSHOT"
}
},
Expand Down Expand Up @@ -101,7 +101,7 @@
"nodes": [
{
"nodeId": "root-node",
"pkgId": ".@1.0.0-SNAPSHOT",
"pkgId": "kts-strict-lock-mode@1.0.0-SNAPSHOT",
"deps": [
{
"nodeId": "org.jetbrains.kotlin:[email protected]"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"graph": {
"nodes": [{ "deps": [], "nodeId": "root-node", "pkgId": ".@1.0" }],
"nodes": [{ "deps": [], "nodeId": "root-node", "pkgId": "kts-version-catalog-module@1.0" }],
"rootNodeId": "root-node"
},
"pkgManager": { "name": "gradle" },
"pkgs": [
{
"id": ".@1.0",
"info": { "name": ".", "version": "1.0" }
"id": "kts-version-catalog-module@1.0",
"info": { "name": "kts-version-catalog-module", "version": "1.0" }
}
],
"schemaVersion": "1.2.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
},
"pkgs": [
{
"id": ".@unspecified",
"id": "platform-project-mvn-bom@unspecified",
"info": {
"name": ".",
"name": "platform-project-mvn-bom",
"version": "unspecified"
}
},
Expand Down Expand Up @@ -45,7 +45,7 @@
"nodes": [
{
"nodeId": "root-node",
"pkgId": ".@unspecified",
"pkgId": "platform-project-mvn-bom@unspecified",
"deps": [
{
"nodeId": "org.springframework.boot:[email protected]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
},
"pkgs": [
{
"id": ".@unspecified",
"id": "repo-content-filtering@unspecified",
"info": {
"name": ".",
"name": "repo-content-filtering",
"version": "unspecified"
}
},
Expand Down Expand Up @@ -80,7 +80,7 @@
"nodes": [
{
"nodeId": "root-node",
"pkgId": ".@unspecified",
"pkgId": "repo-content-filtering@unspecified",
"deps": [
{
"nodeId": "com.google.guava:[email protected]"
Expand Down
Loading