Search code examples
github-api

How to get the comments from a GitHub pull request using GitHub API?


I am trying to get the comments on a pull request using GitHub API, for example:

https://api.github.com/repos/mapra99/members-only/pulls/1/comments

But it returns an empty array.

The same if I try to use the review comments endpoint:

https://api.github.com/repos/mapra99/members-only/issues/1/comments

Although there is a comment on that pull request:

https://github.com/mapra99/members-only/pull/1

How can I get the comment that is on that pull request using GitHub API? Why is it not possible to get if from any of those endpoints?


Solution

  • In fact, this is not a comment but a review :

    Pull Request Reviews are groups of Pull Request Review Comments on the Pull Request, grouped together with a state and optional body comment.

    You can get the reviews using :

    https://api.github.com/repos/mapra99/members-only/pulls/1/reviews

    Or using GraphQL v4 to get reviews & comments :

    {
      repository(owner: "mapra99", name: "members-only") {
        pullRequest(number: 1) {
          reviews(first: 100) {
            nodes {
              body
            }
          }
          comments(first: 100) {
            nodes {
              body
            }
          }
        }
      }
    }