@devries-cameron20 / t3code-1 / commits / 444df61850
Fork 0

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

fix(gitlab): merge requests show their line counts (#22374)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: maria-rcks <maria@kuuro.net>

Antony committed Oct 30, 2026 at 21:40 UTC 444df6185081c577266e66a01e9b5762d15e8453
6 files changed +493 -4
packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts
+88
@@ -1184,4 +1184,92 @@ layer("GitLabPullRequestCli.layer", (it) => {
1184 assert.strictEqual(mockedExecute.mock.calls.length, 3);
1185 }),
1186 );
1187 +
1188 + it.effect(
1189 + "reads line counts a hundred at a time per project, keeping the reads that answered",
1190 + () =>
1191 + Effect.gen(function* () {
1192 + const variablesOf = (stdin: string | undefined) =>
1193 + (
1194 + JSON.parse(stdin ?? "{}") as {
1195 + readonly variables: {
1196 + readonly fullPath: string;
1197 + readonly iids: ReadonlyArray<string>;
1198 + };
1199 + }
1200 + ).variables;
1201 + mockedExecute.mockImplementation((request) => {
1202 + const { fullPath, iids } = variablesOf(request.stdin);
1203 + // GitLab answers only what it has (iid 3 is missing), and one project's read fails.
1204 + const nodes = iids
1205 + .filter((iid) => iid !== "3")
1206 + .map((iid) => ({ iid, diffStatsSummary: { additions: Number(iid), deletions: 1 } }));
1207 + return Effect.succeed(
1208 + output(
1209 + JSON.stringify(
1210 + fullPath === "acme/api"
1211 + ? { errors: [{ message: "Query timed out" }] }
1212 + : { data: { project: { mergeRequests: { nodes } } } },
1213 + ),
1214 + ),
1215 + );
1216 + });
1217 + const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1218 +
1219 + const stats = yield* cli.listMergeRequestStats({
1220 + cwd: "/w",
1221 + changeRequests: [
1222 + ...Array.from({ length: 150 }, (_, index) => ({
1223 + repository: "acme/web",
1224 + number: index + 1,
1225 + })),
1226 + { repository: "acme/api", number: 9 },
1227 + ],
1228 + });
1229 +
1230 + expect(stats).toHaveLength(149);
1231 + expect(stats).toContainEqual({
1232 + repository: "acme/web",
1233 + number: 150,
1234 + additions: 150,
1235 + deletions: 1,
1236 + });
1237 + expect(stats.some((stat) => stat.number === 3 || stat.repository === "acme/api")).toBe(
1238 + false,
1239 + );
1240 + expect(
1241 + mockedExecute.mock.calls.map(([request]) => {
1242 + const { fullPath, iids } = variablesOf(request.stdin);
1243 + return [fullPath, iids.length];
1244 + }),
1245 + ).toEqual([
1246 + ["acme/web", 100],
1247 + ["acme/web", 50],
1248 + ["acme/api", 1],
1249 + ]);
1250 + }),
1251 + );
1252 + it.effect.each([
1253 + GitLabCli.GitLabCliUnavailableError,
1254 + GitLabCli.GitLabCliAuthenticationError,
1255 + GitLabCli.GitLabCliRateLimitError,
1256 + ])("propagates unusable CLI and account failures from stats (%s)", (ErrorType) =>
1257 + Effect.gen(function* () {
1258 + const error = new ErrorType({
1259 + operation: "execute",
1260 + command: "glab",
1261 + cwd: "/w",
1262 + cause: null,
1263 + });
1264 + mockedExecute.mockReturnValueOnce(Effect.fail(error));
1265 + const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1266 + const result = yield* Effect.result(
1267 + cli.listMergeRequestStats({
1268 + cwd: "/w",
1269 + changeRequests: [{ repository: "acme/web", number: 7 }],
1270 + }),
1271 + );
1272 + expect(result).toMatchObject({ _tag: "Failure", failure: error });
1273 + }),
1274 + );
1275 });
packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts
+134 -1
@@ -37,6 +37,8 @@ import {
37 decodeMergeRequestDiffsJson,
38 decodeMergeRequestChangesJson,
39 decodeMergeRequestListJson,
40 + decodeMergeRequestFileStatsJson,
41 + decodeMergeRequestStatsJson,
42 decodeNotesJson,
43 decodeOwnAwardIdJson,
44 decodeProjectMergeCapabilitiesJson,
@@ -45,13 +47,18 @@ import {
47 decodeRepositoryBlobsJson,
48 decodeViewerJson,
49 gitLabAwardName,
50 + MERGE_REQUEST_FILE_STATS_GRAPHQL_QUERY,
51 + MERGE_REQUEST_STATS_GRAPHQL_QUERY,
52 REPOSITORY_BLOBS_GRAPHQL_QUERY,
53 type GitLabDiffRefs,
54 type GitLabMergeRequestDetail,
55 type GitLabMergeRequestListItem,
56 type GitLabProjectUsers,
57 } from "./gitLabMergeRequestJson.ts";
54 -import type { ProviderListCursor } from "@t3tools/source-control-core/server/PullRequestProvider";
58 +import type {
59 + ProviderFileStats,
60 + ProviderListCursor,
61 +} from "@t3tools/source-control-core/server/PullRequestProvider";
62
63 export class GitLabMergeRequestReadError extends Schema.TaggedError<GitLabMergeRequestReadError>()(
64 "GitLabMergeRequestReadError",
@@ -237,6 +244,29 @@ export class GitLabPullRequestCli extends Context.Service<
244 readonly number: number;
245 }) => Effect.Effect<GitLabMergeRequestDetail, GitLabPullRequestCliError>;
246
247 + /** Line counts for the given merge requests; one with no answer is left out. */
248 + readonly listMergeRequestStats: (input: {
249 + readonly cwd: string;
250 + readonly changeRequests: ReadonlyArray<{
251 + readonly repository: string;
252 + readonly number: number;
253 + }>;
254 + }) => Effect.Effect<
255 + ReadonlyArray<{
256 + readonly repository: string;
257 + readonly number: number;
258 + readonly additions: number;
259 + readonly deletions: number;
260 + }>,
261 + GitLabPullRequestCliError
262 + >;
263 +
264 + readonly getMergeRequestFileStats: (input: {
265 + readonly cwd: string;
266 + readonly repository: string;
267 + readonly number: number;
268 + }) => Effect.Effect<ProviderFileStats, GitLabPullRequestCliError>;
269 +
270 readonly listNotes: (input: {
271 readonly cwd: string;
272 readonly repository: string;
@@ -1021,6 +1051,97 @@ export const make = Effect.gen(function* () {
1051 }),
1052 );
1053
1054 + /** The stats connection hands back one page, so a repository's rows are asked for 100 at a time. */
1055 + const STAT_IIDS_PER_REQUEST = 100;
1056 +
1057 + const graphqlRead = <A>(input: {
1058 + readonly cwd: string;
1059 + readonly operation: string;
1060 + readonly query: string;
1061 + readonly variables: Record<string, unknown>;
1062 + readonly decode: (raw: string) => Result.Result<A, unknown>;
1063 + }) =>
1064 + api({
1065 + cwd: input.cwd,
1066 + path: "graphql",
1067 + method: "POST",
1068 + stdin: JSON.stringify({ query: input.query, variables: input.variables }),
1069 + }).pipe(
1070 + Effect.flatMap((result) => {
1071 + const decoded = input.decode(result.stdout.trim());
1072 + return Result.isSuccess(decoded)
1073 + ? Effect.succeed(decoded.success)
1074 + : Effect.fail(
1075 + new GitLabMergeRequestReadError({
1076 + command: "glab",
1077 + cwd: input.cwd,
1078 + operation: input.operation,
1079 + cause: decoded.failure,
1080 + }),
1081 + );
1082 + }),
1083 + );
1084 +
1085 + const mergeRequestStats = (input: {
1086 + readonly cwd: string;
1087 + readonly repository: string;
1088 + readonly numbers: ReadonlyArray<number>;
1089 + }) =>
1090 + graphqlRead({
1091 + cwd: input.cwd,
1092 + operation: "listMergeRequestStats",
1093 + query: MERGE_REQUEST_STATS_GRAPHQL_QUERY,
1094 + variables: { fullPath: input.repository, iids: input.numbers.map(String) },
1095 + decode: decodeMergeRequestStatsJson,
1096 + });
1097 +
1098 + const STAT_REQUEST_CONCURRENCY = 4;
1099 +
1100 + const listMergeRequestStats: GitLabPullRequestCli["Service"]["listMergeRequestStats"] = (
1101 + input,
1102 + ) => {
1103 + const byRepository = new Map<string, Set<number>>();
1104 + for (const { repository, number } of input.changeRequests) {
1105 + const numbers = byRepository.get(repository) ?? new Set<number>();
1106 + numbers.add(number);
1107 + byRepository.set(repository, numbers);
1108 + }
1109 + const reads: Array<{ readonly repository: string; readonly numbers: ReadonlyArray<number> }> =
1110 + [];
1111 + for (const [repository, numberSet] of byRepository) {
1112 + const numbers = [...numberSet];
1113 + for (let start = 0; start < numbers.length; start += STAT_IIDS_PER_REQUEST) {
1114 + reads.push({ repository, numbers: numbers.slice(start, start + STAT_IIDS_PER_REQUEST) });
1115 + }
1116 + }
1117 + return Effect.forEach(
1118 + reads,
1119 + (read) =>
1120 + mergeRequestStats({ cwd: input.cwd, ...read }).pipe(
1121 + Effect.map((stats) =>
1122 + read.numbers.flatMap((number) => {
1123 + const stat = stats.get(number);
1124 + return stat === undefined ? [] : [{ repository: read.repository, number, ...stat }];
1125 + }),
1126 + ),
1127 + // One read GitLab could not answer costs its own rows their counts, not every other
1128 + // read's on the same page. An unusable CLI or account still reaches the caller.
1129 + Effect.catchIf(
1130 + (cause) =>
1131 + cause._tag !== "GitLabCliUnavailableError" &&
1132 + cause._tag !== "GitLabCliAuthenticationError" &&
1133 + cause._tag !== "GitLabCliRateLimitError",
1134 + (cause) =>
1135 + Effect.logWarning("GitLab merge request line count read failed", {
1136 + repository: read.repository,
1137 + reason: cause._tag,
1138 + }).pipe(Effect.as([])),
1139 + ),
1140 + ),
1141 + { concurrency: STAT_REQUEST_CONCURRENCY },
1142 + ).pipe(Effect.map((results) => results.flat()));
1143 + };
1144 +
1145 // GitLab charges by path count and returns one connection page; respect both limits.
1146 const BLOB_PATHS_PER_REQUEST = 100;
1147
@@ -1125,6 +1246,18 @@ export const make = Effect.gen(function* () {
1246
1247 getMergeRequestDetail: mergeRequestDetail,
1248
1249 + listMergeRequestStats,
1250 +
1251 + getMergeRequestFileStats: Effect.fn("GitLabPullRequestCli.getMergeRequestFileStats")((input) =>
1252 + graphqlRead({
1253 + cwd: input.cwd,
1254 + operation: "getMergeRequestFileStats",
1255 + query: MERGE_REQUEST_FILE_STATS_GRAPHQL_QUERY,
1256 + variables: { fullPath: input.repository, iid: String(input.number) },
1257 + decode: decodeMergeRequestFileStatsJson,
1258 + }),
1259 + ),
1260 +
1261 listNotes: Effect.fn("GitLabPullRequestCli.listNotes")(function (input) {
1262 return notesPage({ ...input, page: 1, collected: [] });
1263 }),
packages/source-control-gitlab/src/server/GitLabPullRequestProvider.test.ts
+69 -1
@@ -74,6 +74,7 @@ describe("getChangeRequest base freshness", () => {
74 Effect.provide(
75 Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
76 getMergeRequestDetail: () => Effect.succeed(detail),
77 + listMergeRequestStats: () => Effect.succeed([]),
78 }),
79 ),
80 );
@@ -85,7 +86,11 @@ describe("getChangeRequest base freshness", () => {
86 }),
87 );
88
88 - const readWith = (divergence: { readonly divergedCommits?: number }) =>
89 + const readWith = (
90 + divergence: { readonly divergedCommits?: number },
91 + stats: GitLabPullRequestCli.GitLabPullRequestCli["Service"]["listMergeRequestStats"] = () =>
92 + Effect.succeed([]),
93 + ) =>
94 Effect.gen(function* () {
95 const provider = yield* make;
96 return yield* provider.getChangeRequest({
@@ -100,6 +105,7 @@ describe("getChangeRequest base freshness", () => {
105 getMergeRequestDetail: () => Effect.succeed({ ...detail, ...divergence }),
106 getProjectMergeCapabilities: () =>
107 Effect.succeed({ merge: true, squash: true, rebase: true }),
108 + listMergeRequestStats: stats,
109 }),
110 ),
111 );
@@ -121,6 +127,62 @@ describe("getChangeRequest base freshness", () => {
127 expect(changeRequest.behindBy).toBeUndefined();
128 }),
129 );
130 +
131 + it.effect("fills in the line counts the REST detail leaves out", () =>
132 + Effect.gen(function* () {
133 + const changeRequest = yield* readWith({}, () =>
134 + Effect.succeed([{ repository: "acme/web", number: 7, additions: 40, deletions: 2 }]),
135 + );
136 +
137 + expect(changeRequest.additions).toBe(40);
138 + expect(changeRequest.deletions).toBe(2);
139 + }),
140 + );
141 +
142 + it.effect("reports an unauthenticated line count read instead of a detail without counts", () =>
143 + Effect.gen(function* () {
144 + const result = yield* Effect.result(
145 + readWith({}, () =>
146 + Effect.fail(
147 + new GitLabCli.GitLabCliAuthenticationError({
148 + operation: "execute",
149 + command: "glab",
150 + cwd: "/w",
151 + cause: null,
152 + }),
153 + ),
154 + ),
155 + );
156 +
157 + expect(result).toMatchObject({
158 + _tag: "Failure",
159 + failure: { _tag: "PullRequestProviderError", reason: "unauthenticated" },
160 + });
161 + }),
162 + );
163 +
164 + it.effect("gives linked threads line counts without reading the merge settings", () =>
165 + Effect.gen(function* () {
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: () => Effect.die("linked threads skip merge settings"),
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 + }),
185 + );
186 });
187
188 describe("merge request reads", () => {
@@ -157,6 +219,7 @@ describe("merge request reads", () => {
219 const provider = yield* make.pipe(
220 Effect.provide(
221 Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
222 + listMergeRequestStats: () => Effect.succeed([]),
223 getMergeRequestDetail: (input) =>
224 Effect.sync(() => {
225 calls.detail += 1;
@@ -199,6 +262,11 @@ describe("merge request reads", () => {
262 yield* checks(reference);
263
264 expect(calls.detail).toBe(1);
265 +
266 + const summary = provider.getChangeRequestSummary;
267 + assert.isDefined(summary);
268 + yield* summary(reference);
269 + expect(calls.detail).toBe(2);
270 }),
271 );
272
packages/source-control-gitlab/src/server/GitLabPullRequestProvider.ts
+29 -2
@@ -173,6 +173,25 @@ export const make = Effect.gen(function* () {
173 }),
174 );
175
176 + // REST omits line counts; the CLI recovers optional stats failures per batch.
177 + const mergeRequestWithStats = (
178 + input: {
179 + readonly cwd: string;
180 + readonly repository: string;
181 + readonly number: number;
182 + },
183 + detail = cli.getMergeRequestDetail(input),
184 + ) =>
185 + Effect.all([detail, cli.listMergeRequestStats({ cwd: input.cwd, changeRequests: [input] })], {
186 + concurrency: 2,
187 + }).pipe(
188 + Effect.map(([mergeRequest, [stat]]) =>
189 + stat === undefined
190 + ? mergeRequest
191 + : { ...mergeRequest, additions: stat.additions, deletions: stat.deletions },
192 + ),
193 + );
194 +
195 const provider: PullRequestProviderApi = {
196 kind: SourceControlProviderKind.make("gitlab"),
197 capabilities: CAPABILITIES,
@@ -199,6 +218,14 @@ export const make = Effect.gen(function* () {
218 Effect.map((batch) => ({ ...batch, continues: true })),
219 ),
220
221 + getFileStats: (input) =>
222 + cli.getMergeRequestFileStats(input).pipe(Effect.mapError(fail("getFileStats"))),
223 +
224 + listChangeRequestStats: (input) =>
225 + cli
226 + .listMergeRequestStats({ cwd: input.cwd, changeRequests: input.changeRequests })
227 + .pipe(Effect.mapError(fail("listChangeRequestStats"))),
228 +
229 getChangeRequestChecks: (input) =>
230 mergeRequestDetail(input).pipe(
231 Effect.map(({ state, checks }) => ({ state, checks })),
@@ -210,12 +237,12 @@ export const make = Effect.gen(function* () {
237 cli.getMergeRequestDetail(input).pipe(Effect.mapError(fail("getChangeRequestPreview"))),
238
239 getChangeRequestSummary: (input) =>
213 - cli.getMergeRequestDetail(input).pipe(Effect.mapError(fail("getChangeRequestSummary"))),
240 + mergeRequestWithStats(input).pipe(Effect.mapError(fail("getChangeRequestSummary"))),
241
242 getChangeRequest: (input) =>
243 Effect.all(
244 [
218 - mergeRequestDetail(input),
245 + mergeRequestWithStats(input, mergeRequestDetail(input)),
246 Cache.get(projectMergeCapabilities, JSON.stringify([input.cwd, input.repository])),
247 ],
248 { concurrency: 2 },
packages/source-control-gitlab/src/server/gitLabMergeRequestJson.test.ts
+58
@@ -8,6 +8,8 @@ import {
8 decodeMergeRequestDetailJson,
9 decodeMergeRequestDiffsJson,
10 decodeMergeRequestListJson,
11 + decodeMergeRequestFileStatsJson,
12 + decodeMergeRequestStatsJson,
13 decodeNotesJson,
14 decodeOwnAwardIdJson,
15 } from "./gitLabMergeRequestJson.ts";
@@ -620,3 +622,59 @@ describe("decodeRepositoryBlobsJson", () => {
622 ).toEqual(new Map());
623 });
624 });
625 +
626 +describe("decodeMergeRequestStatsJson", () => {
627 + it("keys the counts by merge request number", () => {
628 + const stats = expectSuccess(
629 + decodeMergeRequestStatsJson(
630 + JSON.stringify({
631 + data: {
632 + project: {
633 + mergeRequests: {
634 + nodes: [
635 + { iid: "7", diffStatsSummary: { additions: 12, deletions: 3 } },
636 + { iid: "8", diffStatsSummary: null },
637 + ],
638 + },
639 + },
640 + },
641 + }),
642 + ),
643 + );
644 +
645 + // A merge request without a summary is left out rather than shown as zero lines changed.
646 + expect([...stats]).toEqual([[7, { additions: 12, deletions: 3 }]]);
647 + });
648 +
649 + it("answers nothing for a project the token cannot see", () => {
650 + const stats = expectSuccess(
651 + decodeMergeRequestStatsJson(JSON.stringify({ data: { project: null } })),
652 + );
653 +
654 + expect(stats.size).toBe(0);
655 + });
656 +
657 + it("reports a file list shorter than the summary as truncated", () => {
658 + const read = (fileCount: number) =>
659 + expectSuccess(
660 + decodeMergeRequestFileStatsJson(
661 + JSON.stringify({
662 + data: {
663 + project: {
664 + mergeRequest: {
665 + diffStatsSummary: { fileCount },
666 + diffStats: [{ path: "src/a.ts", additions: 2, deletions: 1 }],
667 + },
668 + },
669 + },
670 + }),
671 + ),
672 + );
673 +
674 + expect(read(1)).toEqual({
675 + files: [{ path: "src/a.ts", additions: 2, deletions: 1 }],
676 + truncated: false,
677 + });
678 + expect(read(2).truncated).toBe(true);
679 + });
680 +});
packages/source-control-gitlab/src/server/gitLabMergeRequestJson.ts
+115
@@ -946,3 +946,118 @@ export function decodeRepositoryBlobsJson(
946 }
947 return Result.succeed(blobs);
948 }
949 +
950 +/**
951 + * Line counts for merge requests of one project. The REST listing carries no line counts, and
952 + * reading each diff to count them would cost a request per row, so GraphQL's per-merge-request
953 + * summary is asked for in one read instead.
954 + */
955 +export const MERGE_REQUEST_STATS_GRAPHQL_QUERY = `query($fullPath: ID!, $iids: [String!]) {
956 + project(fullPath: $fullPath) {
957 + mergeRequests(iids: $iids, first: 100) {
958 + nodes { iid diffStatsSummary { additions deletions } }
959 + }
960 + }
961 +}`;
962 +
963 +const LineCountsSchema = Schema.Struct({ additions: Schema.Int, deletions: Schema.Int });
964 +
965 +const RawMergeRequestStatsSchema = Schema.Struct({
966 + data: Schema.Struct({
967 + project: Schema.NullOr(
968 + Schema.Struct({
969 + mergeRequests: Schema.NullOr(
970 + Schema.Struct({
971 + nodes: Schema.Array(
972 + Schema.NullOr(
973 + Schema.Struct({
974 + iid: Schema.String,
975 + diffStatsSummary: Schema.NullOr(LineCountsSchema),
976 + }),
977 + ),
978 + ),
979 + }),
980 + ),
981 + }),
982 + ),
983 + }),
984 +});
985 +
986 +const decodeMergeRequestStats = decodeJsonResult(RawMergeRequestStatsSchema);
987 +
988 +/**
989 + * The counts by merge request number. A merge request GitLab returned no summary for is left
990 + * out rather than counted as zero, so its row keeps whatever it already showed.
991 + */
992 +export function decodeMergeRequestStatsJson(
993 + raw: string,
994 +): Result.Result<
995 + ReadonlyMap<number, { readonly additions: number; readonly deletions: number }>,
996 + DecodeFailure
997 +> {
998 + const decoded = decodeMergeRequestStats(raw);
999 + if (!Result.isSuccess(decoded)) {
1000 + return Result.fail(decoded.failure);
1001 + }
1002 + const stats = new Map<number, { readonly additions: number; readonly deletions: number }>();
1003 + for (const node of decoded.success.data.project?.mergeRequests?.nodes ?? []) {
1004 + const iid = Number(node?.iid);
1005 + if (!Number.isSafeInteger(iid) || iid <= 0 || node?.diffStatsSummary == null) continue;
1006 + stats.set(iid, node.diffStatsSummary);
1007 + }
1008 + return Result.succeed(stats);
1009 +}
1010 +
1011 +/** Per-file line counts for one merge request, which the code/tests/generated breakdown sorts. */
1012 +export const MERGE_REQUEST_FILE_STATS_GRAPHQL_QUERY = `query($fullPath: ID!, $iid: String!) {
1013 + project(fullPath: $fullPath) {
1014 + mergeRequest(iid: $iid) {
1015 + diffStatsSummary { fileCount }
1016 + diffStats { path additions deletions }
1017 + }
1018 + }
1019 +}`;
1020 +
1021 +const decodeMergeRequestFileStats = decodeJsonResult(
1022 + Schema.Struct({
1023 + data: Schema.Struct({
1024 + project: Schema.NullOr(
1025 + Schema.Struct({
1026 + mergeRequest: Schema.NullOr(
1027 + Schema.Struct({
1028 + diffStatsSummary: Schema.NullOr(Schema.Struct({ fileCount: Schema.Int })),
1029 + diffStats: Schema.NullOr(
1030 + Schema.Array(Schema.Struct({ path: Schema.String, ...LineCountsSchema.fields })),
1031 + ),
1032 + }),
1033 + ),
1034 + }),
1035 + ),
1036 + }),
1037 + }),
1038 +);
1039 +
1040 +/**
1041 + * GitLab stops listing files past its diff limits while the summary still counts them all, so a
1042 + * shorter list is reported as truncated rather than as a breakdown that misses lines.
1043 + */
1044 +export function decodeMergeRequestFileStatsJson(raw: string): Result.Result<
1045 + {
1046 + readonly files: ReadonlyArray<{
1047 + readonly path: string;
1048 + readonly additions: number;
1049 + readonly deletions: number;
1050 + }>;
1051 + readonly truncated: boolean;
1052 + },
1053 + DecodeFailure
1054 +> {
1055 + const decoded = decodeMergeRequestFileStats(raw);
1056 + if (!Result.isSuccess(decoded)) {
1057 + return Result.fail(decoded.failure);
1058 + }
1059 + const mergeRequest = decoded.success.data.project?.mergeRequest;
1060 + const files = mergeRequest?.diffStats ?? [];
1061 + const fileCount = mergeRequest?.diffStatsSummary?.fileCount;
1062 + return Result.succeed({ files, truncated: fileCount === undefined || files.length < fileCount });
1063 +}