GeezerVison is showing github.com — fix(forgejo): finding a branch's pull requests no longer walks every … · pingdotgg/t3code@a17ec50
File treeExpand file treeCollapse file tree Open diff view settings packages/source-control-forgejo/src/server
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/source-control-forgejo/src/server/ForgejoSourceControlProvider.test.ts +79Lines changed: 79 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -908,6 +908,85 @@ it.effect("names the web server behind an SSH alias from the login that serves i | 908 | 908 | ), | 909 | 909 | ); | 910 | 910 |
| | 911 | +it.effect("finds a branch's pull requests in a bounded number of Forgejo requests", () => | | 912 | + Effect.gen(function* () { | | 913 | + const paths: string[] = []; | | 914 | + const pull = (number: number, ref: string, state = "open") => ({ | | 915 | + number, | | 916 | + title: `PR ${number}`, | | 917 | + html_url: `https://forgejo.test/maria/project/pulls/${number}`, | | 918 | + state, | | 919 | + merged: state === "closed", | | 920 | + base: { ref: "main", sha: "base", repo: null }, | | 921 | + head: { ref, sha: "head", repo: null }, | | 922 | + }); | | 923 | + const provider = yield* ForgejoSourceControlProvider.make.pipe( | | 924 | + Effect.provide( | | 925 | + Layer.mergeAll( | | 926 | + Layer.succeed(FileSystem.FileSystem, FileSystem.makeNoop({})), | | 927 | + TestSourceControlHost.layer({ process: {} }), | | 928 | + Layer.mock(ForgejoCli.ForgejoCli)({ | | 929 | + resolveRepository: () => | | 930 | + Effect.succeed({ | | 931 | + login: "work", | | 932 | + repository: "maria/project", | | 933 | + baseUrl: "https://forgejo.test", | | 934 | + }), | | 935 | + api: (input) => { | | 936 | + paths.push(input.path); | | 937 | + const page = Number(/[?&]page=(\d+)/.exec(input.path)?.[1] ?? 0); | | 938 | + const body = input.path.includes("/pulls?") | | 939 | + ? Array.from({ length: page > 60 ? 0 : 50 }, (_, index) => | | 940 | + pull(page * 100 + index, "other"), | | 941 | + ) | | 942 | + : input.path === "repos/maria/project" | | 943 | + ? { | | 944 | + full_name: "maria/project", | | 945 | + clone_url: "", | | 946 | + ssh_url: "", | | 947 | + default_branch: "main", | | 948 | + } | | 949 | + : input.path === "repos/maria/project/pulls/main/old-feature" | | 950 | + ? pull(7, "old-feature", "closed") | | 951 | + : undefined; | | 952 | + return body === undefined | | 953 | + ? Effect.fail( | | 954 | + new ForgejoCli.ForgejoCliError({ | | 955 | + command: "tea", | | 956 | + cwd: "/repo", | | 957 | + detail: "not found", | | 958 | + reason: "not-found", | | 959 | + }), | | 960 | + ) | | 961 | + : encodeJsonEffect(body).pipe(Effect.orDie, Effect.map(processOutput)); | | 962 | + }, | | 963 | + }), | | 964 | + ), | | 965 | + ), | | 966 | + ); | | 967 | + | | 968 | + const none = yield* provider.listChangeRequests({ | | 969 | + cwd: "/repo", | | 970 | + headSelector: "no-pull-request", | | 971 | + state: "all", | | 972 | + }); | | 973 | + assert.deepStrictEqual(none, []); | | 974 | + assert.strictEqual(paths.length, 6); | | 975 | + | | 976 | + paths.length = 0; | | 977 | + const old = yield* provider.listChangeRequests({ | | 978 | + cwd: "/repo", | | 979 | + headSelector: "old-feature", | | 980 | + state: "all", | | 981 | + }); | | 982 | + assert.deepStrictEqual( | | 983 | + old.map((pr) => [pr.number, pr.state]), | | 984 | + [[7, "merged"]], | | 985 | + ); | | 986 | + assert.strictEqual(paths.at(-1), "repos/maria/project/pulls/main/old-feature"); | | 987 | + }), | | 988 | +); | | 989 | + | 911 | 990 | it.effect("loads Forgejo pull request references from files and commits views", () => | 912 | 991 | Effect.gen(function* () { | 913 | 992 | const provider = yield* ForgejoSourceControlProvider.make; | |
Collapse file Copy file name to clipboardExpand all lines: packages/source-control-forgejo/src/server/ForgejoSourceControlProvider.ts +43-11Lines changed: 43 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -265,6 +265,8 @@ const cloneUrls = (raw: typeof RepositorySchema.Type) => ({ | 265 | 265 | url: raw.clone_url, | 266 | 266 | sshUrl: raw.ssh_url, | 267 | 267 | }); | | 268 | +// 200 most recently updated pull requests per lookup; every page is one request on each poll. | | 269 | +const MAX_PULL_LIST_PAGES = 4; | 268 | 270 | const repositoryPath = (repository: string) => | 269 | 271 | `repos/${repository.split("/").map(encodeURIComponent).join("/")}`; | 270 | 272 |
| @@ -334,25 +336,55 @@ export const make = Effect.gen(function* () { | 334 | 336 | const branch = SourceControlProvider.sourceBranch(input); | 335 | 337 | const results: ReturnType<typeof toForgejoChangeRequest>[] = []; | 336 | 338 | const limit = input.limit ?? 20; | 337 | | - for (let page = 1; results.length < limit; page++) { | | 339 | + const collect = (item: typeof ForgejoPullRequestSchema.Type) => { | | 340 | + if ( | | 341 | + item.head.ref !== branch || | | 342 | + (source?.repository && item.head.repo?.full_name !== source.repository) || | | 343 | + (source?.owner && item.head.repo?.owner.login !== source.owner) || | | 344 | + results.some((known) => known.number === item.number) | | 345 | + ) | | 346 | + return; | | 347 | + const normalized = toForgejoChangeRequest(item); | | 348 | + if (input.state === "all" || normalized.state === input.state) results.push(normalized); | | 349 | + }; | | 350 | + // Gitea ignores `head`, so this pages through every pull request of the repository. | | 351 | + let exhausted = false; | | 352 | + for (let page = 1; results.length < limit && page <= MAX_PULL_LIST_PAGES; page++) { | 338 | 353 | const items = yield* request( | 339 | 354 | { | 340 | 355 | ...input, | 341 | 356 | path: `${repositoryPath(repo.repository)}/pulls?state=${input.state === "merged" ? "closed" : input.state}&head=${encodeURIComponent(branch)}&sort=recentupdate&limit=50&page=${page}`, | 342 | 357 | }, | 343 | 358 | Schema.Array(ForgejoPullRequestSchema), | 344 | 359 | ); | 345 | | - for (const item of items) { | 346 | | - if ( | 347 | | - item.head.ref !== branch || | 348 | | - (source?.repository && item.head.repo?.full_name !== source.repository) || | 349 | | - (source?.owner && item.head.repo?.owner.login !== source.owner) | 350 | | - ) | 351 | | - continue; | 352 | | - const normalized = toForgejoChangeRequest(item); | 353 | | - if (input.state === "all" || normalized.state === input.state) results.push(normalized); | | 360 | + for (const item of items) collect(item); | | 361 | + if (items.length === 0) { | | 362 | + exhausted = true; | | 363 | + break; | 354 | 364 | } | 355 | | - if (items.length === 0) break; | | 365 | + } | | 366 | + // Past the scanned window, still find the branch's latest pull request into the default branch. | | 367 | + if (!exhausted && results.length < limit) { | | 368 | + const { default_branch } = yield* request( | | 369 | + { ...input, path: repositoryPath(repo.repository) }, | | 370 | + RepositorySchema, | | 371 | + ); | | 372 | + const owner = source?.owner ?? source?.repository?.split("/")[0]; | | 373 | + const direct = | | 374 | + default_branch && | | 375 | + (yield* request( | | 376 | + { | | 377 | + ...input, | | 378 | + path: `${repositoryPath(repo.repository)}/pulls/${encodeURIComponent(default_branch)}/${encodeURIComponent(owner ? `${owner}:${branch}` : branch)}`, | | 379 | + }, | | 380 | + ForgejoPullRequestSchema, | | 381 | + ).pipe( | | 382 | + Effect.catchIf( | | 383 | + (cause) => isForgejoCliError(cause) && cause.reason === "not-found", | | 384 | + () => Effect.succeed(null), | | 385 | + ), | | 386 | + )); | | 387 | + if (direct) collect(direct); | 356 | 388 | } | 357 | 389 | return results.slice(0, limit); | 358 | 390 | }, | |
|