繁体   English   中英

使用 GitHub GraphQL API v4 查询单个存储库中的所有提交

[英]Querying all commits in a single repository with the GitHub GraphQL API v4

我正在尝试通过 GitHub 的 GraphQL API v4 查询对 GitHub 上指定存储库的所有提交。

我只想提取他们提交的日期,以估计贡献给该存储库的总时间(类似于git-hours

这是我的初始查询:(注意:您可以尝试在Explorer 中运行它)

{
  repository(owner: "facebook", name: "react") {
    object(expression: "master") {
      ... on Commit {
        history {
          nodes {
            committedDate
          }
        }
      }
    }
  }
}

不幸的是,由于 API 的资源限制,它只返回最新的 100 次提交:

节点限制

要通过模式验证,所有 GraphQL API v4 调用都必须满足以下标准:

  • 客户端必须在任何连接上提供第一个或最后一个参数。
  • first 和 last 的值必须在 1-100 之间。
  • 单个调用不能请求超过 500,000 个节点。

因此,由于我没有提供firstlast参数,API 假定我正在查询history(first: 100) 而且我不能在单个连接中查询超过 100 个节点。

但是,总节点限制要高得多(500,000),我应该能够以 100 个为一组查询提交,直到我拥有所有提交。

我能够使用此查询查询最新的 200 次提交:

{
  repository(owner: "facebook", name: "react") {
    object(expression: "master") {
      ... on Commit {
        total: history {
          totalCount
        }
        first100: history(first: 100) {
          edges {
            cursor
            node {
              committedDate
            }
          }
        }
        second100: history(after: "700f17be6752a13a8ead86458e343d2d637ee3ee 99") {
          edges {
            cursor
            node {
              committedDate
            }
          }
        }
      }
    }
  }
}

但是,我必须手动输入我在第二个连接中传递的游标字符串: second100: history(after: "cursor-string") {}

我怎么可以递归运行这个连接,直到我有所有的查询committedDate在仓库提交第?

尽管可能有一种递归查询存储库上所有提交的方法,但我找不到可行的解决方案。

这是我的解决方案

我的需要是:

我只想提取他们提交的日期,以估计贡献给该存储库的总时间(类似于 git-hours)

由于我无法查询完整的提交历史,我不得不假设最近 100 次提交的贡献时间与任何 100 次提交的时间相同。

从 GitHub GraphQL API 查询数据

  • 提交历史的totalCount
  • committedDate最新的100所提交的
{
  repository(owner: "facebook", name: "react") {
    object(expression: "master") {
      ... on Commit {
        history {
          totalCount
          nodes {
            committedDate
          }
        }
      }
    }
  }
}

今天运行,查询返回:

{
  "data": {
    "repository": {
      "object": {
        "history": {
          "totalCount": 10807,
          "nodes": [
            {
              "committedDate": "2019-04-04T01:15:33Z"
            },
            {
              "committedDate": "2019-04-03T22:07:09Z"
            },
            {
              "committedDate": "2019-04-03T20:21:27Z"
            },
            // 97 other committed dates
          ]
        }
      }
    }
  }
}

估计总贡献时间

我使用类似于git-hours的 README 中解释的算法估算了最近 100 次提交中贡献的时间。

然后我将其缩放到totalCount

const timeContributedTotal = timeContributedLatest100 * totalCount / 100;

我估计截至今天在 Twitter 的 Bootstrap 上投入了 13152 小时,而 7 个月前的git-hours估计为 9959 小时。 听起来还不错。

至于 React,我总共得到 15097 小时,或 629 天。

估计非常粗略,但它尽可能接近我所需要的。 如果您发现任何可能的改进,请随时发表评论或回答。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM