Skip to content

Commit 7039d90

Browse files
oss96claudemaria-rcks
authored
fix(gitlab): merge requests show their approval state (#26877)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 0da2bb6 commit 7039d90

6 files changed

Lines changed: 420 additions & 3 deletions

File tree

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,58 @@ afterEach(() => {
9696
});
9797

9898
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+
99151
it.effect("asks GitLab for one row more than the page, to probe for a next page", () =>
100152
Effect.gen(function* () {
101153
mockedExecute.mockReturnValueOnce(Effect.succeed(output(mergeRequests(3, 1))));

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import type {
2020
PullRequestReaction,
2121
PullRequestReactionContent,
2222
PullRequestReviewCommentDraft,
23+
PullRequestReviewDecision,
2324
PullRequestReviewPosition,
2425
PullRequestReviewThread,
2526
PullRequestReviewVerdict,
@@ -46,12 +47,14 @@ import {
4647
decodeProjectLabelsJson,
4748
decodeProjectUsersJson,
4849
decodeRepositoryBlobsJson,
50+
decodeReviewDecisionsJson,
4951
decodeTriggerJobChecksJson,
5052
decodeViewerJson,
5153
gitLabAwardName,
5254
MERGE_REQUEST_FILE_STATS_GRAPHQL_QUERY,
5355
MERGE_REQUEST_STATS_GRAPHQL_QUERY,
5456
REPOSITORY_BLOBS_GRAPHQL_QUERY,
57+
REVIEW_DECISIONS_GRAPHQL_QUERY,
5558
type GitLabDiffRefs,
5659
type GitLabMergeRequestDetail,
5760
type GitLabMergeRequestListItem,
@@ -251,6 +254,16 @@ export class GitLabPullRequestCli extends Context.Service<
251254
readonly downstreamChecks?: boolean;
252255
}) => Effect.Effect<GitLabMergeRequestDetail, GitLabPullRequestCliError>;
253256

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+
254267
/** Line counts for the given merge requests; one with no answer is left out. */
255268
readonly listMergeRequestStats: (input: {
256269
readonly cwd: string;
@@ -1295,6 +1308,24 @@ export const make = Effect.gen(function* () {
12951308
}),
12961309
);
12971310

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+
12981329
const viewerUsername = (input: { readonly cwd: string; readonly host?: string | undefined }) =>
12991330
api({ cwd: input.cwd, host: input.host, path: "user" }).pipe(
13001331
Effect.flatMap((result): Effect.Effect<string, GitLabPullRequestCliError> => {
@@ -1338,6 +1369,16 @@ export const make = Effect.gen(function* () {
13381369
},
13391370
),
13401371

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+
13411382
listMergeRequestStats,
13421383

13431384
getMergeRequestFileStats: Effect.fn("GitLabPullRequestCli.getMergeRequestFileStats")((input) =>

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

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ it.effect.each(["merge", "disable-auto-merge"] as const)(
112112
yield* provider.searchRepositoryAuthors(reference);
113113
yield* provider.runAction({ ...reference, action });
114114

115-
expect(readHosts).toEqual(Array(action === "merge" ? 11 : 10).fill(selectedHost));
115+
expect(readHosts).toEqual(Array(action === "merge" ? 12 : 11).fill(selectedHost));
116116
expect({ url: detail.url, mutatedHosts: mergedHosts }).toEqual({
117117
url: `https://${selectedHost}/acme/web/-/merge_requests/7`,
118118
mutatedHosts: [selectedHost],
@@ -187,6 +187,8 @@ describe("getChangeRequest base freshness", () => {
187187
Effect.provide(
188188
Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
189189
getMergeRequestDetail: () => Effect.succeed(detail),
190+
getReviewDecisions: () => Effect.succeed(new Map()),
191+
190192
listMergeRequestStats: () => Effect.succeed([]),
191193
}),
192194
),
@@ -286,6 +288,7 @@ describe("getChangeRequest base freshness", () => {
286288
Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
287289
getMergeRequestDetail: () => Effect.succeed(detail),
288290
getProjectMergeCapabilities: () => Effect.die("linked threads skip merge settings"),
291+
getReviewDecisions: () => Effect.succeed(new Map()),
289292
listMergeRequestStats: () =>
290293
Effect.succeed([{ repository: "acme/web", number: 7, additions: 40, deletions: 2 }]),
291294
}),
@@ -333,6 +336,7 @@ describe("merge request reads", () => {
333336
Effect.provide(
334337
Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
335338
listMergeRequestStats: () => Effect.succeed([]),
339+
getReviewDecisions: () => Effect.succeed(new Map()),
336340
getMergeRequestDetail: (input) =>
337341
Effect.sync(() => {
338342
calls.detail += 1;
@@ -558,3 +562,119 @@ describe("optional activity reads", () => {
558562
}),
559563
);
560564
});
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‎

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as Exit from "effect/Exit";
66
import type {
77
PullRequestCapabilities,
88
PullRequestReaction,
9+
PullRequestReviewDecision,
910
PullRequestViewerPermissions,
1011
} from "@t3tools/contracts";
1112

@@ -180,6 +181,17 @@ export const make = Effect.gen(function* () {
180181
}),
181182
);
182183

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+
183195
// REST omits line counts; the CLI recovers optional stats failures per batch.
184196
const mergeRequestWithStats = (
185197
input: {
@@ -228,8 +240,21 @@ export const make = Effect.gen(function* () {
228240
filters: input.filters,
229241
})
230242
.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+
),
231257
Effect.mapError(fail("listChangeRequests")),
232-
Effect.map((batch) => ({ ...batch, continues: true })),
233258
),
234259

235260
getFileStats: (input) =>
@@ -249,7 +274,21 @@ export const make = Effect.gen(function* () {
249274
cli.getMergeRequestDetail(input).pipe(Effect.mapError(fail("getChangeRequestPreview"))),
250275

251276
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+
),
253292

254293
getChangeRequest: (input) =>
255294
Effect.all(
@@ -483,3 +522,11 @@ export const make = Effect.gen(function* () {
483522

484523
return provider;
485524
});
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+
}

0 commit comments

Comments
 (0)