Github API - retrieve user commits?

后端 未结 5 1142
日久生厌
日久生厌 2021-02-13 11:36

I\'m trying build a method in which I can access a Github user name, and publish either all commits or at least a number of commits from that user.

Is there a call to GE

相关标签:
5条回答
  • 2021-02-13 11:56

    UPDATE 2018-11-12

    The URLs mentioned below have now moved to a single URL that looks like https://github.com/AurelienLourot?from=2018-10-09 but the idea remains the same. See github-contribs.


    As pointed out by others, the official API won't allow you to get all GitHub repos a user has contributed to since the beginning of time.

    Still you can get that information by querying unofficial pages and parsing them in a loop:

    • https://github.com/users/AurelienLourot/created_commits?from=2018-05-17&to=2018-05-17
    • https://github.com/users/AurelienLourot/created_repositories?from=2018-05-17&to=2018-05-17
    • https://github.com/users/AurelienLourot/created_pull_requests?from=2018-05-17&to=2018-05-17
    • https://github.com/users/AurelienLourot/created_pull_request_reviews?from=2018-05-17&to=2018-05-17

    (Disclaimer: I'm the maintainer.)

    This is exactly what github-contribs does for you:

    $ sudo npm install -g @ghuser/github-contribs
    $ github-contribs AurelienLourot
    ✔ Fetched first day at GitHub: 2015-04-04.
    ⚠ Be patient. The whole process might take up to an hour... Consider using --since and/or --until
    ✔ Fetched all commits and PRs.
    35 repo(s) found:
    AurelienLourot/lsankidb
    reframejs/reframe
    dracula/gitk
    ...
    
    0 讨论(0)
  • 2021-02-13 12:01

    Iterating through a user's repositories is sub-optimal because it misses any commits they make in other repositories. A better way is to use the Events API instead.

    The first step is to get the user's events:

    GET /users/:username/events
    

    Next you need to iterate through the returned events, checking for items where result.type is set to PushEvent. Each one of these corresponds to a git push by the user and the commits from that push are available (in reverse chronological order) as result.payload.commits.

    You can filter those to ignore any commits made by other users, by checking that commit.author.email matches what you expect. You also have access to properties like sha, message and url on that object, and you can eliminate duplicate commits across multiple pushes using the distinct property.

    Overall there is more legwork involved, but it also gives you a far more accurate representation of what a user has actually committed.

    In case it helps, here's some example code taken from my website, which uses the above approach to fetch the last commit for a user (implemented using Node.js and the octokat npm module):

    const USER = 'TODO: your GitHub user name'
    const EMAIL = 'TODO: your GitHub email address'
    
    const github = require('octokat')({ token: 'TODO: your GitHub API token' })
    
    return github.fromUrl(`https://api.github.com/users/${USER}/events`)
      .fetch()
      .then(events => {
        let lastCommit
    
        events.some(event => {
          return event.type === 'PushEvent' && event.payload.commits.reverse().some(commit => {
            if (commit.author.email === EMAIL) {
              lastCommit = {
                repo: event.repo.name,
                sha: commit.sha,
                time: new Date(event.createdAt),
                message: commit.message,
                url: commit.url
              }
    
              return true
            }
    
            return false
          })
        })
    
        return lastCommit
      })
    
    0 讨论(0)
  • 2021-02-13 12:12

    You can use the commit search API and filter by the author.

    You can search by author or committer using author:USERNAME.

    Here is an example CURL request:

    curl -u <username>:<personal_token> \
        -X GET \
      'https://api.github.com/search/commits?q=author:<username>&sort=author-date&order=desc&page=1' \
      -H 'Accept: application/vnd.github.cloak-preview'
    

    See this article on how to create a personal access token.

    Remember: the search API only allows 30 results/minute for authenticated API calls and 10 for unauthenticated calls, and a maximum of 1000 results overall.

    Furthermore, use pagination in order to check for all the results, otherwise, they will be maximum 30 per page.

    0 讨论(0)
  • 2021-02-13 12:13

    UPDATE MAY 2019

    You can get the commit count by iterating over the repos and using the Contributors API. This is faster and easier than parsing commit events in the Events API.

    basically query the user repos making a get request to /users/<username>/repos and then iterate over the repository names making requests to /repos/<username>/<repo_name>/contributors

    0 讨论(0)
  • 2021-02-13 12:15

    Maybe others are interested in this at later time.

    1. There is no API for retrieving all the commits of one user -> you have to do it yourself.

    2. The way you described it is good, but you missed out that from 2 and 4 you will get all commits, not only of that user.

    Github API allows you to get the list of commits by author filtering https://developer.github.com/v3/repos/commits/#list-commits-on-a-repository

    My suggestion is to do the following:

    1. Retrieve the repositories of that user, parse the JSON response and get the names of the repository in an array.

      • API Link - api.github.com/users/:user/repos
      • Replace :user with your desired user.
    2. For each repository, get the list of commits authored by that user.

      • API Link - api.github.com/repos/:user/{repositoryNameFromArray}/commits?author=:user.
      • Replace :user with your desired user, and {repositoryNameFromArray} should come from your array.

    Here be careful that Github retrieves by default only the last 30 commits. You need to use pagination to get larger chunks, max 100.

    You're done. The rest is up to you and what you want to do with the data.

    0 讨论(0)
提交回复
热议问题