Skip to content

Commit

Permalink
chore: export function to map severity to color
Browse files Browse the repository at this point in the history
  • Loading branch information
karenyavine committed Feb 16, 2021
1 parent ea94dea commit 96f619e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 26 deletions.
9 changes: 2 additions & 7 deletions src/cli/commands/protect/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ import * as config from '../../../lib/config';
import * as snykPolicy from 'snyk-policy';
import chalk from 'chalk';
import { AnnotatedIssue, SEVERITY } from '../../../lib/snyk-test/legacy';
import {
legacySeveritiesColourMapping,
defaultSeverityColor,
} from '../../../lib/snyk-test/common';
import { getLegacySeveritiesColour } from '../../../lib/snyk-test/common';
import { titleCaseText } from '../test/formatters/legacy-format-issue';

const debug = debugModule('snyk');
Expand All @@ -50,9 +47,7 @@ function sort(prop) {

function createSeverityBasedIssueHeading(msg: string, severity: SEVERITY) {
// Example: ✗ Medium severity vulnerability found in xmldom
const severityColor = legacySeveritiesColourMapping[severity]
? legacySeveritiesColourMapping[severity]
: defaultSeverityColor;
const severityColor = getLegacySeveritiesColour(severity);
return severityColor.colorFunc(msg);
}

Expand Down
9 changes: 2 additions & 7 deletions src/cli/commands/test/formatters/legacy-format-issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ import {
import { formatLegalInstructions } from './legal-license-instructions';
import { getReachabilityText } from './format-reachability';
import { PATH_SEPARATOR } from '../../constants';
import {
legacySeveritiesColourMapping,
defaultSeverityColor,
} from '../../../../lib/snyk-test/common';
import { getLegacySeveritiesColour } from '../../../../lib/snyk-test/common';

export function formatIssues(
vuln: GroupedVuln,
Expand Down Expand Up @@ -99,9 +96,7 @@ function createSeverityBasedIssueHeading({
}: CreateSeverityBasedIssueHeading) {
// Example: ✗ Medium severity vulnerability found in xmldom
const vulnTypeText = type === 'license' ? 'issue' : 'vulnerability';
const severityColor = legacySeveritiesColourMapping[severity]
? legacySeveritiesColourMapping[severity]
: defaultSeverityColor;
const severityColor = getLegacySeveritiesColour(severity);

let originalSeverityStr = '';
if (originalSeverity && originalSeverity !== severity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import {
} from '../../../../lib/snyk-test/legacy';
import {
SEVERITIES,
severitiesColourMapping,
defaultSeverityColor,
getSeveritiesColour,
} from '../../../../lib/snyk-test/common';
import { formatLegalInstructions } from './legal-license-instructions';
import {
Expand Down Expand Up @@ -442,9 +441,7 @@ export function formatIssue(
reachability?: REACHABILITY,
sampleReachablePaths?: SampleReachablePaths,
): string {
const severityColor = severitiesColourMapping[severity]
? severitiesColourMapping[severity]
: defaultSeverityColor;
const severityColor = getSeveritiesColour(severity);

const newBadge = isNew ? ' (new)' : '';
const name = vulnerableModule ? ` in ${chalk.bold(vulnerableModule)}` : '';
Expand Down
9 changes: 2 additions & 7 deletions src/cli/commands/test/iac-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import { printPath } from './formatters/remediation-based-format-issues';
import { titleCaseText } from './formatters/legacy-format-issue';
import * as sarif from 'sarif';
import { SEVERITY } from '../../../lib/snyk-test/legacy';
import {
severitiesColourMapping,
defaultSeverityColor,
} from '../../../lib/snyk-test/common';
import { getSeveritiesColour } from '../../../lib/snyk-test/common';
import { IacFileInDirectory } from '../../../lib/types';
import upperFirst = require('lodash.upperfirst');
const debug = Debug('iac-output');
Expand All @@ -35,9 +32,7 @@ function formatIacIssue(

const description = extractOverview(issue.description).trim();
const descriptionLine = `\n ${description}\n`;
const severityColor = severitiesColourMapping[issue.severity]
? severitiesColourMapping[issue.severity]
: defaultSeverityColor;
const severityColor = getSeveritiesColour(issue.severity);

return (
severityColor.colorFunc(
Expand Down
8 changes: 8 additions & 0 deletions src/lib/snyk-test/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ export const defaultSeverityColor = {
},
};

export function getSeveritiesColour(severity: string) {
return severitiesColourMapping[severity] || defaultSeverityColor;
}

export function getLegacySeveritiesColour(severity: string) {
return legacySeveritiesColourMapping[severity] || defaultSeverityColor;
}

export enum FAIL_ON {
all = 'all',
upgradable = 'upgradable',
Expand Down

0 comments on commit 96f619e

Please sign in to comment.