Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2572,6 +2572,85 @@ 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<unknown>) => {
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.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 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);
}),
);

it.effect("says an already merged pull request was merged without asking GitHub again", () =>
Effect.gen(function* () {
route(["query PullRequestActionState", actionState({ merged: true })]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -2675,14 +2696,69 @@ 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 unfinished = (message: string) =>
new GitHubRequestPolicy.GitHubApiResponseError({
host: input.host,
operation: "runPullRequestAction",
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.",
);
}
});
yield* (
auto
? client.EnablePullRequestAutoMerge({ input: merge }).pipe(Effect.asVoid)
: client.MergePullRequest({ input: merge }).pipe(Effect.asVoid)
).pipe(
graphql(target),
Effect.catchTags({
GitHubApiResponseError: (error) => Effect.fail(refusedAs ?? error),
GitHubApiResponseError: (error) =>
action === "merge" && isStackedMergeRefusal(error)
? mergeAsync
: Effect.fail(refusedAs ?? error),
}),
);
});
Expand Down
Loading