【问题标题】:How do you get total contributions with Githubs API v4您如何使用 Github API v4 获得总贡献
【发布时间】:2017-11-18 16:16:21
【问题描述】:

我一直在查看 Github V4 API 文档,但似乎找不到查询当年总贡献的方法(如您的 github 个人资料中所示)。是否有人设法使用新 API 从您的个人资料中获取一些统计信息?

我在 github 上使用 graphQL 和个人访问令牌,并设法获得最少的用户配置文件数据;用户名、个人资料名称等。

【问题讨论】:

标签: api github github-api graphql apollo


【解决方案1】:

没有这样的 API。所以有两种方法可以解决。简单的数据抓取用户 url 或遍历每个 repo 用户已经分叉,然后计算贡献。后一个会更费时间。第一个更可靠,因为它由 github 缓存。下面是一个获取相同的python方法

import json
import requests
from bs4 import BeautifulSoup

GITHUB_URL = 'https://github.com/'


def get_contributions(usernames):
    """
    Get a github user's public contributions.
    :param usernames: A string or sequence of github usernames.
    """
    contributions = {'users': [], 'total': 0}

    if isinstance(usernames, str) or isinstance(usernames, unicode):
        usernames = [usernames]

    for username in usernames:
        response = requests.get('{0}{1}'.format(GITHUB_URL, username))

        if not response.ok:
            contributions['users'].append({username: dict(total=0)})
            continue

        bs = BeautifulSoup(response.content, "html.parser")
        total = bs.find('div', {'class': 'js-yearly-contributions'}).findNext('h2')
        contributions['users'].append({username: dict(total=int(total.text.split()[0].replace(',', '')))})
        contributions['total'] += int(total.text.split()[0].replace(',', ''))

    return json.dumps(contributions, indent=4)

PS:取自https://github.com/garnertb/github-contributions

对于后面的方法,有一个 npm 包

https://www.npmjs.com/package/github-user-contributions

但我建议只使用抓取方法

【讨论】:

    【解决方案2】:

    ContributionsCollection 对象提供两个日期之间每种贡献类型的总贡献。

    注意:fromto 最多可以相隔一年,在更长的时间范围内可以提出多个请求。

    query ContributionsView($username: String!, $from: DateTime!, $to: DateTime!) {
      user(login: $username) {
        contributionsCollection(from: $from, to: $to) {
          totalCommitContributions
          totalIssueContributions
          totalPullRequestContributions
          totalPullRequestReviewContributions
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-09-24
      • 2013-08-18
      • 1970-01-01
      • 2020-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-08
      • 2016-02-22
      相关资源
      最近更新 更多