|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off globalConsole:off |
| 2 | +// Checks that pull requests still pass together on current main, right before |
| 3 | +// they merge. Main takes hundreds of merges a day, so the ruleset cannot require |
| 4 | +// branches to be up to date, and GitHub reports CLEAN for a PR that is far |
| 5 | +// behind. A PR's green CI can be days old. Most breaks on main were two changes |
| 6 | +// that each passed alone: a renamed type, a removed dependency, a new request |
| 7 | +// an older test did not stub. |
| 8 | +// |
| 9 | +// vp run pr-merge-check <pr> [<pr>...] |
| 10 | +// |
| 11 | +// Merges GitHub's main and each PR head, in order, in a temporary worktree |
| 12 | +// next to this checkout, then runs check:changed against that main. With |
| 13 | +// `origin` on the GitCafe mirror, which can lag about a minute, fetches use the |
| 14 | +// `github` remote. Pass every PR of a batch in one run. Exit 0 prints the merge commands, |
| 15 | +// pinned to the tested heads. Run them now, in order: each new commit on main |
| 16 | +// makes the result older. |
| 17 | +import * as NodeChildProcess from "node:child_process"; |
| 18 | +import * as NodeFS from "node:fs"; |
| 19 | +import * as NodePath from "node:path"; |
| 20 | + |
| 21 | +const root = NodePath.resolve(import.meta.dirname, ".."); |
| 22 | +const prs = process.argv.slice(2); |
| 23 | +if (prs.length === 0 || prs.some((pr) => !/^\d+$/.test(pr))) { |
| 24 | + console.error("usage: vp run pr-merge-check <pr> [<pr>...]"); |
| 25 | + process.exit(1); |
| 26 | +} |
| 27 | + |
| 28 | +// `vp run` puts this checkout's node_modules/.bin first on PATH. Left there, |
| 29 | +// the merged worktree would run this checkout's Vitest against its own test |
| 30 | +// files, and two Vitest copies fail every suite. |
| 31 | +const env = { |
| 32 | + ...process.env, |
| 33 | + PATH: (process.env.PATH ?? "") |
| 34 | + .split(NodePath.delimiter) |
| 35 | + .filter((entry) => !entry.startsWith(`${root}${NodePath.sep}`)) |
| 36 | + .join(NodePath.delimiter), |
| 37 | +}; |
| 38 | + |
| 39 | +const run = (command: string, args: ReadonlyArray<string>, cwd: string) => |
| 40 | + NodeChildProcess.spawnSync(command, args, { cwd, env, stdio: "inherit" }).status === 0; |
| 41 | + |
| 42 | +const git = (cwd: string, ...args: Array<string>) => |
| 43 | + NodeChildProcess.execFileSync("git", args, { cwd, encoding: "utf8" }).trim(); |
| 44 | + |
| 45 | +const hasGitHubRemote = |
| 46 | + NodeChildProcess.spawnSync("git", ["remote", "get-url", "github"], { cwd: root }).status === 0; |
| 47 | +const remote = hasGitHubRemote ? "github" : "origin"; |
| 48 | +// Signing can prompt or fail, which would read as a conflict. |
| 49 | +const merge = ["-c", "commit.gpgsign=false", "merge", "--quiet", "--no-ff"]; |
| 50 | + |
| 51 | +// The worktree has its own FETCH_HEAD, so a fetch in this checkout or in |
| 52 | +// another run cannot swap the commit under test. |
| 53 | +const dir = NodePath.resolve(root, "..", `${NodePath.basename(root)}-merge-check-${process.pid}`); |
| 54 | +git(root, "worktree", "add", "--quiet", "--detach", "--no-checkout", dir); |
| 55 | +// A terminal signal reaches the running child too. Staying alive lets |
| 56 | +// `finally` remove the worktree. SIGKILL still leaves it behind. |
| 57 | +for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"] as const) process.on(signal, () => {}); |
| 58 | + |
| 59 | +const fetchHead = (ref: string) => { |
| 60 | + git(dir, "fetch", "--quiet", remote, ref); |
| 61 | + return git(dir, "rev-parse", "FETCH_HEAD"); |
| 62 | +}; |
| 63 | + |
| 64 | +const heads: Array<{ pr: string; sha: string }> = []; |
| 65 | +let ok = false; |
| 66 | +try { |
| 67 | + const base = fetchHead("refs/heads/main"); |
| 68 | + git(dir, "checkout", "--quiet", "--detach", base); |
| 69 | + const conflict = prs.find((pr) => { |
| 70 | + const sha = fetchHead(`pull/${pr}/head`); |
| 71 | + heads.push({ pr, sha }); |
| 72 | + return !run("git", [...merge, "-m", `merge-check #${pr}`, sha], dir); |
| 73 | + }); |
| 74 | + if (conflict) { |
| 75 | + console.error(`\npr-merge-check: #${conflict} does not merge cleanly onto main.`); |
| 76 | + } else { |
| 77 | + // A lockfile that two PRs edited can merge as text and still not match |
| 78 | + // the manifests. The frozen install fails on that. |
| 79 | + ok = |
| 80 | + run("vp", ["i", "--frozen-lockfile", "--", "--yes"], dir) && |
| 81 | + run("node", ["scripts/check-changed.ts", base], dir); |
| 82 | + } |
| 83 | +} finally { |
| 84 | + git(root, "worktree", "remove", "--force", dir); |
| 85 | + NodeFS.rmSync(dir, { recursive: true, force: true }); |
| 86 | +} |
| 87 | + |
| 88 | +const list = prs.map((pr) => `#${pr}`).join(" "); |
| 89 | +if (!ok) { |
| 90 | + console.log(`\npr-merge-check: ${list} fail on main. Do not merge.`); |
| 91 | + process.exit(1); |
| 92 | +} |
| 93 | +console.log(`\npr-merge-check: ${list} pass on main. Merge them now, in this order:`); |
| 94 | +for (const { pr, sha } of heads) |
| 95 | + console.log(` gh pr merge ${pr} --squash --match-head-commit ${sha}`); |
0 commit comments