首页 > 解决方案 > Git 命令从问题编号获取拉取请求主题主体

问题描述

我想通过 git 命令尤其是 gitPython 库从问题编号中获取拉取请求正文、主题和 URL。我该怎么做?

标签: githubgithub-apigitpython

解决方案


GitPython用于git相关对象,而Pull RequestGitHub相关,因此不能用于获取 GitHub 数据。

您可以使用GitHub 的 v4 GraphQL API通过以下查询获取拉取请求详细信息

query {
  repository(name: "gitPython",owner:"gitpython-developers"){
    pullRequest(number:974){
      body
      title
      url
    }
  }
}

上述查询的 curl 请求:

curl -L -X POST 'https://api.github.com/graphql' \
-H 'Authorization: bearer <token>' \
-H 'Content-Type: text/plain' \
--data-raw '{"query":"{\n repository(name: \"gitPython\",owner:\"gitpython-developers\"){\n pullRequest(number:974){\n body\n title\n url\n }\n }\n }"'

对上述请求的响应:

{
  "data": {
    "repository": {
      "pullRequest": {
        "body": "Removed A from Dockerfile that I added accidentally. THIS WILL BREAK THE BUILD",
        "title": "Remove A from Dockerfile",
        "url": "https://github.com/gitpython-developers/GitPython/pull/974"
      }
    }
  }
}

注意:您需要生成令牌才能访问 GraphQL API,您可以按照此处给出的步骤生成令牌

或者,您甚至可以使用下面的 GitHub 的 v3 API来获取拉取请求详细信息,其中包含bodytitleurl字段作为响应的一部分

GET https://api.github.com/repos/{owner}/{repoName}/pulls/{pullRequestNumber}

GET https://api.github.com/repos/gitpython-developers/GitPython/pulls/974

推荐阅读