github api:如何有效地查找存储库的提交数量?

时间:2013-04-10 07:27:32

标签: github github-api

我想查找对特定github项目执行的提交数,并在其中查找特定文件。我检查了github api docs,但只发现了一个用于实际返回所有提交的API。这将是非常低效的,因为我必须通过所有提交进行多个api调用以进行分页。

任何人都有更好的主意吗?

3 个答案:

答案 0 :(得分:12)

2013年5月更新:请参阅“File CRUD and repository statistics now available in the API

您现在可以Get the last year of commit activity data

GET /repos/:owner/:repo/stats/commit_activity
  

返回按周分组的提交活动的最后一年。 days数组是每天提交的一组,从星期日开始。

不是完全你在寻找什么,但更接近。


原始答案(2010年4月)

不,当前的API不支持“log --all”列出所有分支机构的所有提交。

唯一的选择在“Github API: Retrieve all commits for all branches for a repo”中显示,并列出所有提交的所有页面,分支后的分支

这似乎比其他替代方案更加麻烦,实际上克隆 Github仓库和apply git commands on that local clone! (主要是git shortlog


注意:您还可以查看由python script创建的Arcsector

答案 1 :(得分:1)

使用GraphQL API v4,您可以为每个分支获得每个分支的总提交计数{/ 3}}:

{
  repository(owner: "google", name: "gson") {
    name
    refs(first: 100, refPrefix: "refs/heads/") {
      edges {
        node {
          name
          target {
            ... on Commit {
              id
              history(first: 0) {
                totalCount
              }
            }
          }
        }
      }
    }
  }
}

totalCount

答案 2 :(得分:0)

纯JS实现

const base_url = 'https://api.github.com';

    function httpGet(theUrl, return_headers) {
        var xmlHttp = new XMLHttpRequest();
        xmlHttp.open("GET", theUrl, false); // false for synchronous request
        xmlHttp.send(null);
        if (return_headers) {
            return xmlHttp
        }
        return xmlHttp.responseText;
    }

    function get_all_commits_count(owner, repo, sha) {
        let first_commit = get_first_commit(owner, repo);
        let compare_url = base_url + '/repos/' + owner + '/' + repo + '/compare/' + first_commit + '...' + sha;
        let commit_req = httpGet(compare_url);
        let commit_count = JSON.parse(commit_req)['total_commits'] + 1;
        console.log('Commit Count: ', commit_count);
        return commit_count
    }

    function get_first_commit(owner, repo) {
        let url = base_url + '/repos/' + owner + '/' + repo + '/commits';
        let req = httpGet(url, true);
        let first_commit_hash = '';
        if (req.getResponseHeader('Link')) {
            let page_url = req.getResponseHeader('Link').split(',')[1].split(';')[0].split('<')[1].split('>')[0];
            let req_last_commit = httpGet(page_url);
            let first_commit = JSON.parse(req_last_commit);
            first_commit_hash = first_commit[first_commit.length - 1]['sha']
        } else {
            let first_commit = JSON.parse(req.responseText);
            first_commit_hash = first_commit[first_commit.length - 1]['sha'];
        }
        return first_commit_hash;
    }

    let owner = 'getredash';
    let repo = 'redash';
    let sha = 'master';
    get_all_commits_count(owner, repo, sha);

信用-https://gist.github.com/yershalom/a7c08f9441d1aadb13777bce4c7cdc3b

相关问题