Skip to content

Commit

Permalink
feat: add summary
Browse files Browse the repository at this point in the history
  • Loading branch information
antoine-coulon committed Sep 1, 2022
1 parent e33f843 commit 7816dd7
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 98 deletions.
102 changes: 4 additions & 98 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,15 @@
import core from "@actions/core";
import { runPipeline } from "@nodesecure/ci";
import { generateSummary } from "./src/summary.js";

const directory = core.getInput("directory") ?? process.env.GITHUB_WORKSPACE;
const strategy = core.getInput("strategy");
const vulnerabilities = core.getInput("vulnerabilities");
const warnings = core.getInput("warnings");
const reporters = core.getInput("reporters");

function generateOutcomeWithEmoji(reportData, hasSpecificOutcome) {
if (hasSpecificOutcome) {
if (warnings === "warning") {
return `🟡 ${reportData.length}`;
} else if (warnings === "off") {
return "(skipped)";
}
}

if (reportData.length === 0) {
return `✅ 0`;
}

return `❌ ${reportData.length}`;
}

function generateOutcomeDepsWarnings(depsWarnings) {
return depsWarnings.flatMap(({ warnings, package: packageName }) =>
warnings.map((warning) => {
const location = warning.location.flatMap((location) =>
location.join(":")
);
return `${packageName} from ${warning.file}:${location}`;
})
);
}

function generateOutcomeVulns(vulns) {
return vulns.map((vuln) => {
const vulnRanges = vuln.vulnerableRanges.join(", ");

return `[${vuln.severity}] ${vuln.package}: ${vuln.title} ${vulnRanges}`;
});
}

try {
const result = await runPipeline({
const report = await runPipeline({
warnings,
strategy,
reporters,
Expand All @@ -52,69 +18,9 @@ try {
autoExitAfterFailure: false,
});

const vulns = result.data.dependencies.vulnerabilities;
const depsWarnings = result.data.dependencies.warnings;
const globalWarnings = result.data.warnings;
const isReportSuccessful = result.status === "success";

await core.summary
.addHeading(
`${
isReportSuccessful ? "✅" : "❌"
} [${result.status.toUpperCase()}]: @nodesecure/ci analysis`
)
.addTable([
[
{ data: "Global warnings", header: true },
{ data: "Dependency warnings", header: true },
{ data: "Dependency vulnerabilities", header: true },
],
[
generateOutcomeWithEmoji(globalWarnings),
generateOutcomeWithEmoji(depsWarnings, true),
generateOutcomeWithEmoji(vulns),
],
])
.addBreak();

if (vulns.length > 0) {
await core.summary
.addHeading(
`(${generateOutcomeWithEmoji(vulns)}) Dependencies vulnerabilities:`
)
.addList(generateOutcomeVulns(vulns));
await core.summary.addSeparator();
}

if (globalWarnings.length > 0) {
await core.summary
.addHeading(
`(${generateOutcomeWithEmoji(globalWarnings)}) Global warnings:`
)
.addList(globalWarnings);
await core.summary.addSeparator();
}

if (depsWarnings.length > 0) {
await core.summary
.addHeading(
`(${generateOutcomeWithEmoji(
depsWarnings,
true
)}) Dependencies warnings:`
)
.addList(generateOutcomeDepsWarnings(depsWarnings));
}

await core.summary
.addSeparator()
.addLink(
"View @nodesecure/ci documentation",
"https://github.com/NodeSecure/ci"
)
.write();
await generateSummary(report);

if (result.status === "failure") {
if (report.status === "failure") {
core.setFailed(`[FAILURE]: @nodesecure/ci checks failed.`);
}
} catch (error) {
Expand Down
151 changes: 151 additions & 0 deletions src/summary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import core from "@actions/core";

const kSuccessEmoji = "✅";
const kFailureEmoji = "❌";
const kInfoEmoji = "🟡";

function generateEmojiOutcome(reportData, hasSpecificOutcome) {
if (hasSpecificOutcome) {
const warnings = core.getInput("warnings");
if (warnings === "warning") {
return `${kInfoEmoji} ${reportData.length}`;
} else if (warnings === "off") {
return "(skipped)";
}
}

if (reportData.length === 0) {
return `${kSuccessEmoji} 0`;
}

return `${kFailureEmoji} ${reportData.length}`;
}

function generateGlobalWarningsOutcome(globalWarnings) {
return `<ul>
${globalWarnings.map((warning) => `<li>${warning}</li>`).join("")}
</ul>`;
}

function generateDepsWarningsOutcome(depsWarnings) {
return `
<br />
<table>
<tbody>
<tr>
<th>Package</th>
<th>Kind</th>
<th>File</th>
<th>Location</th>
</tr>
${depsWarnings
.flatMap(({ warnings, package: packageName }) =>
warnings.map((warning) => {
const location = warning.location.flatMap((location) =>
location.join(":")
);
return `<tr>
<td>${packageName}</td>
<td>${warning.kind}</td>
<td>${warning.file}</td>
<td>${location}</td>
</tr>`;
})
)
.join("")}
</tbody>
</table>
`;
}

function generateVulnsOutcome(vulns) {
const vulnRanges = vuln.vulnerableRanges.join(", ");
return `
<br />
<table>
<tbody>
<tr>
<th>Package</th>
<th>Severity</th>
<th>Title</th>
<th>Ranges</th>
</tr>
${vulns
.map(
(vuln) =>
`<tr>
<td>${vuln.package}</td>
<td>${vuln.severity}</td>
<td>${vuln.title}</td>
<td>${vulnRanges}</td>
</tr>`
)
.join("")}
</tbody>
</table>
`;
}

function getActionOutcome(report) {
const isReportSuccessful = report.status === "success";
const emojiOutcome = isReportSuccessful ? kSuccessEmoji : kFailureEmoji;
return `${emojiOutcome} [${report.status.toUpperCase()}]`;
}

export async function generateSummary(report) {
const vulns = report.data.dependencies.vulnerabilities;
const depsWarnings = report.data.dependencies.warnings;
const globalWarnings = report.data.warnings;

await core.summary
.addHeading(getActionOutcome(report), 5)
.addTable([
[
{ data: "Global warnings", header: true },
{ data: "Dependency warnings", header: true },
{ data: "Dependency vulnerabilities", header: true },
],
[
generateEmojiOutcome(globalWarnings),
generateEmojiOutcome(depsWarnings, true),
generateEmojiOutcome(vulns),
],
])
.addBreak();

if (vulns.length > 0) {
await core.summary
.addDetails(
`(${generateEmojiOutcome(vulns)}) <u>Dependencies vulnerabilities:</u>`,
generateVulnsOutcome(vulns)
)
.addList(generateVulnsOutcome(vulns));
await core.summary.addSeparator();
}

if (globalWarnings.length > 0) {
await core.summary.addDetails(
`(${generateEmojiOutcome(globalWarnings)}) <u>Global warnings:</u>`,
generateGlobalWarningsOutcome(globalWarnings)
);
await core.summary.addSeparator();
}

if (depsWarnings.length > 0) {
await core.summary.addDetails(
`(${generateEmojiOutcome(
depsWarnings,
true
)}) <u>Dependencies warnings:</u>`,
generateDepsWarningsOutcome(depsWarnings)
);
}

await core.summary
.addSeparator()
.addLink(
"View @nodesecure/ci documentation",
"https://github.com/NodeSecure/ci"
)
.write();
}

0 comments on commit 7816dd7

Please sign in to comment.