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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,456,707
Community Members
 
Community Events
176
Community Groups

How to find the branch name of a commit by using API?

The ultimate task I need to perform is to extract the the commits that are made to a specific branch between two time points. This is quite easy to achieve with git CLI but I am limited to Bitbucket API v1.

Of course it's not a problem to get all commits:

<stash-url>/rest/api/1.0/projects/<proj_name>/repos/<repo_name>/commits?since=<start>&until=<end>

But then I have no branch context.

The only query that I can made successfully is to retrieve the latest commit of the branch:


<stash-url>/rest/api/1.0/projects/<proj_name>/repos/<repo_name>/commits?until=<branch_name>

 

Basically what I am missing is the ability to make a connection between a branch and a commit.



Any help will be appreciated. 


  

2 answers

Please try this. It gives the branch of a commit. 

 

rest/branch-utils/1.0/projects/{projectKey}/repos/{repositorySlug}/branches/info/{commitId} 

1 vote

Hello Taras and welcome to the Community!

I research a bit on your question about linking commits to a specific Bitbucket branch and found there is a possible solution within the v1 API for Bitbucket Server. While the documentation is not exactly clear on how to accomplish this, I did find a community post where someone figured out how to do this along with a StackOverflow thread on a similar topic.

The StackOverflow thread reports the following may work for your needs:

https://<stash-url>/stash/rest/api/latest/projects/<project-ID>/repos/<repo-slug>/commits?until=<my-target-feature-branch>&limit=0&start=0

Source thread: I want to get commits on a branch in bitbucket stash via rest API

Along with this, the Community post reports the following also worked for their requirements (This is similar to your API call on getting the last commit):

http://<stashurl>/stash/rest/api/latest/projects/<projectname>/repos/<myrepo>/commits?until=deploy&limit=0&start=0

Source post: Querying the last commit on a particular branch in Bitbucket using the Rest API

From my understanding on this, you should be able to use the StackOverflow response to call against the feature branch within Bitbucket and have it return commits for the said branch. Please give this a try and let us know the results.

We look forward to hearing back from you so we may find a way to link Commits to Branches within Bitbucket Server.

Regards,
Stephen Sifers

Hi Stephen,

Unfortunately both solutions (which are in fact identical) produce the same outcome - the latest commit of the desired branch. Meaning that it leavens me with nothing to retrieve the other commits from the same branch.

I kind of found a partial solution by reverse engineering. Thus master branch commits always have their previous commit as a parent. Thus it is only a matter of traversing "parent->parent->parent..." until the  required depth is reached. All these "parent" items are the commits of the master branch.

But this only works because in my case we have a strict policy of not committing to master directly and only merging feature branches instead.

Meaning that this problem cannot be solved reliably. Unfortunately.

Thank you Stephen anyway.
 

Like constantinosm likes this

Did you ever find a solution to this? I am facing a similar problem

Nope :o(

I am afraid this can only be achieved with the new/latest API but not with the legacy version.

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events