Skip to content

Commit 40decbc

Browse files
oss96claude
andcommitted
fix(gitlab): merge requests show their approval state
GitLab rows never carried a review decision, so the pull request list showed no review badge for merge requests and its Review filter could not find an approved one. Read approvals for listed and summarized merge requests with one GraphQL request per page, using fields both GitLab editions offer. Enterprise Edition's `approved` follows approval rules; Community Edition has none, so an approval there reads as approved and nothing reads as required. A failed read leaves the decision unknown, as before. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f855f01 commit 40decbc

5 files changed

Lines changed: 330 additions & 2 deletions

File tree

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type {
1919
PullRequestReaction,
2020
PullRequestReactionContent,
2121
PullRequestReviewCommentDraft,
22+
PullRequestReviewDecision,
2223
PullRequestReviewPosition,
2324
PullRequestReviewThread,
2425
PullRequestReviewVerdict,
@@ -43,9 +44,11 @@ import {
4344
decodeProjectLabelsJson,
4445
decodeProjectUsersJson,
4546
decodeRepositoryBlobsJson,
47+
decodeReviewDecisionsJson,
4648
decodeViewerJson,
4749
gitLabAwardName,
4850
REPOSITORY_BLOBS_GRAPHQL_QUERY,
51+
REVIEW_DECISIONS_GRAPHQL_QUERY,
4952
type GitLabDiffRefs,
5053
type GitLabMergeRequestDetail,
5154
type GitLabMergeRequestListItem,
@@ -237,6 +240,15 @@ export class GitLabPullRequestCli extends Context.Service<
237240
readonly number: number;
238241
}) => Effect.Effect<GitLabMergeRequestDetail, GitLabPullRequestCliError>;
239242

243+
readonly getReviewDecisions: (input: {
244+
readonly cwd: string;
245+
readonly repository: string;
246+
readonly numbers: ReadonlyArray<number>;
247+
}) => Effect.Effect<
248+
ReadonlyMap<number, PullRequestReviewDecision | null>,
249+
GitLabPullRequestCliError
250+
>;
251+
240252
readonly listNotes: (input: {
241253
readonly cwd: string;
242254
readonly repository: string;
@@ -1094,6 +1106,38 @@ export const make = Effect.gen(function* () {
10941106
}),
10951107
);
10961108

1109+
const reviewDecisionsOf = (input: {
1110+
readonly cwd: string;
1111+
readonly repository: string;
1112+
readonly numbers: ReadonlyArray<number>;
1113+
}): Effect.Effect<
1114+
ReadonlyMap<number, PullRequestReviewDecision | null>,
1115+
GitLabPullRequestCliError
1116+
> =>
1117+
api({
1118+
cwd: input.cwd,
1119+
path: "graphql",
1120+
method: "POST",
1121+
stdin: JSON.stringify({
1122+
query: REVIEW_DECISIONS_GRAPHQL_QUERY,
1123+
variables: { fullPath: input.repository, iids: input.numbers.map(String) },
1124+
}),
1125+
}).pipe(
1126+
Effect.flatMap((result) => {
1127+
const decoded = decodeReviewDecisionsJson(result.stdout.trim());
1128+
return Result.isSuccess(decoded)
1129+
? Effect.succeed(decoded.success)
1130+
: Effect.fail(
1131+
new GitLabMergeRequestReadError({
1132+
command: "glab",
1133+
cwd: input.cwd,
1134+
operation: "getReviewDecisions",
1135+
cause: decoded.failure,
1136+
}),
1137+
);
1138+
}),
1139+
);
1140+
10971141
const viewerUsername = (input: { readonly cwd: string }) =>
10981142
api({ cwd: input.cwd, path: "user" }).pipe(
10991143
Effect.flatMap((result): Effect.Effect<string, GitLabPullRequestCliError> => {
@@ -1125,6 +1169,16 @@ export const make = Effect.gen(function* () {
11251169

11261170
getMergeRequestDetail: mergeRequestDetail,
11271171

1172+
getReviewDecisions: Effect.fn("GitLabPullRequestCli.getReviewDecisions")(function (input) {
1173+
const batches: Array<ReadonlyArray<number>> = [];
1174+
for (let at = 0; at < input.numbers.length; at += MAX_PAGE_SIZE) {
1175+
batches.push(input.numbers.slice(at, at + MAX_PAGE_SIZE));
1176+
}
1177+
return Effect.forEach(batches, (numbers) => reviewDecisionsOf({ ...input, numbers }), {
1178+
concurrency: 2,
1179+
}).pipe(Effect.map((pages) => new Map(pages.flatMap((page) => [...page]))));
1180+
}),
1181+
11281182
listNotes: Effect.fn("GitLabPullRequestCli.listNotes")(function (input) {
11291183
return notesPage({ ...input, page: 1, collected: [] });
11301184
}),

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

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ describe("getChangeRequest base freshness", () => {
7373
Effect.provide(
7474
Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
7575
getMergeRequestDetail: () => Effect.succeed(detail),
76+
getReviewDecisions: () => Effect.succeed(new Map()),
7677
}),
7778
),
7879
);
@@ -221,3 +222,101 @@ describe("optional activity reads", () => {
221222
}),
222223
);
223224
});
225+
226+
describe("review decisions", () => {
227+
const reference = { cwd: "/w", repository: "acme/web", host: "gitlab.com", number: 7 };
228+
const item = {
229+
number: 7,
230+
title: "Merge request 7",
231+
url: "https://gitlab.com/acme/web/-/merge_requests/7",
232+
author: null,
233+
headBranch: "feat/page",
234+
baseBranch: "main",
235+
state: "open" as const,
236+
isDraft: false,
237+
mergeability: "mergeable" as const,
238+
additions: 0,
239+
deletions: 0,
240+
createdAt: "2026-07-01T00:00:00Z",
241+
updatedAt: "2026-07-02T00:00:00Z",
242+
reviewRequestLogins: [],
243+
labels: [],
244+
};
245+
const detail = {
246+
...item,
247+
body: "",
248+
changedFiles: 1,
249+
mergedAt: null,
250+
closedAt: null,
251+
reviewers: [],
252+
checks: [],
253+
viewerCanMerge: true,
254+
reviewerIds: [],
255+
};
256+
const providerWith = (
257+
getReviewDecisions: GitLabPullRequestCli.GitLabPullRequestCli["Service"]["getReviewDecisions"],
258+
) =>
259+
make.pipe(
260+
Effect.provide(
261+
Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
262+
listMergeRequests: () =>
263+
Effect.succeed({
264+
items: [item, { ...item, number: 8 }],
265+
truncated: false,
266+
cursorAdvance: 2,
267+
}),
268+
getMergeRequestDetail: () => Effect.succeed(detail),
269+
getReviewDecisions,
270+
}),
271+
),
272+
);
273+
const listing = {
274+
cwd: "/w",
275+
repository: "acme/web",
276+
host: "gitlab.com",
277+
state: "open" as const,
278+
involvement: "all" as const,
279+
viewer: "julius",
280+
limit: 2,
281+
};
282+
283+
it.effect("puts GitLab's approval state on listed and summarized merge requests", () =>
284+
Effect.gen(function* () {
285+
const provider = yield* providerWith(() =>
286+
Effect.succeed(
287+
new Map([
288+
[7, "approved" as const],
289+
[8, null],
290+
]),
291+
),
292+
);
293+
const listed = yield* provider.listChangeRequests(listing);
294+
assert.isDefined(provider.getChangeRequestSummary);
295+
const summary = yield* provider.getChangeRequestSummary(reference);
296+
297+
expect(listed.items.map(({ reviewDecision }) => reviewDecision)).toEqual(["approved", null]);
298+
expect(summary.reviewDecision).toBe("approved");
299+
}),
300+
);
301+
302+
it.effect("lists merge requests with an unknown decision when GitLab will not say", () =>
303+
Effect.gen(function* () {
304+
const provider = yield* providerWith(() =>
305+
Effect.fail(
306+
new GitLabPullRequestCli.GitLabMergeRequestReadError({
307+
command: "glab",
308+
cwd: "/w",
309+
operation: "getReviewDecisions",
310+
cause: null,
311+
}),
312+
),
313+
);
314+
const listed = yield* provider.listChangeRequests(listing);
315+
316+
expect(listed.items.map(({ reviewDecision }) => reviewDecision)).toEqual([
317+
undefined,
318+
undefined,
319+
]);
320+
}),
321+
);
322+
});

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

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as Effect from "effect/Effect";
33
import type {
44
PullRequestCapabilities,
55
PullRequestReaction,
6+
PullRequestReviewDecision,
67
PullRequestViewerPermissions,
78
} from "@t3tools/contracts";
89

@@ -111,6 +112,9 @@ export const make = Effect.gen(function* () {
111112
}),
112113
);
113114

115+
const reviewDecisions = (input: Parameters<typeof cli.getReviewDecisions>[0]) =>
116+
recoverRead(cli.getReviewDecisions(input), new Map<number, PullRequestReviewDecision | null>());
117+
114118
const provider: PullRequestProviderApi = {
115119
kind: SourceControlProviderKind.make("gitlab"),
116120
capabilities: CAPABILITIES,
@@ -133,8 +137,20 @@ export const make = Effect.gen(function* () {
133137
filters: input.filters,
134138
})
135139
.pipe(
140+
Effect.flatMap((batch) =>
141+
reviewDecisions({
142+
cwd: input.cwd,
143+
repository: input.repository,
144+
numbers: batch.items.map((item) => item.number),
145+
}).pipe(
146+
Effect.map((decisions) => ({
147+
...batch,
148+
items: batch.items.map((item) => withReviewDecision(item, decisions)),
149+
continues: true,
150+
})),
151+
),
152+
),
136153
Effect.mapError(fail("listChangeRequests")),
137-
Effect.map((batch) => ({ ...batch, continues: true })),
138154
),
139155

140156
getChangeRequestChecks: (input) =>
@@ -148,7 +164,20 @@ export const make = Effect.gen(function* () {
148164
cli.getMergeRequestDetail(input).pipe(Effect.mapError(fail("getChangeRequestPreview"))),
149165

150166
getChangeRequestSummary: (input) =>
151-
cli.getMergeRequestDetail(input).pipe(Effect.mapError(fail("getChangeRequestSummary"))),
167+
Effect.all(
168+
[
169+
cli.getMergeRequestDetail(input),
170+
reviewDecisions({
171+
cwd: input.cwd,
172+
repository: input.repository,
173+
numbers: [input.number],
174+
}),
175+
],
176+
{ concurrency: 2 },
177+
).pipe(
178+
Effect.map(([mergeRequest, decisions]) => withReviewDecision(mergeRequest, decisions)),
179+
Effect.mapError(fail("getChangeRequestSummary")),
180+
),
152181

153182
getChangeRequest: (input) =>
154183
Effect.all(
@@ -330,3 +359,11 @@ export const make = Effect.gen(function* () {
330359

331360
return provider;
332361
});
362+
363+
function withReviewDecision<Item extends { readonly number: number }>(
364+
item: Item,
365+
decisions: ReadonlyMap<number, PullRequestReviewDecision | null>,
366+
): Item & { readonly reviewDecision?: PullRequestReviewDecision | null } {
367+
const reviewDecision = decisions.get(item.number);
368+
return reviewDecision === undefined ? item : { ...item, reviewDecision };
369+
}

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
decodeMergeRequestListJson,
1111
decodeNotesJson,
1212
decodeOwnAwardIdJson,
13+
decodeReviewDecisionsJson,
1314
} from "./gitLabMergeRequestJson.ts";
1415

1516
function listJson(entries: ReadonlyArray<Record<string, unknown>>): string {
@@ -620,3 +621,65 @@ describe("decodeRepositoryBlobsJson", () => {
620621
).toEqual(new Map());
621622
});
622623
});
624+
625+
describe("decodeReviewDecisionsJson", () => {
626+
const decisionsOf = (enterprise: boolean, nodes: ReadonlyArray<unknown>) =>
627+
expectSuccess(
628+
decodeReviewDecisionsJson(
629+
JSON.stringify({
630+
data: { metadata: { enterprise }, project: { mergeRequests: { nodes } } },
631+
}),
632+
),
633+
);
634+
const approvedBy = (...usernames: ReadonlyArray<string>) => ({
635+
nodes: usernames.map((username) => ({ username })),
636+
});
637+
638+
it.each([
639+
{
640+
reading: "an approval short of Enterprise approval rules",
641+
enterprise: true,
642+
approval: { approved: false, approvedBy: approvedBy("GitLabDuo") },
643+
decision: "review-required",
644+
},
645+
{
646+
reading: "Enterprise approval rules no one has approved against",
647+
enterprise: true,
648+
approval: { approved: false, approvedBy: approvedBy() },
649+
decision: "review-required",
650+
},
651+
{
652+
reading: "Enterprise approval rules met",
653+
enterprise: true,
654+
approval: { approved: true, approvedBy: approvedBy("julius") },
655+
decision: "approved",
656+
},
657+
{
658+
reading: "Enterprise without approval rules or approvals",
659+
enterprise: true,
660+
approval: { approved: true, approvedBy: approvedBy() },
661+
decision: null,
662+
},
663+
{
664+
reading: "Community Edition with an approval",
665+
enterprise: false,
666+
approval: { approved: true, approvedBy: approvedBy("julius") },
667+
decision: "approved",
668+
},
669+
{
670+
reading: "Community Edition without approvals",
671+
enterprise: false,
672+
approval: { approved: false, approvedBy: approvedBy() },
673+
decision: null,
674+
},
675+
])("reads $reading as $decision", ({ enterprise, approval, decision }) => {
676+
expect([...decisionsOf(enterprise, [{ iid: "7", ...approval }])]).toEqual([[7, decision]]);
677+
});
678+
679+
it("leaves out a merge request it cannot key, and a project the reader cannot see", () => {
680+
expect([...decisionsOf(true, [null, { iid: "x", approved: true }])]).toEqual([]);
681+
expect(
682+
expectSuccess(decodeReviewDecisionsJson(JSON.stringify({ data: { project: null } }))),
683+
).toEqual(new Map());
684+
});
685+
});

0 commit comments

Comments
 (0)