如何使用GitHub V3 API来获取repo的提交次数?

问题描述 投票:13回答:6

我正在尝试使用API​​计算许多大型github存储库的提交,所以我想避免获取整个提交列表(这种方式作为示例:api.github.com/repos/jasonrudolph/keyboard/commits)和计数他们。

如果我有第一个(初始)提交的哈希,我可以use this technique to compare the first commit to the latest,并愉快地报告中间的total_commits(所以我需要添加一个)这样。不幸的是,我看不出如何使用API​​优雅地获得第一次提交。

基本的repo URL确实给了我created_at(这个url是一个例子:api.github.com/repos/jasonrudolph/keyboard),所以我可以通过限制提交到创建日期来获得减少的提交集(这个url是一个例子:api.github.com/repos/jasonrudolph/keyboard/commits?until=2013-03-30T16:01:43Z)并使用最早的一个(总是列在最后?)或者可能是空父母的一个(不确定分叉项目是否有初始父提交)。

获得repo的第一个提交哈希的更好方法是什么?

更好的是,对于一个简单的统计来说,整个事情似乎很复杂,我想知道我是否遗漏了一些东西。使用API​​获取repo提交计数的任何更好的想法?

编辑:这个somewhat similar question试图过滤某些文件(“并在其中的特定文件。”),所以有一个不同的答案。

git github github-api
6个回答
5
投票

您可以考虑使用GraphQL API v4同时使用aliases对多个存储库执行提交计数。以下内容将获取3个不同存储库的所有分支的提交计数(每个repo最多100个分支):

{
  gson: repository(owner: "google", name: "gson") {
    ...RepoFragment
  }
  martian: repository(owner: "google", name: "martian") {
    ...RepoFragment
  }
  keyboard: repository(owner: "jasonrudolph", name: "keyboard") {
    ...RepoFragment
  }
}

fragment RepoFragment on Repository {
  name
  refs(first: 100, refPrefix: "refs/heads/") {
    edges {
      node {
        name
        target {
          ... on Commit {
            id
            history(first: 0) {
              totalCount
            }
          }
        }
      }
    }
  }
}

Try it in the explorer

RepoFragment是一个fragment,它有助于避免每个repo的重复查询字段

如果您只需要在默认分支上提交计数,那么它更直接:

{
  gson: repository(owner: "google", name: "gson") {
    ...RepoFragment
  }
  martian: repository(owner: "google", name: "martian") {
    ...RepoFragment
  }
  keyboard: repository(owner: "jasonrudolph", name: "keyboard") {
    ...RepoFragment
  }
}

fragment RepoFragment on Repository {
  name
  defaultBranchRef {
    name
    target {
      ... on Commit {
        id
        history(first: 0) {
          totalCount
        }
      }
    }
  }
}

Try it in the explorer


8
投票

如果您要查找默认分支中的提交总数,可以考虑采用不同的方法。

使用Repo Contributors API获取所有贡献者的列表:

https://developer.github.com/v3/repos/#list-contributors

列表中的每个项目都包含一个contributions字段,该字段告诉您用户在默认分支中创建了多少次提交。在所有贡献者中对这些字段求和,您应该获得默认分支中的提交总数。

贡献者列表通常比提交列表短得多,因此应该花费更少的请求来计算默认分支中的提交总数。


3
投票

我只是制作了一个小脚本来做到这一点。它可能不适用于大型存储库,因为它不处理GitHub的速率限制。它还需要Python requests包。

#!/bin/env python3.4
import requests

GITHUB_API_BRANCHES = 'https://%(token)[email protected]/repos/%(namespace)s/%(repository)s/branches'
GUTHUB_API_COMMITS = 'https://%(token)[email protected]/repos/%(namespace)s/%(repository)s/commits?sha=%(sha)s&page=%(page)i'


def github_commit_counter(namespace, repository, access_token=''):
    commit_store = list()

    branches = requests.get(GITHUB_API_BRANCHES % {
        'token': access_token,
        'namespace': namespace,
        'repository': repository,
    }).json()

    print('Branch'.ljust(47), 'Commits')
    print('-' * 55)

    for branch in branches:
        page = 1
        branch_commits = 0

        while True:
            commits = requests.get(GUTHUB_API_COMMITS % {
                'token': access_token,
                'namespace': namespace,
                'repository': repository,
                'sha': branch['name'],
                'page': page
            }).json()

            page_commits = len(commits)

            for commit in commits:
                commit_store.append(commit['sha'])

            branch_commits += page_commits

            if page_commits == 0:
                break

            page += 1

        print(branch['name'].ljust(45), str(branch_commits).rjust(9))

    commit_store = set(commit_store)
    print('-' * 55)
    print('Total'.ljust(42), str(len(commit_store)).rjust(12))

# for private repositories, get your own token from
# https://github.com/settings/tokens
# github_commit_counter('github', 'gitignore', access_token='fnkr:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
github_commit_counter('github', 'gitignore')

2
投票

简单的解决方案:查看页码。 Github为你分页。所以你可以通过从Link标题中获取最后一个页码,减去一个(你需要手动添加最后一页),乘以页面大小,抓住结果的最后一页,然后轻松计算提交次数。获取该数组的大小并将两个数字加在一起。这是最多两个API调用!

这是我使用ruby中的octokit gem获取整个组织的提交总数的实现:

@github = Octokit::Client.new access_token: key, auto_traversal: true, per_page: 100

Octokit.auto_paginate = true
repos = @github.org_repos('my_company', per_page: 100)

# * take the pagination number
# * get the last page
# * see how many items are on it
# * multiply the number of pages - 1 by the page size
# * and add the two together. Boom. Commit count in 2 api calls
def calc_total_commits(repos)
    total_sum_commits = 0

    repos.each do |e| 
        repo = Octokit::Repository.from_url(e.url)
        number_of_commits_in_first_page = @github.commits(repo).size
        repo_sum = 0
        if number_of_commits_in_first_page >= 100
            links = @github.last_response.rels

            unless links.empty?
                last_page_url = links[:last].href

                /.*page=(?<page_num>\d+)/ =~ last_page_url
                repo_sum += (page_num.to_i - 1) * 100 # we add the last page manually
                repo_sum += links[:last].get.data.size
            end
        else
            repo_sum += number_of_commits_in_first_page
        end
        puts "Commits for #{e.name} : #{repo_sum}"
        total_sum_commits += repo_sum
    end
    puts "TOTAL COMMITS #{total_sum_commits}"
end

是的,我知道代码是脏的,这只是在几分钟内被抛出。


2
投票

如果你是在一个新项目中开始使用GraphQL API v4可能是处理这个问题的方法,但如果你仍在使用REST API v3,你可以通过将请求限制为每个结果只有1个来解决分页问题。页。通过设置该限制,在最后一个链接中返回的pages的数量将等于总数。

例如,使用python3和请求库

def commit_count(project, sha='master', token=None):
    """
    Return the number of commits to a project
    """
    token = token or os.environ.get('GITHUB_API_TOKEN')
    url = f'https://api.github.com/repos/{project}/commits'
    headers = {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'Authorization': f'token {token}',
    }
    params = {
        'sha': sha,
        'per_page': 1,
    }
    resp = requests.request('GET', url, params=params, headers=headers)
    if (resp.status_code // 100) != 2:
        raise Exception(f'invalid github response: {resp.content}')
    # check the resp count, just in case there are 0 commits
    commit_count = len(resp.json())
    last_page = resp.links.get('last')
    # if there are no more pages, the count must be 0 or 1
    if last_page:
        # extract the query string from the last page url
        qs = urllib.parse.urlparse(last_page['url']).query
        # extract the page number from the query string
        commit_count = int(dict(urllib.parse.parse_qsl(qs))['page'])
    return commit_count

1
投票

我使用python创建了一个生成器,它返回一个贡献者列表,总计提交总数,然后检查它是否有效。如果qzxswpoi较少则返回True,如果相同或更大提交则返回False。您必须填写的唯一内容是使用您的凭据的请求会话。这是我为你写的:

from requests import session
def login()
    sess = session()

    # login here and return session with valid creds
    return sess

def generateList(link):
    # you need to login before you do anything
    sess = login()

    # because of the way that requests works, you must start out by creating an object to
    # imitate the response object. This will help you to cleanly while-loop through
    # github's pagination
    class response_immitator:
        links = {'next': {'url':link}}
    response = response_immitator() 
    while 'next' in response.links:
        response = sess.get(response.links['next']['url'])
        for repo in response.json():
            yield repo

def check_commit_count(baseurl, user_name, repo_name, max_commit_count=None):
    # login first
    sess = login()
    if max_commit_count != None:
        totalcommits = 0

        # construct url to paginate
        url = baseurl+"repos/" + user_name + '/' + repo_name + "/stats/contributors"
        for stats in generateList(url):
            totalcommits+=stats['total']

        if totalcommits >= max_commit_count:
            return False
        else:
            return True

def main():
    # what user do you want to check for commits
    user_name = "arcsector"

    # what repo do you want to check for commits
    repo_name = "EyeWitness"

    # github's base api url
    baseurl = "https://api.github.com/"

    # call function
    check_commit_count(baseurl, user_name, repo_name, 30)

if __name__ == "__main__":
    main()
© www.soinside.com 2019 - 2024. All rights reserved.