Skip to content

Commit

Permalink
fix: formatting & vuln colors
Browse files Browse the repository at this point in the history
  • Loading branch information
lili2311 committed Apr 16, 2021
1 parent 7f9c9f6 commit cfd5698
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 53 deletions.
2 changes: 1 addition & 1 deletion packages/snyk-fix/src/lib/issues/fixable-issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ function getUpgradableIssues(
issues.push(...updates[id].vulns);
}

return new Set(issues).size;
return issues.length;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ export async function showResultsSummary(
resultsByPlugin,
exceptionsByScanType,
);
const fixedIssuesSummary = `${calculateFixedIssues(resultsByPlugin)} fixed issues`;
const fixedIssuesSummary = `${chalk.bold(
calculateFixedIssues(resultsByPlugin),
)} fixed issues`;
return `\n${successfulFixesSummary}${
unresolvedSummary ? `\n\n${unresolvedSummary}` : ''
}${
Expand Down Expand Up @@ -145,15 +147,21 @@ export function calculateFixedIssues(
): number {
const fixedIssues: string[] = [];
for (const plugin of Object.keys(resultsByPlugin)) {
for (const i of resultsByPlugin[plugin].succeeded) {
i.changes
for (const entity of resultsByPlugin[plugin].succeeded) {
// count unique vulns fixed per scanned entity
// some fixed may need to be made in multiple places
// and would count multiple times otherwise.
const fixedPerEntity = new Set<string>();
entity.changes
.filter((c) => c.success)
.forEach((c) => {
fixedIssues.push(...c.issueIds);
c.issueIds.map((i) => fixedPerEntity.add(i));
});
fixedIssues.push(...Array.from(fixedPerEntity));
}
}
return fixedIssues.length;

return fixedIssues.length;
}

export function calculateFailed(
Expand Down Expand Up @@ -209,22 +217,22 @@ export const severitiesColourMapping: {
} = {
low: {
colorFunc(text) {
return chalk.blueBright(text);
return chalk.hex('#BCBBC8')(text);
},
},
medium: {
colorFunc(text) {
return chalk.yellowBright(text);
return chalk.hex('#EDD55E')(text);
},
},
high: {
colorFunc(text) {
return chalk.redBright(text);
return chalk.hex('#FF872F')(text);
},
},
critical: {
colorFunc(text) {
return chalk.magentaBright(text);
return chalk.hex('#FF0B0B')(text);
},
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
EntityToFix,
FixChangesSummary,
FixOptions,
Issue,
RemediationChanges,
Workspace,
} from '../../../../types';
Expand Down Expand Up @@ -110,7 +111,10 @@ async function fixAll(
{
success: true,
userMessage: `Fixed through ${fixedCache[filePath].fixedIn}`,
issueIds: fixedCache[filePath].issueIds,
issueIds: getFixedEntityIssues(
fixedCache[filePath].issueIds,
entity.testResult.issues,
),
},
],
});
Expand All @@ -122,15 +126,16 @@ async function fixAll(
throw new NoFixesCouldBeAppliedError();
}

// can't use .flat() or .flatMap() because it's not supported in Node 10
const issueIds: string[] = [];
// keep fixed issues unique across files that are part of the same project
// the test result is for 1 entry entity.
const uniqueIssueIds = new Set<string>();
for (const c of changes) {
issueIds.push(...c.issueIds);
c.issueIds.map((i) => uniqueIssueIds.add(i));
}
Object.keys(fixedMeta).forEach((f) => {
fixedCache[f] = {
fixedIn: targetFile,
issueIds,
issueIds: Array.from(uniqueIssueIds),
};
});
handlerResult.succeeded.push({ original: entity, changes });
Expand Down Expand Up @@ -193,7 +198,6 @@ export async function applyAllFixes(
const { dir, base } = pathLib.parse(entryFileName);
const provenance = await extractProvenance(workspace, dir, base);
const upgradeChanges: FixChangesSummary[] = [];
const appliedUpgradeRemediation: string[] = [];
/* Apply all upgrades first across all files that are included */
for (const fileName of Object.keys(provenance)) {
const skipApplyingPins = true;
Expand Down Expand Up @@ -308,3 +312,16 @@ export async function selectFileForPinning(
}
return { fileContent: requirementsTxt, fileName };
}

function getFixedEntityIssues(
fixedIssueIds: string[],
issues: Issue[],
): string[] {
const fixed: string[] = [];
for (const { issueId } of issues) {
if (fixedIssueIds.includes(issueId)) {
fixed.push(issueId);
}
}
return fixed;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Summary:
2 items were successfully fixed
6 total issues: 2 High | 2 Medium | 2 Low
6 fixable issues
8 fixed issues"
6 fixed issues"
`;

exports[`fix *req*.txt / *.txt Python projects fixes multiple files via -r with the same name (some were already fixed) 1`] = `
Expand All @@ -43,7 +43,7 @@ Summary:
3 items were successfully fixed
6 total issues: 3 High | 3 Medium
6 fixable issues
9 fixed issues"
6 fixed issues"
`;

exports[`fix *req*.txt / *.txt Python projects retains python markers 1`] = `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ describe('selectFileForPinning', () => {
patch: {},
ignore: {},
pin: {
'Django@1.6.1': {
upgradeTo: 'Django@2.0.1',
'django@1.6.1': {
upgradeTo: 'django@2.0.1',
vulns: [],
isTransitive: false,
},
Expand Down Expand Up @@ -63,8 +63,8 @@ describe('selectFileForPinning', () => {
patch: {},
ignore: {},
pin: {
'Django@1.6.1': {
upgradeTo: 'Django@2.0.1',
'django@1.6.1': {
upgradeTo: 'django@2.0.1',
vulns: [],
isTransitive: false,
},
Expand Down Expand Up @@ -103,8 +103,8 @@ describe('selectFileForPinning', () => {
patch: {},
ignore: {},
pin: {
'Django@1.6.1': {
upgradeTo: 'Django@2.0.1',
'django@1.6.1': {
upgradeTo: 'django@2.0.1',
vulns: [],
isTransitive: false,
},
Expand Down Expand Up @@ -157,8 +157,8 @@ describe('fix *req*.txt / *.txt Python projects', () => {
patch: {},
ignore: {},
pin: {
'Django@1.6.1': {
upgradeTo: 'Django@2.0.1',
'django@1.6.1': {
upgradeTo: 'django@2.0.1',
vulns: [],
isTransitive: false,
},
Expand Down Expand Up @@ -225,8 +225,8 @@ describe('fix *req*.txt / *.txt Python projects', () => {
patch: {},
ignore: {},
pin: {
'Django@1.6.1': {
upgradeTo: 'Django@2.0.1',
'django@1.6.1': {
upgradeTo: 'django@2.0.1',
vulns: [],
isTransitive: false,
},
Expand Down Expand Up @@ -300,8 +300,8 @@ describe('fix *req*.txt / *.txt Python projects', () => {
patch: {},
ignore: {},
pin: {
'Django@1.6.1': {
upgradeTo: 'Django@2.0.1',
'django@1.6.1': {
upgradeTo: 'django@2.0.1',
vulns: [],
isTransitive: false,
},
Expand Down Expand Up @@ -390,8 +390,8 @@ describe('fix *req*.txt / *.txt Python projects', () => {
patch: {},
ignore: {},
pin: {
'Django@1.6.1': {
upgradeTo: 'Django@2.0.1',
'django@1.6.1': {
upgradeTo: 'django@2.0.1',
vulns: [],
isTransitive: false,
},
Expand Down Expand Up @@ -532,9 +532,9 @@ describe('fix *req*.txt / *.txt Python projects', () => {
patch: {},
ignore: {},
pin: {
'Django@1.6.1': {
'django@1.6.1': {
// matches as the same when file has Django
upgradeTo: 'Django@2.0.1',
upgradeTo: 'django@2.0.1',
vulns: [],
isTransitive: false,
},
Expand Down Expand Up @@ -665,8 +665,8 @@ describe('fix *req*.txt / *.txt Python projects', () => {
patch: {},
ignore: {},
pin: {
'Django@1.6.1': {
upgradeTo: 'Django@2.0.1',
'django@1.6.1': {
upgradeTo: 'django@2.0.1',
vulns: [],
isTransitive: false,
},
Expand Down Expand Up @@ -799,8 +799,8 @@ describe('fix *req*.txt / *.txt Python projects', () => {
patch: {},
ignore: {},
pin: {
'Django@1.6.1': {
upgradeTo: 'Django@2.0.1',
'django@1.6.1': {
upgradeTo: 'django@2.0.1',
vulns: [],
isTransitive: false,
},
Expand Down Expand Up @@ -870,7 +870,7 @@ describe('fix *req*.txt / *.txt Python projects', () => {
...generateTestResult(),
issues: [
{
pkgName: 'Django@1.6.1',
pkgName: 'django@1.6.1',
issueId: 'SNYK-1',
fixInfo: {},
},
Expand Down Expand Up @@ -898,8 +898,8 @@ describe('fix *req*.txt / *.txt Python projects', () => {
patch: {},
ignore: {},
pin: {
'Django@1.6.1': {
upgradeTo: 'Django@2.0.1',
'django@1.6.1': {
upgradeTo: 'django@2.0.1',
vulns: ['SNYK-1'],
isTransitive: false,
},
Expand Down Expand Up @@ -1039,7 +1039,7 @@ describe('fix *req*.txt / *.txt Python projects', () => {
...generateTestResult(),
issues: [
{
pkgName: 'Django@1.6.1',
pkgName: 'django@1.6.1',
issueId: 'SNYK-1',
fixInfo: {
upgradePaths: [],
Expand Down Expand Up @@ -1092,8 +1092,8 @@ describe('fix *req*.txt / *.txt Python projects', () => {
patch: {},
ignore: {},
pin: {
'Django@1.6.1': {
upgradeTo: 'Django@2.0.1',
'django@1.6.1': {
upgradeTo: 'django@2.0.1',
vulns: ['SNYK-1'],
isTransitive: false,
},
Expand Down Expand Up @@ -1126,7 +1126,6 @@ describe('fix *req*.txt / *.txt Python projects', () => {
quiet: true,
stripAnsi: true,
});

const requirements = fs.readFileSync(
pathLib.resolve(
workspacesPath,
Expand Down Expand Up @@ -1218,7 +1217,7 @@ describe('fix *req*.txt / *.txt Python projects', () => {
expect(result.results.python.succeeded[1].changes).toEqual([
{
success: true,
issueIds: ['SNYK-1', 'SNYK-1', 'SNYK-2', 'SNYK-3'],
issueIds: ['SNYK-1', 'SNYK-2', 'SNYK-3'],
userMessage: 'Fixed through app-with-constraints/requirements.txt',
},
]);
Expand Down
10 changes: 8 additions & 2 deletions packages/snyk-fix/test/unit/__snapshots__/fix.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ Summary:
1 items were not fixed
0 items were successfully fixed
1 total issues: 1 High
1 fixable issues",
1 fixable issues
0 fixed issues",
"meta": Object {
"failed": 1,
"fixed": 0,
Expand All @@ -95,7 +96,8 @@ Summary:
0 items were not fixed
1 items were successfully fixed
1 total issues: 1 High
1 fixable issues",
1 fixable issues
1 fixed issues",
"meta": Object {
"failed": 0,
"fixed": 1,
Expand All @@ -108,10 +110,12 @@ Summary:
Object {
"changes": Array [
Object {
"from": "django@1.6.1",
"issueIds": Array [
"vuln-id",
],
"success": true,
"to": "django@2.0.1",
"userMessage": "Pinned django from 1.6.1 to 2.0.1",
},
],
Expand Down Expand Up @@ -336,10 +340,12 @@ Object {
Object {
"changes": Array [
Object {
"from": "django@1.6.1",
"issueIds": Array [
"vuln-id",
],
"success": true,
"to": "django@2.0.1",
"userMessage": "Upgraded django from 1.6.1 to 2.0.1",
},
],
Expand Down
2 changes: 1 addition & 1 deletion packages/snyk-fix/test/unit/fix.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('Snyk fix', () => {
expect(writeFileSpy).not.toHaveBeenCalled();
});

it.only('Snyk fix returns results for supported & unsupported type', async () => {
it('Snyk fix returns results for supported & unsupported type', async () => {
// Arrange
const projectTestResult = generateEntityToFix(
'pip',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('format successful item', () => {
{
success: true,
userMessage: 'Upgraded Django from 1.6.1 to 2.0.1',
issueIds: ['vuln-2'],
},
];
const res = await formatChangesSummary(entity, changesSummary);
Expand Down

0 comments on commit cfd5698

Please sign in to comment.