Skip to content

Commit bea8fa0

Browse files
TonybynMp4claude
andcommitted
fix(gitlab): refuse to revert a merge request that is not merged
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent c5a9985 commit bea8fa0

3 files changed

Lines changed: 65 additions & 3 deletions

File tree

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

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ layer("GitLabPullRequestCli.layer", (it) => {
478478
Effect.succeed(
479479
output(
480480
mergeRequestJson({
481+
state: "merged",
481482
merge_commit_sha: mergeCommit,
482483
squash_commit_sha: "cb2d9f660ebe946e5056ae98993db5ea72b9bad1",
483484
}),
@@ -536,7 +537,13 @@ layer("GitLabPullRequestCli.layer", (it) => {
536537
}),
537538
)
538539
: Effect.succeed(
539-
output(mergeRequestJson({ merge_commit_sha: null, squash_commit_sha: "abc12345ff" })),
540+
output(
541+
mergeRequestJson({
542+
state: "merged",
543+
merge_commit_sha: null,
544+
squash_commit_sha: "abc12345ff",
545+
}),
546+
),
540547
),
541548
);
542549
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
@@ -571,7 +578,13 @@ layer("GitLabPullRequestCli.layer", (it) => {
571578
mockedExecute
572579
.mockReturnValueOnce(
573580
Effect.succeed(
574-
output(mergeRequestJson({ merge_commit_sha: null, squash_commit_sha: null })),
581+
output(
582+
mergeRequestJson({
583+
state: "merged",
584+
merge_commit_sha: null,
585+
squash_commit_sha: null,
586+
}),
587+
),
575588
),
576589
)
577590
.mockReturnValueOnce(Effect.succeed(output(JSON.stringify(commits))))
@@ -596,6 +609,40 @@ layer("GitLabPullRequestCli.layer", (it) => {
596609
}),
597610
);
598611

612+
it.effect("refuses to revert a merge request that is not merged before any write", () =>
613+
Effect.gen(function* () {
614+
mockedExecute
615+
.mockReturnValueOnce(
616+
Effect.succeed(
617+
output(
618+
mergeRequestJson({
619+
state: "opened",
620+
merge_commit_sha: null,
621+
squash_commit_sha: null,
622+
}),
623+
),
624+
),
625+
)
626+
.mockReturnValue(Effect.succeed(output(JSON.stringify([commitJson("1111111122")]))));
627+
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
628+
629+
const result = yield* Effect.result(
630+
cli.runMergeRequestAction({
631+
cwd: "/w",
632+
repository: "acme/web",
633+
number: 7,
634+
action: "revert",
635+
}),
636+
);
637+
638+
expect(result).toMatchObject({
639+
_tag: "Failure",
640+
failure: { _tag: "GitLabRevertUnavailableError", reason: "not-merged" },
641+
});
642+
expect(mockedExecute).toHaveBeenCalledTimes(1);
643+
}),
644+
);
645+
599646
it.effect("sends a comment body over stdin, never in argv", () =>
600647
Effect.gen(function* () {
601648
mockedExecute.mockReturnValueOnce(Effect.succeed(output("")));

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,13 @@ export class GitLabRevertUnavailableError extends Schema.TaggedError<GitLabRever
123123
command: Schema.Literal("glab"),
124124
cwd: Schema.String,
125125
number: Schema.Int,
126+
reason: Schema.Literals(["not-merged", "several-commits"]),
126127
},
127128
) {
128129
get detail(): string {
129-
return "This merge request landed as several commits, so there is no single commit to revert.";
130+
return this.reason === "not-merged"
131+
? "Only a merged merge request can be reverted."
132+
: "This merge request landed as several commits, so there is no single commit to revert.";
130133
}
131134

132135
override get message(): string {
@@ -989,12 +992,21 @@ export const make = Effect.gen(function* () {
989992
});
990993
}
991994
const target = decoded.success;
995+
if (!target.merged) {
996+
return yield* new GitLabRevertUnavailableError({
997+
command: "glab",
998+
cwd: input.cwd,
999+
number: input.number,
1000+
reason: "not-merged",
1001+
});
1002+
}
9921003
const commitSha = target.commitSha ?? (yield* soleCommitSha(input));
9931004
if (commitSha === null) {
9941005
return yield* new GitLabRevertUnavailableError({
9951006
command: "glab",
9961007
cwd: input.cwd,
9971008
number: input.number,
1009+
reason: "several-commits",
9981010
});
9991011
}
10001012
const branch = `revert-${commitSha.slice(0, 8)}`;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ const RawDiffRefsSchema = Schema.Struct({
137137

138138
const RawRevertTargetSchema = Schema.Struct({
139139
title: Schema.String,
140+
state: Schema.String,
140141
target_branch: Schema.String,
141142
merge_commit_sha: Schema.optional(Schema.NullOr(Schema.String)),
142143
squash_commit_sha: Schema.optional(Schema.NullOr(Schema.String)),
@@ -591,6 +592,7 @@ export function decodeDiffRefsJson(
591592
}
592593

593594
export interface GitLabRevertTarget {
595+
readonly merged: boolean;
594596
readonly title: string;
595597
readonly targetBranch: string;
596598
/** Null for a fast-forward merge without squash, which records neither SHA. */
@@ -604,6 +606,7 @@ export function decodeRevertTargetJson(
604606
if (!Result.isSuccess(decoded)) return Result.fail(decoded.failure);
605607
const mr = decoded.success;
606608
return Result.succeed({
609+
merged: mr.state === "merged",
607610
title: mr.title,
608611
targetBranch: mr.target_branch,
609612
commitSha: trimmed(mr.merge_commit_sha) ?? trimmed(mr.squash_commit_sha),

0 commit comments

Comments
 (0)