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: dep-graph json output #5618

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 9 additions & 20 deletions src/lib/print-deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as depGraphLib from '@snyk/dep-graph';
import { DepDict, Options, MonitorOptions } from './types';
import { legacyCommon as legacyApi } from '@snyk/cli-interface';
import { countPathsToGraphRoot } from './utils';
import { jsonStringifyLargeObject } from './json';

export async function maybePrintDepGraph(
options: Options | MonitorOptions,
Expand All @@ -20,18 +19,11 @@ export async function maybePrintDepGraph(
)) as legacyApi.DepTree;
maybePrintDepTree(options, depTree);
} else {
if (options['print-deps']) {
if (options.json) {
console.warn(
'--print-deps --json option not yet supported for large projects. Displaying graph json output instead',
);
// TODO @boost: add as output graphviz 'dot' file to visualize?
console.log(jsonStringifyLargeObject(depGraph.toJSON()));
} else {
console.warn(
'--print-deps option not yet supported for large projects. Try with --json.',
);
}
if (options['print-deps'] && !options.json) {
// don't print a warning when --json is being used, it can invalidate the JSON output
console.warn(
'--print-deps option not yet supported for large projects. Try with --json.',
);
}
}
}
Expand All @@ -42,13 +34,10 @@ export function maybePrintDepTree(
options: Options | MonitorOptions,
rootPackage: legacyApi.DepTree,
) {
if (options['print-deps']) {
if (options.json) {
// Will produce 2 JSON outputs, one for the deps, one for the vuln scan.
console.log(jsonStringifyLargeObject(rootPackage));
} else {
printDepsForTree({ [rootPackage.name!]: rootPackage });
}
if (options['print-deps'] && !options.json) {
// only print human readable output tree if NOT using --json
// to ensure this output does not invalidate JSON output
printDepsForTree({ [rootPackage.name!]: rootPackage });
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/lib/snyk-test/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export interface LegacyVulnApiResult extends BasicResultData {
filesystemPolicy?: boolean;
uniqueCount?: any;
remediation?: RemediationChanges;
depGraph?: depGraphLib.DepGraphData;
}

export interface BaseImageRemediation {
Expand Down Expand Up @@ -451,6 +452,9 @@ function convertTestDepGraphResultToLegacy(
severityThreshold,
remediation: result.remediation,
};
if (options['print-deps']) {
legacyRes.depGraph = depGraph.toJSON();
}

return legacyRes;
}
Expand Down
42 changes: 42 additions & 0 deletions test/jest/acceptance/cli-json-file-output.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createProjectFromWorkspace } from '../util/createProject';
import { runSnykCLI } from '../util/runSnykCLI';
import { humanFileSize } from '../../utils';
import { getServerPort } from '../util/getServerPort';
import * as depGraphLib from '@snyk/dep-graph';

jest.setTimeout(1000 * 60);

Expand Down Expand Up @@ -112,4 +113,45 @@ describe('test --json-file-output', () => {
expect(fileExists).toBeFalsy();
expect(code).toEqual(0);
});

describe('print-deps', () => {
it('saves JSON output to file with depGraph when --print-deps is used', async () => {
const project = await createProjectFromWorkspace('maven-app');
const outputPath = 'json-file-output.json';

const { code } = await runSnykCLI(
`test --print-deps --json-file-output=${outputPath}`,
{
cwd: project.path(),
env,
},
);

expect(code).toEqual(0);
const json = await project.readJSON(outputPath);
expect(json.depGraph).toBeTruthy();
const depGraph = depGraphLib.createFromJSON(json.depGraph);
expect(depGraph.getPkgs()).toContainEqual({
name: 'axis:axis',
version: '1.4',
});
});

it('saves JSON output to file without a depGraph when --print-deps is not used', async () => {
const project = await createProjectFromWorkspace('maven-app');
const outputPath = 'json-file-output.json';

const { code } = await runSnykCLI(
`test --json-file-output=${outputPath}`,
{
cwd: project.path(),
env,
},
);

expect(code).toEqual(0);
const json = await project.readJSON(outputPath);
expect(json.depGraph).toBeUndefined();
});
});
});
35 changes: 35 additions & 0 deletions test/jest/acceptance/cli-json-output.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getServerPort } from '../util/getServerPort';
import { runSnykCLI } from '../util/runSnykCLI';
import { AppliedPolicyRules } from '../../../src/lib/formatters/types';
import * as Parser from 'jsonparse';
import * as depGraphLib from '@snyk/dep-graph';

jest.setTimeout(1000 * 60);

Expand Down Expand Up @@ -193,4 +194,38 @@ describe('test --json', () => {
}
});
});

describe('print-deps', () => {
it('JSON output contains depGraph when --print-deps is used', async () => {
const project = await createProjectFromWorkspace('maven-app');

const { code, stdout } = await runSnykCLI(`test --print-deps --json`, {
cwd: project.path(),
env,
});

expect(code).toEqual(0);
console.log(stdout);

const json = JSON.parse(stdout);
const depGraph = depGraphLib.createFromJSON(json.depGraph);
expect(depGraph.getPkgs()).toContainEqual({
name: 'axis:axis',
version: '1.4',
});
});

it('JSON output has no depGraph when --print-deps is not used', async () => {
const project = await createProjectFromWorkspace('maven-app');

const { code, stdout } = await runSnykCLI(`test --json`, {
cwd: project.path(),
env,
});

expect(code).toEqual(0);
const json = JSON.parse(stdout);
expect(json.depGraph).toBeUndefined();
});
});
});
Loading