@devries-cameron20 / t3code-1 / commits / 7039d907bd
Fork 0

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

fix(gitlab): merge requests show their approval state (#26877)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <maria@kuuro.net>

oss96 committed Nov 1, 2026 at 22:02 UTC 7039d907bd79111270e9fb7108d11d12f21d6d0f
6 files changed +420 -3
packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts
+52
@@ -96,6 +96,58 @@ afterEach(() => {
96 });
97
98 layer("GitLabPullRequestCli.layer", (it) => {
99 + it.effect("batches approval reads at GitLab's page limit and skips an empty list", () =>
100 + Effect.gen(function* () {
101 + mockedExecute.mockImplementation(({ stdin }) => {
102 + const { variables } = JSON.parse(stdin ?? "{}");
103 + return Effect.succeed(
104 + output(
105 + JSON.stringify({
106 + data: {
107 + metadata: { enterprise: true },
108 + project: {
109 + mergeRequests: {
110 + nodes: variables.iids.map((iid: string) => ({
111 + iid,
112 + approved: true,
113 + approvedBy: { nodes: [{ username: "reviewer" }] },
114 + })),
115 + },
116 + },
117 + },
118 + }),
119 + ),
120 + );
121 + });
122 + const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
123 + const reference = { cwd: "/w", repository: "acme/web", host: "gitlab.internal" };
124 + expect(yield* cli.getReviewDecisions({ ...reference, numbers: [] })).toEqual(new Map());
125 + expect(mockedExecute).not.toHaveBeenCalled();
126 + const decisions = yield* cli.getReviewDecisions({
127 + ...reference,
128 + numbers: Array.from({ length: 101 }, (_, index) => index + 1),
129 + });
130 + expect(callAt(0).host).toBe(reference.host);
131 + expect(callAt(1).host).toBe(reference.host);
132 + expect(decisions.size).toBe(101);
133 + expect(decisions.get(101)).toBe("approved");
134 + expect(mockedExecute).toHaveBeenCalledTimes(2);
135 + expect(
136 + [0, 1].map((index) => JSON.parse(callAt(index).stdin ?? "{}").variables.iids.length),
137 + ).toEqual([100, 1]);
138 + expect(argsOfCall(0)).toEqual([
139 + "api",
140 + "graphql",
141 + "--method",
142 + "POST",
143 + "--input",
144 + "-",
145 + "--header",
146 + "Content-Type: application/json",
147 + ]);
148 + }),
149 + );
150 +
151 it.effect("asks GitLab for one row more than the page, to probe for a next page", () =>
152 Effect.gen(function* () {
153 mockedExecute.mockReturnValueOnce(Effect.succeed(output(mergeRequests(3, 1))));
packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts
+41
@@ -20,6 +20,7 @@ import type {
20 PullRequestReaction,
21 PullRequestReactionContent,
22 PullRequestReviewCommentDraft,
23 + PullRequestReviewDecision,
24 PullRequestReviewPosition,
25 PullRequestReviewThread,
26 PullRequestReviewVerdict,
@@ -46,12 +47,14 @@ import {
47 decodeProjectLabelsJson,
48 decodeProjectUsersJson,
49 decodeRepositoryBlobsJson,
50 + decodeReviewDecisionsJson,
51 decodeTriggerJobChecksJson,
52 decodeViewerJson,
53 gitLabAwardName,
54 MERGE_REQUEST_FILE_STATS_GRAPHQL_QUERY,
55 MERGE_REQUEST_STATS_GRAPHQL_QUERY,
56 REPOSITORY_BLOBS_GRAPHQL_QUERY,
57 + REVIEW_DECISIONS_GRAPHQL_QUERY,
58 type GitLabDiffRefs,
59 type GitLabMergeRequestDetail,
60 type GitLabMergeRequestListItem,
@@ -251,6 +254,16 @@ export class GitLabPullRequestCli extends Context.Service<
254 readonly downstreamChecks?: boolean;
255 }) => Effect.Effect<GitLabMergeRequestDetail, GitLabPullRequestCliError>;
256
257 + readonly getReviewDecisions: (input: {
258 + readonly cwd: string;
259 + readonly host?: string | undefined;
260 + readonly repository: string;
261 + readonly numbers: ReadonlyArray<number>;
262 + }) => Effect.Effect<
263 + ReadonlyMap<number, PullRequestReviewDecision | null>,
264 + GitLabPullRequestCliError
265 + >;
266 +
267 /** Line counts for the given merge requests; one with no answer is left out. */
268 readonly listMergeRequestStats: (input: {
269 readonly cwd: string;
@@ -1295,6 +1308,24 @@ export const make = Effect.gen(function* () {
1308 }),
1309 );
1310
1311 + const reviewDecisionsOf = (input: {
1312 + readonly cwd: string;
1313 + readonly host?: string | undefined;
1314 + readonly repository: string;
1315 + readonly numbers: ReadonlyArray<number>;
1316 + }): Effect.Effect<
1317 + ReadonlyMap<number, PullRequestReviewDecision | null>,
1318 + GitLabPullRequestCliError
1319 + > =>
1320 + graphqlRead({
1321 + cwd: input.cwd,
1322 + host: input.host,
1323 + operation: "getReviewDecisions",
1324 + query: REVIEW_DECISIONS_GRAPHQL_QUERY,
1325 + variables: { fullPath: input.repository, iids: input.numbers.map(String) },
1326 + decode: decodeReviewDecisionsJson,
1327 + });
1328 +
1329 const viewerUsername = (input: { readonly cwd: string; readonly host?: string | undefined }) =>
1330 api({ cwd: input.cwd, host: input.host, path: "user" }).pipe(
1331 Effect.flatMap((result): Effect.Effect<string, GitLabPullRequestCliError> => {
@@ -1338,6 +1369,16 @@ export const make = Effect.gen(function* () {
1369 },
1370 ),
1371
1372 + getReviewDecisions: Effect.fn("GitLabPullRequestCli.getReviewDecisions")(function (input) {
1373 + const batches: Array<ReadonlyArray<number>> = [];
1374 + for (let at = 0; at < input.numbers.length; at += MAX_PAGE_SIZE) {
1375 + batches.push(input.numbers.slice(at, at + MAX_PAGE_SIZE));
1376 + }
1377 + return Effect.forEach(batches, (numbers) => reviewDecisionsOf({ ...input, numbers }), {
1378 + concurrency: 2,
1379 + }).pipe(Effect.map((pages) => new Map(pages.flatMap((page) => [...page]))));
1380 + }),
1381 +
1382 listMergeRequestStats,
1383
1384 getMergeRequestFileStats: Effect.fn("GitLabPullRequestCli.getMergeRequestFileStats")((input) =>
packages/source-control-gitlab/src/server/GitLabPullRequestProvider.test.ts
+121 -1
@@ -112,7 +112,7 @@ it.effect.each(["merge", "disable-auto-merge"] as const)(
112 yield* provider.searchRepositoryAuthors(reference);
113 yield* provider.runAction({ ...reference, action });
114
115 - expect(readHosts).toEqual(Array(action === "merge" ? 11 : 10).fill(selectedHost));
115 + expect(readHosts).toEqual(Array(action === "merge" ? 12 : 11).fill(selectedHost));
116 expect({ url: detail.url, mutatedHosts: mergedHosts }).toEqual({
117 url: `https://${selectedHost}/acme/web/-/merge_requests/7`,
118 mutatedHosts: [selectedHost],
@@ -187,6 +187,8 @@ describe("getChangeRequest base freshness", () => {
187 Effect.provide(
188 Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
189 getMergeRequestDetail: () => Effect.succeed(detail),
190 + getReviewDecisions: () => Effect.succeed(new Map()),
191 +
192 listMergeRequestStats: () => Effect.succeed([]),
193 }),
194 ),
@@ -286,6 +288,7 @@ describe("getChangeRequest base freshness", () => {
288 Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
289 getMergeRequestDetail: () => Effect.succeed(detail),
290 getProjectMergeCapabilities: () => Effect.die("linked threads skip merge settings"),
291 + getReviewDecisions: () => Effect.succeed(new Map()),
292 listMergeRequestStats: () =>
293 Effect.succeed([{ repository: "acme/web", number: 7, additions: 40, deletions: 2 }]),
294 }),
@@ -333,6 +336,7 @@ describe("merge request reads", () => {
336 Effect.provide(
337 Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
338 listMergeRequestStats: () => Effect.succeed([]),
339 + getReviewDecisions: () => Effect.succeed(new Map()),
340 getMergeRequestDetail: (input) =>
341 Effect.sync(() => {
342 calls.detail += 1;
@@ -558,3 +562,119 @@ describe("optional activity reads", () => {
562 }),
563 );
564 });
565 +
566 +describe("review decisions", () => {
567 + const reference = { cwd: "/w", repository: "acme/web", host: "gitlab.com", number: 7 };
568 + const item = {
569 + number: 7,
570 + title: "Merge request 7",
571 + url: "https://gitlab.com/acme/web/-/merge_requests/7",
572 + author: null,
573 + headBranch: "feat/page",
574 + baseBranch: "main",
575 + state: "open" as const,
576 + isDraft: false,
577 + mergeability: "mergeable" as const,
578 + additions: 0,
579 + deletions: 0,
580 + createdAt: "2026-07-01T00:00:00Z",
581 + updatedAt: "2026-07-02T00:00:00Z",
582 + reviewRequestLogins: [],
583 + labels: [],
584 + };
585 + const detail = {
586 + ...item,
587 + body: "",
588 + changedFiles: 1,
589 + mergedAt: null,
590 + closedAt: null,
591 + reviewers: [],
592 + checks: [],
593 + viewerCanMerge: true,
594 + reviewerIds: [],
595 + };
596 + const providerWith = (
597 + getReviewDecisions: GitLabPullRequestCli.GitLabPullRequestCli["Service"]["getReviewDecisions"],
598 + ) =>
599 + make.pipe(
600 + Effect.provide(
601 + Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
602 + listMergeRequests: () =>
603 + Effect.succeed({
604 + items: [item, { ...item, number: 8 }],
605 + truncated: false,
606 + cursorAdvance: 2,
607 + }),
608 + getMergeRequestDetail: () => Effect.succeed(detail),
609 + getReviewDecisions,
610 + listMergeRequestStats: () =>
611 + Effect.succeed([{ repository: "acme/web", number: 7, additions: 40, deletions: 2 }]),
612 + }),
613 + ),
614 + );
615 + const listing = {
616 + cwd: "/w",
617 + repository: "acme/web",
618 + host: "gitlab.com",
619 + state: "open" as const,
620 + involvement: "all" as const,
621 + viewer: "julius",
622 + limit: 2,
623 + };
624 +
625 + it.effect("puts GitLab's approval state on listed and summarized merge requests", () =>
626 + Effect.gen(function* () {
627 + const provider = yield* providerWith((input) => {
628 + expect(input.host).toBe(reference.host);
629 + return Effect.succeed(
630 + new Map([
631 + [7, "approved" as const],
632 + [8, null],
633 + ]),
634 + );
635 + });
636 + const listed = yield* provider.listChangeRequests(listing);
637 + assert.isDefined(provider.getChangeRequestSummary);
638 + const summary = yield* provider.getChangeRequestSummary(reference);
639 +
640 + expect(listed.items.map(({ reviewDecision }) => reviewDecision)).toEqual(["approved", null]);
641 + expect(summary.reviewDecision).toBe("approved");
642 + expect(summary.additions).toBe(40);
643 + expect(summary.deletions).toBe(2);
644 + }),
645 + );
646 +
647 + it.effect.each([
648 + {
649 + failure: "a failed read",
650 + error: new GitLabPullRequestCli.GitLabMergeRequestReadError({
651 + command: "glab",
652 + cwd: "/w",
653 + operation: "getReviewDecisions",
654 + cause: null,
655 + }),
656 + },
657 + {
658 + failure: "a rate limit",
659 + error: new GitLabCli.GitLabCliRateLimitError({
660 + operation: "execute",
661 + command: "glab",
662 + cwd: "/w",
663 + cause: null,
664 + }),
665 + },
666 + ])("keeps rows and summaries with an unknown decision after $failure", ({ error }) =>
667 + Effect.gen(function* () {
668 + const provider = yield* providerWith(() => Effect.fail(error));
669 + const listed = yield* provider.listChangeRequests(listing);
670 + assert.isDefined(provider.getChangeRequestSummary);
671 + const summary = yield* provider.getChangeRequestSummary(reference);
672 +
673 + expect(listed.items.map(({ reviewDecision }) => reviewDecision)).toEqual([
674 + undefined,
675 + undefined,
676 + ]);
677 + expect(summary.reviewDecision).toBeUndefined();
678 + }),
679 + );
680 +});
packages/source-control-gitlab/src/server/GitLabPullRequestProvider.ts
+49 -2
@@ -6,6 +6,7 @@ import * as Exit from "effect/Exit";
6 import type {
7 PullRequestCapabilities,
8 PullRequestReaction,
9 + PullRequestReviewDecision,
10 PullRequestViewerPermissions,
11 } from "@t3tools/contracts";
12
@@ -180,6 +181,17 @@ export const make = Effect.gen(function* () {
181 }),
182 );
183
184 + // The rows were already read, so even a rate limit only leaves their decision unknown.
185 + const reviewDecisions = (input: Parameters<typeof cli.getReviewDecisions>[0]) =>
186 + cli.getReviewDecisions(input).pipe(
187 + Effect.catch((error) =>
188 + Effect.logWarning("GitLab merge request approval read failed", {
189 + repository: input.repository,
190 + reason: error._tag,
191 + }).pipe(Effect.as<ReadonlyMap<number, PullRequestReviewDecision | null>>(new Map())),
192 + ),
193 + );
194 +
195 // REST omits line counts; the CLI recovers optional stats failures per batch.
196 const mergeRequestWithStats = (
197 input: {
@@ -228,8 +240,21 @@ export const make = Effect.gen(function* () {
240 filters: input.filters,
241 })
242 .pipe(
243 + Effect.flatMap((batch) =>
244 + reviewDecisions({
245 + cwd: input.cwd,
246 + host: input.host,
247 + repository: input.repository,
248 + numbers: batch.items.map((item) => item.number),
249 + }).pipe(
250 + Effect.map((decisions) => ({
251 + ...batch,
252 + items: batch.items.map((item) => withReviewDecision(item, decisions)),
253 + continues: true,
254 + })),
255 + ),
256 + ),
257 Effect.mapError(fail("listChangeRequests")),
232 - Effect.map((batch) => ({ ...batch, continues: true })),
258 ),
259
260 getFileStats: (input) =>
@@ -249,7 +274,21 @@ export const make = Effect.gen(function* () {
274 cli.getMergeRequestDetail(input).pipe(Effect.mapError(fail("getChangeRequestPreview"))),
275
276 getChangeRequestSummary: (input) =>
252 - mergeRequestWithStats(input).pipe(Effect.mapError(fail("getChangeRequestSummary"))),
277 + Effect.all(
278 + [
279 + mergeRequestWithStats(input),
280 + reviewDecisions({
281 + cwd: input.cwd,
282 + host: input.host,
283 + repository: input.repository,
284 + numbers: [input.number],
285 + }),
286 + ],
287 + { concurrency: 2 },
288 + ).pipe(
289 + Effect.map(([mergeRequest, decisions]) => withReviewDecision(mergeRequest, decisions)),
290 + Effect.mapError(fail("getChangeRequestSummary")),
291 + ),
292
293 getChangeRequest: (input) =>
294 Effect.all(
@@ -483,3 +522,11 @@ export const make = Effect.gen(function* () {
522
523 return provider;
524 });
525 +
526 +function withReviewDecision<Item extends { readonly number: number }>(
527 + item: Item,
528 + decisions: ReadonlyMap<number, PullRequestReviewDecision | null>,
529 +): Item & { readonly reviewDecision?: PullRequestReviewDecision | null } {
530 + const reviewDecision = decisions.get(item.number);
531 + return reviewDecision === undefined ? item : { ...item, reviewDecision };
532 +}
packages/source-control-gitlab/src/server/gitLabMergeRequestJson.test.ts
+75
@@ -12,6 +12,7 @@ import {
12 decodeMergeRequestStatsJson,
13 decodeNotesJson,
14 decodeOwnAwardIdJson,
15 + decodeReviewDecisionsJson,
16 decodeTriggerJobChecksJson,
17 } from "./gitLabMergeRequestJson.ts";
18
@@ -660,6 +661,80 @@ describe("decodeRepositoryBlobsJson", () => {
661 });
662 });
663
664 +describe("decodeReviewDecisionsJson", () => {
665 + const decisionsOf = (enterprise: boolean | null, nodes: ReadonlyArray<unknown>) =>
666 + expectSuccess(
667 + decodeReviewDecisionsJson(
668 + JSON.stringify({
669 + data: {
670 + metadata: enterprise === null ? null : { enterprise },
671 + project: { mergeRequests: { nodes } },
672 + },
673 + }),
674 + ),
675 + );
676 + const approvedBy = (...usernames: ReadonlyArray<string>) => ({
677 + nodes: usernames.map((username) => ({ username })),
678 + });
679 +
680 + it.each([
681 + {
682 + reading: "an approval short of Enterprise approval rules",
683 + enterprise: true,
684 + approval: { approved: false, approvedBy: approvedBy("GitLabDuo") },
685 + decision: "review-required",
686 + },
687 + {
688 + reading: "Enterprise approval rules no one has approved against",
689 + enterprise: true,
690 + approval: { approved: false, approvedBy: approvedBy() },
691 + decision: "review-required",
692 + },
693 + {
694 + reading: "Enterprise approval rules met",
695 + enterprise: true,
696 + approval: { approved: true, approvedBy: approvedBy("julius") },
697 + decision: "approved",
698 + },
699 + {
700 + reading: "Enterprise without approval rules or approvals",
701 + enterprise: true,
702 + approval: { approved: true, approvedBy: approvedBy() },
703 + decision: null,
704 + },
705 + {
706 + reading: "Community Edition with an approval",
707 + enterprise: false,
708 + approval: { approved: true, approvedBy: approvedBy("julius") },
709 + decision: "approved",
710 + },
711 + {
712 + reading: "Community Edition without approvals",
713 + enterprise: false,
714 + approval: { approved: false, approvedBy: approvedBy() },
715 + decision: null,
716 + },
717 + ])("reads $reading as $decision", ({ enterprise, approval, decision }) => {
718 + expect([...decisionsOf(enterprise, [{ iid: "7", ...approval }])]).toEqual([[7, decision]]);
719 + });
720 +
721 + it("leaves out a merge request it cannot key, and a project the reader cannot see", () => {
722 + expect([...decisionsOf(true, [null, { iid: "x", approved: true }])]).toEqual([]);
723 + expect([
724 + ...decisionsOf(null, [{ iid: "7", approved: false, approvedBy: approvedBy() }]),
725 + ]).toEqual([]);
726 + expect([
727 + ...decisionsOf(true, [
728 + { iid: "7", approved: null, approvedBy: approvedBy("julius") },
729 + { iid: "8", approved: true, approvedBy: null },
730 + ]),
731 + ]).toEqual([]);
732 + expect(
733 + expectSuccess(decodeReviewDecisionsJson(JSON.stringify({ data: { project: null } }))),
734 + ).toEqual(new Map());
735 + });
736 +});
737 +
738 describe("decodeMergeRequestStatsJson", () => {
739 it("keys the counts by merge request number", () => {
740 const stats = expectSuccess(
packages/source-control-gitlab/src/server/gitLabMergeRequestJson.ts
+82
@@ -15,6 +15,7 @@ import type {
15 PullRequestMergeMethod,
16 PullRequestReaction,
17 PullRequestReactionContent,
18 + PullRequestReviewDecision,
19 PullRequestReviewThread,
20 PullRequestReviewerCandidate,
21 PullRequestState,
@@ -996,6 +997,87 @@ export function decodeRepositoryBlobsJson(
997 return Result.succeed(blobs);
998 }
999
1000 +export const REVIEW_DECISIONS_GRAPHQL_QUERY = `query($fullPath: ID!, $iids: [String!]) {
1001 + metadata { enterprise }
1002 + project(fullPath: $fullPath) {
1003 + mergeRequests(iids: $iids, first: 100) {
1004 + nodes { iid approved approvedBy(first: 1) { nodes { username } } }
1005 + }
1006 + }
1007 +}`;
1008 +
1009 +const RawReviewDecisionsSchema = Schema.Struct({
1010 + data: Schema.Struct({
1011 + metadata: Schema.optional(
1012 + Schema.NullOr(Schema.Struct({ enterprise: Schema.optional(Schema.NullOr(Schema.Boolean)) })),
1013 + ),
1014 + project: Schema.NullOr(
1015 + Schema.Struct({
1016 + mergeRequests: Schema.optional(
1017 + Schema.NullOr(
1018 + Schema.Struct({
1019 + nodes: Schema.Array(
1020 + Schema.NullOr(
1021 + Schema.Struct({
1022 + iid: Schema.String,
1023 + approved: Schema.optional(Schema.NullOr(Schema.Boolean)),
1024 + approvedBy: Schema.optional(
1025 + Schema.NullOr(
1026 + Schema.Struct({
1027 + nodes: Schema.optional(Schema.NullOr(Schema.Array(Schema.Unknown))),
1028 + }),
1029 + ),
1030 + ),
1031 + }),
1032 + ),
1033 + ),
1034 + }),
1035 + ),
1036 + ),
1037 + }),
1038 + ),
1039 + }),
1040 +});
1041 +
1042 +const decodeReviewDecisions = decodeJsonResult(RawReviewDecisionsSchema);
1043 +
1044 +/**
1045 + * Only Enterprise Edition holds `approved` to the project's approval rules, so an unapproved
1046 + * merge request there still needs review even when someone, such as a bot, approved it.
1047 + * Community Edition reports `approved` once anyone approves and requires no review.
1048 + */
1049 +export function decodeReviewDecisionsJson(
1050 + raw: string,
1051 +): Result.Result<ReadonlyMap<number, PullRequestReviewDecision | null>, DecodeFailure> {
1052 + const decoded = decodeReviewDecisions(raw);
1053 + if (!Result.isSuccess(decoded)) {
1054 + return Result.fail(decoded.failure);
1055 + }
1056 + const enterprise = decoded.success.data.metadata?.enterprise;
1057 + const decisions = new Map<number, PullRequestReviewDecision | null>();
1058 + for (const node of decoded.success.data.project?.mergeRequests?.nodes ?? []) {
1059 + const number = Number(node?.iid);
1060 + if (
1061 + node === null ||
1062 + !Number.isSafeInteger(number) ||
1063 + node.approved == null ||
1064 + node.approvedBy?.nodes == null
1065 + )
1066 + continue;
1067 + const approvals = node.approvedBy.nodes.length;
1068 + if (node.approved === false && approvals === 0 && enterprise == null) continue;
1069 + decisions.set(
1070 + number,
1071 + node.approved === false && (enterprise || approvals > 0)
1072 + ? "review-required"
1073 + : approvals > 0
1074 + ? "approved"
1075 + : null,
1076 + );
1077 + }
1078 + return Result.succeed(decisions);
1079 +}
1080 +
1081 /**
1082 * Line counts for merge requests of one project. The REST listing carries no line counts, and
1083 * reading each diff to count them would cost a request per row, so GraphQL's per-merge-request