|
| 1 | +// The pull request body gate: `pnpm check:pr-body`. |
| 2 | +// |
| 3 | +// Enforces the `## PR body` rule in docs/agents/pull-requests.md. It reads the title and the body from |
| 4 | +// the event payload at `GITHUB_EVENT_PATH`, never from the checked-out tree, so it has nothing to |
| 5 | +// grade outside CI. |
| 6 | + |
| 7 | +import fs from 'node:fs'; |
| 8 | + |
| 9 | +const WORD_LIMIT = 250; |
| 10 | +/** Review bots append a footer to the body. The author did not write it, so it is not counted. */ |
| 11 | +const BOT_FOOTER = /^<!--\s*(?:codesmith:footer|This is an auto-generated description)/m; |
| 12 | + |
| 13 | +const SECTION_HINTS: Readonly<Record<string, string>> = { |
| 14 | + Summary: 'the invariant and behavior, the reason, the linked issue, and scope expansion', |
| 15 | + 'Before / after': |
| 16 | + 'up to 3 calls at base and head with the output that differs, or `No observable change` with a reason', |
| 17 | + Value: '2-4 numbers measured at base and head', |
| 18 | + Validation: 'the tested SHA, command results, CI obligations, and unresolved risks', |
| 19 | +}; |
| 20 | + |
| 21 | +type PullRequest = { |
| 22 | + title?: string; |
| 23 | + body?: string | null; |
| 24 | + draft?: boolean; |
| 25 | + user?: { login?: string; type?: string }; |
| 26 | +}; |
| 27 | + |
| 28 | +function requiredSections(title: string): string[] { |
| 29 | + const type = /^([a-z]+)(?:\([^)]*\))?!?:/.exec(title.trim())?.[1]; |
| 30 | + if (type === 'feat' || type === 'fix') return ['Summary', 'Before / after', 'Validation']; |
| 31 | + if (type === 'refactor') return ['Summary', 'Value', 'Validation']; |
| 32 | + return ['Summary', 'Validation']; |
| 33 | +} |
| 34 | + |
| 35 | +/** The text under a `## <heading>` line, up to the next heading. Undefined without that heading. */ |
| 36 | +function sectionText(prose: string, heading: string): string | undefined { |
| 37 | + const lines = prose.split('\n'); |
| 38 | + const wanted = `## ${heading}`.toLowerCase(); |
| 39 | + const start = lines.findIndex( |
| 40 | + (line) => |
| 41 | + line |
| 42 | + .trim() |
| 43 | + .toLowerCase() |
| 44 | + .replace(/\s*\/\s*/, ' / ') === wanted, |
| 45 | + ); |
| 46 | + if (start === -1) return undefined; |
| 47 | + const rest = lines.slice(start + 1); |
| 48 | + const end = rest.findIndex((line) => /^#{1,2} /.test(line)); |
| 49 | + return (end === -1 ? rest : rest.slice(0, end)).join('\n').trim(); |
| 50 | +} |
| 51 | + |
| 52 | +/** Drops HTML comments, such as the template's prompts. An unclosed `<!--` is kept as text. */ |
| 53 | +function withoutComments(text: string): string { |
| 54 | + return text |
| 55 | + .split('<!--') |
| 56 | + .map((part, index) => { |
| 57 | + if (index === 0) return part; |
| 58 | + const end = part.indexOf('-->'); |
| 59 | + return end === -1 ? part : part.slice(end + 3); |
| 60 | + }) |
| 61 | + .join(''); |
| 62 | +} |
| 63 | + |
| 64 | +function bodyViolations(title: string, body: string): string[] { |
| 65 | + // A fenced block becomes a bare fence: it keeps its section non-empty, hides a `<!--` inside it |
| 66 | + // from the comment strip, and adds no word to the count. |
| 67 | + const fenceless = body |
| 68 | + .replaceAll('\r', '') |
| 69 | + .replaceAll(/^[ \t]*(`{3,}|~{3,})[\s\S]*?^[ \t]*\1[^\n]*$/gm, '```'); |
| 70 | + const prose = withoutComments(fenceless.split(BOT_FOOTER)[0] ?? ''); |
| 71 | + const violations = requiredSections(title) |
| 72 | + .filter((heading) => !sectionText(prose, heading)) |
| 73 | + .map((heading) => `Add a "## ${heading}" section: ${SECTION_HINTS[heading]}.`); |
| 74 | + const words = prose.split(/\s+/).filter((word) => /[\p{L}\p{N}]/u.test(word)).length; |
| 75 | + if (words >= WORD_LIMIT) { |
| 76 | + violations.push( |
| 77 | + `Shorten the body: it has ${words} words outside code blocks, and the limit is below ${WORD_LIMIT}.`, |
| 78 | + ); |
| 79 | + } |
| 80 | + return violations; |
| 81 | +} |
| 82 | + |
| 83 | +function main(): number { |
| 84 | + const eventPath = process.env.GITHUB_EVENT_PATH; |
| 85 | + if (!eventPath) { |
| 86 | + console.error('check:pr-body grades a pull request event. It runs in CI only.'); |
| 87 | + return 1; |
| 88 | + } |
| 89 | + const pullRequest: PullRequest | undefined = JSON.parse( |
| 90 | + fs.readFileSync(eventPath, 'utf8'), |
| 91 | + ).pull_request; |
| 92 | + if (!pullRequest) { |
| 93 | + console.error(`${eventPath} holds no pull_request event.`); |
| 94 | + return 1; |
| 95 | + } |
| 96 | + const { title = '', body, draft, user } = pullRequest; |
| 97 | + if (draft || user?.type === 'Bot' || user?.login?.endsWith('[bot]')) { |
| 98 | + console.log('PR body: not checked for a draft or a bot author.'); |
| 99 | + return 0; |
| 100 | + } |
| 101 | + const violations = bodyViolations(title, body ?? ''); |
| 102 | + if (violations.length === 0) { |
| 103 | + console.log('PR body: ok.'); |
| 104 | + return 0; |
| 105 | + } |
| 106 | + console.error(`PR body: ${violations.length} rule(s) failed. See docs/agents/pull-requests.md.`); |
| 107 | + for (const violation of violations) console.error(`- ${violation}`); |
| 108 | + return 1; |
| 109 | +} |
| 110 | + |
| 111 | +process.exitCode = main(); |
0 commit comments