如何获取用户的所有公共github提交

问题描述 投票:28回答:5

无论项目如何,我都想知道是否有一种简单的方法可以为所有公共存储库提供单个用户名的所有提交。

由于我属于多个组织,我正在尝试编译我作为贡献者的项目列表,以及我已接受拉取请求的项目。

到目前为止,我的google-fu和浏览github api docs证明是不够的。

github github-api
5个回答
20
投票

http://zmoazeni.github.com/gitspective/是你的朋友。 :-)过滤除“推”之外的所有内容,你有了自己的观点,尽管没有编码工作,你自己先实现它。

如果您想自己重做工作,检查Chrome Devtools“网络”选项卡的内容可能会帮助您模拟API查询。


11
投票

正确的方法是通过Events API

首先你需要fetch the user's events

GET /users/:username/events

然后,您将需要过滤项目where type is set to PushEvent的响应数组。这些项中的每一个对应于用户的git push。来自该推送的提交在payload.commits数组中以反向时间顺序提供。

下一步是通过检查每个提交对象的author.email属性来过滤掉其他用户提交的提交。您还可以访问同一对象上的shamessageurl等属性,并且可以使用distinct属性消除多次推送中的重复提交。

编辑:正如Adam Taylor在评论中指出的那样,这种做法是错误的。我没能通过RTFM,抱歉。 API允许您获取最多300个事件,事件也仅限于过去90天。为了完整起见,我将在这里留下答案,但是对于提取所有提交的所述问题,它将无效。


4
投票

更新2018-11-12

下面提到的URL现在已经移动到一个看起来像https://github.com/AurelienLourot?from=2018-10-09的URL,但这个想法保持不变。见github-contribs


我想知道是否有一种简单的方法可以为一个用户名获取所有公共存储库的所有提交。

第一个挑战是列出用户曾经贡献的所有回购。正如其他人所指出的那样,官方API将不允许您从一开始就获得此信息。

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

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

这正是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
...

1
投票

我知道这个问题很老了,但我最终编写了自己的解决方案。

最后,解决方案是找到用户使用organization_repositorieslist_repositories服务贡献的所有潜在存储库(我正在使用octokit)。

然后我们在这些存储库中找到所有活动分支(服务branches),并且每个存储库只查找来自用户(服务commits)的提交。

示例代码有点广泛,但可以找到here

OBS: As pointed out, this solution does not consider organizations and repositories where you contributed but are not part of.

0
投票

您可以使用API​​方法获取有关用户的信息:get-a-single-user

之后,您可以找到所有用户存储库,然后使用以下功能提交:

def get_github_email(user_login, user_name, key):
    '''
    :param str user_login: user login for GitHub
    :param str key: your client_id + client_secret from GitHub, 
                string like '&client_id=your_id&client_secret=yoursecret'
    :param str user_name: user GitHub name (could be not equeal to user_login)
    :return: email (str or None) or False
    '''
    url = "https://api.github.com/users/{}/repos?{}".format(user_login, key)
    #get repositories
    reps_req = requests.get(url)

    for i in reps_req.json():
        if "fork" in i:
            # take only repositories created by user not forks
            if i["fork"] == False:
                commits_url = "https://api.github.com/repos/{}/{}/commits?{}".format(user_login, i["name"], key)
                #get commits
                commits_req = requests.get(commits_url)

                for j in commits_req.json():
                    #check if author is user (there may be commits from someone else)
                    if j.get("commit", {}).get("author", {}).get("name") == user_name:
                        return j["commit"]["author"]["email"]
    return False

0
投票

GitGub GraphQL API v4 ContributionsCollection对象提供了两个日期之间按存储库分组的贡献,最多可达100个存储库。 fromto相隔最多一年,因此要获取所有贡献,您需要提出多个请求。

query ContributionsView($username: String!, $from: DateTime!, $to: DateTime!) {
  user(login: $username) {
    contributionsCollection(from: $from, to: $to) {
      commitContributionsByRepository(maxRepositories: 100) {
        repository {
          nameWithOwner
        }
        contributions {
          totalCount
        }
      }
      pullRequestContributionsByRepository(maxRepositories: 100) {
        repository {
          nameWithOwner
        }
        contributions {
          totalCount
        }
      }
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.