import { COMMENT_LIMIT, SECTIONS, getSection } from './sections.ts'; import type { SectionDefinition } from './sections.ts'; export const COMMENT_MARKER = ''; const COMMENT_HEADING = '### 🤖 PR meta 🤖'; export type ParsedSection = { id: string; body: string; sha?: string; runUrl?: string; }; export type SectionUpdate = { id: string; body: string; sha?: string; runUrl?: string; }; const SECTION_PATTERN = /\n([\s\S]*?)\n/g; export function isPrMetaComment( body: string ): boolean { return body.startsWith( COMMENT_MARKER ); } /** * Neutralises section delimiters in producer content. * * Some bodies carry text this repository does not control, most of all the * flaky tests section, which interpolates errors thrown by PR-authored tests. * An unescaped delimiter there could close its own section early and forge * another one. * * @param body Untrusted markdown. * @return The same markdown with every delimiter rendered inert. */ export function sanitizeBody( body: string ): string { return body.replace( /`; const close = ``; const heading = definition?.heading ? `#### ${ definition.heading }\n\n` : ''; const body = section.body; const delimited = `${ open }\n${ body }\n${ close }`; /* * The heading, the fold and the footer all sit outside the delimiters. * Everything between them is read back verbatim as the body, so a * decoration kept inside would be re-rendered on top of itself at every * write. The blank lines are what let GitHub render markdown inside the * fold. */ const content = definition?.summary ? `
\n${ definition.summary }\n\n${ delimited }\n\n
` : delimited; return `${ heading }${ content }${ renderFooter( section, headSha ) }`; } /** * Orders sections by the registry, keeping ids it does not know at the end so * a workflow running an older revision of this action never loses its section. * * @param sections Sections in no particular order. * @return The same sections in render order. */ function inRegistryOrder( sections: ParsedSection[] ): ParsedSection[] { const known = SECTIONS.map( ( { id } ) => sections.find( ( section ) => section.id === id ) ).filter( ( section ): section is ParsedSection => Boolean( section ) ); const unknown = sections.filter( ( section ) => ! getSection( section.id ) ); return [ ...known, ...unknown ]; } /** * Drops trailing sections until the comment fits. * * Section budgets are chosen to fit together, but a section written by a newer * revision of this action is outside that arithmetic, and GitHub rejects an * oversized body outright. Registry order puts what a human acts on first, so * trimming from the end sheds the least important content. * * @param sections Sections in render order. * @param render Renders the whole comment from a list of sections. * @return The sections that fit. */ function withinLimit( sections: ParsedSection[], render: ( kept: ParsedSection[] ) => string ): ParsedSection[] { const kept = [ ...sections ]; while ( kept.length > 1 && render( kept ).length > COMMENT_LIMIT ) { kept.pop(); } return kept; } export function renderComment( sections: ParsedSection[], headSha?: string ): string { const render = ( kept: ParsedSection[] ) => `${ COMMENT_MARKER }\n${ COMMENT_HEADING }\n\n${ kept .map( ( section ) => renderSection( section, getSection( section.id ), headSha ) ) .join( '\n\n' ) }\n`; return render( withinLimit( inRegistryOrder( sections ), render ) ); } export type MergeResult = { /** The full comment body to write, when there is one. */ body?: string; /** Whether the comment should be deleted, its last section having gone. */ remove?: boolean; /** Set when the update was dropped, explaining why. */ rejected?: string; }; /** * Applies one section update to an existing unified comment. * * @param existing The current comment body, or `undefined` when there is none. * @param update The section to insert, replace or, with an empty body, remove. * @param headSha Current head SHA of the pull request, when known. * @return The merged comment body, or the reason the update was dropped. */ export function mergeSection( existing: string | undefined, update: SectionUpdate, headSha?: string ): MergeResult { const definition = getSection( update.id ); if ( ! definition ) { return { rejected: `Unknown section "${ update.id }".` }; } const sections = existing ? parseSections( existing ) : []; const current = sections.find( ( section ) => section.id === update.id ); /* Before the check below, so an unusable one is rejected, not stored. */ const sha = safeSha( update.sha ); /* * A rerun of an older commit, or a run cancelled mid-flight, can finish * after a newer one. Only the current head, or a refresh of what the * section already shows, may write it. This covers clearing a section as * much as filling one: an old rerun that happens to be clean would * otherwise wipe a newer result. Both SHAs are required rather than * optional, since without either one the two cases are indistinguishable * and guessing lets the stale run through. */ if ( definition.scope === 'commit' && ( update.body || current ) ) { if ( ! sha || ! headSha ) { return { rejected: 'A commit-scoped section needs both its own commit and the current head.', }; } if ( sha !== headSha && sha !== current?.sha ) { return { rejected: `Result is for ${ sha }, which is neither the head (${ headSha }) nor the commit already reported.`, }; } } const runUrl = safeRunUrl( update.runUrl ); /* * Leading spaces stay: trimming them would turn indented code into a * fence. Truncated here rather than at render, so a writer running an * older revision cannot cut a section it did not produce. */ const body = truncate( demoteHeadings( sanitizeBody( update.body ) ) .replace( /^[\r\n]+/, '' ) .trimEnd(), definition, runUrl ); const remaining = sections.filter( ( section ) => section.id !== update.id ); const next = body ? [ ...remaining, { id: update.id, body, sha: definition.scope === 'commit' ? sha : undefined, runUrl: definition.scope === 'commit' ? runUrl : undefined, }, ] : remaining; if ( next.length === 0 ) { return { remove: Boolean( existing ) }; } return { body: renderComment( next, headSha ) }; }