Skip to content

Commit

Permalink
feat: Multi licenses formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
lili2311 committed Oct 3, 2019
1 parent c965884 commit 3a7e995
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 16 deletions.
11 changes: 6 additions & 5 deletions src/cli/commands/test/formatters/legacy-format-issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import {
GroupedVuln,
AnnotatedIssue,
DockerIssue,
LegalInstruction,
} from '../../../../lib/snyk-test/legacy';
import { formatLegalInstructions } from './legal-license-instructions';

export function formatIssues(
vuln: GroupedVuln,
Expand Down Expand Up @@ -50,11 +52,10 @@ export function formatIssues(
: '',
fixedIn: options.docker ? createFixedInText(vuln) : '',
dockerfilePackage: options.docker ? dockerfileInstructionText(vuln) : '',
legalInstructions: vuln.legalInstructions
? '\n Legal instructions:\n ' +
wrap(vuln.legalInstructions, 100)
.split('\n')
.join('\n ')
legalInstructions: vuln.legalInstructionsArray
? chalk.bold('\n Legal instructions:\n') +
' '.repeat(2) +
formatLegalInstructions(vuln.legalInstructionsArray, 2)
: '',
};

Expand Down
18 changes: 18 additions & 0 deletions src/cli/commands/test/formatters/legal-license-instructions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { LegalInstruction } from '../../../../lib/snyk-test/legacy';
import * as wrap from 'wrap-ansi';
import chalk from 'chalk';

export function formatLegalInstructions(
legalInstructions: LegalInstruction[],
paddingLength = 4,
): string {
const legalContent: string[] = legalInstructions.map((legalData) =>
wrap(
chalk.bold(`○ for ${legalData.licenseName}: `) + legalData.legalContent,
100,
)
.split('\n')
.join('\n' + ' '.repeat(paddingLength)),
);
return legalContent.join('\n' + ' '.repeat(paddingLength));
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import {
DependencyPins,
UpgradeRemediation,
PinRemediation,
LegalInstruction,
} from '../../../../lib/snyk-test/legacy';
import { SEVERITIES } from '../../../../lib/snyk-test/common';
import { formatLegalInstructions } from './legal-license-instructions';

interface BasicVulnInfo {
type: string;
Expand All @@ -23,7 +25,7 @@ interface BasicVulnInfo {
name: string;
version: string;
fixedIn: string[];
legalInstructions?: string;
legalInstructions?: LegalInstruction[];
paths: string[][];
}

Expand Down Expand Up @@ -58,7 +60,7 @@ export function formatIssuesWithRemediation(
type: vuln.metadata.type,
version: vuln.version,
fixedIn: vuln.fixedIn,
legalInstructions: vuln.legalInstructions,
legalInstructions: vuln.legalInstructionsArray,
paths: vuln.list.map((v) => v.from),
};

Expand Down Expand Up @@ -409,7 +411,7 @@ function formatIssue(
title: string,
severity: SEVERITY,
isNew: boolean,
legalInstructions: string | undefined,
legalInstructions: LegalInstruction[] | undefined,
vulnerableModule: string,
paths: string[][],
testOptions: TestOptions,
Expand All @@ -433,8 +435,10 @@ function formatIssue(
};
const newBadge = isNew ? ' (new)' : '';
const name = vulnerableModule ? ` in ${chalk.bold(vulnerableModule)}` : '';
const wrapLegalText = wrap(`${legalInstructions}`, 100);
const formatLegalText = wrapLegalText.split('\n').join('\n ');
let legalLicenseInstructionsText;
if (legalInstructions) {
legalLicenseInstructionsText = formatLegalInstructions(legalInstructions);
}

let introducedBy = '';
if (
Expand All @@ -452,10 +456,10 @@ function formatIssue(
)} other path(s)`;
} else if (testOptions.showVulnPaths === 'all' && paths) {
introducedBy =
`\n introduced by:` +
'\n introduced by:' +
paths
.slice(0, 1000)
.map((p) => `\n ` + printPath(p))
.map((p) => '\n ' + printPath(p))
.join('');
if (paths.length > 1000) {
introducedBy += `\n and ${chalk.cyanBright(
Expand All @@ -473,8 +477,10 @@ function formatIssue(
`[${config.ROOT}/vuln/${id}]` +
name +
introducedBy +
(legalInstructions
? `${chalk.bold('\n Legal instructions')}:\n ${formatLegalText}`
(legalLicenseInstructionsText
? `${chalk.bold(
'\n Legal instructions',
)}:\n ${legalLicenseInstructionsText}`
: '')
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ function groupVulnerabilities(vulns): GroupedVuln[] {
map[curr.id].dockerfileInstruction = curr.dockerfileInstruction;
map[curr.id].dockerBaseImage = curr.dockerBaseImage;
map[curr.id].nearestFixedInVersion = curr.nearestFixedInVersion;
map[curr.id].legalInstructions = curr.legalInstructions;
map[curr.id].legalInstructionsArray = curr.legalInstructionsArray;
}

map[curr.id].list.push(curr);
Expand Down
6 changes: 5 additions & 1 deletion src/lib/snyk-test/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ export interface GroupedVuln {
version: string;
isFixable: boolean;
fixedIn: string[];
legalInstructions?: string;
legalInstructionsArray?: LegalInstruction[];
}

export interface LegalInstruction {
licenseName: string;
legalContent: string;
}
export interface IssueData {
id: string;
packageName: string;
Expand Down

0 comments on commit 3a7e995

Please sign in to comment.