Skip to content

fix: stop Gitea changed-files pagination at the advertised total - #4306

Merged
IsmaelMartinez merged 1 commit into
The-PR-Agent:mainfrom
fixfon:fix/4305
Oct 23, 2026
Merged

IsmaelMartinez merged 1 commit into
The-PR-Agent:mainfrom
fixfon:fix/4305

Conversation

@fixfon

@fixfon fixfon commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

User description

Summary

RepoApi.get_change_file_pull_request() only stopped at an empty page, so it always asked for one page past the last. Forgejo answers that extra page with HTTP 200 and a literal null. The changed-files validation from #3857 rejects null, so /review failed on a complete file list.

The loop now reads X-Total-Count from the response headers and stops once that many files have been collected. The lookup ignores header case and works with both a plain dict and urllib3's HTTPHeaderDict, which is what giteapy returns with _preload_content=False.

The #3857 protection still holds:

  • A failed request, a null or malformed page that arrives before the advertised total is reached, and a malformed entry all still raise.
  • A collected count that differs from a present X-Total-Count now raises too, whether the walk ends at the total or at an empty page.
  • Responses with no usable X-Total-Count (missing, non-numeric or negative) keep the old empty-page ending.

Fixes #4305

Behavior change

A server that advertises more files than it delivers, or fewer, now stops the review. Before this change it returned the partial list without any error. The issue asks for this ("a count that does not match the advertised total should still stop the review"). The empty-page half of the check is the or not page_items clause, so it is easy to drop if you'd rather not have it.

Tests

All tests are mock-only and added to tests/unittest/test_gitea_provider.py. I have not checked against a real plain Gitea server that this endpoint returns X-Total-Count; the tests use mocks and the Forgejo reproduction from the issue.

  • The reporter's case. A 15-file page followed by a null page now returns the 15 files with one call_api call. This is run against a plain dict, lowercase header names and an HTTPHeaderDict.
  • Count mismatches. A null next page, an empty next page, or more files than advertised all raise.
  • Malformed entries still raise when the total matches.
  • Missing, non-numeric or negative X-Total-Count falls back to the empty-page ending.
Run Result
New tests on main 5 failed, 6 passed. The failures are ValueError: Invalid Gitea changed-files page and DID NOT RAISE for the two mismatch cases.
New tests with the fix 11 passed
tests/unittest/test_gitea_*.py 243 passed
tests/unittest 14244 passed, 33 skipped

ruff check on the changed files reports "All checks passed!", and pre-commit run --files passes on the changed files (with the ruff-check hook skipped, since ruff check was run directly).

Sibling code paths

The other paginated helper in the same provider is _list_all_pages (gitea_provider.py ~1187-1219), which is used for PR commits and review comments. It does not raise on null; it treats null as the end of the list. So it doesn't hit this failure, but it also always makes one trailing request, and a null page in the middle of a walk would cut the list short without an error. That is a different code path, so I left it out of this PR. I'm happy to follow up if you want the same header-based stop there.

No other git provider shares this loop.

I used an AI coding assistant for this change, then reviewed the diff and re-ran the tests myself.


PR Type

Bug fix, Tests


Description

  • Stop Gitea pagination at advertised file totals.

  • Reject mismatched counts and malformed file entries.

  • Preserve fallback pagination for unusable total headers.

  • Add mocked Forgejo pagination regression tests.


Diagram Walkthrough

flowchart LR
  response["Changed-files response"]
  validation["Validate page and file entries"]
  total["Read case-insensitive X-Total-Count"]
  complete["Return files when total matches"]
  mismatch["Reject mismatched counts"]
  fallback["Continue until empty page without usable total"]
  response --> validation
  validation --> total
  total -- "Advertised total reached" --> complete
  total -- "Overflow or premature empty page" --> mismatch
  total -- "Missing or unusable total" --> fallback
Loading

File Walkthrough

Relevant files
Bug fix
gitea_provider.py
Honor advertised totals during changed-files pagination   

pr_agent/git_providers/gitea_provider.py

  • Add _advertised_total() to parse nonnegative X-Total-Count values
    case-insensitively.
  • Stop fetching changed-file pages when the accumulated count matches
    the advertised total.
  • Raise an error for excess files or an empty page before the advertised
    total.
  • Retain empty-page termination when the total header is missing or
    unusable.
+26/-3   
Tests
test_gitea_provider.py
Cover advertised totals and pagination validation regressions

tests/unittest/test_gitea_provider.py

  • Reproduce Forgejo's trailing null response and verify no extra request
    occurs.
  • Cover standard dictionaries, lowercase headers, and HTTPHeaderDict.
  • Verify mismatched counts and malformed file entries raise errors.
  • Test fallback pagination for missing, nonnumeric, and negative totals.
+60/-0   


⚙️ Agent run details
  • Model: openai/gpt-6.1-sol
  • Tokens: 7,158 in / 425 out / 7,583 total
  • Time cost: 10.1s
  • AI calls: 1

get_change_file_pull_request only ended at an empty page, so it always
requested one page past the last. Forgejo answers that page with HTTP 200
and a literal `null`, which the changed-files validation rejects, and
/review stopped on a complete 15-file list.

Read X-Total-Count from the response headers (case-insensitive, plain
dict or urllib3 HTTPHeaderDict) and end the walk once that many files
are collected. A count that differs from the advertised total, a `null`
or malformed page before it is reached, and malformed entries still
raise. Responses without a usable X-Total-Count keep the empty-page
ending.

Fixes The-PR-Agent#4305
@the-pr-agent-bot

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

4305 - Partially compliant

Compliant requirements:

  • Return the complete changed-file list without requesting another page once pagination metadata confirms completion.
  • Continue rejecting failed requests, malformed pages, and malformed file entries.

Non-compliant requirements:

  • Reject collected file counts that do not match the advertised total.
⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Forgotten total

The advertised total is discarded on every subsequent response. If the first page contains 10 files with X-Total-Count: 15, but the next page is empty and omits that header, this returns the incomplete 10-file inventory instead of raising. Retain the previously advertised total when later responses omit or contain an unusable header, and add a regression test for this sequence.

total = self._advertised_total(response)
if total is None:
    if not page_items:
        return files

⚙️ Agent run details
  • Model: openai/gpt-6.1-sol
  • Tokens: 8,463 in / 470 out / 8,933 total
  • Time cost: 11.3s
  • AI calls: 1

@the-pr-agent-bot

Copy link
Copy Markdown

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible bug
Preserve totals across pagination responses

Preserve the advertised total across pages instead of replacing it with each
response's header. If a later response omits X-Total-Count, the current code can
return a partial inventory on an empty page or request past the known total; add a
regression test with a header only on the first page.

pr_agent/git_providers/gitea_provider.py [1265-1272]

+            if page == 1:
+                expected_total = None
             total = self._advertised_total(response)
-            if total is None:
+            if total is not None:
+                if expected_total is not None and total != expected_total:
+                    raise ValueError("Gitea changed-files X-Total-Count changed during pagination")
+                expected_total = total
+            if expected_total is None:
                 if not page_items:
                     return files
-            elif len(files) >= total or not page_items:
-                if len(files) != total:
+            elif len(files) >= expected_total or not page_items:
+                if len(files) != expected_total:
                     raise ValueError("Gitea changed-files count does not match X-Total-Count")
                 return files
Suggestion importance[1-10]: 7

__

Why: The current code forgets an earlier advertised total when a later response omits the header, potentially returning a partial inventory or requesting beyond the known total. The proposed change correctly preserves expected_total across pages and rejects conflicting advertised counts.

Medium

⚙️ Agent run details
  • Model: openai/gpt-6.1-sol
  • Tokens: 10,078 in / 1,000 out / 11,078 total
  • Time cost: 26.7s
  • AI calls: 2

@IsmaelMartinez IsmaelMartinez left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking up #4305 so quickly, approving and merging.

@IsmaelMartinez
IsmaelMartinez merged commit ab90e6e into The-PR-Agent:main Oct 23, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Forgejo reviews fail when an extra changed-files page returns null

2 participants