How to retrieve pull request comments using REST API
Platform notice: Server and Data Center only. This article only applies to Atlassian products on the Server and Data Center platforms.
Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.
*Except Fisheye and Crucible
The content on this page relates to platforms which are not supported. Consequently, Atlassian Support cannot guarantee providing any support for it. Please be aware that this material is provided for your information only and using it is done so at your own risk.
Summary
The purpose of this article is to retrieve a particular Pull Request's comments via REST API.
Environment
- Bitbucket Server/DC
- Sample Pull Request with comments:
Solution
You will need jq installed on the machine.
Use this REST API to retrieve all the Pull Requests for the particular repository - this will return the ID of the Pull Request which will be used in the next step:
This is a paginated REST API, the maximum number of Pull Requests will be 25. If you have more than 25 Pull Requests in the repository, please iterate through the pages.
curl --request GET \ --user <username> \ --header 'Accept: application/json' \ --url '<base url>/rest/api/latest/projects/<project key>/repos/<repo key>/pull-requests' \ --no-progress-meter \ | jq '.values[] | {id, title, createdDate, updatedDate}'
Result:
{ "id": 2, "title": "Test2", "createdDate": 1704146205340, "updatedDate": 1704146205340 } { "id": 1, "title": "file1.txt edited online with Bitbucket", "createdDate": 1704087646718, "updatedDate": 1704087646718 }
The following REST API will return all the comments and the authors under that particular Pull Request:
curl --request GET \ --user <username> \ --header 'Accept: application/json' \ --url '<base url>/rest/api/latest/projects/<project key>/repos/<repo key>/pull-requests/<pull request id>/activities' \ --no-progress-meter \ | jq '.values[] | select(.action=="COMMENTED") | {text: .comment.text, author: .comment.author.name,id: .comment.id}'
Result:
{ "text": "incorrect code", "author": "admin", "id": 182 } { "text": "A pithy comment on a file.", "author": "admin", "id": 174 }