worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

Gitea CI status misses a branch's PR when the repository has more than 20 open PRs

Open worktrunk-bot opened this issue 3 weeks ago • 7 comments

In a Gitea or Forgejo repository with more than 20 open PRs, wt list --full and the statusline can miss a branch's PR. The branch then shows its commit status instead of the PR's.

detect_gitea_pr requests repos/{owner}/{repo}/pulls?state=open&limit=20 and matches head.ref/owner on the client side. Gitea's list endpoint has no head-branch filter, so only the first 20 open PRs in the repository are searched. By contrast, the GitHub and GitLab backends filter by head branch on the server (--head / --source-branch), so their 20-result page only holds PRs for that branch name. The MAX_PRS_TO_FETCH limitation note in ci_status/mod.rs describes the GitHub/GitLab case (20 PRs for the same branch name), not this one.

Reached via compute_ci_status → PrStatus::detect → detect_pr_mr → gitea::detect_gitea_pr when forge.platform (or the host) selects Gitea.

Possible approaches, none verified against a live Gitea or Forgejo instance (CI here has neither):

  • GET /repos/{owner}/{repo}/pulls/{base}/{head} returns the PR for one base/head pair. The base would be the default branch, which misses PRs targeting other branches unless the list scan stays as a fallback.
  • Page through pulls?state=open until the branch matches. This costs one tea api call per 20 PRs on busy repositories, on the wt list --full path.
  • Keep the first-page scan and document it as a Gitea limitation.

#4745 corrects the code comment that claimed parity with GitHub's page semantics. The behavior is unchanged.

worktrunk-bot avatar Nov 09 '26 06:11 worktrunk-bot

Holding this for a maintainer decision rather than opening a fix PR. The cause is settled and the code change would be small. What's open is the cost each approach adds to wt list --full and the statusline, and none of them can be checked against a real Gitea or Forgejo server from CI.

  • Paging through pulls?state=open is the only option that finds every PR, and it costs the most on exactly the repositories this issue is about. It stops early once it finds a match, but a branch with no open PR can only be ruled out by reading every page. With 200 open PRs, that is 10 tea api calls for each such branch on every refresh, against 1 today and against the single server-filtered call GitHub and GitLab make.
  • pulls/{base}/{head} is one call, but it needs a base. detect_gitea_pr doesn't have one, and filling in the default branch misses PRs that target any other branch. I also haven't confirmed how that endpoint resolves fork heads, which the current owner match handles.
  • Documenting the first-page limit under MAX_PRS_TO_FETCH keeps the cost at one call. Beyond the comment fix already in #4745, it would mean describing this Gitea-specific case in ci_status/mod.rs and in the docs.

A middle option is to page with a fixed cap, for example 3 pages. That bounds the cost, but it only moves the cutoff rather than removing it.

Once a direction is chosen, the change is testable here. setup_mock_tea_with_ci_data currently registers only the limit=20 first-page path. Adding a second-page response lets an integration test show a PR on page 2 being missed today and found after the fix. Whichever option is picked, it would ship without a check against a live server.

worktrunk-bot avatar Nov 09 '26 06:11 worktrunk-bot

Taking this: paginate the Gitea open-PR list (bounded) until the branch's PR is found, instead of matching only the first page. I'll add a regression test and open a PR against main.

Ethereal49 avatar Nov 10 '26 06:11 Ethereal49

I missed the earlier hold when I claimed this. Standing down until a maintainer picks a direction. If bounded paging is chosen, I can implement the second-page regression via setup_mock_tea_with_ci_data and the paging loop; otherwise I'll leave it to the chosen option.

Ethereal49 avatar Nov 10 '26 06:11 Ethereal49

what do others do / what does gitea recommend? there's no way to single select without knowing the base?

we could just presume the base is the default branch?

max-sixty avatar Nov 10 '26 06:11 max-sixty

I'd avoid presuming the default branch with /pulls/{base}/{head}. Neither Gitea nor Forgejo filters that endpoint by state, and it returns whichever PR matches first. Gitea users report that this is the oldest PR for the pair (go-gitea/gitea#35217). So a reused branch name with an old merged PR would hide the open one. It also misses PRs that target other branches, and an approved Gitea PR removes the endpoint.

Both projects are converging on GitHub's approach instead, which is a head filter on the list endpoint:

  • Forgejo v16.0+ (July 2026) added ?head=<branch> to GET /repos/{owner}/{repo}/pulls (forgejo#12104). It matches head_branch literally, so it takes a bare branch name and returns fork PRs too.
  • Gitea has no head filter in any release yet. go-gitea/gitea#40164 (approved, labelled pr/breaking) adds ?head= in GitHub syntax and removes /pulls/{base}/{head}. There a bare branch means the base repo, and forks need owner:branch.
  • Older servers ignore unknown query parameters, since ListPullRequests only reads the ones it knows. On those servers the request behaves exactly as it does today.

As for what other clients do, tea itself has no lookup by branch. Its interactive tea pr merge picker pages through open PRs and offers a "Load more PRs…" option (pull_merge.go).

The two servers read head differently, so the proposal is:

  • Same-repo branch: add &head=<branch> to the existing request and keep the client-side match. That is exact on Forgejo 16+ and on Gitea once #40164 ships, and it keeps today's first-page behavior elsewhere.
  • Fork branch: keep today's unfiltered scan. owner:branch would match nothing on Forgejo, and a bare branch would match nothing on new Gitea.

It's still one tea api call, and no server does worse than it does now. The Forgejo fork case could become exact later if Forgejo adopts the owner: syntax. I can open that PR, with a mock-tea test for the filtered path, if this direction works for you.

Sources (read from server source, not tested against a live instance)
  • Gitea main: ListPullRequests reads labels, poster, state, sort, milestone, base_branch only. GetPullRequestByBaseHeadInfo in models/issues/pull.go is a sess.Get on base_repo_id/base_branch/head_repo_id/head_branch with no state condition or ordering.
  • Forgejo: head appears in models/issues/pull_list.go at v16.0.0 but not at v15.0.0. The filter is pull_request.head_branch=?.
  • go-gitea/gitea#40164 diff: parsePullRequestHead → GetHeadOwnerAndRepo, where an empty owner resolves to the base repo. Its tests include head=branch2, head=user2:branch2, and head=user2/repo1:branch2.

worktrunk-bot avatar Nov 10 '26 06:11 worktrunk-bot

@worktrunk-bot go ahead with &head=<branch> for same-repo branches. Keep the client-side match, and the unfiltered scan for forks. It stays one call, is exact on Forgejo 16+ and on Gitea once go-gitea/gitea#40164 ships, and does no worse than today on older servers.

This was written by Claude Code on behalf of max-sixty

max-sixty avatar Nov 20 '26 18:11 max-sixty

Opened #4864 with that change. Same-repo branches add &head=<branch> to the existing request, and fork branches keep the unfiltered scan. The client-side match is unchanged. CI and codecov/patch are green. As expected, none of it was run against a live Gitea or Forgejo server.

worktrunk-bot avatar Nov 20 '26 19:11 worktrunk-bot