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

标签 git github github-api

我正在尝试使用 API 计算许多大型 github repos 的提交,因此我想避免获取整个提交列表(以这种方式为例: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)并使用最早的一个(总是排在最后?)或者可能是一个空父项(不确定 fork 项目是否有初始父项提交)。

有没有更好的方法来获取存储库的第一个提交哈希值?

更好的是,对于一个简单的统计数据来说,这整件事似乎很复杂,我想知道我是否遗漏了什么。使用 API 获取 repo 提交计数有什么更好的想法吗?

编辑:这个somewhat similar question正在尝试按某些文件进行过滤(“并在其中过滤到特定文件。”),因此有不同的答案。

最佳答案

可以考虑使用GraphQL API v4使用 aliases 同时对多个存储库执行提交计数.以下将获取 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 是一个 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

关于git - 如何使用 GitHub V3 API 获取 repo 的提交计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27931139/

相关文章:

node.js - NPM 会忽略 .gitignore 中列出的文件吗?

git - 将配置参数保存到存储库

javascript - 使用 javascript/node js 获取用户的所有公共(public) github 存储库

github - 如果在 Github 上被提及,则获得一个 slack 通知

reactjs - 将 fetch 与 github api v3 结合使用的正确方法?

github - 在 GitHub 中查看最活跃的 fork 的直观方式

git - 在Drupal环境中: is there a better git deployment than my current one?

git - 我添加了一个名为 "mine"的 Remote ,但我的分支显示在 "origin"下

github - 在其他设备上注销 Github 网站

github - Jenkins ⇔ 多个存储库的 Github-Webhook 设置