Skip to content

Commit

Permalink
fix: Remove scanResult from json output
Browse files Browse the repository at this point in the history
The scanResult object was added to the json output where
it should only have been added to calculate the the
dockerfile warning string.
This change removes the scanResult from the json output
as it was not supposed to be exposed.
  • Loading branch information
snaftaly committed Jan 5, 2022
1 parent d9494db commit fcff339
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1,144 deletions.
25 changes: 15 additions & 10 deletions src/lib/formatters/test/format-test-results.ts
Expand Up @@ -35,12 +35,18 @@ import {
} from 'snyk-docker-plugin/dist';
import { ScanResult } from '../../ecosystems/types';

export function formatJsonVulnerabilityStructure(jsonData, options: Options) {
const jsonDataClone = cloneDeep(jsonData);
function createJsonResultOutput(jsonResult, options: Options) {
const jsonResultClone = cloneDeep(jsonResult);
delete jsonResultClone.scanResult;

formatJsonVulnerabilityStructure(jsonResultClone, options);
return jsonResultClone;
}

function formatJsonVulnerabilityStructure(jsonResult, options: Options) {
if (options['group-issues']) {
jsonDataClone.vulnerabilities = Object.values(
(jsonDataClone.vulnerabilities || []).reduce((acc, vuln): Record<
jsonResult.vulnerabilities = Object.values(
(jsonResult.vulnerabilities || []).reduce((acc, vuln): Record<
string,
any
> => {
Expand All @@ -52,14 +58,13 @@ export function formatJsonVulnerabilityStructure(jsonData, options: Options) {
);
}

if (jsonDataClone.vulnerabilities) {
jsonDataClone.vulnerabilities.forEach((vuln) => {
if (jsonResult.vulnerabilities) {
jsonResult.vulnerabilities.forEach((vuln) => {
if (vuln.reachability) {
vuln.reachability = getReachabilityJson(vuln.reachability);
}
});
}
return jsonDataClone;
}

export function extractDataToSendFromResults(
Expand All @@ -84,12 +89,12 @@ export function extractDataToSendFromResults(
if (options.json || options['json-file-output']) {
if (Array.isArray(jsonData)) {
const jsonResult = jsonData.map((res) =>
formatJsonVulnerabilityStructure(res, options),
createJsonResultOutput(res, options),
);
stringifiedJsonData = jsonStringifyLargeObject(jsonResult);
} else {
stringifiedJsonData = jsonStringifyLargeObject(
formatJsonVulnerabilityStructure(jsonData, options),
createJsonResultOutput(jsonData, options),
);
}
}
Expand All @@ -100,7 +105,7 @@ export function extractDataToSendFromResults(
: stringifiedJsonData;

return {
stdout: dataToSend, // this is for the human-readable stdout output and is set (but not used) even if --json or --sarif is set
stdout: dataToSend, // this is for the human-readable stdout output and is set (but not used) even if --json or --sarif is not set
stringifiedData, // this will be used to display either the Snyk or SARIF format JSON to stdout if --json or --sarif is set
stringifiedJsonData, // this will be used for the --json-file-output=<file.json> option
stringifiedSarifData, // this will be used for the --sarif-file-output=<file.json> option
Expand Down

0 comments on commit fcff339

Please sign in to comment.