Is there a way to efficiently get the amount of all different contributors of a GitHub repository using the GitHub API?
I mean, without necessarily getting all the contributor objects and manually count the total amount.
Yes, the trick is to request the list of contributors with one item per page. But include anon=true as a query param to include anonymous contributors as well
https://api.github.com/repos/:owner/:repo/contributors?per_page=1&anon=true
And in the response header (like it was mentioned in the previous answer), look for the Link property and get the value just before rel="last"
A solution may be using pagination.
When we use pagination, we get some information in the Response Header about the total amount of pages according to how many items per page we are requesting (using the per_page
parameter).
So a trick could be requesting the list of contributors with one item per page:
https://api.github.com/repos/:owner/:repo/contributors?per_page=1
doing this in our Response Header there will be a Link
property with the following content:
Link:https://api.github.com/repositories/ID/contributors?per_page=1&page=2; rel="next", https://api.github.com/repositories/ID/contributors?per_page=1&page=XXXXXXXX; rel="last"
the XXXXXXXX value, just before rel="last"
will be the total amount of pages, but since we are requesting one item per page, it will be also the total amount of contributors.