Skip to content

Commit c5a9985

Browse files
TonybynMp4claude
andcommitted
fix(gitlab): revert retries and single-commit fast-forward merges
Delete the revert branch when opening the merge request fails, so a retry can create it again, and revert the sole commit of a fast-forward merge without squash, as GitLab's own Revert button does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 5586c4c commit c5a9985

3 files changed

Lines changed: 95 additions & 20 deletions

File tree

‎packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts‎

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ function mergeRequestJson(overrides: Record<string, unknown>): string {
8181
});
8282
}
8383

84+
function commitJson(id: string) {
85+
return { id, title: "Commit", committed_date: "2026-07-01T00:00:00Z" };
86+
}
87+
8488
function argsOfCall(index: number): ReadonlyArray<string> {
8589
return callAt(index).args;
8690
}
@@ -516,16 +520,19 @@ layer("GitLabPullRequestCli.layer", (it) => {
516520
}),
517521
);
518522

519-
it.effect("deletes the revert branch when GitLab cannot revert cleanly", () =>
523+
it.effect.each([
524+
["the revert conflicts", "/revert"],
525+
["opening the merge request fails", "/merge_requests"],
526+
] as const)("deletes the revert branch when %s", ([, failingPath]) =>
520527
Effect.gen(function* () {
521528
mockedExecute.mockImplementation(({ args, cwd }) =>
522-
args[1]?.endsWith("/revert")
529+
args[1]?.endsWith(failingPath) && args.includes("POST")
523530
? Effect.fail(
524531
new GitLabCli.GitLabCliCommandError({
525532
operation: "execute",
526533
command: "glab",
527534
cwd,
528-
cause: new Error("Merge request revert failed"),
535+
cause: new Error("GitLab rejected the write"),
529536
}),
530537
)
531538
: Effect.succeed(
@@ -544,15 +551,51 @@ layer("GitLabPullRequestCli.layer", (it) => {
544551
);
545552

546553
expect(result).toMatchObject({ _tag: "Failure", failure: { _tag: "GitLabCliCommandError" } });
547-
expect(mockedExecute.mock.calls.map(([input]) => input.args.slice(1, 4))).toEqual([
548-
["projects/acme%2Fweb/merge_requests/7"],
549-
["projects/acme%2Fweb/repository/branches", "--method", "POST"],
550-
["projects/acme%2Fweb/repository/commits/abc12345ff/revert", "--method", "POST"],
551-
["projects/acme%2Fweb/repository/branches/revert-abc12345", "--method", "DELETE"],
554+
expect(mockedExecute.mock.calls.at(-1)?.[0].args.slice(1, 4)).toEqual([
555+
"projects/acme%2Fweb/repository/branches/revert-abc12345",
556+
"--method",
557+
"DELETE",
552558
]);
553559
}),
554560
);
555561

562+
it.effect.each([
563+
["reverts the sole commit of", [commitJson("1111111122")], "revert-11111111"],
564+
[
565+
"refuses to revert several commits of",
566+
[commitJson("1111111122"), commitJson("2222222233")],
567+
"GitLabRevertUnavailableError",
568+
],
569+
] as const)("%s a fast-forward merge without squash", ([, commits, expected]) =>
570+
Effect.gen(function* () {
571+
mockedExecute
572+
.mockReturnValueOnce(
573+
Effect.succeed(
574+
output(mergeRequestJson({ merge_commit_sha: null, squash_commit_sha: null })),
575+
),
576+
)
577+
.mockReturnValueOnce(Effect.succeed(output(JSON.stringify(commits))))
578+
.mockReturnValue(Effect.succeed(output("{}")));
579+
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
580+
581+
const result = yield* Effect.result(
582+
cli.runMergeRequestAction({
583+
cwd: "/w",
584+
repository: "acme/web",
585+
number: 7,
586+
action: "revert",
587+
}),
588+
);
589+
590+
expect(argsOfCall(1)[1]).toBe("projects/acme%2Fweb/merge_requests/7/commits?per_page=2");
591+
if (expected === "GitLabRevertUnavailableError") {
592+
expect(result).toMatchObject({ _tag: "Failure", failure: { _tag: expected } });
593+
} else {
594+
expect(JSON.parse(callAt(2).stdin ?? "")).toEqual({ branch: expected, ref: "main" });
595+
}
596+
}),
597+
);
598+
556599
it.effect("sends a comment body over stdin, never in argv", () =>
557600
Effect.gen(function* () {
558601
mockedExecute.mockReturnValueOnce(Effect.succeed(output("")));

‎packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts‎

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,35 @@ export const make = Effect.gen(function* () {
928928
}),
929929
);
930930

931+
/** GitLab reverts a fast-forward merge without squash only when it landed as one commit. */
932+
const soleCommitSha = (input: {
933+
readonly cwd: string;
934+
readonly host?: string | undefined;
935+
readonly repository: string;
936+
readonly number: number;
937+
}) =>
938+
api({
939+
cwd: input.cwd,
940+
host: input.host,
941+
path: `projects/${projectPath(input.repository)}/merge_requests/${input.number}/commits?per_page=2`,
942+
}).pipe(
943+
Effect.flatMap((result) => {
944+
const decoded = decodeCommitsJson(result.stdout.trim());
945+
if (!Result.isSuccess(decoded)) {
946+
return Effect.fail(
947+
new GitLabMergeRequestReadError({
948+
command: "glab",
949+
cwd: input.cwd,
950+
operation: "runMergeRequestAction",
951+
cause: decoded.failure,
952+
}),
953+
);
954+
}
955+
const [only, ...rest] = decoded.success;
956+
return Effect.succeed(only !== undefined && rest.length === 0 ? only.oid : null);
957+
}),
958+
);
959+
931960
/** What GitLab's own Revert button does: revert onto a new branch and open a merge request. */
932961
const revertMergeRequest = Effect.fn("GitLabPullRequestCli.revertMergeRequest")(
933962
function* (input: {
@@ -960,17 +989,27 @@ export const make = Effect.gen(function* () {
960989
});
961990
}
962991
const target = decoded.success;
963-
if (target.commitSha === null) {
992+
const commitSha = target.commitSha ?? (yield* soleCommitSha(input));
993+
if (commitSha === null) {
964994
return yield* new GitLabRevertUnavailableError({
965995
command: "glab",
966996
cwd: input.cwd,
967997
number: input.number,
968998
});
969999
}
970-
const branch = `revert-${target.commitSha.slice(0, 8)}`;
1000+
const branch = `revert-${commitSha.slice(0, 8)}`;
9711001
yield* write("repository/branches", { branch, ref: target.targetBranch });
972-
yield* write(`repository/commits/${target.commitSha}/revert`, { branch }).pipe(
973-
// A revert that conflicts leaves nothing worth keeping on the new branch.
1002+
yield* Effect.andThen(
1003+
write(`repository/commits/${commitSha}/revert`, { branch }),
1004+
write("merge_requests", {
1005+
source_branch: branch,
1006+
target_branch: target.targetBranch,
1007+
title: `Revert "${target.title}"`,
1008+
description: `This reverts merge request !${input.number}`,
1009+
remove_source_branch: true,
1010+
}),
1011+
).pipe(
1012+
// Leaving the branch behind would make every retry fail on creating it again.
9741013
Effect.tapError(() =>
9751014
api({
9761015
cwd: input.cwd,
@@ -980,13 +1019,6 @@ export const make = Effect.gen(function* () {
9801019
}).pipe(Effect.ignore),
9811020
),
9821021
);
983-
yield* write("merge_requests", {
984-
source_branch: branch,
985-
target_branch: target.targetBranch,
986-
title: `Revert "${target.title}"`,
987-
description: `This reverts merge request !${input.number}`,
988-
remove_source_branch: true,
989-
});
9901022
},
9911023
);
9921024

‎packages/source-control-gitlab/src/server/gitLabMergeRequestJson.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ export function decodeDiffRefsJson(
593593
export interface GitLabRevertTarget {
594594
readonly title: string;
595595
readonly targetBranch: string;
596-
/** Null for a fast-forward merge without squash, which landed as several commits. */
596+
/** Null for a fast-forward merge without squash, which records neither SHA. */
597597
readonly commitSha: string | null;
598598
}
599599

0 commit comments

Comments
 (0)