Fetch All Pull-Request Comments Via Bitbucket REST API

Viewed 1826

This is how retrieve a particular pull-request's comments according to bitbucket's documentation:

Bitbucket REST API /Comment Documentation

While I do have the pull-request ID and format a correct URL I still get a 400 response error. I am able to make a POST request to comment but I cannot make a GET. After further reading I noticed the six parameters listed for this endpoint do not say 'optional'. It looks like these need to be supplied in order to retrieve all the comments.

But what exactly are these parameters? I don't find their descriptions to be helpful in the slightest. Any and all help would be greatly appreciated!

1 Answers

fromHash and toHash are only required if diffType is'nt set to EFFECTIVE. state also seems optional to me (didn't give me an error when not including it), and anchorState specifies which kind of comments to fetch - you'd probably want ALL there. As far as I understand it, path contains the path of the file to read comments from. (ex: src/a.py and src/b.py were changed -> specify which of them to fetch comments for) However, that's probably not what you want. I'm assuming you want to fetch all comments.

You can do that via /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/pull-requests/{pullRequestId}/activities which also includes other activities like reviews, so you'll have to do some filtering.

I won't paste example data from the documentation or the bitbucket instance I tested this once since the json response is quite long. As I've said, there is an example response on the linked page. I also think you'll figure out how to get to the data you want once downloaded since this is a Q&A forum and not a "program this for me" page :b

As a small quickstart: you can use curl like this

curl -u <your_username>:<your_password> https://<bitbucket-url>/rest/api/1.0/projects/<project-key>/repos/<repo-name>/pull-requests/<pr-id>/activities

which will print the response json.

Python version of that curl snippet using the requests module:

import requests

url = "<your-url>"  # see above on how to assemble your url
r = requests.get(
    url,
    params={},  # you'll need this later
    auth=requests.auth.HTTPBasicAuth("your-username", "your-password")
)

Note that the result is paginated according to the api documentation, so you'll have to do some extra work to build a full list: Either set an obnoxiously high limit (dirty) or keep making requests until you've fetched everything. I stronly recommend the latter.

You can control which data you get using the start and limit parameters which you can either append to the url directly (e.g. https://bla/asdasdasd/activity?start=25) or - more cleanly - add to the params dict like so:

requests.get(
    url,
    params={
        "start": 25,
        "limit": 123
    }
)

Putting it all together:

def get_all_pr_activity(url):
    start = 0
    values = []
    while True:
        r = requests.get(url, params={
            "limit": 10,  # adjust this limit to you liking - 10 is probably too low
            "start": start
        }, auth=requests.auth.HTTPBasicAuth("your-username", "your-password"))
        values.extend(r.json()["values"])
        if r.json()["isLastPage"]:
            return values
        start = r.json()["nextPageStart"]


print([x["id"] for x in get_all_pr_activity("my-bitbucket-url")])

will print a list of activity ids, e.g. [77190, 77188, 77123, 77136] and so on. Of course, you should probably not hardcode your username and password there - it's just meant as an example, not production-ready code.

Finally, to filter by action inside the function, you can replace the return values with something like

return [activity for activity in values if activity["action"] == "COMMENTED"]
Related