首页 > 解决方案 > 如何使用 GraphQL 获取 GitHub 存储库的提交以及已更改的文件和更改本身

问题描述

我能够获取提交列表(包含提交消息oid提交 url等字段)以及在分支changedFiles的存储库中创建的数量。 但是,我无法弄清楚如何获取有关更改本身和已更改文件的任何信息。master

在 REST API 的 v3 中,有关更改的信息包含在files->patchfiles->中,raw_url或者blob_url在该阶段提供了有关原始文件本身的信息。

Q) 在使用 GraphQL 的 GitHub API v4 中,我如何获得相应的信息?

我现在坚持的查询(为简洁起见仅显示 1 个提交)-

query {
  rateLimit{
    cost
    remaining
  }
  repository(owner: "elastic", name: "elasticsearch") {
    name
    defaultBranchRef {
      name
      target {
        ... on Commit {
          history(first:1){
            nodes{
              message
              changedFiles
              id
              oid
              treeUrl
              url
              tree{
                oid
              }
            }
            pageInfo{
              hasNextPage
              startCursor
              endCursor
            }
          }
        }
      }
    }
  }
}

输出:

{
  "data": {
    "rateLimit": {
      "cost": 1,
      "remaining": 4999
    },
    "repository": {
      "name": "elasticsearch",
      "defaultBranchRef": {
        "name": "master",
        "target": {
          "history": {
            "nodes": [
              {
                "message": "Small corrections to HLRC doc for _termvectors (#35221)\n\nRelates to #33447",
                "changedFiles": 2,
                "id": "MDY6Q29tbWl0NTA3Nzc1OmEyYzIyYWQ3YWViMGY4ZDUxNDg2NzdkZDcyMjJhZDQzYWZlZTlhMTc=",
                "oid": "a2c22ad7aeb0f8d5148677dd7222ad43afee9a17",
                "treeUrl": "https://github.com/elastic/elasticsearch/tree/a2c22ad7aeb0f8d5148677dd7222ad43afee9a17",
                "url": "https://github.com/elastic/elasticsearch/commit/a2c22ad7aeb0f8d5148677dd7222ad43afee9a17",
                "tree": {
                  "oid": "4f5f11e0e55aeafc4677800959232726a2cd787c"
                }
              }
            ],
            "pageInfo": {
              "hasNextPage": true,
              "startCursor": "a2c22ad7aeb0f8d5148677dd7222ad43afee9a17 0",
              "endCursor": "a2c22ad7aeb0f8d5148677dd7222ad43afee9a17 0"
            }
          }
        }
      }
    }
  }
}

标签: graphqlgithub-graphql

解决方案


推荐阅读