Skip to content

Commit 8dae589

Browse files
committed
fix(source-control): stop Forgejo status refresh from scanning every pull request
Forgejo listed pull requests unfiltered and matched the head branch in JS, so a branch with one pull request or none walked the repository's entire history 50 rows at a time until a page came back empty. Status refreshes run that under the remote write lock, which is why vcs.refreshStatus hung past 15s on repositories with any real pull request history. Forgejo's pulls endpoint accepts a head parameter, so narrow server-side. The client-side branch match stays: a server too old for the filter ignores it and answers unfiltered. Signed-off-by: Loïs Postula <[email protected]>
1 parent 0150c6a commit 8dae589

2 files changed

Lines changed: 80 additions & 1 deletion

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { assert, it } from "@effect/vitest";
2+
import * as Effect from "effect/Effect";
3+
import * as Layer from "effect/Layer";
4+
import { ChildProcessSpawner } from "effect/unstable/process";
5+
import * as FileSystem from "effect/FileSystem";
6+
7+
import * as ForgejoCli from "./ForgejoCli.ts";
8+
import * as ForgejoSourceControlProvider from "./ForgejoSourceControlProvider.ts";
9+
import * as VcsProcess from "../vcs/VcsProcess.ts";
10+
11+
const output = (stdout: string) => ({
12+
exitCode: ChildProcessSpawner.ExitCode(0),
13+
stdout,
14+
stderr: "",
15+
stdoutTruncated: false,
16+
stderrTruncated: false,
17+
});
18+
19+
const pull = (number: number, headRef: string) => ({
20+
number,
21+
title: `PR ${number}`,
22+
html_url: `https://forge.test/owner/repo/pulls/${number}`,
23+
state: "open",
24+
merged: false,
25+
base: { ref: "main", sha: "base", repo: { full_name: "owner/repo", owner: { login: "owner" } } },
26+
head: { ref: headRef, sha: "head", repo: { full_name: "owner/repo", owner: { login: "owner" } } },
27+
});
28+
29+
function makeProvider(cli: Partial<ForgejoCli.ForgejoCli["Service"]>) {
30+
return ForgejoSourceControlProvider.make.pipe(
31+
Effect.provide(
32+
Layer.mergeAll(
33+
Layer.mock(ForgejoCli.ForgejoCli)({
34+
resolveRepository: () =>
35+
Effect.succeed({
36+
login: "forge",
37+
repository: "owner/repo",
38+
baseUrl: "https://forge.test",
39+
}),
40+
...cli,
41+
}),
42+
Layer.mock(VcsProcess.VcsProcess)({}),
43+
FileSystem.layerNoop({}),
44+
),
45+
),
46+
);
47+
}
48+
49+
it.effect("narrows the pull request listing to the head branch server-side", () =>
50+
Effect.gen(function* () {
51+
const paths: string[] = [];
52+
const provider = yield* makeProvider({
53+
api: (input) => {
54+
paths.push(input.path);
55+
return Effect.succeed(
56+
output(JSON.stringify(paths.length === 1 ? [pull(7, "feature/branch")] : [])),
57+
);
58+
},
59+
});
60+
61+
const results = yield* provider.listChangeRequests({
62+
cwd: "/repo",
63+
headSelector: "feature/branch",
64+
state: "all",
65+
limit: 20,
66+
});
67+
68+
assert.deepStrictEqual(
69+
results.map((result) => result.number),
70+
[7],
71+
);
72+
// The scan stops once the server answers an empty page, which only stays cheap
73+
// while the server does the branch filtering.
74+
assert.strictEqual(paths.length, 2);
75+
for (const path of paths) {
76+
assert.include(path, "head=feature%2Fbranch");
77+
}
78+
}),
79+
);

‎apps/server/src/sourceControl/ForgejoSourceControlProvider.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ export const make = Effect.gen(function* () {
239239
const items = yield* request(
240240
{
241241
...input,
242-
path: `${repositoryPath(repo.repository)}/pulls?state=${input.state === "merged" ? "closed" : input.state}&sort=recentupdate&limit=50&page=${page}`,
242+
path: `${repositoryPath(repo.repository)}/pulls?state=${input.state === "merged" ? "closed" : input.state}&head=${encodeURIComponent(branch)}&sort=recentupdate&limit=50&page=${page}`,
243243
},
244244
Schema.Array(ForgejoPullRequestSchema),
245245
);

0 commit comments

Comments
 (0)