Fix pagination headers for "references a commit is pushed to" API

What does this MR do and why?

Fix offset pagination headers for "references a commit is pushed to" API

The "references a commit is pushed to" API was not returning correct offset pagination headers in the response. The Link header did not contain a "next" entry even when there were more pages available, and x-next-page header was blank. This breaks API pagination because the Link and x-next-page headers are commonly used to determine if there are more pages available. For example, this broke the gitlab gem pagination and caused ChatOps to return wrong results. See gitlab-com/gl-infra/delivery#21314 (comment 2607444902) for more details.

This was because we were providing Kaminari with only enough records to respond to the given request, which resulted in Kaminari behaving as if there were no more pages available.

This MR gives Kaminari one extra record so that it knows there are more records available and accordingly sets the headers. For example, if 100 records were requested, we fetch 101 records, so that there is one more record than required.

When we actually reach the end of the data and there are no more pages, Kaminari will see that and set headers correctly.

In addition, this MR sets without_count: true when calling the paginate method, so that the x-total and x-total-pages headers are not set, and the "last" URL is not added to the Link header. These headers will always have incorrect values since Kaminari does not have all the data.

Since the absence of the API pagination headers was not caught by any tests, there seems to be a test gap where we are not testing for the presence of important headers that should be present on all APIs. This MR adds tests for the specific API being fixed here, but this does not help all the other APIs that might not have tests for these headers.

Headers before

link: <https://test.workspaces.gitlab.dev/api/v4/projects/gitlab-org%2Fgitlab-test/repository/commits/HEAD/refs?id=gitlab-org%2Fgitlab-test&page=1&per_page=5&sha=HEAD&type=all>; rel="prev", <https://test.workspaces.gitlab.dev/api/v4/projects/gitlab-org%2Fgitlab-test/repository/commits/HEAD/refs?id=gitlab-org%2Fgitlab-test&page=1&per_page=5&sha=HEAD&type=all>; rel="first", <https://test.workspaces.gitlab.dev/api/v4/projects/gitlab-org%2Fgitlab-test/repository/commits/HEAD/refs?id=gitlab-org%2Fgitlab-test&page=2&per_page=5&sha=HEAD&type=all>; rel="last"
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-gitlab-meta: {"correlation_id":"01JZJC4QNZM89D0HRAP0DZ7J58","version":"1"}
x-next-page: 
x-page: 2
x-per-page: 5
x-prev-page: 1
x-request-id: 01JZJC4QNZM89D0HRAP0DZ7J58
x-runtime: 0.282828
x-total: 10
x-total-pages: 2

Headers after

link: <https://test.workspaces.gitlab.dev/api/v4/projects/gitlab-org%2Fgitlab-test/repository/commits/HEAD/refs?id=gitlab-org%2Fgitlab-test&page=1&per_page=5&sha=HEAD&type=all>; rel="prev", <https://test.workspaces.gitlab.dev/api/v4/projects/gitlab-org%2Fgitlab-test/repository/commits/HEAD/refs?id=gitlab-org%2Fgitlab-test&page=3&per_page=5&sha=HEAD&type=all>; rel="next", <https://test.workspaces.gitlab.dev/api/v4/projects/gitlab-org%2Fgitlab-test/repository/commits/HEAD/refs?id=gitlab-org%2Fgitlab-test&page=1&per_page=5&sha=HEAD&type=all>; rel="first"
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-gitlab-meta: {"correlation_id":"01JZJCCFM17759HSFB17B2TQ1G","version":"1"}
x-next-page: 3
x-page: 2
x-per-page: 5
x-prev-page: 1
x-request-id: 01JZJCCFM17759HSFB17B2TQ1G
x-runtime: 13.924871

References

Issue: gitlab-com/gl-infra/delivery#21314 (closed)

MR that introduced bug: !194863 (merged)

Screenshots or screen recordings

Before After

How to set up and validate locally

  1. Visit http://gdk.test:3000/api/v4/projects/gitlab-org%2Fgitlab-test/repository/commits/HEAD/refs?per_page=5&page=2
  2. Check that the response headers have the "Next" rel in the Link, and that x-total and x-total-pages headers are not present

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 Reuben Pereira

Merge request reports

Loading