Skip to content

Paginating public GitHub repos identifies the last page

Administrator requested to merge rs/public-repo-pagination into main

Created by: vrto

Problem

Our paging logic thru public repositories in GitHub enterprise doesn't gracefully terminate the pagination on the last page.

Public repositories are paged using since query parameter, which carries the offset where the pagination ought to resume.

The problem is that GitHub API somewhat surprisingly returns an OK (HTTP 200) response when more items are to be served, but NOT-FOUND (HTTP 404) when the provided since value is greater than the last known entry ID.

This error bubbles up to the user interface, which we don't want to.

Solution

Checking for status code (200 vs. 404) might not be enough, as GitHub can serve a 404 response for many different reasons.

The proper way to determine whether we've reached the last page (hence an empty result) is by looking at the Link response header.

It carries a first relationship attribute, and an optional next relationship attribute - only when there's another page to look at. If next is missing, we know we've reached the end.

b083426f23c2470ea44332e7f7f6b0a0

Test plan

  • see TestPublicRepos_PaginationTerminatesGracefully test

Merge request reports

Loading