【问题标题】:Querying all commits in a single repository with the GitHub GraphQL API v4使用 GitHub GraphQL API v4 查询单个存储库中的所有提交
【发布时间】:2019-08-20 12:01:16
【问题描述】:

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

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

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

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

不幸的是,由于 API 的 resource limitations,它只返回最新的 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") {}

如何递归地运行此连接,直到查询到存储库中所有 committedDates 的提交?

【问题讨论】:

    标签: github graphql github-api


    【解决方案1】:

    虽然有一种方法可以递归查询 repo 上的所有提交,但我找不到可行的解决方案。

    这是我的解决方案

    我的需要是:

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

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

    从 GitHub GraphQL API 查询数据

    • 提交历史的totalCount
    • 最近 100 次提交中的committedDate
    {
      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's README 中解释的算法估计了最近 100 次提交中所贡献的时间。

    然后我将它缩放到totalCount

    const timeContributedTotal = timeContributedLatest100 * totalCount / 100;
    

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

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

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

    【讨论】:

      猜你喜欢
      • 2020-11-20
      • 2018-03-26
      • 2018-09-01
      • 2021-08-29
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 2018-09-09
      • 2018-08-21
      相关资源
      最近更新 更多