|
| 1 | +#!/usr/bin/env node |
| 2 | +// Guard: a pull request into `main` brings nothing that is not already on |
| 3 | +// `v2/main` (#5119). |
| 4 | +// |
| 5 | +// `main` is the release line. The one PR that may target it is a milestone's |
| 6 | +// merge PR, a pure merge of `v2/main` (AGENTS.md, "Contributing"). GitHub |
| 7 | +// rulesets can require a PR and its approvals, but cannot say where a PR comes |
| 8 | +// from, so before this guard a PR from any branch could merge into `main`. |
| 9 | +// |
| 10 | +// The rule is on the COMMIT, not the branch name: the PR's head must be an |
| 11 | +// ancestor of the source (`origin/v2/main`), or the source itself. The |
| 12 | +// `release` skill opens the merge PR from `v2/chore/<N>-release-<milestone>`, |
| 13 | +// a branch pushed from `origin/v2/main`, so a check on the head's name would |
| 14 | +// refuse the one PR the rule exists to allow. An ancestor rather than only an |
| 15 | +// equal commit, because `v2/main` keeps moving while the merge PR is open, and |
| 16 | +// the PR is still a pure merge of what it was opened with. |
| 17 | +// |
| 18 | +// CI runs it from `main-source.yml` on `pull_request_target`, which runs |
| 19 | +// `main`'s copy of the workflow and of this script, so a PR cannot edit the |
| 20 | +// guard into passing. The PR's head is only fetched as git objects; none of |
| 21 | +// its code runs. It is not a `local:gate` stage: every local push targets a |
| 22 | +// v2 branch, where the rule does not apply. |
| 23 | + |
| 24 | +import { spawnSync } from "node:child_process"; |
| 25 | +import path from "node:path"; |
| 26 | +import { fileURLToPath, pathToFileURL } from "node:url"; |
| 27 | + |
| 28 | +/** The branch every commit on `main` must already be on. */ |
| 29 | +export const DEFAULT_SOURCE = "origin/v2/main"; |
| 30 | + |
| 31 | +/** How many of the offending commits the report lists. */ |
| 32 | +export const MAX_LISTED = 20; |
| 33 | + |
| 34 | +/** |
| 35 | + * Pure: `--head <rev>` (required) and `--source <rev>`. |
| 36 | + * |
| 37 | + * @param {string[]} argv |
| 38 | + */ |
| 39 | +export function parseArgs(argv) { |
| 40 | + const opts = { source: DEFAULT_SOURCE, head: "" }; |
| 41 | + for (let i = 0; i < argv.length; i++) { |
| 42 | + const flag = argv[i]; |
| 43 | + if (flag !== "--source" && flag !== "--head") |
| 44 | + throw new Error(`unknown argument: ${flag}`); |
| 45 | + const value = argv[++i]; |
| 46 | + if (!value) throw new Error(`${flag} needs a revision`); |
| 47 | + opts[flag.slice(2)] = value; |
| 48 | + } |
| 49 | + if (!opts.head) throw new Error("--head is required"); |
| 50 | + return opts; |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Pure: the report for a head that carries commits the source does not. |
| 55 | + * |
| 56 | + * @param {{ source: string, head: string, commits: string[] }} args |
| 57 | + * `commits` is `git log --oneline <source>..<head>`, one per entry |
| 58 | + */ |
| 59 | +export function formatReport({ source, head, commits }) { |
| 60 | + const n = commits.length; |
| 61 | + const listed = commits.slice(0, MAX_LISTED).map((c) => ` ${c}`); |
| 62 | + if (n > MAX_LISTED) listed.push(` … and ${n - MAX_LISTED} more`); |
| 63 | + return [ |
| 64 | + `verify:main-source: ${head} carries ${n} commit${n === 1 ? "" : "s"} that ${source} does not:`, |
| 65 | + ...listed, |
| 66 | + "", |
| 67 | + "A pull request into `main` must be a pure merge of `v2/main`: its head", |
| 68 | + "is a commit already on `v2/main`. Land the change on `v2/main` through", |
| 69 | + "its own PR, and it reaches `main` with the next milestone merge (the", |
| 70 | + "`release` skill).", |
| 71 | + ].join("\n"); |
| 72 | +} |
| 73 | + |
| 74 | +function git(args, cwd) { |
| 75 | + const r = spawnSync("git", args, { cwd, encoding: "utf8" }); |
| 76 | + if (r.error) throw r.error; |
| 77 | + return r; |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Check that `head` is `source` or an ancestor of it, in the repository at |
| 82 | + * `cwd`. |
| 83 | + * |
| 84 | + * @returns {{ code: number, output: string }} |
| 85 | + */ |
| 86 | +export function verifyMainSource({ source, head, cwd }) { |
| 87 | + for (const rev of [source, head]) { |
| 88 | + if ( |
| 89 | + git(["rev-parse", "--verify", "--quiet", `${rev}^{commit}`], cwd).status |
| 90 | + ) |
| 91 | + return { |
| 92 | + code: 1, |
| 93 | + output: |
| 94 | + `verify:main-source: cannot resolve ${rev}.` + |
| 95 | + (rev === DEFAULT_SOURCE |
| 96 | + ? " Fetch it (`git fetch origin v2/main`), or name another source with --source." |
| 97 | + : ""), |
| 98 | + }; |
| 99 | + } |
| 100 | + const anc = git(["merge-base", "--is-ancestor", head, source], cwd); |
| 101 | + if (anc.status === 0) |
| 102 | + return { |
| 103 | + code: 0, |
| 104 | + output: `verify:main-source: ${head} is on ${source}; nothing else comes with it.`, |
| 105 | + }; |
| 106 | + if (anc.status !== 1) |
| 107 | + return { |
| 108 | + code: 1, |
| 109 | + output: `verify:main-source: git merge-base failed:\n${anc.stderr}`, |
| 110 | + }; |
| 111 | + const log = git( |
| 112 | + ["log", "--oneline", "--no-decorate", `${source}..${head}`], |
| 113 | + cwd, |
| 114 | + ); |
| 115 | + if (log.status) |
| 116 | + return { |
| 117 | + code: 1, |
| 118 | + output: `verify:main-source: git log failed:\n${log.stderr}`, |
| 119 | + }; |
| 120 | + const commits = log.stdout.split("\n").filter((l) => l.trim() !== ""); |
| 121 | + return { code: 1, output: formatReport({ source, head, commits }) }; |
| 122 | +} |
| 123 | + |
| 124 | +if ( |
| 125 | + process.argv[1] && |
| 126 | + import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href |
| 127 | +) { |
| 128 | + let opts; |
| 129 | + try { |
| 130 | + opts = parseArgs(process.argv.slice(2)); |
| 131 | + } catch (e) { |
| 132 | + console.error(`verify:main-source: ${e.message}`); |
| 133 | + process.exit(2); |
| 134 | + } |
| 135 | + const repoRoot = path.resolve( |
| 136 | + path.dirname(fileURLToPath(import.meta.url)), |
| 137 | + "..", |
| 138 | + ); |
| 139 | + const { code, output } = verifyMainSource({ ...opts, cwd: repoRoot }); |
| 140 | + (code === 0 ? console.log : console.error)(output); |
| 141 | + process.exit(code); |
| 142 | +} |
0 commit comments