fix: Use finder page size for keyset next link

Why

With pagination=keyset and per_page above 100, the tags and branches APIs return 100 records and no rel="next" link, so clients stop after the first page without an error. This affects tags today, and branches through the default finder (Gitlab::Git::Finders::BranchesFinder, used when use_new_branches_finder_api is on and there's no search or regex). The same bug affects the commits API behind commits_keyset_pagination, and it blocks that flag's global rollout.

What

GitalyKeysetPager added the next link only when the page held exactly params[:per_page] records. The finders cap the page size at 100, so a larger per_page never matched.

The pager now compares against finder.per_page, the page size the finder actually requested from Gitaly. Each keyset finder exposes per_page publicly. CommitsFinder#limit is renamed to #per_page, with no behaviour change. Comparing against the finder's own value also keeps the tree finder correct, because it does not cap.

This MR adds or changes no SQL. The finders only call Gitaly, so no query plans apply.

How to validate locally

1. Create a test project with more than 100 commits, tags and branches. Pushing to a project that doesn't exist yet creates it as a private project:

git init keyset-validate && cd keyset-validate
for i in $(seq 1 150); do git commit -q --allow-empty -m "Commit $i"; done
for i in $(seq 1 120); do git tag "t$i"; git branch "b$i"; done
git push http://<gitlab-host>/<namespace>/keyset-validate.git HEAD:main --tags 'refs/heads/b*:refs/heads/b*'

2. Enable the flag for that project in a Rails console:

Feature.enable(:commits_keyset_pagination, Project.find_by_full_path('<namespace>/keyset-validate'))

3. Request more than 100 per page and check for a next link:

export TOKEN=<personal access token with read_api>
export API="http://<gitlab-host>/api/v4/projects/<namespace>%2Fkeyset-validate/repository"

curl -s -o /dev/null -D - -H "PRIVATE-TOKEN: $TOKEN" "$API/commits?pagination=keyset&per_page=150" | grep -i '^link:'
curl -s -o /dev/null -D - -H "PRIVATE-TOKEN: $TOKEN" "$API/tags?pagination=keyset&order_by=name&sort=asc&per_page=150" | grep -i '^link:'
curl -s -o /dev/null -D - -H "PRIVATE-TOKEN: $TOKEN" "$API/branches?pagination=keyset&per_page=150" | grep -i '^link:'
On master With this MR
Each request above 100 records and no rel="next" link 100 records and a rel="next" link

4. Follow the link to make sure nothing is lost. For commits, request the URL from the rel="next" link as-is. The second page should hold the remaining 50 commits:

curl -s -H "PRIVATE-TOKEN: $TOKEN" "<rel=next URL>" | jq length   # => 50
Edited by Hunter Stewart

Merge request reports

Loading
Loading