Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to link commits with branch name ? Bitbucket REST API v2

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 ?

1 answer

0 votes
seanaty Atlassian Team Oct 12, 2020

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.

Hi @seanaty  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

seanaty Atlassian Team Oct 13, 2020

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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Bitbucket

New improvements to user management in Bitbucket Cloud 👥

Hey Community! We’re willing to wager that quite a few of you not only use Bitbucket, but administer it too. Our team is excited to share that we’ll be releasing improvements throughout this month of...

3,792 views 10 16
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you