Skip to content

Commit c64b33f

Browse files
committed
Hide columns conditionally when applying severity filter
1 parent a122958 commit c64b33f

File tree

3 files changed

+101
-43
lines changed

3 files changed

+101
-43
lines changed

dist/index.js

Lines changed: 43 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/summary.ts

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as core from "@actions/core";
2-
import { FilterOptions, filterPackages, Package, Report, Rule } from "./report";
2+
import { FilterOptions, filterPackages, Package, Severity, SeverityNames, Report, Rule } from "./report";
33
import { ActionInputs } from "./action";
44

55
const EVALUATION: any = {
@@ -24,36 +24,72 @@ export async function generateSummary(opts: ActionInputs, data: Report, filters?
2424
await core.summary.write({ overwrite: true });
2525
}
2626

27-
function addVulnTableToSummary(data: Report) {
28-
// Fallback to empty object if undefined
29-
const totalVuln = data.result.vulnTotalBySeverity ?? {};
30-
const fixableVuln = data.result.fixableVulnTotalBySeverity ?? {};
27+
const SEVERITY_LABELS: Record<Severity, string> = {
28+
critical: "🟣 Critical",
29+
high: "🔴 High",
30+
medium: "🟠 Medium",
31+
low: "🟡 Low",
32+
negligible: "⚪ Negligible"
33+
};
34+
35+
function countVulnsBySeverity(
36+
packages: Package[],
37+
severities: Severity[]
38+
): {
39+
total: Record<Severity, number>;
40+
fixable: Record<Severity, number>;
41+
} {
42+
const result = {
43+
total: { critical: 0, high: 0, medium: 0, low: 0, negligible: 0 },
44+
fixable: { critical: 0, high: 0, medium: 0, low: 0, negligible: 0 }
45+
};
46+
47+
for (const pkg of packages) {
48+
for (const vuln of pkg.vulns ?? []) {
49+
const sev = (vuln.severity.value as Severity).toLowerCase() as Severity;
50+
if (severities.includes(sev)) {
51+
result.total[sev]++;
52+
if (vuln.fixedInVersion || pkg.suggestedFix) {
53+
result.fixable[sev]++;
54+
}
55+
}
56+
}
57+
}
58+
return result;
59+
}
60+
61+
function getVisibleSeverities(minSeverity?: Severity): Severity[] {
62+
const SEVERITY_ORDER: Severity[] = ["critical", "high", "medium", "low", "negligible"];
63+
if (!minSeverity || minSeverity === "negligible") {
64+
return SEVERITY_ORDER;
65+
}
66+
const minIndex = SEVERITY_ORDER.indexOf(minSeverity);
67+
return minIndex >= 0 ? SEVERITY_ORDER.slice(0, minIndex + 1) : SEVERITY_ORDER;
68+
}
69+
70+
// Updated summary function
71+
function addVulnTableToSummary(
72+
data: Report,
73+
minSeverity?: Severity
74+
) {
75+
const pkgs = data.result.packages;
76+
const visibleSeverities = getVisibleSeverities(minSeverity);
77+
78+
const totalVulns = countVulnsBySeverity(pkgs, visibleSeverities);
3179

3280
core.summary.addHeading(`Vulnerabilities summary`, 2);
3381
core.summary.addTable([
3482
[
3583
{ data: '', header: true },
36-
{ data: '🟣 Critical', header: true },
37-
{ data: '🔴 High', header: true },
38-
{ data: '🟠 Medium', header: true },
39-
{ data: '🟡 Low', header: true },
40-
{ data: '⚪ Negligible', header: true }
84+
...visibleSeverities.map(s => ({ data: SEVERITY_LABELS[s], header: true }))
4185
],
4286
[
4387
{ data: '⚠️ Total Vulnerabilities', header: true },
44-
`${totalVuln.critical ?? 0}`,
45-
`${totalVuln.high ?? 0}`,
46-
`${totalVuln.medium ?? 0}`,
47-
`${totalVuln.low ?? 0}`,
48-
`${totalVuln.negligible ?? 0}`
88+
...visibleSeverities.map(s => `${totalVulns.total[s] ?? 0}`)
4989
],
5090
[
5191
{ data: '🔧 Fixable Vulnerabilities', header: true },
52-
`${fixableVuln.critical ?? 0}`,
53-
`${fixableVuln.high ?? 0}`,
54-
`${fixableVuln.medium ?? 0}`,
55-
`${fixableVuln.low ?? 0}`,
56-
`${fixableVuln.negligible ?? 0}`
92+
...visibleSeverities.map(s => `${totalVulns.fixable[s] ?? 0}`)
5793
],
5894
]);
5995
}
@@ -166,7 +202,7 @@ function addReportToSummary(data: Report) {
166202
core.summary.addHeading(`Rule Bundle: ${bundle.name}`, 4)
167203

168204
bundle.rules.forEach(rule => {
169-
core.summary.addHeading(`${EVALUATION[rule.evaluationResult]} Rule: ${rule.description}`, 5)
205+
core.summary.addHeading(`Rule: ${rule.description}`, 5)
170206

171207
if (rule.evaluationResult != "passed") {
172208
if (rule.failureType == "pkgVulnFailure") {

0 commit comments

Comments
 (0)