Github API-检索用户提交? [英] Github API - retrieve user commits?

查看:132
本文介绍了Github API-检索用户提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一种方法,在该方法中,我可以访问Github用户名,并从该用户发布所有提交或至少一些提交.

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.

是否有对GET用户/回购/提交关联或直接用户/提交的呼叫?

Is there a call to GET user/repo/commit association or a direct user/commit?

现在,我认为需要采取以下措施:

Right now, I think what it will take is the following:

  1. 获取与特定名称关联的存储库: api.github.com/users/:name/repos.

  1. Obtain repos associated with a particular name: api.github.com/users/:name/repos.

从提要中获取回购名称.

From feed obtain repo name.

将回购名称放置在数组中,例如:

Place repo names in an array, such as:

    api.github.com/repos/:user/:repo1/commits
    api.github.com/repos/:user/:repo2/commits
    api.github.com/repos/:user/:repo3/commits

  1. 从提要中获得性腺计数吗?

推荐答案

在用户存储库中进行迭代是次优的,因为它会丢失他们在其他存储库中所做的任何提交.更好的方法是改用事件API .

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.

第一步是获取用户事件:

GET /users/:username/events

接下来,您需要遍历返回的事件,检查项目其中result.type设置为PushEvent .这些中的每一个都对应于用户的git push,并且来自该推送的提交可以(按时间顺序)作为result.payload.commits.

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.

通过检查commit.author.email与您期望的内容是否匹配,可以过滤那些内容以忽略其他用户的任何提交.您还可以访问该对象上的shamessageurl之类的属性,并且可以使用distinct属性来消除多次推送中的重复提交.

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.

如果有帮助,请参考以下示例代码(取自我的网站),该代码使用上述方法为用户获取最后一次提交(使用Node.js和 octokat npm模块):

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
  })

这篇关于Github API-检索用户提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆