Return 422 for an invalid page_token in the branches API

What does this MR do and why?

With the use_new_branches_finder_api feature flag on, as on GitLab.com, GET /projects/:id/repository/branches answers 500 when page_token is not a cursor Gitaly accepts, for example a branch name, which is what the parameter's description asked for:

curl -s -o /dev/null -w '%{http_code}\n' "https://gitlab.com/api/v4/projects/gitlab-org%2Fgitlab-test/repository/branches?per_page=2&page_token=master"
# 500

Gitlab::Git::Finders::RefsFinder raises Gitlab::Git::InvalidPageToken for a token Gitaly refuses, and the endpoint does not rescue it.

This MR:

  • answers such a token with 422 Invalid page token: <token>, as the tags API does, instead of 500, and lists the 422 among the endpoint's OpenAPI responses;
  • describes page_token in the endpoint, the OpenAPI document and doc/api/branches.md as the token for the next page of results from the Link header of the previous response, which a client should not create or edit, instead of the name of a branch to start from;
  • adds a history entry for the 422 to doc/api/branches.md.

The finder still passes the token to Gitaly unchanged, so the cursor format stays Gitaly's. What a client sees: a token that is not a valid cursor returns 422 instead of 500, and following the Link header works as before.

References

Related to #607442

It is the flag's cleanup, so this MR does not close it. Agreed in #607442 (comment 4243668633); the scope follows the review on this MR.

!262927 (merged) changes the lines next to these in branches_spec.rb, so whichever MR merges second needs a rebase.

I found this while working on gitlab-mcp-server.

Screenshots or screen recordings

No UI change.

How to set up and validate locally

  1. Keep use_new_branches_finder_api enabled (the default).
  2. Request GET /api/v4/projects/<id>/repository/branches?per_page=2&page_token=does-not-exist. On master it answers 500; on this branch it answers 422 Invalid page token: does-not-exist.
  3. Request ?pagination=keyset&per_page=2 and follow the rel="next" link: the next page works as before.

I ran these steps as the examples in spec/requests/api/branches_spec.rb; the 500 on master is the GitLab.com request above.

Checks I ran

With master's lib/api/branches.rb and this branch's request spec: 288 examples, 1 failure, 8 pending. The failure is the invalid-cursor example, which answers 500 with Gitlab::Git::InvalidPageToken (Invalid page token: unknown).

On this branch, the branches request spec, the finder spec and the keyset pager spec read 374 examples, 0 failures, 8 pending (the pending examples are pending on master too).

gitlab:openapi:v3:generate changes only this route in openapi_v3.yaml (the 422 and the page_token description), apart from the order of one database enum that comes from my local configuration and is left as committed.

RuboCop, the commit linter, Danger's linters, markdownlint, rumdl and Vale pass, on Ruby 3.3. Left to the pipeline: the Ruby 3.4 and FOSS runs.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by José M. Requena Plens

Merge request reports

Loading
Loading