Search code examples
githubgithub-apigitpython

Git command to get pull request subject body from issue number


I would like to get pull request body, subject and URL from issue number by git command especially by gitPython library. How can I do it?


Solution

  • GitPython is used for git related objects whereas a Pull Request is GitHub related and hence cannot be used to get GitHub data.

    You may use GitHub's v4 GraphQL API to get the pull request details with the below query

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

    The curl request for the above query:

    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 }"'
    

    The response for the above request:

    {
      "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"
          }
        }
      }
    }
    

    Note: You need to generate a token to access the GraphQL API, which you can generate by following the steps given here

    Alternatively, you can even use GitHub's v3 API as below to get the pull request details, which would have the body, title and url fields as part of the response

    GET https://api.github.com/repos/{owner}/{repoName}/pulls/{pullRequestNumber}
    
    GET https://api.github.com/repos/gitpython-developers/GitPython/pulls/974