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

Bitbucket REST API - getting branches

tomer bar November 21, 2022

Hi all,

Using the REST API we are trying to retrieve data about branches in the workspace. 

Using https://api.bitbucket.org/2.0/repositories/{workspace}/(repository}/refs/branches we get a response exampled in the link. It has values holding the various branches but does not hold any of the wanted data - author, created date etc. 

Unless it is the data specified under 'target' .. Is that the case (target references the requested branch), or are we using it wrong?

response example  

Thanks!

1 answer

1 accepted

1 vote
Answer accepted
Ben
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 23, 2022

G'day Tomer!

Welcome to the Bitbucket Cloud community! :) 

To retrieve information about certain ref types (ie branches/tags), you would need to modify the URL slightly by adding "/branches" or "/tags" after "ref", for example:

https://api.bitbucket.org/2.0/repositories/{workspace}/(repository}/refs/branches
https://api.bitbucket.org/2.0/repositories/{workspace}/(repository}/refs/tags

To filter this information further (for example - looking for a certain branch) the format is:

name ~ "insertnamehere"

Formatting this to unicode, you can include this in the url with the ?q parameter to view information about a certain branch:

https://api.bitbucket.org/2.0/repositories/{workspace}/(repository}/refs/branches?q=name%7E%22{branchname}%22

Further information regarding querying/sorting can be found here:
https://developer.atlassian.com/cloud/bitbucket/rest/intro/#filtering

Hope this helps.

Cheers!

- Ben (Bitbucket Cloud Support)

tomer bar November 26, 2022

.

tomer bar November 26, 2022

Hi Ben,

Thanks for your response. As detailed, we are using the URL as you provided -

https://api.bitbucket.org/2.0/repositories/{workspace}/(repository}/refs/branches

  but we do not see the data we are looking for. Maybe asking this differently - what is the data provided under "target" in the response JSON?

We couldn't find what the target is referencing to.

Thanks again!

Ben
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 27, 2022

Hey Tomer,

 

Target refers to a few fields, "type", "hash" and "date", for example:

"type": "commit",
"hash": "b41a86ce2a7b1bd0c002743240a7cf93c09ce513",
"date": "2022-11-23T05:25:56+00:00",

This provides details about the branches, for example the latest commit hash and the date that the branch was last committed to.

What is the information you are hoping to return from the branches?

Hope this helps.

Cheers!

- Ben (Bitbucket Cloud Support)

tomer bar November 27, 2022

Hi Ben,

For example who created the branch and when,amount of commit

Ben
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 28, 2022

Hey Tomer,

Unfortunately - the API will not show when the branch was created, only when the last commit was made.

There is a GIT command you can execute to show when each branch was created and by which author - although sometimes this is inaccurate:

git for-each-ref --format='%(committerdate)%09%(authorname)%09%(refname)' | sort -k5n -k2M -k3n -k4n | grep remotes | awk -F "\t" '{ printf "%-32s %-27s %s\n", $1, $2, $3 }'

As for total number of commits - this is also not reported by the API, however you can execute the following GIT command to count this on a per-branch basis:

git rev-list --all --count

Ultimately, the best way to find who created a branch is to check the first commit in the commit history. As reflogs are checked for author details using GIT commands such as above, these are set to expire in the GIT config (90 days by default) and this is what causes the inaccuracy.

Hope this helps.

Cheers!

- Ben (Bitbucket Cloud Support)

Like tomer bar likes this
Saumya Kumar
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 21, 2024

@Ben Can we get the date when first commit was checked in to the branch and all other information commit endpoint provides using REST API?

Ben
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 21, 2024

Hi @Saumya Kumar 

Unfortunately, the REST API does not have this level of filtering or sorting.

There are a couple of git commands you can run locally that may be combined, this will print the first commit along with the date:

git log --pretty=oneline --reverse | head -1 && git rev-list --max-parents=0 HEAD | xargs git log --pretty=format:'%ad'

Cheers!

- Ben (Bitbucket Cloud Support)

Bob Swift {Appfire} July 19, 2024

Ben As you have indicated, this REST API is missing a lot of basic information. Is there plans to make this more complete? I see the UI uses an internal api that has some more information but also missing things. 

"pullrequests": [],
"permissions": {
"push": "allow",
"delete": "allow"
},
"ahead": 0,
"behind": 2,
"ismainbranch": false,
"branchtype": "feature",

Ben
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 21, 2024

Hi @Bob Swift {Appfire} 

There are some FR tickets present surrounding our API (including branches), but nothing is planned in the immediate Bitbucket Cloud roadmap, unfortunately. For example:

Cheers!

- Ben (Bitbucket Cloud Support)

 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events