Skip to content

Commit 62d54dd

Browse files
fix(gitlab): pin merge actions to the current source SHA
Rebuild the focused guard from PR #10946 in the extracted GitLab package.
1 parent 3a78a2d commit 62d54dd

2 files changed

Lines changed: 105 additions & 64 deletions

File tree

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

Lines changed: 85 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -374,56 +374,94 @@ layer("GitLabPullRequestCli.layer", (it) => {
374374
}),
375375
);
376376

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" },
379+
{ action: "enable-auto-merge", autoMerge: "true" },
380+
] as const)(
381+
"$action succeeds on a project requiring the current source SHA",
382+
({ action, autoMerge }) =>
383+
Effect.gen(function* () {
384+
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
385+
386+
for (const headSha of ["first-head", "updated-head"]) {
387+
mockedExecute.mockImplementation(({ cwd, args }) => {
388+
if (args[0] === "api") {
389+
return Effect.succeed(
390+
output(
391+
mergeRequestJson({
392+
diff_refs: { base_sha: "base", head_sha: headSha, start_sha: "start" },
393+
}),
394+
),
395+
);
396+
}
397+
if (args[args.indexOf("--sha") + 1] !== headSha) {
398+
return Effect.fail(
399+
new GitLabCli.GitLabCliCommandError({
400+
operation: "execute",
401+
command: "glab",
402+
cwd,
403+
cause: new Error("SHA must match the current source branch HEAD"),
404+
}),
405+
);
406+
}
407+
return Effect.succeed(output(""));
408+
});
409+
410+
yield* cli.runMergeRequestAction({
411+
cwd: "/w",
412+
repository: "acme/web",
413+
number: 7,
414+
action,
415+
mergeMethod: "squash",
416+
});
417+
418+
expect(mockedExecute.mock.calls.slice(-2).map(([input]) => input.args)).toEqual([
419+
["api", "projects/acme%2Fweb/merge_requests/7"],
420+
[
421+
"mr",
422+
"merge",
423+
"7",
424+
"--repo",
425+
"acme/web",
426+
`--auto-merge=${autoMerge}`,
427+
"--yes",
428+
"--squash",
429+
"--sha",
430+
headSha,
431+
],
432+
]);
433+
}
434+
expect(mockedExecute).toHaveBeenCalledTimes(4);
435+
}),
401436
);
402437

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-
});
438+
it.effect.each(["merge", "enable-auto-merge"] as const)(
439+
"does not run %s when GitLab has no diff revisions",
440+
(action) =>
441+
Effect.gen(function* () {
442+
mockedExecute.mockReturnValue(
443+
Effect.succeed(output(mergeRequestJson({ diff_refs: null }))),
444+
);
445+
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
446+
447+
const result = yield* Effect.result(
448+
cli.runMergeRequestAction({
449+
cwd: "/w",
450+
repository: "acme/web",
451+
number: 7,
452+
action,
453+
mergeMethod: "squash",
454+
}),
455+
);
415456

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-
}),
457+
expect(mockedExecute.mock.calls.map(([input]) => input.args)).toEqual([
458+
["api", "projects/acme%2Fweb/merge_requests/7"],
459+
]);
460+
expect(result).toMatchObject({
461+
_tag: "Failure",
462+
failure: { _tag: "GitLabDiffRefsUnavailableError" },
463+
});
464+
}),
427465
);
428466

429467
it.effect("cancels an armed auto-merge through the API glab has no flag for", () =>

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

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,25 +1277,28 @@ export const make = Effect.gen(function* () {
12771277
Effect.asVoid,
12781278
),
12791279

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({
1280+
runMergeRequestAction: (input) =>
1281+
Effect.gen(function* () {
1282+
// `glab mr merge` arms auto-merge and never disarms it, so the one direction the CLI has
1283+
// no flag for is asked of GitLab directly through the same `api` passthrough the rest of
1284+
// this module writes with.
1285+
if (input.action === "disable-auto-merge") {
1286+
return yield* api({
1287+
cwd: input.cwd,
1288+
path: `projects/${projectPath(input.repository)}/merge_requests/${input.number}/cancel_merge_when_pipeline_succeeds`,
1289+
method: "POST",
1290+
}).pipe(Effect.asVoid);
1291+
}
1292+
const [subcommand, ...flags] = actionArgs(input.action, input.mergeMethod);
1293+
if (input.action === "merge" || input.action === "enable-auto-merge") {
1294+
const refs = yield* getDiffRefs(input);
1295+
flags.push("--sha", refs.headSha);
1296+
}
1297+
yield* gitlab.execute({
12941298
cwd: input.cwd,
12951299
args: ["mr", subcommand!, String(input.number), "--repo", input.repository, ...flags],
1296-
})
1297-
.pipe(Effect.asVoid);
1298-
},
1300+
});
1301+
}),
12991302

13001303
updateMergeRequest: (input) =>
13011304
api({

0 commit comments

Comments
 (0)