使用 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
          }
        }
      }
    }
  }
}

不幸的是它 returns 只有最近的 100 次提交,因为 API 的 resource limitations:

Node Limit

To pass schema validation, all GraphQL API v4 calls must meet these standards:

  • Clients must supply a first or last argument on any connection.
  • Values of first and last must be within 1-100.
  • Individual calls cannot request more than 500,000 total nodes.

因此,由于我没有提供 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 次提交?

尽管可以递归查询存储库中的所有提交,但我找不到可行的解决方案。

这是我的解决方案

我的需求是:

I only want to pull the dates they were committed at, in order to estimate the total time that was contributed to that repository (something along the lines of git-hours)

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

从 GitHub GraphQL 查询数据 API

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

运行今天查询returns:

{
  "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's README 上解释的算法估算了最近 100 次提交中贡献的时间。

然后我将它缩放到 totalCount:

const timeContributedTotal = timeContributedLatest100 * totalCount / 100;

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

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

估算值非常粗略,但已尽可能接近我的需要。如果您看到任何可能的改进,请随时发表评论或回答。