|
1 | | -import { SourceControlProviderKind } from "@t3tools/contracts"; |
| 1 | +import { resolvePullRequestAuthorFilter, SourceControlProviderKind } from "@t3tools/contracts"; |
2 | 2 | import * as Effect from "effect/Effect"; |
3 | 3 | import * as Schema from "effect/Schema"; |
4 | 4 | import * as Result from "effect/Result"; |
@@ -254,13 +254,36 @@ export const make = Effect.gen(function* () { |
254 | 254 | const offset = input.cursor?.delivered ?? 0; |
255 | 255 | const items: ReturnType<typeof forgejoChangeRequest>[] = []; |
256 | 256 | const state = input.state === "merged" ? "closed" : input.state; |
257 | | - const query = { |
258 | | - ...input, |
259 | | - path: `${repoPath(input)}/pulls?state=${state}&sort=recentupdate`, |
260 | | - }; |
| 257 | + const params = new URLSearchParams({ state, sort: "recentupdate" }); |
| 258 | + const filters = input.filters; |
| 259 | + // Several `labels` match a pull request holding any of them but list it once per label it |
| 260 | + // holds, which would throw the row offset off, so one required label is all Forgejo is |
| 261 | + // asked for. The service narrows the rest. |
| 262 | + const single = filters?.labels?.find((group) => group.length === 1)?.[0]; |
| 263 | + if (single !== undefined) { |
| 264 | + const labels = yield* page({ ...input, path: `${repoPath(input)}/labels` }, ForgejoLabel); |
| 265 | + const label = labels.items.find( |
| 266 | + (candidate) => candidate.name.toLowerCase() === single.trim().toLowerCase(), |
| 267 | + ); |
| 268 | + if (label !== undefined) params.set("labels", String(label.id)); |
| 269 | + } |
| 270 | + if (filters?.author !== undefined) |
| 271 | + params.set("poster", resolvePullRequestAuthorFilter(filters.author, input.viewer)); |
| 272 | + const query = { ...input, path: `${repoPath(input)}/pulls?${params}` }; |
261 | 273 | // Self-hosted servers may cap pages below 50. Establish their actual page size before |
262 | 274 | // translating the service's row offset into an API page number. |
263 | | - const first = yield* readPage(query, Schema.NullOr(ForgejoPullRequest), 1); |
| 275 | + const first = yield* readPage(query, Schema.NullOr(ForgejoPullRequest), 1).pipe( |
| 276 | + Effect.catchTags({ |
| 277 | + // Forgejo answers a `poster` naming no account with a 400: nobody to list for. |
| 278 | + PullRequestProviderError: (error) => |
| 279 | + params.has("poster") && |
| 280 | + isForgejoCliError(error.cause) && |
| 281 | + error.cause.httpStatus === 400 |
| 282 | + ? Effect.succeed(null) |
| 283 | + : Effect.fail(error), |
| 284 | + }), |
| 285 | + ); |
| 286 | + if (first === null) return { items: [], truncated: false, continues: false }; |
264 | 287 | const pageSize = first.rows.length || 50; |
265 | 288 | const firstIndex = Math.floor(offset / pageSize) + 1; |
266 | 289 | let consumed = 0; |
|
0 commit comments