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

Rob*_*ral 1 github github-api graphql

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

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

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

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

Run Code Online (Sandbox Code Playgroud)

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

节点限制

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

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

因此,由于我没有提供firstorlast参数,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
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

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

在查询committedDate存储库中的所有提交之前,如何递归运行此连接?

Rob*_*ral 5

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

这是我的解决方案

我的需要是:

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

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

从 GitHub GraphQL API 查询数据

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

今天运行,查询返回:

{
  "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
          ]
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

估计总贡献时间

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

然后我将其缩放为totalCount

const timeContributedTotal = timeContributedLatest100 * totalCount / 100;
Run Code Online (Sandbox Code Playgroud)

我估计截至今天,Twitter 的 Bootstrap 上有 13152 小时,而git-hours7 个月前估计为 9959 小时。听起来还不错。

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

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