@devries-cameron20 / t3code Mirror · read-only / commits / 1e951e9e3b
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 23, 00:30 UTC

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

Julius Marminge committed Oct 19, 2026 at 16:28 UTC 1e951e9e3b20127e3c69adc3e1898d71d9115949
2 files changed +130 -72
packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts
+102 -47
@@ -374,56 +374,111 @@ layer("GitLabPullRequestCli.layer", (it) => {
374 }),
375 );
376
377 - it.effect("merges immediately rather than leaving auto-merge armed", () =>
378 - Effect.gen(function* () {
379 - mockedExecute.mockReturnValueOnce(Effect.succeed(output("")));
380 - const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
381 -
382 - yield* cli.runMergeRequestAction({
383 - cwd: "/w",
384 - repository: "acme/web",
385 - number: 7,
386 - action: "merge",
387 - mergeMethod: "squash",
388 - });
389 -
390 - expect(argsOfCall(0)).toEqual([
391 - "mr",
392 - "merge",
393 - "7",
394 - "--repo",
395 - "acme/web",
396 - "--auto-merge=false",
397 - "--yes",
398 - "--squash",
399 - ]);
400 - }),
377 + it.effect.each([
378 + { action: "merge", autoMerge: "false", mergeMethod: "squash" },
379 + { action: "enable-auto-merge", autoMerge: "true", mergeMethod: "squash" },
380 + { action: "merge", autoMerge: "false", mergeMethod: "rebase" },
381 + { action: "enable-auto-merge", autoMerge: "true", mergeMethod: "rebase" },
382 + ] as const)(
383 + "$action with $mergeMethod succeeds on a project requiring the current source SHA",
384 + ({ action, autoMerge, mergeMethod }) =>
385 + Effect.gen(function* () {
386 + const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
387 +
388 + for (const headSha of ["first-head", "updated-head"]) {
389 + let currentHeadSha = headSha;
390 + mockedExecute.mockImplementation(({ cwd, args }) => {
391 + if (args[0] === "mr" && args[1] === "rebase") {
392 + return Effect.sync(() => {
393 + currentHeadSha = `${headSha}-rebased`;
394 + return output("");
395 + });
396 + }
397 + if (args[0] === "api") {
398 + return Effect.succeed(
399 + output(
400 + mergeRequestJson({
401 + diff_refs: { base_sha: "base", head_sha: currentHeadSha, start_sha: "start" },
402 + }),
403 + ),
404 + );
405 + }
406 + if (args.includes("--rebase") || args[args.indexOf("--sha") + 1] !== currentHeadSha) {
407 + return Effect.fail(
408 + new GitLabCli.GitLabCliCommandError({
409 + operation: "execute",
410 + command: "glab",
411 + cwd,
412 + cause: new Error(
413 + args.includes("--rebase")
414 + ? "Merge must not combine --rebase with --sha: rebase changes the source HEAD"
415 + : "SHA must match the current source branch HEAD",
416 + ),
417 + }),
418 + );
419 + }
420 + return Effect.succeed(output(""));
421 + });
422 +
423 + yield* cli.runMergeRequestAction({
424 + cwd: "/w",
425 + repository: "acme/web",
426 + number: 7,
427 + action,
428 + mergeMethod,
429 + });
430 +
431 + const callsPerAction = mergeMethod === "rebase" ? 3 : 2;
432 + expect(
433 + mockedExecute.mock.calls.slice(-callsPerAction).map(([input]) => input.args),
434 + ).toEqual([
435 + ...(mergeMethod === "rebase" ? [["mr", "rebase", "7", "--repo", "acme/web"]] : []),
436 + ["api", "projects/acme%2Fweb/merge_requests/7"],
437 + [
438 + "mr",
439 + "merge",
440 + "7",
441 + "--repo",
442 + "acme/web",
443 + `--auto-merge=${autoMerge}`,
444 + "--yes",
445 + ...(mergeMethod === "squash" ? ["--squash"] : []),
446 + "--sha",
447 + mergeMethod === "rebase" ? `${headSha}-rebased` : headSha,
448 + ],
449 + ]);
450 + }
451 + expect(mockedExecute).toHaveBeenCalledTimes(mergeMethod === "rebase" ? 6 : 4);
452 + }),
453 );
454
403 - it.effect("arms auto-merge with the same strategy a merge would have used", () =>
404 - Effect.gen(function* () {
405 - mockedExecute.mockReturnValueOnce(Effect.succeed(output("")));
406 - const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
407 -
408 - yield* cli.runMergeRequestAction({
409 - cwd: "/w",
410 - repository: "acme/web",
411 - number: 7,
412 - action: "enable-auto-merge",
413 - mergeMethod: "squash",
414 - });
455 + it.effect.each(["merge", "enable-auto-merge"] as const)(
456 + "does not run %s when GitLab has no diff revisions",
457 + (action) =>
458 + Effect.gen(function* () {
459 + mockedExecute.mockReturnValue(
460 + Effect.succeed(output(mergeRequestJson({ diff_refs: null }))),
461 + );
462 + const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
463 +
464 + const result = yield* Effect.result(
465 + cli.runMergeRequestAction({
466 + cwd: "/w",
467 + repository: "acme/web",
468 + number: 7,
469 + action,
470 + mergeMethod: "squash",
471 + }),
472 + );
473
416 - expect(argsOfCall(0)).toEqual([
417 - "mr",
418 - "merge",
419 - "7",
420 - "--repo",
421 - "acme/web",
422 - "--auto-merge=true",
423 - "--yes",
424 - "--squash",
425 - ]);
426 - }),
474 + expect(mockedExecute.mock.calls.map(([input]) => input.args)).toEqual([
475 + ["api", "projects/acme%2Fweb/merge_requests/7"],
476 + ]);
477 + expect(result).toMatchObject({
478 + _tag: "Failure",
479 + failure: { _tag: "GitLabDiffRefsUnavailableError" },
480 + });
481 + }),
482 );
483
484 it.effect("cancels an armed auto-merge through the API glab has no flag for", () =>
packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts
+28 -25
@@ -440,7 +440,6 @@ function actionArgs(
440 "--auto-merge=false",
441 "--yes",
442 ...(mergeMethod === "squash" ? ["--squash"] : []),
443 - ...(mergeMethod === "rebase" ? ["--rebase"] : []),
443 ];
444 case "enable-auto-merge":
445 return [
@@ -448,7 +447,6 @@ function actionArgs(
447 "--auto-merge=true",
448 "--yes",
449 ...(mergeMethod === "squash" ? ["--squash"] : []),
451 - ...(mergeMethod === "rebase" ? ["--rebase"] : []),
450 ];
451 case "disable-auto-merge":
452 return [];
@@ -772,10 +770,6 @@ export const make = Effect.gen(function* () {
770 }),
771 );
772
775 - /**
776 - * GitLab resolves a comment's line against these three shas, so a review with line
777 - * comments cannot be sent without them.
778 - */
773 const getDiffRefs = (input: {
774 readonly cwd: string;
775 readonly repository: string;
@@ -1277,25 +1271,34 @@ export const make = Effect.gen(function* () {
1271 Effect.asVoid,
1272 ),
1273
1280 - runMergeRequestAction: (input) => {
1281 - // `glab mr merge` arms auto-merge and never disarms it, so the one direction the CLI has
1282 - // no flag for is asked of GitLab directly through the same `api` passthrough the rest of
1283 - // this module writes with.
1284 - if (input.action === "disable-auto-merge") {
1285 - return api({
1286 - cwd: input.cwd,
1287 - path: `projects/${projectPath(input.repository)}/merge_requests/${input.number}/cancel_merge_when_pipeline_succeeds`,
1288 - method: "POST",
1289 - }).pipe(Effect.asVoid);
1290 - }
1291 - const [subcommand, ...flags] = actionArgs(input.action, input.mergeMethod);
1292 - return gitlab
1293 - .execute({
1294 - cwd: input.cwd,
1295 - args: ["mr", subcommand!, String(input.number), "--repo", input.repository, ...flags],
1296 - })
1297 - .pipe(Effect.asVoid);
1298 - },
1274 + runMergeRequestAction: (input) =>
1275 + Effect.gen(function* () {
1276 + // `glab mr merge` arms auto-merge and never disarms it, so the one direction the CLI has
1277 + // no flag for is asked of GitLab directly through the same `api` passthrough the rest of
1278 + // this module writes with.
1279 + if (input.action === "disable-auto-merge") {
1280 + return yield* api({
1281 + cwd: input.cwd,
1282 + path: `projects/${projectPath(input.repository)}/merge_requests/${input.number}/cancel_merge_when_pipeline_succeeds`,
1283 + method: "POST",
1284 + }).pipe(Effect.asVoid);
1285 + }
1286 + const executeMr = (subcommand: string, ...flags: ReadonlyArray<string>) =>
1287 + gitlab.execute({
1288 + cwd: input.cwd,
1289 + args: ["mr", subcommand, String(input.number), "--repo", input.repository, ...flags],
1290 + });
1291 + const [subcommand, ...flags] = actionArgs(input.action, input.mergeMethod);
1292 + if (input.action === "merge" || input.action === "enable-auto-merge") {
1293 + if (input.mergeMethod === "rebase") {
1294 + // A rebase changes the source SHA, so wait for it before reading the merge guard.
1295 + yield* executeMr("rebase");
1296 + }
1297 + const refs = yield* getDiffRefs(input);
1298 + flags.push("--sha", refs.headSha);
1299 + }
1300 + yield* executeMr(subcommand!, ...flags);
1301 + }),
1302
1303 updateMergeRequest: (input) =>
1304 api({