8

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.

2 Answers 2

11

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"

1
  • Is it possible to get the total number of contributors unique to a repository that is forked from a different repository?
    – SanMelkote
    Commented Nov 27, 2022 at 15:06
6

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.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.