Skip to content

Commit 0aaa11d

Browse files
TonybynMp4claude
andcommitted
fix(gitlab): merge requests show their line counts
GitLab's REST responses carry no line counts, so merge request rows and the detail page showed none. Read them from GraphQL's diffStatsSummary, batched per project, and let mobile request them for non-GitHub rows. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 680152e commit 0aaa11d

7 files changed

Lines changed: 475 additions & 5 deletions

File tree

‎apps/mobile/src/features/pull-requests/usePullRequestList.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,17 @@ export function usePullRequestList(targets: readonly ListTarget[]) {
201201
visible.key === scopeKey && visible.rows.length ? visible.rows : data.entries.slice(0, 8);
202202
const statsKey = JSON.stringify(
203203
targets.flatMap(({ environmentId }) => {
204+
const answered = sources.find(
205+
(source) => source.target.environmentId === environmentId,
206+
)?.stats;
207+
// Zero totals mean "not counted yet" only until the host answers, so a row whose real
208+
// counts are zero is not asked for again each time the visible rows change.
204209
const refs = visibleRows
205210
.filter(
206211
(row) =>
207212
row.environmentId === environmentId &&
208-
row.provider === "github" &&
209-
row.additions + row.deletions === 0,
213+
row.additions + row.deletions === 0 &&
214+
!answered?.has(statKey(row)),
210215
)
211216
.map(({ projectId, host, repository, number }) => ({
212217
projectId,

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

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,4 +1162,119 @@ layer("GitLabPullRequestCli.layer", (it) => {
11621162
assert.strictEqual(mockedExecute.mock.calls.length, 3);
11631163
}),
11641164
);
1165+
1166+
it.effect("reads line counts per repository, a hundred merge requests at a time", () =>
1167+
Effect.gen(function* () {
1168+
mockedExecute.mockImplementation((request) => {
1169+
const body = JSON.parse(request.stdin ?? "{}") as {
1170+
readonly variables: { readonly fullPath: string; readonly iids: ReadonlyArray<string> };
1171+
};
1172+
return Effect.succeed(
1173+
output(
1174+
JSON.stringify({
1175+
data: {
1176+
project: {
1177+
mergeRequests: {
1178+
// GitLab answers only what it has: iid 3 is left out on purpose.
1179+
nodes: body.variables.iids
1180+
.filter((iid) => iid !== "3")
1181+
.map((iid) => ({
1182+
iid,
1183+
diffStatsSummary: { additions: Number(iid), deletions: 1 },
1184+
})),
1185+
},
1186+
},
1187+
},
1188+
}),
1189+
),
1190+
);
1191+
});
1192+
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1193+
1194+
const stats = yield* cli.listMergeRequestStats({
1195+
cwd: "/w",
1196+
changeRequests: [
1197+
...Array.from({ length: 150 }, (_, index) => ({
1198+
repository: "acme/web",
1199+
number: index + 1,
1200+
})),
1201+
{ repository: "acme/api", number: 9 },
1202+
],
1203+
});
1204+
1205+
expect(stats).toHaveLength(150);
1206+
expect(stats).toContainEqual({
1207+
repository: "acme/web",
1208+
number: 150,
1209+
additions: 150,
1210+
deletions: 1,
1211+
});
1212+
expect(stats).toContainEqual({
1213+
repository: "acme/api",
1214+
number: 9,
1215+
additions: 9,
1216+
deletions: 1,
1217+
});
1218+
expect(stats.some((stat) => stat.number === 3)).toBe(false);
1219+
// Two reads for the hundred and fifty in one project, one for the other project.
1220+
const reads = mockedExecute.mock.calls.map(([request]) => {
1221+
const body = JSON.parse(request.stdin ?? "{}") as {
1222+
readonly variables: { readonly fullPath: string; readonly iids: ReadonlyArray<string> };
1223+
};
1224+
return [body.variables.fullPath, body.variables.iids.length];
1225+
});
1226+
expect(reads).toEqual([
1227+
["acme/web", 100],
1228+
["acme/web", 50],
1229+
["acme/api", 1],
1230+
]);
1231+
}),
1232+
);
1233+
1234+
it.effect("keeps the counts of reads that answered when another read fails", () =>
1235+
Effect.gen(function* () {
1236+
mockedExecute.mockImplementation((request) => {
1237+
const body = JSON.parse(request.stdin ?? "{}") as {
1238+
readonly variables: { readonly fullPath: string };
1239+
};
1240+
return Effect.succeed(
1241+
output(
1242+
body.variables.fullPath === "acme/api"
1243+
? JSON.stringify({ errors: [{ message: "Query timed out" }] })
1244+
: JSON.stringify({
1245+
data: {
1246+
project: {
1247+
mergeRequests: {
1248+
nodes: [{ iid: "7", diffStatsSummary: { additions: 5, deletions: 2 } }],
1249+
},
1250+
},
1251+
},
1252+
}),
1253+
),
1254+
);
1255+
});
1256+
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1257+
1258+
const stats = yield* cli.listMergeRequestStats({
1259+
cwd: "/w",
1260+
changeRequests: [
1261+
{ repository: "acme/web", number: 7 },
1262+
{ repository: "acme/api", number: 9 },
1263+
],
1264+
});
1265+
1266+
expect(stats).toEqual([{ repository: "acme/web", number: 7, additions: 5, deletions: 2 }]);
1267+
}),
1268+
);
1269+
1270+
it.effect("asks GitLab nothing for no merge requests", () =>
1271+
Effect.gen(function* () {
1272+
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1273+
1274+
const stats = yield* cli.listMergeRequestStats({ cwd: "/w", changeRequests: [] });
1275+
1276+
expect(stats).toEqual([]);
1277+
expect(mockedExecute).not.toHaveBeenCalled();
1278+
}),
1279+
);
11651280
});

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

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ import {
3333
decodeMergeRequestDiffsJson,
3434
decodeMergeRequestChangesJson,
3535
decodeMergeRequestListJson,
36+
decodeMergeRequestStatsJson,
3637
decodeNotesJson,
3738
decodeOwnAwardIdJson,
3839
decodeProjectMergeCapabilitiesJson,
3940
decodeProjectUsersJson,
4041
decodeRepositoryBlobsJson,
4142
decodeViewerJson,
4243
gitLabAwardName,
44+
MERGE_REQUEST_STATS_GRAPHQL_QUERY,
4345
REPOSITORY_BLOBS_GRAPHQL_QUERY,
4446
type GitLabDiffRefs,
4547
type GitLabMergeRequestDetail,
@@ -231,6 +233,23 @@ export class GitLabPullRequestCli extends Context.Service<
231233
readonly number: number;
232234
}) => Effect.Effect<GitLabMergeRequestDetail, GitLabPullRequestCliError>;
233235

236+
/** Line counts for the given merge requests; one with no answer is left out. */
237+
readonly listMergeRequestStats: (input: {
238+
readonly cwd: string;
239+
readonly changeRequests: ReadonlyArray<{
240+
readonly repository: string;
241+
readonly number: number;
242+
}>;
243+
}) => Effect.Effect<
244+
ReadonlyArray<{
245+
readonly repository: string;
246+
readonly number: number;
247+
readonly additions: number;
248+
readonly deletions: number;
249+
}>,
250+
GitLabPullRequestCliError
251+
>;
252+
234253
readonly listNotes: (input: {
235254
readonly cwd: string;
236255
readonly repository: string;
@@ -972,6 +991,85 @@ export const make = Effect.gen(function* () {
972991
}),
973992
);
974993

994+
/** The stats connection hands back one page, so a repository's rows are asked for 100 at a time. */
995+
const STAT_IIDS_PER_REQUEST = 100;
996+
997+
const mergeRequestStats = (input: {
998+
readonly cwd: string;
999+
readonly repository: string;
1000+
readonly numbers: ReadonlyArray<number>;
1001+
}) =>
1002+
api({
1003+
cwd: input.cwd,
1004+
path: "graphql",
1005+
method: "POST",
1006+
stdin: JSON.stringify({
1007+
query: MERGE_REQUEST_STATS_GRAPHQL_QUERY,
1008+
variables: { fullPath: input.repository, iids: input.numbers.map(String) },
1009+
}),
1010+
}).pipe(
1011+
Effect.flatMap((result) => {
1012+
const decoded = decodeMergeRequestStatsJson(result.stdout.trim());
1013+
return Result.isSuccess(decoded)
1014+
? Effect.succeed(decoded.success)
1015+
: Effect.fail(
1016+
new GitLabMergeRequestReadError({
1017+
command: "glab",
1018+
cwd: input.cwd,
1019+
operation: "listMergeRequestStats",
1020+
cause: decoded.failure,
1021+
}),
1022+
);
1023+
}),
1024+
);
1025+
1026+
const STAT_REQUEST_CONCURRENCY = 4;
1027+
1028+
const listMergeRequestStats: GitLabPullRequestCli["Service"]["listMergeRequestStats"] = (
1029+
input,
1030+
) => {
1031+
const byRepository = new Map<string, Set<number>>();
1032+
for (const { repository, number } of input.changeRequests) {
1033+
const numbers = byRepository.get(repository) ?? new Set<number>();
1034+
numbers.add(number);
1035+
byRepository.set(repository, numbers);
1036+
}
1037+
const reads: Array<{ readonly repository: string; readonly numbers: ReadonlyArray<number> }> =
1038+
[];
1039+
for (const [repository, numberSet] of byRepository) {
1040+
const numbers = [...numberSet];
1041+
for (let start = 0; start < numbers.length; start += STAT_IIDS_PER_REQUEST) {
1042+
reads.push({ repository, numbers: numbers.slice(start, start + STAT_IIDS_PER_REQUEST) });
1043+
}
1044+
}
1045+
return Effect.forEach(
1046+
reads,
1047+
(read) =>
1048+
mergeRequestStats({ cwd: input.cwd, ...read }).pipe(
1049+
Effect.map((stats) =>
1050+
read.numbers.flatMap((number) => {
1051+
const stat = stats.get(number);
1052+
return stat === undefined ? [] : [{ repository: read.repository, number, ...stat }];
1053+
}),
1054+
),
1055+
// One read GitLab could not answer costs its own rows their counts, not every other
1056+
// read's on the same page. An unusable CLI or account still reaches the caller.
1057+
Effect.catchIf(
1058+
(cause) =>
1059+
cause._tag !== "GitLabCliUnavailableError" &&
1060+
cause._tag !== "GitLabCliAuthenticationError" &&
1061+
cause._tag !== "GitLabCliRateLimitError",
1062+
(cause) =>
1063+
Effect.logWarning("GitLab merge request line count read failed", {
1064+
repository: read.repository,
1065+
reason: cause._tag,
1066+
}).pipe(Effect.as([])),
1067+
),
1068+
),
1069+
{ concurrency: STAT_REQUEST_CONCURRENCY },
1070+
).pipe(Effect.map((results) => results.flat()));
1071+
};
1072+
9751073
/**
9761074
* GitLab charges the blobs query by how many paths it is handed, and its connection hands back
9771075
* one page. A hundred at a time keeps each request inside both.
@@ -1082,6 +1180,8 @@ export const make = Effect.gen(function* () {
10821180

10831181
getMergeRequestDetail: mergeRequestDetail,
10841182

1183+
listMergeRequestStats,
1184+
10851185
listNotes: (input) => notesPage({ ...input, page: 1, collected: [] }),
10861186

10871187
listReactions: (input) => awardsPage({ ...input, cursor: null, page: 1, collected: null }),

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

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, it } from "@effect/vitest";
1+
import { describe, expect, it, vi } from "@effect/vitest";
22
import * as Effect from "effect/Effect";
33
import * as Layer from "effect/Layer";
44

@@ -65,7 +65,11 @@ describe("getChangeRequest base freshness", () => {
6565
reviewerIds: [],
6666
};
6767

68-
const readWith = (divergence: { readonly divergedCommits?: number }) =>
68+
const readWith = (
69+
divergence: { readonly divergedCommits?: number },
70+
stats: GitLabPullRequestCli.GitLabPullRequestCli["Service"]["listMergeRequestStats"] = () =>
71+
Effect.succeed([]),
72+
) =>
6973
Effect.gen(function* () {
7074
const provider = yield* make;
7175
return yield* provider.getChangeRequest({
@@ -80,6 +84,7 @@ describe("getChangeRequest base freshness", () => {
8084
getMergeRequestDetail: () => Effect.succeed({ ...detail, ...divergence }),
8185
getProjectMergeCapabilities: () =>
8286
Effect.succeed({ merge: true, squash: true, rebase: true }),
87+
listMergeRequestStats: stats,
8388
}),
8489
),
8590
);
@@ -101,6 +106,84 @@ describe("getChangeRequest base freshness", () => {
101106
expect(changeRequest.behindBy).toBeUndefined();
102107
}),
103108
);
109+
110+
it.effect("fills in the line counts the REST detail leaves out", () =>
111+
Effect.gen(function* () {
112+
const changeRequest = yield* readWith({}, () =>
113+
Effect.succeed([{ repository: "acme/web", number: 7, additions: 40, deletions: 2 }]),
114+
);
115+
116+
expect(changeRequest.additions).toBe(40);
117+
expect(changeRequest.deletions).toBe(2);
118+
}),
119+
);
120+
121+
it.effect("keeps the detail when the line counts cannot be read", () =>
122+
Effect.gen(function* () {
123+
const changeRequest = yield* readWith({}, () =>
124+
Effect.fail(
125+
new GitLabPullRequestCli.GitLabMergeRequestReadError({
126+
command: "glab",
127+
cwd: "/w",
128+
operation: "listMergeRequestStats",
129+
cause: new Error("unknown field"),
130+
}),
131+
),
132+
);
133+
134+
expect(changeRequest.number).toBe(7);
135+
expect(changeRequest.additions).toBe(0);
136+
}),
137+
);
138+
139+
it.effect("reports an unauthenticated line count read instead of a detail without counts", () =>
140+
Effect.gen(function* () {
141+
const result = yield* Effect.result(
142+
readWith({}, () =>
143+
Effect.fail(
144+
new GitLabCli.GitLabCliAuthenticationError({
145+
operation: "execute",
146+
command: "glab",
147+
cwd: "/w",
148+
cause: null,
149+
}),
150+
),
151+
),
152+
);
153+
154+
expect(result).toMatchObject({
155+
_tag: "Failure",
156+
failure: { _tag: "PullRequestProviderError", reason: "unauthenticated" },
157+
});
158+
}),
159+
);
160+
161+
it.effect("gives linked threads line counts without reading the merge settings", () =>
162+
Effect.gen(function* () {
163+
const getProjectMergeCapabilities = vi.fn(() =>
164+
Effect.succeed({ merge: true, squash: true, rebase: true }),
165+
);
166+
const summary = yield* Effect.gen(function* () {
167+
const provider = yield* make;
168+
const read = provider.getChangeRequestSummary;
169+
if (read === undefined) return yield* Effect.die("summary read missing");
170+
return yield* read({ cwd: "/w", repository: "acme/web", host: "gitlab.com", number: 7 });
171+
}).pipe(
172+
Effect.provide(
173+
Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
174+
getMergeRequestDetail: () => Effect.succeed(detail),
175+
getProjectMergeCapabilities,
176+
listMergeRequestStats: () =>
177+
Effect.succeed([{ repository: "acme/web", number: 7, additions: 40, deletions: 2 }]),
178+
}),
179+
),
180+
);
181+
182+
expect(summary.additions).toBe(40);
183+
expect(summary.deletions).toBe(2);
184+
expect(getProjectMergeCapabilities).not.toHaveBeenCalled();
185+
}),
186+
);
104187
});
105188

106189
describe("optional activity reads", () => {

0 commit comments

Comments
 (0)