GitHub API: Repositories Contributed To

Is there a way to get access to the data in the “Repositories contributed to” module on GitHub profile pages via the GitHub API? Ideally the entire list, not just the top five, which are all you can get on the web apparently.


Solution 1:

With GraphQL API v4, you can now get these contributed repo using :

{
  viewer {
    repositoriesContributedTo(first: 100, contributionTypes: [COMMIT, ISSUE, PULL_REQUEST, REPOSITORY]) {
      totalCount
      nodes {
        nameWithOwner
      }
      pageInfo {
        endCursor
        hasNextPage
      }
    }
  }
}

Try it in the explorer

If you have more than 100 contributed repo (including yours), you will have to go through pagination specifying after: "END_CURSOR_VALUE" in repositoriesContributedTo for the next request.

Solution 2:

Using Google BigQuery with the GitHub Archive, I pulled all the repositories I made a pull request to using:

SELECT repository_url 
FROM [githubarchive:github.timeline]
WHERE payload_pull_request_user_login ='rgbkrk'
GROUP BY repository_url;

You can use similar semantics to pull out just the quantities of repositories you contributed to as well as the languages they were in:

SELECT COUNT(DISTINCT repository_url) AS count_repositories_contributed_to,
       COUNT(DISTINCT repository_language) AS count_languages_in
FROM [githubarchive:github.timeline]
WHERE payload_pull_request_user_login ='rgbkrk';

If you're looking for overall contributions, which includes issues reported use

SELECT COUNT(DISTINCT repository_url) AS count_repositories_contributed_to,
       COUNT(DISTINCT repository_language) AS count_languages_in
FROM [githubarchive:github.timeline]
WHERE actor_attributes_login = 'rgbkrk'
GROUP BY repository_url;

The difference there is actor_attributes_login which comes from the Issue Events API.

You may also want to capture your own repos, which may not have issues or PRs filed by yourself.

Solution 3:

I tried implementing something like this a while ago for a Github summarizer... My steps to get the repositories the user contributed to, which they didn't own, was as follows (going to use my own user as an example):

  • Search for that last 100 closed pull requests the user submitted. Of course you could request the second page if the first page is full to get even older prs

https://api.github.com/search/issues?q=type:pr+state:closed+author:megawac&per_page=100&page=1

  • Next I would request each of these repos contributors. If the user in question is in the contributors list we add the repo to the list. Eg:

https://api.github.com/repos/jashkenas/underscore/contributors

  • We might also try checking all the repos the user is watching. Again we would check each repos repos/:owner/:repo/contributors

https://api.github.com/users/megawac/subscriptions

  • In addition I would iterate all the repos of the organizations the user is in

https://api.github.com/users/megawac/orgs
https://api.github.com/orgs/jsdelivr/repos

  • If the user is listed as a contributor to any of the repos there we add the repo to the list (same step as above)

This misses repos where the user has submitted no pull requests but has been added as a contributor. We can increase our odds of finding these repos by searching for

1) any issue opened (not just closed pull requests)
2) repos the user has starred

Clearly, this requires many more requests than we would like to make but what can you do when they make you fudge features \o/