How to link commits with branch name ? Bitbucket REST API v2
Asked by Ali Hussaini:

I would like to know if there is a way to get the branch name , commit ID , commit author , lines added , removed ,modified from one single endpoint in bitbucket ?


Answers:
author: seanaty
body: The only place to get the modification counts is the diffstat API: <https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/diffstat/%7Bspec%7D>

That works on a range of commits so there isn't any commit metadata. If you'd like to look up author and commit info you can use either the branches API or the commits API.

Branches: <https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/refs/branches/%7Bname%7D#get>

Commits: <https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/commits/%7Brevision%7D>

Both of these APIs require you to have a branch name or commit ID already. Without those, how do you plan on identifying the changes you're trying to look at?

So there isn't 1 place to get all this information but you can probably get it all with 2 requests.

comments: author: Ali Hussaini
body: Hi [@seanaty](/t5/user/viewprofilepage/user-id/1301071) thanks for the response really appreciate it. I have one follow up question that if we use branches end point it does give a commit hash in the response but that commit id is only the latest commit done to that branch is there a way to get all the commits done in one branch exclusively . I would like to know how to use the REST API 2.0 to achieve this . Currently I am calling the branches endpoint and from that I calling the link to commits to get all the commits. But this is proving to be tedious also as downward merge happens in my organization the commits from this path gets duplicated . I woul like some further assistance . Thanks


author: seanaty
body: You can look up the diff (or diffstat) for any range of commits. If you're doing this for a PR there are some nifty PR APIs which will end up issuing an HTTP Redirect to the corresponding diff or diffstat API.

Pull request Diff:

<https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/pullrequests/%7Bpull_request_id%7D/diff>

Pull request Diff stat:

<https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/pullrequests/%7Bpull_request_id%7D/diffstat>

As an example,

<https://api.bitbucket.org/2.0/repositories/atlassian/bbql/pullrequests/28/diffstat>

will issue a redirect to:

<https://api.bitbucket.org/2.0/repositories/atlassian/bbql/diffstat/opensrckenh/bbql:642188fb450b%0D36a8e140e5ec?from_pullrequest_id=28>

which is the API for diffstat with \`opensrckenh/bbql:642188fb450b%0D36a8e140e5ec\` as the "spec" param.1

If you look at this it has the pattern

{branch}:{commit on the branch}%0D{commit defaults to main branch}

This is how you can use both diff and diff stat to look up ranges.

You can do a similar thing with the commits API. This is from the docs:
> GET /repositories/{workspace}/{repo_slug}/commits/dev?exclude=master  
> Returns all commits on ref dev, except those that are reachable on master (similar to git log dev \^master).

<https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/commits/%7Brevision%7D#get>


Tags: bitbucket-cloud

cloud