Create comment on pull request
GitHub's comment API seems to allow you to create comments on a pull request, but only if you supply a specific line number in the diff to comment on. Is there a way to create a comment on the pull request as a whole, the equivalent of typing at the bottom of the pull request screen in GitHub's web interface?
Solution 1:
Yes, it is possible. The section of the API docs you are referencing relates to line comments (comments on specific lines of the commits in the pull req), and the docs say:
Pull Request Review Comments are comments on a portion of the unified diff. These are separate from Commit Comments (which are applied directly to a commit, outside of the Pull Request view), and Issue Comments (which do not reference a portion of the unified diff).
What you need are those Issue comments, which are explained at the top of this page:
The Pull Request API allows you to list, view, edit, create, and even merge pull requests. Comments on pull requests can be managed via the Issue Comments API.
So, in order to create a Pull Request comment, you actually need to create an Issue comment (since a pull request creates an issue to manage it). The page for Issue comments confirms this:
The Issue Comments API supports listing, viewing, editing, and creating comments on issues and pull requests.
So, the request you need to make is:
POST /repos/:owner/:repo/issues/:number/comments
How do you know which issue comment URL to POST to? Well, if you look at the response for getting a single pull request, you will see that it contains an attribute called _links
, and that this attribute has a nested comments
attribute. This is the URL which you should use for reading and creating pull request comments, the same one as to be used in the POST request above.
Solution 2:
According to Ivan, I was able to do something like:
$ curl -s -H "Authorization: token ${ACCESS_TOKEN}" \
-X POST -d '{"body": "Your Message to Comment"}' \
"https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/issues/${PR_NUMBER}/comments"
or
$ curl -s -u "${GITHUB_ACCOUNT} \
-X POST -d '{"body": "Your Message to Comment"}' \
"https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/issues/${PR_NUMBER}/comments"
with password prompt
Solution 3:
Using GraphQL API v4, you would need to get the Pull Request id using pullrequest
Schema and perform addComment
mutation afterwards :
query FindPullRequestID {
repository(owner:"bertrandmartel", name:"ustream-dl") {
pullRequest(number:2) {
id
}
}
}
mutation AddPullRequestComment {
addComment(input:{subjectId:"MDExOlB1bGxSZXF1ZXN0MTU0NzExOTA0",body: "test comment"}) {
commentEdge {
node {
createdAt
body
}
}
subject {
id
}
}
}
Try it in the explorer