pythonpython-3.xgitbitbucketatlassian-python-api

How to Retrieve Lines of Code Changed in a Bitbucket Pull Request using atlassian-python-api?


I'm working on a project and using the atlassian-python-api to interact with Bitbucket. I'm trying to retrieve the lines of code changed in a pull request, but I'm unable to find a specific function in the atlassian library that directly provides this information.

I have successfully fetched the pull requests using the get_pull_requests() function, but it doesn't include the lines of code changed (loc_changed). I understand that I might need to retrieve the diff information for each pull request to calculate this.

However, upon checking the available functions in the atlassian library, I couldn't find a direct method to fetch the diff for a pull request.

Could someone guide me on how to retrieve the lines of code changed in a Bitbucket pull request using the atlassian-python-api or suggest an alternative approach within the library to obtain this information?

I was expecting to find a method within the atlassian library that directly retrieves the diff or changes for a specific pull request. I aimed to use this information to calculate the lines of code changed (additions and deletions) and update the loc_changed field in a custom PullRequest object.


Solution

  • You can use the diffstat method on the pull request object

    bitbucket = Cloud(...)
    workspace = bitbucket.workspaces.get("workspace")
    repository = workspace.repositories.get("id")
    prs = repository.pullrequests.each()
    
    for pr in prs:
        loc_changed = sum([stat.lines_added + stat.lines_removed for stat in pr.diffstat()])