|
| 1 | +/** |
| 2 | + * Markdown rendering (`--format markdown`), for the places CI reads a report: |
| 3 | + * a GitHub job summary (`$GITHUB_STEP_SUMMARY`) or a PR comment. |
| 4 | + * |
| 5 | + * The pretty renderer is a terminal artifact — ANSI colors, fixed-width |
| 6 | + * severity labels, a long scrollable list. A PR comment is read once, by |
| 7 | + * someone deciding whether to look further, so this renders the same report |
| 8 | + * as tables: scores first, then findings grouped by rule, with the noisy |
| 9 | + * parts (internal advisories, accepted debt) collapsed into `<details>`. |
| 10 | + */ |
| 11 | + |
| 12 | +import type { Score } from '../score/score'; |
| 13 | +import type { Finding, Report, Severity } from '../types'; |
| 14 | + |
| 15 | +const SEV_ICON: Record<Severity, string> = { |
| 16 | + critical: '🔴', |
| 17 | + high: '🟠', |
| 18 | + medium: '🟡', |
| 19 | + low: '🔵', |
| 20 | + info: '⚪' |
| 21 | +}; |
| 22 | + |
| 23 | +export interface RenderMarkdownOptions { |
| 24 | + /** Scores and counts only — no per-finding tables. */ |
| 25 | + summary?: boolean; |
| 26 | + /** Expand internal (non-exposed) advisories instead of collapsing them. */ |
| 27 | + verbose?: boolean; |
| 28 | + /** Heading text. Default `safegres`. */ |
| 29 | + title?: string; |
| 30 | +} |
| 31 | + |
| 32 | +export function renderMarkdown(report: Report, options: RenderMarkdownOptions = {}): string { |
| 33 | + const out: string[] = [`## ${options.title ?? 'safegres'}`, '']; |
| 34 | + |
| 35 | + out.push(...scoreTable(report), ''); |
| 36 | + |
| 37 | + if (report.exposure) { |
| 38 | + const { known, source, exposedTables, totalTables, roles } = report.exposure; |
| 39 | + out.push( |
| 40 | + known |
| 41 | + ? `Exposure (${source}): **${exposedTables}/${totalTables}** tables reachable` |
| 42 | + + `${roles && roles.length > 0 ? ` via \`${roles.join('`, `')}\`` : ''}.` |
| 43 | + : '> [!WARNING]\n> Exposure unknown — the entire database is assumed reachable and the score is capped.', |
| 44 | + '' |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + out.push(countsTable(report), ''); |
| 49 | + if (options.summary) return out.join('\n'); |
| 50 | + |
| 51 | + const security = report.perf |
| 52 | + ? report.findings.filter((f) => f.dimension !== 'perf') |
| 53 | + : report.findings; |
| 54 | + out.push(...findingSection('Security findings', security, options), ''); |
| 55 | + |
| 56 | + if (report.perf) { |
| 57 | + out.push(...findingSection('Performance findings', report.perf.findings, options)); |
| 58 | + const { stats, explain, diff } = report.perf; |
| 59 | + const notes: string[] = []; |
| 60 | + if (stats) { |
| 61 | + notes.push( |
| 62 | + `Runtime statistics: ${stats.tables} tables, counters since ${stats.statsReset ?? 'server start'}` |
| 63 | + + `${stats.scored ? '' : ' (advisory — `includeStats` is false)'}.` |
| 64 | + ); |
| 65 | + for (const note of stats.notes ?? []) notes.push(note); |
| 66 | + } |
| 67 | + if (explain) { |
| 68 | + notes.push( |
| 69 | + explain.unavailable |
| 70 | + ?? `Planner proof: ${explain.confirmed} confirmed, ${explain.refuted} refuted, ` |
| 71 | + + `${explain.inconclusive} inconclusive of ${explain.probed} probed.` |
| 72 | + ); |
| 73 | + } |
| 74 | + if (notes.length > 0) out.push('', notes.map((n) => `_${n}_`).join(' \n')); |
| 75 | + |
| 76 | + if (diff) { |
| 77 | + out.push('', '### Performance vs baseline', ''); |
| 78 | + out.push( |
| 79 | + diff.added.length === 0 |
| 80 | + ? `No new perf debt (${diff.accepted.length} accepted, ${diff.removed.length} fixed).` |
| 81 | + : `**${diff.added.length} new** since the baseline ` |
| 82 | + + `(${diff.accepted.length} accepted, ${diff.removed.length} fixed):` |
| 83 | + ); |
| 84 | + if (diff.added.length > 0) out.push('', findingTable(diff.added)); |
| 85 | + } |
| 86 | + out.push(''); |
| 87 | + } |
| 88 | + |
| 89 | + return out.join('\n'); |
| 90 | +} |
| 91 | + |
| 92 | +function scoreTable(report: Report): string[] { |
| 93 | + const rows: string[] = []; |
| 94 | + if (report.score) rows.push(scoreRow('Security', report.score)); |
| 95 | + if (report.perf) rows.push(scoreRow('Performance', report.perf.score)); |
| 96 | + if (rows.length === 0) return []; |
| 97 | + return ['| Dimension | Score | Grade | Top deductions |', '| --- | --- | --- | --- |', ...rows]; |
| 98 | +} |
| 99 | + |
| 100 | +function scoreRow(label: string, score: Score): string { |
| 101 | + const top = score.deductions |
| 102 | + .slice(0, 3) |
| 103 | + .map((d) => `\`${d.code}\` −${d.points} (×${d.count})`) |
| 104 | + .join(', '); |
| 105 | + const capped = score.cappedByUnknownExposure ? ' (capped)' : ''; |
| 106 | + return `| ${label} | **${score.value}**${capped} | **${score.grade}** | ${top || '—'} |`; |
| 107 | +} |
| 108 | + |
| 109 | +function countsTable(report: Report): string { |
| 110 | + const s = report.summary; |
| 111 | + return [ |
| 112 | + '| 🔴 critical | 🟠 high | 🟡 medium | 🔵 low | ⚪ info |', |
| 113 | + '| --- | --- | --- | --- | --- |', |
| 114 | + `| ${s.critical} | ${s.high} | ${s.medium} | ${s.low} | ${s.info} |` |
| 115 | + ].join('\n'); |
| 116 | +} |
| 117 | + |
| 118 | +function findingSection( |
| 119 | + heading: string, |
| 120 | + findings: Finding[], |
| 121 | + options: RenderMarkdownOptions |
| 122 | +): string[] { |
| 123 | + const out = [`### ${heading}`, '']; |
| 124 | + const exposed = findings.filter((f) => f.exposed !== false); |
| 125 | + const internal = findings.filter((f) => f.exposed === false); |
| 126 | + |
| 127 | + if (exposed.length === 0) out.push('No findings.'); |
| 128 | + else out.push(findingTable(exposed)); |
| 129 | + |
| 130 | + if (internal.length > 0) { |
| 131 | + const table = findingTable(internal); |
| 132 | + out.push( |
| 133 | + '', |
| 134 | + options.verbose |
| 135 | + ? table |
| 136 | + : `<details><summary>${internal.length} internal ` |
| 137 | + + `advisor${internal.length === 1 ? 'y' : 'ies'} — not exposed via any API, ` |
| 138 | + + `excluded from the score</summary>\n\n${table}\n\n</details>` |
| 139 | + ); |
| 140 | + } |
| 141 | + return out; |
| 142 | +} |
| 143 | + |
| 144 | +function findingTable(findings: Array<Pick<Finding, 'code' | 'severity' | 'schema' | 'table' | 'policy' | 'message'>>): string { |
| 145 | + const rows = findings.map((f) => { |
| 146 | + const where = [f.schema, f.table].filter(Boolean).join('.') || '—'; |
| 147 | + const policy = f.policy ? ` \`${f.policy}\`` : ''; |
| 148 | + return `| ${SEV_ICON[f.severity]} ${f.severity} | \`${f.code}\` | ${where}${policy} | ${escapeCell(f.message)} |`; |
| 149 | + }); |
| 150 | + return ['| Severity | Rule | Relation | Finding |', '| --- | --- | --- | --- |', ...rows].join('\n'); |
| 151 | +} |
| 152 | + |
| 153 | +/** Keep a message inside its table cell: no pipes, no line breaks. */ |
| 154 | +function escapeCell(value: string): string { |
| 155 | + return value.replace(/\|/g, '\\|').replace(/\s*\n\s*/g, ' '); |
| 156 | +} |
0 commit comments