// A run as markdown, for a GitHub step summary. import { Fmt, Status } from "@effected/cli-kit"; import { DEFAULT_MAX_DIFF_LINES } from "./DiffDoc.js"; import { formatDelta, formatMs } from "./format.js"; import { STATUS, TONE, diffLines, issueText, oneLine, totalCells, verdictText } from "./Lines.js"; import type { Group, Issue, RunView } from "./RunView.js"; /** The most issues listed in the open before the list goes in a collapsed block. */ const OPEN_ISSUES = 10; // Text is data: a backslash before every character markdown reads, the backslash itself among them, so a name cannot // open a link, an image, emphasis or a code span. `<` because a summary renders HTML. const inlineText = (text: string): string => oneLine(text) .replace(/[\\|[\]()*_`~!#]/g, "\\$&") .replaceAll("<", "<"); const tableRow = (cells: ReadonlyArray): string => `|${cells.map((cell) => (cell === "" ? " " : ` ${cell} `)).join("|")}|`; const table = (header: ReadonlyArray, rows: ReadonlyArray>): string => [tableRow(header), tableRow(header.map(() => "---")), ...rows.map(tableRow)].join("\n"); // A fence longer than any backtick run in the text, because a diagnostic can quote one. const fenced = (text: string, lang = ""): string => { const longest = (text.match(/`+/g) ?? []).reduce((length, run) => Math.max(length, run.length), 2); const fence = "`".repeat(longest + 1); return `${fence}${lang}\n${text}\n${fence}`; }; const codeSpan = (text: string): string => { const clean = oneLine(text); const longest = (clean.match(/`+/g) ?? []).reduce((length, run) => Math.max(length, run.length), 0); const ticks = "`".repeat(longest + 1); return longest > 0 ? `${ticks} ${clean} ${ticks}` : `\`${clean}\``; }; const collapsed = (summary: string, body: string): string => `
${summary}\n\n${body}\n\n
`; const glyph = (status: Parameters[0]): string => Status.core.def(status).glyph; const groupTable = (group: Group): string => { const timed = group.rows.some((row) => row.ms !== undefined); const columns = Math.max(0, ...group.rows.map((row) => row.cells.length)); const reasons = group.rows.some((row) => row.reason !== undefined); const delta = group.total?.deltaBytes; const header = [ inlineText(group.title ?? "step"), ...(timed ? ["time"] : []), ...Array.from({ length: columns }, () => ""), ...(reasons ? ["reason"] : []), ...(delta !== undefined ? ["change"] : []), ]; const rows = group.rows.map((row) => [ `${glyph(STATUS[row.state])} ${inlineText(row.name)}`, ...(timed ? [row.ms !== undefined ? formatMs(row.ms) : ""] : []), ...row.cells.map((cell) => inlineText(cell.text)), ...Array.from({ length: columns - row.cells.length }, () => ""), ...(reasons ? [row.reason !== undefined ? inlineText(row.reason) : ""] : []), ...(delta !== undefined ? [""] : []), ]); const total = group.total; if (total === undefined) return table(header, rows); const { deltaBytes: _delta, ...sizes } = total; return table(header, [ ...rows, [ "**total**", ...(timed ? [""] : []), ...totalCells(sizes, columns), ...(reasons ? [""] : []), ...(delta !== undefined ? [formatDelta(delta)] : []), ], ]); }; const countTable = (issues: ReadonlyArray): string => { const counts = new Map(); for (const issue of issues) { const fatal = issue.ciFatal === true; const key = JSON.stringify([issue.source, issue.code ?? "", fatal]); const entry = counts.get(key) ?? { source: issue.source, code: issue.code ?? "", fatal, count: 0 }; counts.set(key, { ...entry, count: entry.count + 1 }); } return table( ["source", "rule", "count", "fails CI"], [...counts.values()] .sort((a, b) => b.count - a.count) .map((entry) => [ inlineText(entry.source), inlineText(entry.code), String(entry.count), entry.fatal ? "yes" : "", ]), ); }; const issueLine = (issue: Issue): string => `${issue.level === "error" ? "error" : "warn"} ${issueText(issue)}`; const diffBlock = (path: string, diff: RunView["diffs"][number]["diff"]): string => { // Without its first line, which names the document: the code span above the fence does that here. const lines = diffLines(path, diff).slice(1); const shown = lines.slice(0, DEFAULT_MAX_DIFF_LINES); return [ codeSpan(path), "", fenced(shown.join("\n"), "diff"), ...(lines.length > shown.length ? ["", `… ${lines.length - shown.length} more lines`] : []), ].join("\n"); }; /** * The run for a GitHub step summary: a heading, the verdict, a table per group, a count of the issues by rule when * API Extractor reported any, the issues, each diff as a `diff` block, the drift of a check, and what was suppressed. * * @remarks * It is the same at every level: a summary is read after the run, by someone who did not choose its flags. More than * ten issues go in a collapsed block, below the count table, which keeps the whole count in view when GitHub * truncates annotations. A diff is capped as the terminal caps it. Text from a diagnostic is escaped in a table and * fenced everywhere else, with a fence longer than any backtick run it holds. Ends with one newline, so a later * append starts on its own line. * * @param view - the run */ export const renderSummary = (view: RunView): string => { const issues = view.issues.map(issueLine).join("\n"); const drift = view.check?.drift ?? []; const sections: ReadonlyArray = [ `### ${view.command}: ${inlineText(view.subject)}`, `${glyph(TONE[view.verdict.tone])} ${inlineText(verdictText(view.verdict))}`, ...view.notices.map(inlineText), ...(view.failure !== undefined ? [`Failed: ${inlineText(view.failure)}`] : []), ...view.groups.filter((group) => group.rows.length > 0).map(groupTable), ...(view.issues.some((issue) => issue.source === "api-extractor") ? [countTable(view.issues)] : []), ...(view.issues.length > OPEN_ISSUES ? [collapsed(Fmt.plural(view.issues.length, "issue"), fenced(issues))] : view.issues.length > 0 ? [fenced(issues)] : []), ...view.diffs.map(({ path, diff }) => diffBlock(path, diff)), ...(drift.length > 0 ? [ table( ["target", "kind", "path"], drift.map((entry) => [inlineText(entry.target), inlineText(entry.kind), inlineText(entry.path)]), ), ] : view.check !== undefined ? ["matches the committed build"] : []), ...view.hints.map(inlineText), ...(view.suppressed.length > 0 ? [ collapsed( `${view.suppressed.length} suppressed`, fenced(view.suppressed.map((issue) => `${oneLine(issue.source)} ${issueText(issue)}`).join("\n")), ), ] : []), ]; return `${sections.join("\n\n")}\n`; };