@devries-cameron20 / t3code Mirror · read-only / commits / 0a6b3ebe22
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 16:00 UTC

fix(forgejo): Gitea branch PR lookups no longer read the whole pull request list (#36387)

Julius Marminge committed Nov 13, 2026 at 12:56 UTC 0a6b3ebe226a5438eba325c9fb17ca204db5b2ab
2 files changed +282 -11
packages/source-control-forgejo/src/server/ForgejoSourceControlProvider.test.ts
+155
@@ -8,6 +8,7 @@ import * as Option from "effect/Option";
8 import * as PlatformError from "effect/PlatformError";
9 import * as Schema from "effect/Schema";
10 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
11 +import * as TestClock from "effect/testing/TestClock";
12 import * as HttpClient from "effect/http/HttpClient";
13 import * as HttpClientRequest from "effect/http/HttpClientRequest";
14 import * as HttpClientResponse from "effect/http/HttpClientResponse";
@@ -969,6 +970,160 @@ it.effect("loads Forgejo pull request references from files and commits views",
970 ),
971 );
972
973 +// Gitea ignores `head=` on the pull request list, so each page holds every branch's pull requests.
974 +// The second lookup still asks for the front of the list: a pull request may have been opened.
975 +it.effect("reads a Gitea pull request list at most once for branches that have none", () => {
976 + const listPages: Array<string> = [];
977 + return Effect.gen(function* () {
978 + const provider = yield* ForgejoSourceControlProvider.make;
979 + for (const headSelector of ["first-without-pr", "second-without-pr"]) {
980 + const found = yield* provider.listChangeRequests({
981 + cwd: "/repo",
982 + headSelector,
983 + state: "all",
984 + });
985 + assert.deepStrictEqual(found, []);
986 + }
987 + assert.isAtMost(listPages.length, 5);
988 + }).pipe(
989 + Effect.provide(
990 + Layer.mergeAll(
991 + Layer.succeed(FileSystem.FileSystem, FileSystem.makeNoop({})),
992 + TestSourceControlHost.layer({ process: {} }),
993 + Layer.mock(ForgejoCli.ForgejoCli)({
994 + resolveRepository: () =>
995 + Effect.succeed({
996 + login: "work",
997 + repository: "maria/project",
998 + baseUrl: "https://forgejo.test",
999 + }),
1000 + api: (input) => {
1001 + if (!input.path.startsWith("repos/maria/project/pulls?"))
1002 + return Effect.fail(
1003 + new ForgejoCli.ForgejoCliError({
1004 + command: "tea",
1005 + cwd: input.cwd,
1006 + httpStatus: 404,
1007 + detail: "Forgejo API request failed (HTTP 404): Not Found",
1008 + }),
1009 + );
1010 + listPages.push(input.path);
1011 + const repo = { full_name: "maria/project", owner: { login: "maria" } };
1012 + const page = Number(new URLSearchParams(input.path.split("?")[1]).get("page"));
1013 + const items =
1014 + page > 3
1015 + ? []
1016 + : Array.from({ length: 50 }, (_, index) => {
1017 + const number = 200 - (page - 1) * 50 - index;
1018 + return {
1019 + number,
1020 + title: `PR ${number}`,
1021 + html_url: `https://forgejo.test/maria/project/pulls/${number}`,
1022 + state: "open",
1023 + merged: false,
1024 + updated_at: `2026-10-12T00:0${Math.floor(number / 60)}:${String(number % 60).padStart(2, "0")}Z`,
1025 + base: { ref: "main", label: "main", sha: "base", repo },
1026 + head: {
1027 + ref: `feature-${number}`,
1028 + label: `feature-${number}`,
1029 + sha: "h",
1030 + repo,
1031 + },
1032 + };
1033 + });
1034 + return encodeJsonEffect(items).pipe(Effect.orDie, Effect.map(processOutput));
1035 + },
1036 + }),
1037 + ),
1038 + ),
1039 + );
1040 +});
1041 +
1042 +it.effect("reads only the changed front of a Gitea pull request list it has read before", () => {
1043 + const repo = { full_name: "maria/project", owner: { login: "maria" } };
1044 + const fork = { full_name: "alice/renamed", owner: { login: "alice" } };
1045 + const pull = (number: number, updatedSecond: number, head: string, state = "open") => ({
1046 + number,
1047 + title: `PR ${number}`,
1048 + html_url: `https://forgejo.test/maria/project/pulls/${number}`,
1049 + state,
1050 + merged: false,
1051 + updated_at: `2026-10-12T00:0${Math.floor(updatedSecond / 60)}:${String(updatedSecond % 60).padStart(2, "0")}Z`,
1052 + base: { ref: number === 5 ? "release" : "main", label: "main", sha: "base", repo },
1053 + head: { ref: head, label: head, sha: "h", repo: number === 5 ? fork : repo },
1054 + });
1055 + // Newest update first, as `sort=recentupdate` returns them. PR 5 sits on the last page.
1056 + let pulls = Array.from({ length: 120 }, (_, index) =>
1057 + pull(120 - index, 120 - index, 120 - index === 5 ? "feature" : `other-${120 - index}`),
1058 + );
1059 + // List pages read; a match is also read once by number before it is returned.
1060 + let requests = 0;
1061 + return Effect.gen(function* () {
1062 + const provider = yield* ForgejoSourceControlProvider.make;
1063 + const find = (headSelector: string) =>
1064 + provider
1065 + .listChangeRequests({ cwd: "/repo", headSelector, state: "all" })
1066 + .pipe(Effect.map((found) => found.map((pr) => [pr.number, pr.state])));
1067 +
1068 + assert.deepStrictEqual(yield* find("alice:feature"), [[5, "open"]]);
1069 + assert.strictEqual(requests, 4);
1070 +
1071 + pulls = [pull(121, 202, "later"), pull(5, 201, "feature", "closed"), ...pulls].filter(
1072 + (entry, index, all) => all.findIndex((other) => other.number === entry.number) === index,
1073 + );
1074 + assert.deepStrictEqual(yield* find("later"), [[121, "open"]]);
1075 + assert.deepStrictEqual(yield* find("alice:feature"), [[5, "closed"]]);
1076 + assert.strictEqual(requests, 6);
1077 +
1078 + // Deleting a pull request moves nothing in the list.
1079 + pulls = pulls.filter((entry) => entry.number !== 121);
1080 + assert.deepStrictEqual(yield* find("later"), []);
1081 + assert.strictEqual(requests, 7);
1082 +
1083 + pulls = pulls.filter((entry) => entry.number !== 120);
1084 + yield* TestClock.adjust("1 hour");
1085 + assert.deepStrictEqual(yield* find("other-120"), []);
1086 + assert.strictEqual(requests, 11);
1087 + }).pipe(
1088 + Effect.provide(
1089 + Layer.mergeAll(
1090 + Layer.succeed(FileSystem.FileSystem, FileSystem.makeNoop({})),
1091 + TestSourceControlHost.layer({ process: {} }),
1092 + Layer.mock(ForgejoCli.ForgejoCli)({
1093 + resolveRepository: () =>
1094 + Effect.succeed({
1095 + login: "work",
1096 + repository: "maria/project",
1097 + baseUrl: "https://forgejo.test",
1098 + }),
1099 + api: (input) => {
1100 + const single = /\/pulls\/(\d+)$/.exec(input.path)?.[1];
1101 + if (single !== undefined) {
1102 + const found = pulls.find((entry) => entry.number === Number(single));
1103 + return found
1104 + ? encodeJsonEffect(found).pipe(Effect.orDie, Effect.map(processOutput))
1105 + : Effect.fail(
1106 + new ForgejoCli.ForgejoCliError({
1107 + command: "tea",
1108 + cwd: input.cwd,
1109 + httpStatus: 404,
1110 + detail: "Forgejo API request failed (HTTP 404): Not Found",
1111 + }),
1112 + );
1113 + }
1114 + requests++;
1115 + const page = Number(new URLSearchParams(input.path.split("?")[1]).get("page"));
1116 + return encodeJsonEffect(pulls.slice((page - 1) * 50, page * 50)).pipe(
1117 + Effect.orDie,
1118 + Effect.map(processOutput),
1119 + );
1120 + },
1121 + }),
1122 + ),
1123 + ),
1124 + );
1125 +});
1126 +
1127 it.effect("finds a merged Forgejo pull request whose head branch was deleted", () =>
1128 Effect.gen(function* () {
1129 const provider = yield* ForgejoSourceControlProvider.make;
packages/source-control-forgejo/src/server/ForgejoSourceControlProvider.ts
+127 -11
@@ -1,8 +1,12 @@
1 import { SourceControlProviderKind } from "@t3tools/contracts";
2 +import * as Clock from "effect/Clock";
3 +import * as DateTime from "effect/DateTime";
4 import * as Effect from "effect/Effect";
5 import * as FileSystem from "effect/FileSystem";
6 +import * as Option from "effect/Option";
7 import * as Schema from "effect/Schema";
8 import * as Result from "effect/Result";
9 +import * as Semaphore from "effect/Semaphore";
10 import { SourceControlProviderError } from "@t3tools/contracts";
11 import * as HostProcess from "@t3tools/shared/HostProcess";
12 import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
@@ -278,6 +282,20 @@ const cloneUrls = (raw: typeof RepositorySchema.Type) => ({
282 const repositoryPath = (repository: string) =>
283 `repos/${repository.split("/").map(encodeURIComponent).join("/")}`;
284
285 +type ForgejoPull = typeof ForgejoPullRequestSchema.Type;
286 +const updatedMillis = (pull: ForgejoPull) =>
287 + pull.updated_at?.pipe(Option.map(DateTime.toEpochMillis), Option.getOrUndefined);
288 +// Renaming a fork or deleting a pull request moves nothing in the list, so it is re-read in full.
289 +const UNFILTERED_PULLS_MAX_AGE_MS = 60 * 60_000;
290 +/** Every pull request of a repository whose server ignores `head=` (Gitea, Forgejo before 16). */
291 +interface UnfilteredPulls {
292 + readonly lock: Semaphore.Semaphore;
293 + pulls: Map<number, ForgejoPull> | undefined;
294 + /** The newest `updated_at` on the list's first page when it was last read. */
295 + newest: number | undefined;
296 + scannedAt: number;
297 +}
298 +
299 export const make = Effect.gen(function* () {
300 const cli = yield* ForgejoCli.ForgejoCli;
301 const fs = yield* FileSystem.FileSystem;
@@ -332,6 +350,48 @@ export const make = Effect.gen(function* () {
350 ForgejoPullRequestSchema,
351 );
352 });
353 + const unfilteredPulls = new Map<string, UnfilteredPulls>();
354 + // Call holding `state.lock`. The list is sorted newest update first, so after one full read
355 + // everything that changed since sits ahead of the first pull request older than `newest`.
356 + const readUnfilteredPulls = Effect.fn("ForgejoSourceControlProvider.readUnfilteredPulls")(
357 + function* (
358 + input: ForgejoCli.ForgejoRepositoryInput,
359 + repository: string,
360 + state: UnfilteredPulls,
361 + firstPage: ReadonlyArray<ForgejoPull> | undefined,
362 + ) {
363 + const now = yield* Clock.currentTimeMillis;
364 + const since =
365 + state.pulls && now - state.scannedAt < UNFILTERED_PULLS_MAX_AGE_MS
366 + ? state.newest
367 + : undefined;
368 + const pulls =
369 + since !== undefined && state.pulls ? state.pulls : new Map<number, ForgejoPull>();
370 + let newest = since;
371 + for (let page = 1; ; page++) {
372 + const items =
373 + page === 1 && firstPage && !state.pulls
374 + ? firstPage
375 + : yield* request(
376 + {
377 + ...input,
378 + path: `${repositoryPath(repository)}/pulls?state=all&sort=recentupdate&limit=50&page=${page}`,
379 + },
380 + Schema.Array(ForgejoPullRequestSchema),
381 + );
382 + const updated = items.flatMap((item) => updatedMillis(item) ?? []);
383 + for (const item of items) pulls.set(item.number, item);
384 + // Later pages can hold newer updates than a first page read before them, and a mark past
385 + // the first page's would skip the updates that pushed them down.
386 + if (page === 1 && updated.length > 0) newest = Math.max(...updated);
387 + if (items.length === 0 || (since !== undefined && updated.some((at) => at < since))) break;
388 + }
389 + if (since === undefined) state.scannedAt = now;
390 + state.pulls = pulls;
391 + state.newest = newest;
392 + return pulls;
393 + },
394 + );
395 return SourceControlProvider.SourceControlProvider.of({
396 kind: SourceControlProviderKind.make("forgejo"),
397 repositoryNameFromRemoteUrl,
@@ -342,16 +402,9 @@ export const make = Effect.gen(function* () {
402 const repo = yield* cli.resolveRepository(input);
403 const source = SourceControlProvider.sourceControlRefFromInput(input);
404 const branch = SourceControlProvider.sourceBranch(input);
345 - const results: ReturnType<typeof toForgejoChangeRequest>[] = [];
405 const limit = input.limit ?? 20;
347 - for (let page = 1; results.length < limit; page++) {
348 - const items = yield* request(
349 - {
350 - ...input,
351 - path: `${repositoryPath(repo.repository)}/pulls?state=${input.state === "merged" ? "closed" : input.state}&head=${encodeURIComponent(branch)}&sort=recentupdate&limit=50&page=${page}`,
352 - },
353 - Schema.Array(ForgejoPullRequestSchema),
354 - );
406 + const matching = (items: Iterable<ForgejoPull>) => {
407 + const results: ReturnType<typeof toForgejoChangeRequest>[] = [];
408 for (const item of items) {
409 const normalized = toForgejoChangeRequest(item);
410 if (
@@ -362,9 +415,72 @@ export const make = Effect.gen(function* () {
415 continue;
416 if (input.state === "all" || normalized.state === input.state) results.push(normalized);
417 }
365 - if (items.length === 0) break;
418 + return results;
419 + };
420 + const key = `${repo.baseUrl}\n${repo.repository}`;
421 + let unfiltered = unfilteredPulls.get(key);
422 + let firstPage: ReadonlyArray<ForgejoPull> | undefined;
423 + if (!unfiltered) {
424 + const results: ReturnType<typeof toForgejoChangeRequest>[] = [];
425 + for (let page = 1; results.length < limit; page++) {
426 + const items = yield* request(
427 + {
428 + ...input,
429 + path: `${repositoryPath(repo.repository)}/pulls?state=${input.state === "merged" ? "closed" : input.state}&head=${encodeURIComponent(branch)}&sort=recentupdate&limit=50&page=${page}`,
430 + },
431 + Schema.Array(ForgejoPullRequestSchema),
432 + );
433 + // A deleted head branch keeps its name only in `label`.
434 + if (items.some((item) => item.head.ref !== branch && item.head.label !== branch)) {
435 + unfiltered = unfilteredPulls.get(key) ?? {
436 + lock: Semaphore.makeUnsafe(1),
437 + pulls: undefined,
438 + newest: undefined,
439 + scannedAt: 0,
440 + };
441 + unfilteredPulls.set(key, unfiltered);
442 + if (page === 1 && input.state === "all") firstPage = items;
443 + break;
444 + }
445 + results.push(...matching(items));
446 + if (items.length === 0) break;
447 + }
448 + if (!unfiltered) return results.slice(0, limit);
449 + }
450 + const pulls = yield* unfiltered.lock.withPermits(1)(
451 + readUnfilteredPulls(input, repo.repository, unfiltered, firstPage),
452 + );
453 + const candidates = matching(pulls.values()).toSorted(
454 + (left, right) =>
455 + Option.getOrElse(Option.map(right.updatedAt, DateTime.toEpochMillis), () => 0) -
456 + Option.getOrElse(Option.map(left.updatedAt, DateTime.toEpochMillis), () => 0) ||
457 + right.number - left.number,
458 + );
459 + // A deleted pull request leaves the list without moving anything in it, so a remembered
460 + // match is read again before it is returned.
461 + const results: ReturnType<typeof toForgejoChangeRequest>[] = [];
462 + for (const candidate of candidates) {
463 + if (results.length >= limit) break;
464 + const current = yield* request(
465 + {
466 + ...input,
467 + path: `${repositoryPath(repo.repository)}/pulls/${candidate.number}`,
468 + },
469 + ForgejoPullRequestSchema,
470 + ).pipe(
471 + Effect.catchIf(
472 + (error) => isForgejoCliError(error) && error.httpStatus === 404,
473 + () => Effect.undefined,
474 + ),
475 + );
476 + if (current?.number !== candidate.number) {
477 + pulls.delete(candidate.number);
478 + continue;
479 + }
480 + // Not written back: a refresh may have stored a newer copy while this was read.
481 + results.push(...matching([current]));
482 }
367 - return results.slice(0, limit);
483 + return results;
484 },
485 (effect, input) => effect.pipe(mapError("listChangeRequests", input.cwd)),
486 ),