|
| 1 | +import type { MapReport, ScoredEntry } from "../score.js"; |
| 2 | +import { auditLine, contextLine, contextOnly, scoredFailures } from "./terminal.js"; |
| 3 | + |
| 4 | +/** First line of every comment this job posts, so the upsert step can find its own comment again. */ |
| 5 | +export const MARKER = "<!-- observability-map-report -->"; |
| 6 | + |
| 7 | +const MAX_CHANGED_ROWS = 15; |
| 8 | + |
| 9 | +const failingIds = (e: ScoredEntry) => e.checks.filter((c) => c.status === "fail").map((c) => c.id); |
| 10 | + |
| 11 | +function scoreLine(head: MapReport, base: MapReport | null): string { |
| 12 | + const headline = |
| 13 | + head.global === null |
| 14 | + ? `not measured over ${head.measured} measured of ${head.entries.length} entry points` |
| 15 | + : `**${head.global}/100** over ${head.measured} measured of ${head.entries.length} entry points`; |
| 16 | + |
| 17 | + if (!base) return headline; |
| 18 | + if (base.global === null || head.global === null) return `${headline} (base not measured)`; |
| 19 | + |
| 20 | + const diff = head.global - base.global; |
| 21 | + const comparison = |
| 22 | + diff === 0 |
| 23 | + ? `(base ${base.global}, no change)` |
| 24 | + : diff > 0 |
| 25 | + ? `(base ${base.global}, up ${diff})` |
| 26 | + : `(base ${base.global}, down ${-diff})`; |
| 27 | + return `${headline} ${comparison}`; |
| 28 | +} |
| 29 | + |
| 30 | +type ChangedRow = { |
| 31 | + routePath: string; |
| 32 | + sensitive: boolean; |
| 33 | + baseScore: number | "new"; |
| 34 | + headScore: number; |
| 35 | + nowFailing: string[]; |
| 36 | + /** |
| 37 | + * How much the entry got worse, used to sort the table. A new entry has no base score to |
| 38 | + * subtract from, so it is scored against a perfect 100: a new entry landing at 60 sorts the |
| 39 | + * same as an existing one that dropped 40 points, which is the ordering "what needs fixing |
| 40 | + * first" implies. |
| 41 | + */ |
| 42 | + drop: number; |
| 43 | +}; |
| 44 | + |
| 45 | +function changedRows(head: MapReport, base: MapReport): { rows: ChangedRow[]; removed: number } { |
| 46 | + const baseByFile = new Map(base.entries.map((e) => [e.fileName, e])); |
| 47 | + const headFiles = new Set(head.entries.map((e) => e.fileName)); |
| 48 | + |
| 49 | + const rows: ChangedRow[] = []; |
| 50 | + for (const h of head.entries) { |
| 51 | + const b = baseByFile.get(h.fileName); |
| 52 | + if (!b) { |
| 53 | + rows.push({ |
| 54 | + routePath: h.routePath, |
| 55 | + sensitive: h.sensitive, |
| 56 | + baseScore: "new", |
| 57 | + headScore: h.score, |
| 58 | + nowFailing: failingIds(h), |
| 59 | + drop: 100 - h.score, |
| 60 | + }); |
| 61 | + continue; |
| 62 | + } |
| 63 | + if (b.score === h.score) continue; |
| 64 | + const baseFailing = new Set(failingIds(b)); |
| 65 | + rows.push({ |
| 66 | + routePath: h.routePath, |
| 67 | + sensitive: h.sensitive, |
| 68 | + baseScore: b.score, |
| 69 | + headScore: h.score, |
| 70 | + nowFailing: failingIds(h).filter((id) => !baseFailing.has(id)), |
| 71 | + drop: b.score - h.score, |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + rows.sort( |
| 76 | + (a, b) => |
| 77 | + Number(b.sensitive) - Number(a.sensitive) || |
| 78 | + b.drop - a.drop || |
| 79 | + a.routePath.localeCompare(b.routePath) |
| 80 | + ); |
| 81 | + |
| 82 | + const removed = base.entries.filter((e) => !headFiles.has(e.fileName)).length; |
| 83 | + return { rows, removed }; |
| 84 | +} |
| 85 | + |
| 86 | +function whatChangedSection(head: MapReport, base: MapReport | null): string[] { |
| 87 | + const lines = ["**What this PR changed**"]; |
| 88 | + |
| 89 | + if (!base) { |
| 90 | + lines.push("Base comparison unavailable."); |
| 91 | + return lines; |
| 92 | + } |
| 93 | + |
| 94 | + const { rows, removed } = changedRows(head, base); |
| 95 | + |
| 96 | + if (rows.length === 0 && removed === 0) { |
| 97 | + lines.push("No entry point this PR touches changed its score."); |
| 98 | + return lines; |
| 99 | + } |
| 100 | + |
| 101 | + if (rows.length > 0) { |
| 102 | + lines.push(""); |
| 103 | + lines.push("| route | base | head | now failing |"); |
| 104 | + lines.push("| --- | --- | --- | --- |"); |
| 105 | + for (const row of rows.slice(0, MAX_CHANGED_ROWS)) { |
| 106 | + lines.push( |
| 107 | + `| ${row.routePath} | ${row.baseScore} | ${row.headScore} | ${row.nowFailing.join(", ")} |` |
| 108 | + ); |
| 109 | + } |
| 110 | + if (rows.length > MAX_CHANGED_ROWS) { |
| 111 | + lines.push(""); |
| 112 | + lines.push(`and ${rows.length - MAX_CHANGED_ROWS} more`); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if (removed > 0) { |
| 117 | + lines.push(""); |
| 118 | + lines.push(`${removed} entries removed`); |
| 119 | + } |
| 120 | + |
| 121 | + return lines; |
| 122 | +} |
| 123 | + |
| 124 | +function fixFirstSection(head: MapReport): string[] { |
| 125 | + const lines = ["FIX FIRST"]; |
| 126 | + const worst = head.entries |
| 127 | + .filter((e) => scoredFailures(e).length > 0 && !contextOnly(e)) |
| 128 | + .sort( |
| 129 | + (a, b) => |
| 130 | + Number(b.sensitive) - Number(a.sensitive) || |
| 131 | + a.score - b.score || |
| 132 | + a.fileName.localeCompare(b.fileName) |
| 133 | + ); |
| 134 | + |
| 135 | + for (const e of worst.slice(0, 3)) { |
| 136 | + const marks = e.sensitive ? " (sensitive)" : ""; |
| 137 | + lines.push( |
| 138 | + `- ${e.routePath}${marks} - ${scoredFailures(e) |
| 139 | + .map((c) => c.id) |
| 140 | + .join(", ")}` |
| 141 | + ); |
| 142 | + } |
| 143 | + return lines; |
| 144 | +} |
| 145 | + |
| 146 | +/** |
| 147 | + * Pure function, no I/O: `head` and `base` are already-built reports. Matches entries across the |
| 148 | + * two by `fileName`, the same identifier `renderJson` carries. |
| 149 | + */ |
| 150 | +export function renderPrComment(head: MapReport, base: MapReport | null): string { |
| 151 | + const lines = [MARKER, "", "## Observability map", "", scoreLine(head, base), ""]; |
| 152 | + |
| 153 | + lines.push(...whatChangedSection(head, base), ""); |
| 154 | + lines.push(...fixFirstSection(head), ""); |
| 155 | + |
| 156 | + const audit = auditLine(head); |
| 157 | + if (audit) lines.push(audit); |
| 158 | + const context = contextLine(head); |
| 159 | + if (context) lines.push(context); |
| 160 | + if (audit || context) lines.push(""); |
| 161 | + |
| 162 | + lines.push( |
| 163 | + "Report only, nothing here gates the merge. The rules and their reasons: " + |
| 164 | + "internal-packages/observability-map/README.md." |
| 165 | + ); |
| 166 | + |
| 167 | + const headFailures = head.parseFailures.length; |
| 168 | + const baseFailures = base?.parseFailures.length ?? 0; |
| 169 | + if (headFailures > 0 || baseFailures > 0) { |
| 170 | + const parts: string[] = []; |
| 171 | + if (headFailures > 0) parts.push(`${headFailures} at head`); |
| 172 | + if (baseFailures > 0) parts.push(`${baseFailures} at base`); |
| 173 | + lines.push( |
| 174 | + `Warning: parse failures (${parts.join(", ")}) are excluded from the score, shrinking the denominator.` |
| 175 | + ); |
| 176 | + } |
| 177 | + |
| 178 | + return lines.join("\n"); |
| 179 | +} |
0 commit comments