Skip to content

Commit 444df61

Browse files
TonybynMp4claudecoderabbitai[bot]maria-rcks
authored
fix(gitlab): merge requests show their line counts (#22374)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: maria-rcks <[email protected]>
1 parent 5ad7da0 commit 444df61

6 files changed

Lines changed: 493 additions & 4 deletions

File tree

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

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,4 +1184,92 @@ layer("GitLabPullRequestCli.layer", (it) => {
11841184
assert.strictEqual(mockedExecute.mock.calls.length, 3);
11851185
}),
11861186
);
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+
);
11871275
});

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

Lines changed: 134 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ import {
3737
decodeMergeRequestDiffsJson,
3838
decodeMergeRequestChangesJson,
3939
decodeMergeRequestListJson,
40+
decodeMergeRequestFileStatsJson,
41+
decodeMergeRequestStatsJson,
4042
decodeNotesJson,
4143
decodeOwnAwardIdJson,
4244
decodeProjectMergeCapabilitiesJson,
@@ -45,13 +47,18 @@ import {
4547
decodeRepositoryBlobsJson,
4648
decodeViewerJson,
4749
gitLabAwardName,
50+
MERGE_REQUEST_FILE_STATS_GRAPHQL_QUERY,
51+
MERGE_REQUEST_STATS_GRAPHQL_QUERY,
4852
REPOSITORY_BLOBS_GRAPHQL_QUERY,
4953
type GitLabDiffRefs,
5054
type GitLabMergeRequestDetail,
5155
type GitLabMergeRequestListItem,
5256
type GitLabProjectUsers,
5357
} 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";
5562

5663
export class GitLabMergeRequestReadError extends Schema.TaggedError<GitLabMergeRequestReadError>()(
5764
"GitLabMergeRequestReadError",
@@ -237,6 +244,29 @@ export class GitLabPullRequestCli extends Context.Service<
237244
readonly number: number;
238245
}) => Effect.Effect<GitLabMergeRequestDetail, GitLabPullRequestCliError>;
239246

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+
240270
readonly listNotes: (input: {
241271
readonly cwd: string;
242272
readonly repository: string;
@@ -1021,6 +1051,97 @@ export const make = Effect.gen(function* () {
10211051
}),
10221052
);
10231053

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+
10241145
// GitLab charges by path count and returns one connection page; respect both limits.
10251146
const BLOB_PATHS_PER_REQUEST = 100;
10261147

@@ -1125,6 +1246,18 @@ export const make = Effect.gen(function* () {
11251246

11261247
getMergeRequestDetail: mergeRequestDetail,
11271248

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+
11281261
listNotes: Effect.fn("GitLabPullRequestCli.listNotes")(function (input) {
11291262
return notesPage({ ...input, page: 1, collected: [] });
11301263
}),

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

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ describe("getChangeRequest base freshness", () => {
7474
Effect.provide(
7575
Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
7676
getMergeRequestDetail: () => Effect.succeed(detail),
77+
listMergeRequestStats: () => Effect.succeed([]),
7778
}),
7879
),
7980
);
@@ -85,7 +86,11 @@ describe("getChangeRequest base freshness", () => {
8586
}),
8687
);
8788

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+
) =>
8994
Effect.gen(function* () {
9095
const provider = yield* make;
9196
return yield* provider.getChangeRequest({
@@ -100,6 +105,7 @@ describe("getChangeRequest base freshness", () => {
100105
getMergeRequestDetail: () => Effect.succeed({ ...detail, ...divergence }),
101106
getProjectMergeCapabilities: () =>
102107
Effect.succeed({ merge: true, squash: true, rebase: true }),
108+
listMergeRequestStats: stats,
103109
}),
104110
),
105111
);
@@ -121,6 +127,62 @@ describe("getChangeRequest base freshness", () => {
121127
expect(changeRequest.behindBy).toBeUndefined();
122128
}),
123129
);
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+
);
124186
});
125187

126188
describe("merge request reads", () => {
@@ -157,6 +219,7 @@ describe("merge request reads", () => {
157219
const provider = yield* make.pipe(
158220
Effect.provide(
159221
Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
222+
listMergeRequestStats: () => Effect.succeed([]),
160223
getMergeRequestDetail: (input) =>
161224
Effect.sync(() => {
162225
calls.detail += 1;
@@ -199,6 +262,11 @@ describe("merge request reads", () => {
199262
yield* checks(reference);
200263

201264
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);
202270
}),
203271
);
204272

0 commit comments

Comments
 (0)