Github API - 检索用户提交?

时间:2014-02-19 02:27:53

标签: api github

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

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

现在,我认为它将采取以下措施: 1.获取与特定名称相关的回购     api.github.com/users/:name/repos

    来自Feed的
  1. 获取回购名称。

  2. 将repo名称放在数组中,例如:

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

  3. 4.从Feed中获取shas的数量?

5 个答案:

答案 0 :(得分:7)

通过用户的存储库进行迭代是次优的,因为它错过了在其他存储库中进行的任何提交。更好的方法是使用Events API代替。

第一步是get the user's events

GET /users/:username/events

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

您可以通过检查commit.author.email是否符合您的预期来过滤那些忽略其他用户提交的提交。您还可以访问该对象上的shamessageurl等属性,并且可以使用distinct属性消除多次推送中的重复提交。

总的来说,涉及的工作量更多,但它也能让您更准确地表达用户实际承诺的内容。

如果它有帮助,这里有一些example code取自我的网站,它使用上述方法获取用户的最后一次提交(使用Node.js和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
  })

答案 1 :(得分:4)

也许其他人会对此感兴趣。

  1. 没有用于检索一个用户的所有提交的API - >你必须自己做。

  2. 你描述它的方式很好,但你错过了从2和4你将获得所有提交,而不仅仅是那个用户。

  3. Github API允许您通过作者过滤https://developer.github.com/v3/repos/commits/#list-commits-on-a-repository

    获取提交列表

    我的建议是做以下事情:

    1. 检索该用户的存储库,解析JSON响应并获取阵列中存储库的名称。
      API链接 - api.github.com/users/:user/repos;用您想要的用户替换:用户

    2. 对于每个存储库,获取该用户创作的提交列表。

      API链接 - api.github.com/repos/:user/repositoryNameFromArray/commits?author=:user;用你想要的用户替换:user,repositoryNameFromArray应该来自你的数组。

    3. 请注意,Github默认只检索最后30次提交。你需要使用分页来获得更大的块,最多100个。

      你已经完成了。其余的由您和您想要对数据做什么。

答案 2 :(得分:2)

更新2018-11-12

下面提到的网址现已转移到一个类似https://github.com/AurelienLourot?from=2018-10-09的网址,但这个想法保持不变。请参阅github-contribs

正如其他人所指出的那样,官方API不允许您获得所有 GitHub repos,用户自开始时就已经贡献了

仍然可以通过查询非官方页面并在循环中解析它们来获取该信息:

(免责声明:我是维护者。)

这正是github-contribs为您所做的事情:

$ 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
...

答案 3 :(得分:1)

2019年5月更新

您可以通过遍历存储库并使用Contributors API来获得提交计数。这比在Events API中解析提交事件更快,更容易。

基本上查询向/users/<username>/repos发送请求的用户存储库 然后遍历存储库名称,向/repos/<username>/<repo_name>/contributors

发出请求

答案 4 :(得分:0)

尝试使用搜索API,然后按作者过滤

https://help.github.com/en/articles/searching-commits#search-by-author-or-committer

记住:搜索允许每分钟30个结果,总共最多1000个结果

此外,使用分页检查所有结果,否则,每页最多30个。