如何使用GitHub V3 API获取repo的提交数量? [英] How to use GitHub V3 API to get commit count for a repo?

查看:413
本文介绍了如何使用GitHub V3 API获取repo的提交数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用API​​计算许多大型github 回购的提交,所以我想避免获取整个提交列表(以此方式为例:api.github.com/



如果我有第一个(初始)提交的哈希值,我可以使用此技术将第一次提交最新的,它高兴地报告了这两者之间的total_commits(所以我需要添加一个)。不幸的是,我看不到如何优雅地获得使用API​​的第一次提交。



基本repo网址给了我created_at(这个url是一个例子:api.github .com / repos / jasonrudolph / keyboard),所以我可以通过将提交限制为创建日期来减少提交集(此URL为示例:api.github.com/repos/jasonrudolph/keyboard/commits?until = 2013-03-30T16:01:43Z),并使用最早的一个(总是列出最后?)或者也许是一个空的父(不知道如果分叉的项目有初始父提交)。

任何更好的方法来获得第一次提交散列的回购?

更好的是,这整个事情似乎令人费解的简单的统计,和我想知道我是否错过了一些东西。任何更好的想法使用API​​来获得repo提交计数?

编辑:有点类似的问题正在试图通过某些文件进行过滤(并且在它们内部对特定文件进行过滤)。 ),所以有不同的答案。

解决方案

您可以考虑使用 GraphQL API v4 在同一时间使用别名。以下将获取3个不同存储库的所有分支(每个repo最多100个分支)的提交计数:

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



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

在资源管理器中试用



RepoFragment 片段,这有助于避免每个repo的重复查询字段。如果您只需要在默认分支上进行提交计数,那就更直接了。 :

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

RepoFragment在存储库{
名称
defaultBranchRef {
名称
目标{
...在提交{
id
历史记录(第一位:0) {
totalCou nt
}
}
}
}
}

试试它在资源管理器中


I am trying to count commits for many large github repos using the API, so I would like to avoid getting the entire list of commits (this way as an example: api.github.com/repos/jasonrudolph/keyboard/commits ) and counting them.

If I had the hash of the first (initial) commit , I could use this technique to compare the first commit to the latest and it happily reports the total_commits in between (so I'd need to add one) that way. Unfortunately, I cannot see how to elegantly get the first commit using the API.

The base repo URL does give me the created_at (this url is an example: api.github.com/repos/jasonrudolph/keyboard ), so I could get a reduced commit set by limiting the commits to be until the create date (this url is an example: api.github.com/repos/jasonrudolph/keyboard/commits?until=2013-03-30T16:01:43Z) and using the earliest one (always listed last?) or maybe the one with an empty parent (not sure about if forked projects have initial parent commits).

Any better way to get the first commit hash for a repo?

Better yet, this whole thing seems convoluted for a simple statistic, and I wonder if I'm missing something. Any better ideas for using the API to get the repo commit count?

Edit: This somewhat similar question is trying to filter by certain files (" and within them to specific files."), so has a different answer.

解决方案

You can consider using GraphQL API v4 to perform commit count for multiple repositories at the same times using aliases. The following will fetch commit count for all branches of 3 distinct repositories (up to 100 branches per 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
  refs(first: 100, refPrefix: "refs/heads/") {
    edges {
      node {
        name
        target {
          ... on Commit {
            id
            history(first: 0) {
              totalCount
            }
          }
        }
      }
    }
  }
}

Try it in the explorer

RepoFragment is a fragment which helps to avoid the duplicate query fields for each of those repo

If you only need commit count on the default branch, it's more straightforward :

{
  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

这篇关于如何使用GitHub V3 API获取repo的提交数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆