Skip to content
TypeScript

fix(gitlab): pin merge actions to the current source SHA (#21523)

Julius Marminge1e951e9e3b20
diff --git a/packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts b/packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts
index 3121eed13cc..9d6dfbf108f 100644
--- a/packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts
+++ b/packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts
@@ -374,56 +374,111 @@ layer("GitLabPullRequestCli.layer", (it) => {
}),
);
- it.effect("merges immediately rather than leaving auto-merge armed", () =>
- Effect.gen(function* () {
- mockedExecute.mockReturnValueOnce(Effect.succeed(output("")));
- const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
-
- yield* cli.runMergeRequestAction({
- cwd: "/w",
- repository: "acme/web",
- number: 7,
- action: "merge",
- mergeMethod: "squash",
- });
-
- expect(argsOfCall(0)).toEqual([
- "mr",
- "merge",
- "7",
- "--repo",
- "acme/web",
- "--auto-merge=false",
- "--yes",
- "--squash",
- ]);
- }),
+ it.effect.each([
+ { action: "merge", autoMerge: "false", mergeMethod: "squash" },
+ { action: "enable-auto-merge", autoMerge: "true", mergeMethod: "squash" },
+ { action: "merge", autoMerge: "false", mergeMethod: "rebase" },
+ { action: "enable-auto-merge", autoMerge: "true", mergeMethod: "rebase" },
+ ] as const)(
+ "$action with $mergeMethod succeeds on a project requiring the current source SHA",
+ ({ action, autoMerge, mergeMethod }) =>
+ Effect.gen(function* () {
+ const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
+
+ for (const headSha of ["first-head", "updated-head"]) {
+ let currentHeadSha = headSha;
+ mockedExecute.mockImplementation(({ cwd, args }) => {
+ if (args[0] === "mr" && args[1] === "rebase") {
+ return Effect.sync(() => {
+ currentHeadSha = `${headSha}-rebased`;
+ return output("");
+ });
+ }
+ if (args[0] === "api") {
+ return Effect.succeed(
+ output(
+ mergeRequestJson({
+ diff_refs: { base_sha: "base", head_sha: currentHeadSha, start_sha: "start" },
+ }),
+ ),
+ );
+ }
+ if (args.includes("--rebase") || args[args.indexOf("--sha") + 1] !== currentHeadSha) {
+ return Effect.fail(
+ new GitLabCli.GitLabCliCommandError({
+ operation: "execute",
+ command: "glab",
+ cwd,
+ cause: new Error(
+ args.includes("--rebase")
+ ? "Merge must not combine --rebase with --sha: rebase changes the source HEAD"
+ : "SHA must match the current source branch HEAD",
+ ),
+ }),
+ );
+ }
+ return Effect.succeed(output(""));
+ });
+
+ yield* cli.runMergeRequestAction({
+ cwd: "/w",
+ repository: "acme/web",
+ number: 7,
+ action,
+ mergeMethod,
+ });
+
+ const callsPerAction = mergeMethod === "rebase" ? 3 : 2;
+ expect(
+ mockedExecute.mock.calls.slice(-callsPerAction).map(([input]) => input.args),
+ ).toEqual([
+ ...(mergeMethod === "rebase" ? [["mr", "rebase", "7", "--repo", "acme/web"]] : []),
+ ["api", "projects/acme%2Fweb/merge_requests/7"],
+ [
+ "mr",
+ "merge",
+ "7",
+ "--repo",
+ "acme/web",
+ `--auto-merge=${autoMerge}`,
+ "--yes",
+ ...(mergeMethod === "squash" ? ["--squash"] : []),
+ "--sha",
+ mergeMethod === "rebase" ? `${headSha}-rebased` : headSha,
+ ],
+ ]);
+ }
+ expect(mockedExecute).toHaveBeenCalledTimes(mergeMethod === "rebase" ? 6 : 4);
+ }),
);
- it.effect("arms auto-merge with the same strategy a merge would have used", () =>
- Effect.gen(function* () {
- mockedExecute.mockReturnValueOnce(Effect.succeed(output("")));
- const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
-
- yield* cli.runMergeRequestAction({
- cwd: "/w",
- repository: "acme/web",
- number: 7,
- action: "enable-auto-merge",
- mergeMethod: "squash",
- });
+ it.effect.each(["merge", "enable-auto-merge"] as const)(
+ "does not run %s when GitLab has no diff revisions",
+ (action) =>
+ Effect.gen(function* () {
+ mockedExecute.mockReturnValue(
+ Effect.succeed(output(mergeRequestJson({ diff_refs: null }))),
+ );
+ const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
+
+ const result = yield* Effect.result(
+ cli.runMergeRequestAction({
+ cwd: "/w",
+ repository: "acme/web",
+ number: 7,
+ action,
+ mergeMethod: "squash",
+ }),
+ );
- expect(argsOfCall(0)).toEqual([
- "mr",
- "merge",
- "7",
- "--repo",
- "acme/web",
- "--auto-merge=true",
- "--yes",
- "--squash",
- ]);
- }),
+ expect(mockedExecute.mock.calls.map(([input]) => input.args)).toEqual([
+ ["api", "projects/acme%2Fweb/merge_requests/7"],
+ ]);
+ expect(result).toMatchObject({
+ _tag: "Failure",
+ failure: { _tag: "GitLabDiffRefsUnavailableError" },
+ });
+ }),
);
it.effect("cancels an armed auto-merge through the API glab has no flag for", () =>
diff --git a/packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts b/packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts
index e812b99f3b7..2a2418c6bf4 100644
--- a/packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts
+++ b/packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts
@@ -440,7 +440,6 @@ function actionArgs(
"--auto-merge=false",
"--yes",
...(mergeMethod === "squash" ? ["--squash"] : []),
- ...(mergeMethod === "rebase" ? ["--rebase"] : []),
];
case "enable-auto-merge":
return [
@@ -448,7 +447,6 @@ function actionArgs(
"--auto-merge=true",
"--yes",
...(mergeMethod === "squash" ? ["--squash"] : []),
- ...(mergeMethod === "rebase" ? ["--rebase"] : []),
];
case "disable-auto-merge":
return [];
@@ -772,10 +770,6 @@ export const make = Effect.gen(function* () {
}),
);
- /**
- * GitLab resolves a comment's line against these three shas, so a review with line
- * comments cannot be sent without them.
- */
const getDiffRefs = (input: {
readonly cwd: string;
readonly repository: string;
@@ -1277,25 +1271,34 @@ export const make = Effect.gen(function* () {
Effect.asVoid,
),
- runMergeRequestAction: (input) => {
- // `glab mr merge` arms auto-merge and never disarms it, so the one direction the CLI has
- // no flag for is asked of GitLab directly through the same `api` passthrough the rest of
- // this module writes with.
- if (input.action === "disable-auto-merge") {
- return api({
- cwd: input.cwd,
- path: `projects/${projectPath(input.repository)}/merge_requests/${input.number}/cancel_merge_when_pipeline_succeeds`,
- method: "POST",
- }).pipe(Effect.asVoid);
- }
- const [subcommand, ...flags] = actionArgs(input.action, input.mergeMethod);
- return gitlab
- .execute({
- cwd: input.cwd,
- args: ["mr", subcommand!, String(input.number), "--repo", input.repository, ...flags],
- })
- .pipe(Effect.asVoid);
- },
+ runMergeRequestAction: (input) =>
+ Effect.gen(function* () {
+ // `glab mr merge` arms auto-merge and never disarms it, so the one direction the CLI has
+ // no flag for is asked of GitLab directly through the same `api` passthrough the rest of
+ // this module writes with.
+ if (input.action === "disable-auto-merge") {
+ return yield* api({
+ cwd: input.cwd,
+ path: `projects/${projectPath(input.repository)}/merge_requests/${input.number}/cancel_merge_when_pipeline_succeeds`,
+ method: "POST",
+ }).pipe(Effect.asVoid);
+ }
+ const executeMr = (subcommand: string, ...flags: ReadonlyArray<string>) =>
+ gitlab.execute({
+ cwd: input.cwd,
+ args: ["mr", subcommand, String(input.number), "--repo", input.repository, ...flags],
+ });
+ const [subcommand, ...flags] = actionArgs(input.action, input.mergeMethod);
+ if (input.action === "merge" || input.action === "enable-auto-merge") {
+ if (input.mergeMethod === "rebase") {
+ // A rebase changes the source SHA, so wait for it before reading the merge guard.
+ yield* executeMr("rebase");
+ }
+ const refs = yield* getDiffRefs(input);
+ flags.push("--sha", refs.headSha);
+ }
+ yield* executeMr(subcommand!, ...flags);
+ }),
updateMergeRequest: (input) =>
api({