// comment.js β€” posts a sticky PR comment + a Checks-API check-run from a // `node9 scan-repo --json` result, and applies the `fail-on` gate. // // Dependency-free (Node 20 global fetch). Pure logic (render/decide) is split // from I/O so it can be self-tested: `node comment.js --selftest`. // // Design note: this runs in the PRIVILEGED comment step but only ever handles // the RESULT JSON (data) β€” never the repo's code β€” so it's safe by construction. 'use strict'; const fs = require('fs'); const MARKER = ''; const CHECK_NAME = 'node9 agent-security'; const RANK = { critical: 4, high: 3, medium: 2, advisory: 1 }; const ICON = { critical: 'πŸ”΄', high: 'πŸ”΄', medium: '🟑', advisory: '🟒' }; // ── Pure logic ────────────────────────────────────────────────────────────── /** Decide the check-run conclusion + process exit from the worst severity and * the fail-on threshold. `never` = report-only (never fails). */ function decide(worst, failOn, incomplete = false) { const threshold = RANK[failOn]; // undefined for 'never'/unknown β†’ never fail const fail = !!(threshold && worst && RANK[worst] >= threshold); // A scan that could not read every file has not earned a green check. It does not FAIL // the gate (we have no evidence of a problem), but it must not report success either β€” // "nothing found" is a statement about what we read, not about the change. const conclusion = fail ? 'failure' : worst || incomplete ? 'neutral' : 'success'; return { fail, conclusion, exitCode: fail ? 1 : 0 }; } /** Which severity the gate judges. `all` (the default) keeps the pre-CI-5 behaviour * exactly: the repo's worst finding. `introduced` narrows it to what THIS change is * answerable for, which is what lets a repo that is already dirty turn the gate on today. * * The scan already degrades `worstIntroduced` to the head's absolute worst when the base * could not be read, so an unreadable base falls back to the strict answer here rather * than passing as "nothing new". Missing diff (an older CLI than this action) β†’ `all`. */ /** A value from the scanned repo (a suppression reason) made safe inside a backtick span: * one line, no backtick, bounded. Inside a code span GitHub renders `