Skip to content

Commit 199601a

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 67c0fc0 commit 199601a

7 files changed

Lines changed: 468 additions & 8 deletions

File tree

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,7 @@ export function usePullRequestList(targets: readonly ListTarget[]) {
202202
const statsKey = JSON.stringify(
203203
targets.flatMap(({ environmentId }) => {
204204
const refs = visibleRows
205-
.filter(
206-
(row) =>
207-
row.environmentId === environmentId &&
208-
row.provider === "github" &&
209-
row.additions + row.deletions === 0,
210-
)
205+
.filter((row) => row.environmentId === environmentId && row.additions + row.deletions === 0)
211206
.map(({ projectId, host, repository, number }) => ({
212207
projectId,
213208
host,

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

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,4 +1550,119 @@ layer("GitLabPullRequestCli.layer", (it) => {
15501550
assert.strictEqual(mockedExecute.mock.calls.length, 3);
15511551
}),
15521552
);
1553+
1554+
it.effect("reads line counts per repository, a hundred merge requests at a time", () =>
1555+
Effect.gen(function* () {
1556+
mockedExecute.mockImplementation((request) => {
1557+
const body = JSON.parse(request.stdin ?? "{}") as {
1558+
readonly variables: { readonly fullPath: string; readonly iids: ReadonlyArray<string> };
1559+
};
1560+
return Effect.succeed(
1561+
output(
1562+
JSON.stringify({
1563+
data: {
1564+
project: {
1565+
mergeRequests: {
1566+
// GitLab answers only what it has: iid 3 is left out on purpose.
1567+
nodes: body.variables.iids
1568+
.filter((iid) => iid !== "3")
1569+
.map((iid) => ({
1570+
iid,
1571+
diffStatsSummary: { additions: Number(iid), deletions: 1 },
1572+
})),
1573+
},
1574+
},
1575+
},
1576+
}),
1577+
),
1578+
);
1579+
});
1580+
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1581+
1582+
const stats = yield* cli.listMergeRequestStats({
1583+
cwd: "/w",
1584+
changeRequests: [
1585+
...Array.from({ length: 150 }, (_, index) => ({
1586+
repository: "acme/web",
1587+
number: index + 1,
1588+
})),
1589+
{ repository: "acme/api", number: 9 },
1590+
],
1591+
});
1592+
1593+
expect(stats).toHaveLength(150);
1594+
expect(stats).toContainEqual({
1595+
repository: "acme/web",
1596+
number: 150,
1597+
additions: 150,
1598+
deletions: 1,
1599+
});
1600+
expect(stats).toContainEqual({
1601+
repository: "acme/api",
1602+
number: 9,
1603+
additions: 9,
1604+
deletions: 1,
1605+
});
1606+
expect(stats.some((stat) => stat.number === 3)).toBe(false);
1607+
// Two reads for the hundred and fifty in one project, one for the other project.
1608+
const reads = mockedExecute.mock.calls.map(([request]) => {
1609+
const body = JSON.parse(request.stdin ?? "{}") as {
1610+
readonly variables: { readonly fullPath: string; readonly iids: ReadonlyArray<string> };
1611+
};
1612+
return [body.variables.fullPath, body.variables.iids.length];
1613+
});
1614+
expect(reads).toEqual([
1615+
["acme/web", 100],
1616+
["acme/web", 50],
1617+
["acme/api", 1],
1618+
]);
1619+
}),
1620+
);
1621+
1622+
it.effect("keeps the counts of reads that answered when another read fails", () =>
1623+
Effect.gen(function* () {
1624+
mockedExecute.mockImplementation((request) => {
1625+
const body = JSON.parse(request.stdin ?? "{}") as {
1626+
readonly variables: { readonly fullPath: string };
1627+
};
1628+
return Effect.succeed(
1629+
output(
1630+
body.variables.fullPath === "acme/api"
1631+
? JSON.stringify({ errors: [{ message: "Query timed out" }] })
1632+
: JSON.stringify({
1633+
data: {
1634+
project: {
1635+
mergeRequests: {
1636+
nodes: [{ iid: "7", diffStatsSummary: { additions: 5, deletions: 2 } }],
1637+
},
1638+
},
1639+
},
1640+
}),
1641+
),
1642+
);
1643+
});
1644+
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1645+
1646+
const stats = yield* cli.listMergeRequestStats({
1647+
cwd: "/w",
1648+
changeRequests: [
1649+
{ repository: "acme/web", number: 7 },
1650+
{ repository: "acme/api", number: 9 },
1651+
],
1652+
});
1653+
1654+
expect(stats).toEqual([{ repository: "acme/web", number: 7, additions: 5, deletions: 2 }]);
1655+
}),
1656+
);
1657+
1658+
it.effect("asks GitLab nothing for no merge requests", () =>
1659+
Effect.gen(function* () {
1660+
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1661+
1662+
const stats = yield* cli.listMergeRequestStats({ cwd: "/w", changeRequests: [] });
1663+
1664+
expect(stats).toEqual([]);
1665+
expect(mockedExecute).not.toHaveBeenCalled();
1666+
}),
1667+
);
15531668
});

‎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,
@@ -223,6 +225,23 @@ export class GitLabPullRequestCli extends Context.Service<
223225
readonly number: number;
224226
}) => Effect.Effect<GitLabMergeRequestDetail, GitLabPullRequestCliError>;
225227

228+
/** Line counts for the given merge requests; one with no answer is left out. */
229+
readonly listMergeRequestStats: (input: {
230+
readonly cwd: string;
231+
readonly changeRequests: ReadonlyArray<{
232+
readonly repository: string;
233+
readonly number: number;
234+
}>;
235+
}) => Effect.Effect<
236+
ReadonlyArray<{
237+
readonly repository: string;
238+
readonly number: number;
239+
readonly additions: number;
240+
readonly deletions: number;
241+
}>,
242+
GitLabPullRequestCliError
243+
>;
244+
226245
readonly listNotes: (input: {
227246
readonly cwd: string;
228247
readonly repository: string;
@@ -964,6 +983,85 @@ export const make = Effect.gen(function* () {
964983
}),
965984
);
966985

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

10751173
getMergeRequestDetail: mergeRequestDetail,
10761174

1175+
listMergeRequestStats,
1176+
10771177
listNotes: (input) => notesPage({ ...input, page: 1, collected: [] }),
10781178

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

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

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ describe("getChangeRequest base freshness", () => {
9595
}),
9696
);
9797

98-
const readWith = (divergence: { readonly divergedCommits?: number }) =>
98+
const readWith = (
99+
divergence: { readonly divergedCommits?: number },
100+
stats: GitLabPullRequestCli.GitLabPullRequestCli["Service"]["listMergeRequestStats"] = () =>
101+
Effect.succeed([]),
102+
) =>
99103
Effect.gen(function* () {
100104
const provider = yield* make;
101105
return yield* provider.getChangeRequest({
@@ -110,6 +114,7 @@ describe("getChangeRequest base freshness", () => {
110114
getMergeRequestDetail: () => Effect.succeed({ ...detail, ...divergence }),
111115
getProjectMergeCapabilities: () =>
112116
Effect.succeed({ merge: true, squash: true, rebase: true }),
117+
listMergeRequestStats: stats,
113118
}),
114119
),
115120
);
@@ -140,6 +145,84 @@ describe("getChangeRequest base freshness", () => {
140145
expect(changeRequest.behindBy).toBeUndefined();
141146
}),
142147
);
148+
149+
it.effect("fills in the line counts the REST detail leaves out", () =>
150+
Effect.gen(function* () {
151+
const changeRequest = yield* readWith({}, () =>
152+
Effect.succeed([{ repository: "acme/web", number: 7, additions: 40, deletions: 2 }]),
153+
);
154+
155+
expect(changeRequest.additions).toBe(40);
156+
expect(changeRequest.deletions).toBe(2);
157+
}),
158+
);
159+
160+
it.effect("keeps the detail when the line counts cannot be read", () =>
161+
Effect.gen(function* () {
162+
const changeRequest = yield* readWith({}, () =>
163+
Effect.fail(
164+
new GitLabPullRequestCli.GitLabMergeRequestReadError({
165+
command: "glab",
166+
cwd: "/w",
167+
operation: "listMergeRequestStats",
168+
cause: new Error("unknown field"),
169+
}),
170+
),
171+
);
172+
173+
expect(changeRequest.number).toBe(7);
174+
expect(changeRequest.additions).toBe(0);
175+
}),
176+
);
177+
178+
it.effect("reports an unauthenticated line count read instead of a detail without counts", () =>
179+
Effect.gen(function* () {
180+
const result = yield* Effect.result(
181+
readWith({}, () =>
182+
Effect.fail(
183+
new GitLabCli.GitLabCliAuthenticationError({
184+
operation: "execute",
185+
command: "glab",
186+
cwd: "/w",
187+
cause: null,
188+
}),
189+
),
190+
),
191+
);
192+
193+
expect(result).toMatchObject({
194+
_tag: "Failure",
195+
failure: { _tag: "PullRequestProviderError", reason: "unauthenticated" },
196+
});
197+
}),
198+
);
199+
200+
it.effect("gives linked threads line counts without reading the merge settings", () =>
201+
Effect.gen(function* () {
202+
const getProjectMergeCapabilities = vi.fn(() =>
203+
Effect.succeed({ merge: true, squash: true, rebase: true }),
204+
);
205+
const summary = yield* Effect.gen(function* () {
206+
const provider = yield* make;
207+
const read = provider.getChangeRequestSummary;
208+
if (read === undefined) return yield* Effect.die("summary read missing");
209+
return yield* read({ cwd: "/w", repository: "acme/web", host: "gitlab.com", number: 7 });
210+
}).pipe(
211+
Effect.provide(
212+
Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
213+
getMergeRequestDetail: () => Effect.succeed(detail),
214+
getProjectMergeCapabilities,
215+
listMergeRequestStats: () =>
216+
Effect.succeed([{ repository: "acme/web", number: 7, additions: 40, deletions: 2 }]),
217+
}),
218+
),
219+
);
220+
221+
expect(summary.additions).toBe(40);
222+
expect(summary.deletions).toBe(2);
223+
expect(getProjectMergeCapabilities).not.toHaveBeenCalled();
224+
}),
225+
);
143226
});
144227

145228
describe("rewriting what has already been said", () => {

0 commit comments

Comments
 (0)