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

查看:29
本文介绍了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 user/repo/commit 关联或直接用户/提交?

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.

从提要获取存储库名称.

From feed obtain repo name.

将 repo 名称放在一个数组中,例如:

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. 从提要中获取 shas 的数量?

推荐答案

遍历用户的存储库是次优的,因为它会错过他们在其他存储库中所做的任何提交.更好的方法是改用 Events 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天全站免登陆