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

kyks70gy  于 2023-03-16  发布在  Git
关注(0)|答案(9)|浏览(580)

我尝试使用API来计算许多大型githubrepos的提交次数,因此我希望避免获取整个提交列表(例如: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/committs?until = 2013 - 03 - 30 T16:01:43 Z),并使用最早的一个(总是列在最后吗?),或者可能是父提交为空的那个(不确定派生的项目是否有初始父提交)。
有没有更好的方法来获取回购协议的第一个提交哈希值?
更好的是,对于一个简单的统计数据来说,这整件事似乎很复杂,我想知道我是否遗漏了什么。有没有更好的想法来使用API获得repo提交计数?
编辑:这个somewhat similar question试图按某些文件进行过滤(“并在其中过滤到特定文件”),所以答案不同。

xqkwcwgp

xqkwcwgp1#

您可以考虑使用GraphQL API v4来使用别名同时对多个仓库执行提交计数。以下代码将获取3个不同仓库的所有分支的提交计数(每个仓库最多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是一个片段,有助于避免每个存储库的查询字段重复
如果你只需要默认分支上的提交计数,那就更简单了:

{
  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

5m1hhzi4

5m1hhzi42#

如果您要查找默认分支中的提交总数,您可以考虑一种不同的方法。
使用Repo Contributors API获取所有贡献者的列表:
https://developer.github.com/v3/repos/#list-contributors
列表中的每一项都包含一个contributions字段,它告诉你用户在默认分支中提交了多少次,把所有参与者的字段相加,你就得到了默认分支中提交的总数。
贡献者列表通常比提交列表短得多,所以计算默认分支中的提交总数需要更少的请求。

2vuwiymt

2vuwiymt3#

简单解决方案:看看页码,Github会为你分页,所以你可以很容易地计算出提交次数,只需从Link头中获取最后一页的页码,减去1(你需要手动将最后一页相加),乘以页面大小,获取结果的最后一页,获取数组的大小,然后将两个数字相加。
下面是我在ruby中使用octokitgem获取整个组织提交总数的实现:

@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

是的,我知道代码是脏的,这是几分钟内刚刚拼凑起来的。

ercv8c1e

ercv8c1e4#

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

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
8oomwypt

8oomwypt5#

https://api.github.com/repos/{username}/{repo}/commits?sha={branch}&per_page=1&page=1上提出请求
现在只需要获取响应头的Link参数,并获取位于rel="last"之前的页面计数
这个页面计数等于该分支中的提交总数!
诀窍是使用**&per_page=1&page=1**。它在1页中分配1次提交。因此,提交的总数将等于总页数。

sg3maiej

sg3maiej6#

我只是做了一个小脚本来实现这个功能。它可能不适用于大型仓库,因为它不处理GitHub的速率限制。而且它需要Python requests包。

#!/bin/env python3.4
import requests

GITHUB_API_BRANCHES = 'https://%(token)s@api.github.com/repos/%(namespace)s/%(repository)s/branches'
GUTHUB_API_COMMITS = 'https://%(token)s@api.github.com/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')
ds97pgxw

ds97pgxw7#

下面是一个基于snowe方法使用Fetch的JavaScript示例

获取示例

/**
 * @param {string} owner Owner of repo
 * @param {string} repo Name of repo
 * @returns {number} Number of total commits the repo contains on main master branch
 */
export const getTotalCommits = (owner, repo) => {
  let url = `https://api.github.com/repos/${owner}/${repo}/commits?per_page=100`;
  let pages = 0;

  return fetch(url, {
    headers: {
      Accept: "application/vnd.github.v3+json",
    },
  })
    .then((data) => data.headers)
    .then(
      (result) =>
        result
          .get("link")
          .split(",")[1]
          .match(/.*page=(?<page_num>\d+)/).groups.page_num
    )
    .then((numberOfPages) => {
      pages = numberOfPages;
      return fetch(url + `&page=${numberOfPages}`, {
        headers: {
          Accept: "application/vnd.github.v3+json",
        },
      }).then((data) => data.json());
    })
    .then((data) => {
      return data.length + (pages - 1) * 100;
    })
    .catch((err) => {
      console.log(`ERROR: calling: ${url}`);
      console.log("See below for more info:");
      console.log(err);
    });
};

用法

getTotalCommits('facebook', 'react').then(commits => {
    console.log(commits);
});
bvuwiixz

bvuwiixz8#

我用python创建了一个生成器,它会返回一个贡献者列表,计算总提交次数,然后检查它是否有效。如果提交次数少于True,则返回False。你唯一需要填写的是使用你的凭据的requests会话。下面是我为你写的内容:

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()
0vvn1miw

0vvn1miw9#

与Github企业版合作:

gh api https://github.myenterprise.com/api/v3/repos/myorg/myrepo/commits --paginate | jq length | datamash sum 1

如果您是Unix管道的拥护者,您可以将其与存储库列表合并,以获取组织中的所有提交。

设置注解

对于Mac OS:

brew install gh
brew install datamash
gh auth login

相关问题