From 32215112df9d9be3ee4244f187d32aa2a96a096f Mon Sep 17 00:00:00 2001 From: Yordis Prieto Date: Mon, 2 Nov 2026 16:25:43 -0500 Subject: [PATCH 1/2] fix(source-control-github): merge stacked pull requests through the async merge API Signed-off-by: Yordis Prieto --- .../src/server/GitHubPullRequestApi.test.ts | 55 +++++++++++++ .../src/server/GitHubPullRequestApi.ts | 77 ++++++++++++++++++- 2 files changed, 131 insertions(+), 1 deletion(-) diff --git a/packages/source-control-github/src/server/GitHubPullRequestApi.test.ts b/packages/source-control-github/src/server/GitHubPullRequestApi.test.ts index cf1d66f3e48e2..08e41d99c392a 100644 --- a/packages/source-control-github/src/server/GitHubPullRequestApi.test.ts +++ b/packages/source-control-github/src/server/GitHubPullRequestApi.test.ts @@ -2572,6 +2572,61 @@ layer("GitHubPullRequestApi.layer", (it) => { }), ); + const stackedRefusal = { + data: null, + errors: [ + { + message: + "This pull request is part of a stack and must be merged using the asynchronous merge REST API.", + }, + ], + }; + const mergeStacked = (restReplies: ReadonlyArray) => { + const replies = [...restReplies]; + mockedExecute.mockImplementation((call) => + call.kind === "graphql" && call.query.includes("mergePullRequest(") + ? Effect.succeed(output(encodeJson(stackedRefusal))) + : call.kind === "rest" + ? Effect.succeed(output(encodeJson(replies.shift()), false, false, {}, 202)) + : Effect.succeed(output(encodeJson(actionState()))), + ); + return GitHubPullRequestApi.GitHubPullRequestApi.use((cli) => + cli.runPullRequestAction({ + cwd: "/w", + repository: "acme/web", + host: "github.com", + number: 7, + action: "merge", + mergeMethod: "squash", + }), + ); + }; + + it.effect("merges a stacked pull request through the async REST route GitHub names", () => + Effect.gen(function* () { + yield* mergeStacked([{ status: "merged", details: { message: "Merged", sha: "def456" } }]); + expect(restCallsTo("/pulls/7/merge-async")).toMatchObject([ + { + method: "PUT", + body: { sha: "abc123", merge_method: "squash", merge_action: "default" }, + }, + ]); + }), + ); + + it.effect("reports a stacked pull request merge that GitHub then fails", () => + Effect.gen(function* () { + const error = yield* Effect.flip( + mergeStacked([ + { status: "pending", details: { message: "Queued", uuid: "u-1" } }, + { status: "failed", details: { message: "Required status check is failing" } }, + ]), + ); + expect(error.message).toContain("Required status check is failing"); + expect(restCallsTo("/pulls/7/merge-async/u-1")).toHaveLength(1); + }), + ); + it.effect("says an already merged pull request was merged without asking GitHub again", () => Effect.gen(function* () { route(["query PullRequestActionState", actionState({ merged: true })]); diff --git a/packages/source-control-github/src/server/GitHubPullRequestApi.ts b/packages/source-control-github/src/server/GitHubPullRequestApi.ts index 5c9c3b8703ba8..33f136de291d3 100644 --- a/packages/source-control-github/src/server/GitHubPullRequestApi.ts +++ b/packages/source-control-github/src/server/GitHubPullRequestApi.ts @@ -882,6 +882,27 @@ function searchQuery(input: { ].join(" "); } +const decodeAsyncMerge = Schema.decodeUnknownOption( + Schema.fromJsonString( + Schema.Struct({ + status: Schema.String, + details: Schema.optional( + Schema.Struct({ + message: Schema.optional(Schema.String), + uuid: Schema.optional(Schema.String), + }), + ), + }), + ), +); + +/** GitHub's GraphQL merge refuses a stacked pull request and names the REST route that takes it. */ +const isStackedMergeRefusal = (error: GitHubRequestPolicy.GitHubApiResponseError) => + error.githubErrors?.some((message) => message.includes("asynchronous merge REST API")) === true; + +const ASYNC_MERGE_POLLS = 10; +const ASYNC_MERGE_POLL_INTERVAL = Duration.seconds(1); + const decodeRevisionRefs = Schema.decodeUnknownOption( Schema.fromJsonString( Schema.Struct({ @@ -2675,6 +2696,57 @@ export const make = Effect.gen(function* () { ...(expectedHead === undefined ? {} : { expectedHeadOid: expectedHead }), ...(body === undefined ? {} : { commitBody: body }), }; + // Only the async REST route takes a stacked pull request, and it merges every open one + // below it. It has no auto-merge, so `enable-auto-merge` keeps GitHub's own refusal. + const mergeAsync = Effect.gen(function* () { + const path = `repos/${owner}/${name}/pulls/${input.number}/merge-async`; + const request = yield* api + .rest({ + host: input.host, + operation: "runPullRequestAction", + method: "PUT", + path, + body: { + sha: expectedHead ?? state.headRefOid, + merge_method: input.mergeMethod ?? "merge", + merge_action: bypass ? "direct_merge" : "default", + ...(bypass ? { bypass_rules: true } : {}), + ...(body === undefined ? {} : { commit_message: body }), + }, + }) + .pipe( + // A request already pending for this pull request is the one that was just asked for. + Effect.catchTags({ + GitHubApiResponseError: (error) => + error.status === 409 ? Effect.void : Effect.fail(error), + }), + ); + const first = request === undefined ? Option.none() : decodeAsyncMerge(request.body); + const uuid = Option.flatMapNullishOr(first, (result) => result.details?.uuid); + if (Option.isNone(uuid)) return; + // The merge runs in the background; watch it long enough to report a failure. + for (let poll = 0; poll < ASYNC_MERGE_POLLS; poll++) { + if (poll > 0) yield* Effect.sleep(ASYNC_MERGE_POLL_INTERVAL); + const result = yield* api + .rest({ + host: input.host, + operation: "runPullRequestAction", + path: `${path}/${uuid.value}`, + }) + .pipe(Effect.map((response) => decodeAsyncMerge(response.body))); + if (Option.isNone(result) || result.value.status !== "pending") { + if (Option.isSome(result) && result.value.status === "failed") { + return yield* new GitHubRequestPolicy.GitHubApiResponseError({ + host: input.host, + operation: "runPullRequestAction", + status: 200, + githubErrors: [result.value.details?.message ?? "The merge failed."], + }); + } + return; + } + } + }); yield* ( auto ? client.EnablePullRequestAutoMerge({ input: merge }).pipe(Effect.asVoid) @@ -2682,7 +2754,10 @@ export const make = Effect.gen(function* () { ).pipe( graphql(target), Effect.catchTags({ - GitHubApiResponseError: (error) => Effect.fail(refusedAs ?? error), + GitHubApiResponseError: (error) => + action === "merge" && isStackedMergeRefusal(error) + ? mergeAsync + : Effect.fail(refusedAs ?? error), }), ); }); From 2749fb07882d3a6072f0bc365befe180cc9a4273 Mon Sep 17 00:00:00 2001 From: Yordis Prieto Date: Mon, 2 Nov 2026 16:37:13 -0500 Subject: [PATCH 2/2] fix(source-control-github): report stacked merges that fail, stall, or are already pending Signed-off-by: Yordis Prieto --- .../src/server/GitHubPullRequestApi.test.ts | 42 +++++++-- .../src/server/GitHubPullRequestApi.ts | 85 ++++++++++--------- 2 files changed, 76 insertions(+), 51 deletions(-) diff --git a/packages/source-control-github/src/server/GitHubPullRequestApi.test.ts b/packages/source-control-github/src/server/GitHubPullRequestApi.test.ts index 08e41d99c392a..e12164531cac3 100644 --- a/packages/source-control-github/src/server/GitHubPullRequestApi.test.ts +++ b/packages/source-control-github/src/server/GitHubPullRequestApi.test.ts @@ -2614,16 +2614,40 @@ layer("GitHubPullRequestApi.layer", (it) => { }), ); - it.effect("reports a stacked pull request merge that GitHub then fails", () => + it.effect.each([ + [ + "a failure reported at once", + [{ status: "failed", details: { message: "Merge not allowed" } }], + "Merge not allowed", + ], + [ + "a failure found while polling", + [ + { status: "pending", details: { message: "Queued", uuid: "u-1" } }, + { status: "failed", details: { message: "Required status check is failing" } }, + ], + "Required status check is failing", + ], + [ + "a merge still pending after the last poll", + Array.from({ length: 11 }, () => ({ + status: "pending", + details: { message: "Queued", uuid: "u-1" }, + })), + "still running on GitHub", + ], + [ + "an unreadable poll response", + [{ status: "pending", details: { message: "Queued", uuid: "u-1" } }, "nope"], + "unreadable merge response", + ], + ] as const)("reports a stacked merge with %s", ([, replies, message]) => Effect.gen(function* () { - const error = yield* Effect.flip( - mergeStacked([ - { status: "pending", details: { message: "Queued", uuid: "u-1" } }, - { status: "failed", details: { message: "Required status check is failing" } }, - ]), - ); - expect(error.message).toContain("Required status check is failing"); - expect(restCallsTo("/pulls/7/merge-async/u-1")).toHaveLength(1); + const merge = Effect.flip(mergeStacked(replies)); + const fiber = yield* Effect.forkChild(merge); + yield* TestClock.adjust("10 seconds"); + const error = yield* Fiber.join(fiber); + expect(error.message).toContain(message); }), ); diff --git a/packages/source-control-github/src/server/GitHubPullRequestApi.ts b/packages/source-control-github/src/server/GitHubPullRequestApi.ts index 33f136de291d3..13b96a3c43522 100644 --- a/packages/source-control-github/src/server/GitHubPullRequestApi.ts +++ b/packages/source-control-github/src/server/GitHubPullRequestApi.ts @@ -2700,51 +2700,52 @@ export const make = Effect.gen(function* () { // below it. It has no auto-merge, so `enable-auto-merge` keeps GitHub's own refusal. const mergeAsync = Effect.gen(function* () { const path = `repos/${owner}/${name}/pulls/${input.number}/merge-async`; - const request = yield* api - .rest({ + const unfinished = (message: string) => + new GitHubRequestPolicy.GitHubApiResponseError({ host: input.host, operation: "runPullRequestAction", - method: "PUT", - path, - body: { - sha: expectedHead ?? state.headRefOid, - merge_method: input.mergeMethod ?? "merge", - merge_action: bypass ? "direct_merge" : "default", - ...(bypass ? { bypass_rules: true } : {}), - ...(body === undefined ? {} : { commit_message: body }), - }, - }) - .pipe( - // A request already pending for this pull request is the one that was just asked for. - Effect.catchTags({ - GitHubApiResponseError: (error) => - error.status === 409 ? Effect.void : Effect.fail(error), - }), + status: 200, + githubErrors: [message], + }); + // Only a merged or enqueued result is success; anything else is reported, never assumed. + const settled = (raw: string) => + Option.match(decodeAsyncMerge(raw), { + onNone: () => + Effect.fail(unfinished("GitHub returned an unreadable merge response.")), + onSome: (result) => + result.status === "failed" + ? Effect.fail(unfinished(result.details?.message ?? "The merge failed.")) + : Effect.succeed(result), + }); + const request = yield* api.rest({ + host: input.host, + operation: "runPullRequestAction", + method: "PUT", + path, + body: { + sha: expectedHead ?? state.headRefOid, + merge_method: input.mergeMethod ?? "merge", + merge_action: bypass ? "direct_merge" : "default", + ...(bypass ? { bypass_rules: true } : {}), + ...(body === undefined ? {} : { commit_message: body }), + }, + }); + let result = yield* settled(request.body); + for (let poll = 0; result.status === "pending" && poll < ASYNC_MERGE_POLLS; poll++) { + const uuid = result.details?.uuid; + if (uuid === undefined) break; + yield* Effect.sleep(ASYNC_MERGE_POLL_INTERVAL); + const response = yield* api.rest({ + host: input.host, + operation: "runPullRequestAction", + path: `${path}/${encodeURIComponent(uuid)}`, + }); + result = yield* settled(response.body); + } + if (result.status === "pending") { + return yield* unfinished( + "The merge is still running on GitHub. Check its status there before submitting another request.", ); - const first = request === undefined ? Option.none() : decodeAsyncMerge(request.body); - const uuid = Option.flatMapNullishOr(first, (result) => result.details?.uuid); - if (Option.isNone(uuid)) return; - // The merge runs in the background; watch it long enough to report a failure. - for (let poll = 0; poll < ASYNC_MERGE_POLLS; poll++) { - if (poll > 0) yield* Effect.sleep(ASYNC_MERGE_POLL_INTERVAL); - const result = yield* api - .rest({ - host: input.host, - operation: "runPullRequestAction", - path: `${path}/${uuid.value}`, - }) - .pipe(Effect.map((response) => decodeAsyncMerge(response.body))); - if (Option.isNone(result) || result.value.status !== "pending") { - if (Option.isSome(result) && result.value.status === "failed") { - return yield* new GitHubRequestPolicy.GitHubApiResponseError({ - host: input.host, - operation: "runPullRequestAction", - status: 200, - githubErrors: [result.value.details?.message ?? "The merge failed."], - }); - } - return; - } } }); yield* (