diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d8b2cbce565..0ef49c3e082 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -152,7 +152,8 @@ See [Mergify priorities](https://docs.mergify.com/merge-queue/priority/) and [Mergify stacks](https://docs.mergify.com/merge-queue/stacks/) (`stack/*` heads pushed by `mergify stack push --no-github-native`) use the `changes` queue. A member above the bottom targets the branch below it and carries a -`Depends-On: #N` marker; the queue accepts it on that basis and lands the whole +`Depends-On: #N` or same-repository `Depends-On: https://github.com/OWNER/REPO/pull/N` +marker; the queue accepts it on that basis and lands the whole chain on the stack root, bottom-up. Queueing the top member queues every member below it. The batch validator resolves each member's root through the same marker chain, so a stack branch without it is still refused. diff --git a/scripts/check-mergify-batch.mjs b/scripts/check-mergify-batch.mjs index 3cff129ee86..0bea49aafeb 100644 --- a/scripts/check-mergify-batch.mjs +++ b/scripts/check-mergify-batch.mjs @@ -26,12 +26,24 @@ export function batchPullRequestNumbers(metadata) { // Mergify's queue lands stack members on the stack root, while each member // targets the head branch of the member below it. Resolve that root through // the `Depends-On:` chain, matching the queue's own stack recognition. +export function stackDependency(repository, body) { + const marker = + /^Depends-On: (?:#([1-9]\d*)|https:\/\/github\.com\/([^/\s]+\/[^/\s]+)\/pull\/([1-9]\d*))\s*$/m.exec(body ?? ""); + if ( + !marker || + (marker[2] && marker[2].toLowerCase() !== repository.toLowerCase()) + ) + return null; + const number = Number(marker[1] ?? marker[3]); + return Number.isSafeInteger(number) ? number : null; +} + export async function stackRootRef(repository, pull, api = githubJson) { for (let depth = 0; depth < 20; depth++) { const ref = pull.base?.ref; - const below = /^Depends-On: #(\d+)\s*$/m.exec(pull.body ?? ""); + const below = stackDependency(repository, pull.body); if (!ref?.startsWith("stack/") || !below) return ref; - pull = await api(`/repos/${repository}/pulls/${below[1]}`); + pull = await api(`/repos/${repository}/pulls/${below}`); if (pull.head?.ref !== ref || pull.head?.repo?.full_name !== repository) return ref; } diff --git a/scripts/check-mergify-batch.test.mjs b/scripts/check-mergify-batch.test.mjs index ea937633bc0..bd0f701f5c6 100644 --- a/scripts/check-mergify-batch.test.mjs +++ b/scripts/check-mergify-batch.test.mjs @@ -2,6 +2,7 @@ import assert from "node:assert/strict"; import test from "node:test"; import { batchPullRequestNumbers, + stackDependency, validateBatch, } from "./check-mergify-batch.mjs"; @@ -103,37 +104,58 @@ test("rejects source PRs with invalid titles or another target branch", async () } }); -test("stack members are validated against the stack root", async () => { - const f = fixture(); - const top = { - ...f.source, - head: { - sha: sourceSha, - ref: "stack/u/feat/b--2", - repo: { full_name: "o/r" }, - }, - base: { ref: "stack/u/feat/a--1" }, - body: "Adds b.\n\nDepends-On: #12\n", - }; - const bottom = { - ...f.source, - head: { - sha: sourceSha, - ref: "stack/u/feat/a--1", - repo: { full_name: "o/r" }, - }, - }; - const api = async (path) => { - if (path === "/repos/o/r/pulls/12") return bottom; - if (path === "/repos/o/r/pulls/13") return top; - return f.api(path); - }; - assert.deepEqual( - await validateBatch(f.input, api, f.commits, f.policy), - [12, 13], - ); +test("stack dependencies accept local shorthand and same-repository GitHub URLs", () => { + for (const body of [ + "Depends-On: #12", + "Text\n\nDepends-On: https://github.com/o/r/pull/12\r\n", + "Depends-On: https://github.com/O/R/pull/12", + ]) + assert.equal(stackDependency("o/r", body), 12); + for (const body of [ + undefined, + "Depends-On: #0", + "Depends-On: #9007199254740992", + "Depends-On: https://github.com/other/r/pull/12", + "Depends-On: https://github.com/o/r/pull/12/extra", + "Depends-On: https://github.com.evil/o/r/pull/12", + "Depends-On: http://github.com/o/r/pull/12", + "Depends-On: #12 extra", + ]) + assert.equal(stackDependency("o/r", body), null); }); +for (const dependency of ["#12", "https://github.com/o/r/pull/12"]) + test(`stack members with ${dependency} are validated against the stack root`, async () => { + const f = fixture(); + const top = { + ...f.source, + head: { + sha: sourceSha, + ref: "stack/u/feat/b--2", + repo: { full_name: "o/r" }, + }, + base: { ref: "stack/u/feat/a--1" }, + body: `Adds b.\n\nDepends-On: ${dependency}\n`, + }; + const bottom = { + ...f.source, + head: { + sha: sourceSha, + ref: "stack/u/feat/a--1", + repo: { full_name: "o/r" }, + }, + }; + const api = async (path) => { + if (path === "/repos/o/r/pulls/12") return bottom; + if (path === "/repos/o/r/pulls/13") return top; + return f.api(path); + }; + assert.deepEqual( + await validateBatch(f.input, api, f.commits, f.policy), + [12, 13], + ); + }); + test("rejects stack-like PRs whose chain does not reach the batch base", async () => { for (const [body, below] of [ // No marker: the queue keeps the literal stack branch as the target.