|
| 1 | +import { existsSync } from "node:fs"; |
| 2 | + |
| 3 | +const highRiskPathPatterns = [ |
| 4 | + /^\.github\/workflows\//, |
| 5 | + /^scripts\/release\//, |
| 6 | + /^(?:package\.json|bun\.lock)$/, |
| 7 | + /^packages\/sdk\/src\/internal\/(?:parser\/|types\/(?:config|session)\.ts|providers\/(?:interface|capabilities|registry|base-client)\.ts)/, |
| 8 | +]; |
| 9 | + |
| 10 | +export function requiresMaintainerEvidence(files: readonly string[]): boolean { |
| 11 | + return files.some((file) => highRiskPathPatterns.some((pattern) => pattern.test(file))); |
| 12 | +} |
| 13 | + |
| 14 | +function sectionHasContent(body: string, heading: string): boolean { |
| 15 | + const escapedHeading = heading.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); |
| 16 | + const match = body.match(new RegExp(`^## ${escapedHeading}[ \\t]*\\r?\\n([\\s\\S]*?)(?=^## |(?![\\s\\S]))`, "mi")); |
| 17 | + if (!match) return false; |
| 18 | + return ( |
| 19 | + match[1] |
| 20 | + .replace(/<!--[\s\S]*?(?:-->|$)/g, "") |
| 21 | + .replace(/- \[ \] .*/g, "") |
| 22 | + .trim().length > 0 |
| 23 | + ); |
| 24 | +} |
| 25 | + |
| 26 | +export function validateMaintainerEvidence(body: string, files: readonly string[]): string | undefined { |
| 27 | + if (!requiresMaintainerEvidence(files)) return undefined; |
| 28 | + const missing = ["Behavior / risk", "Validation"].filter((heading) => !sectionHasContent(body, heading)); |
| 29 | + if (missing.length === 0) return undefined; |
| 30 | + return `High-risk changes require a non-empty ${missing.map((heading) => `## ${heading}`).join(" and ")} section.`; |
| 31 | +} |
| 32 | + |
| 33 | +function changedFiles(base: string, head: string): string[] { |
| 34 | + const result = Bun.spawnSync(["git", "diff", "--name-only", "--diff-filter=ACMR", base, head], { |
| 35 | + stdout: "pipe", |
| 36 | + stderr: "pipe", |
| 37 | + }); |
| 38 | + if (result.exitCode !== 0) throw new Error(result.stderr.toString().trim() || "Could not read changed files."); |
| 39 | + return result.stdout.toString().split("\n").filter(Boolean); |
| 40 | +} |
| 41 | + |
| 42 | +function main(): number { |
| 43 | + if (process.env.GITHUB_EVENT_NAME !== "pull_request") { |
| 44 | + console.log("Not a pull request; maintainer-evidence check skipped."); |
| 45 | + return 0; |
| 46 | + } |
| 47 | + const base = process.env.BASE_SHA; |
| 48 | + const head = process.env.HEAD_SHA; |
| 49 | + if (!base || !head || !existsSync(".git")) |
| 50 | + throw new Error("BASE_SHA and HEAD_SHA are required for pull-request evidence checks."); |
| 51 | + const files = changedFiles(base, head); |
| 52 | + const error = validateMaintainerEvidence(process.env.PR_BODY ?? "", files); |
| 53 | + if (!error) { |
| 54 | + console.log( |
| 55 | + requiresMaintainerEvidence(files) |
| 56 | + ? "Maintainer evidence supplied." |
| 57 | + : "Low-risk change; maintainer evidence is optional.", |
| 58 | + ); |
| 59 | + return 0; |
| 60 | + } |
| 61 | + console.error(`::error::${error}`); |
| 62 | + console.error( |
| 63 | + "Describe the user-visible behavior or compatibility risk, then give the test command, test case, or manual reproduction used to validate it.", |
| 64 | + ); |
| 65 | + return 1; |
| 66 | +} |
| 67 | + |
| 68 | +if (import.meta.main) process.exit(main()); |
0 commit comments