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

Get a list of branches based on branch type

Asha
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!
July 20, 2022

Hi,

using the Bitbucket Rest API is there a way to filter the branches based on branch type ?

ex. I want to get the 'release' branches of a particular repository.

 

tried this API, but it is returning all the branches

https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}/refs/branches?branchtype=release

1 answer

1 accepted

0 votes
Answer accepted
Ben
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 21, 2022

Hey Asha,

Welcome to the Bitbucket Cloud Community! :)

The /2.0/repositories endpoint supports filtering/sorting based on the properties of the schema (this can be found in the JSON response).

For your case, the "name" property can be found in the JSON response, and thus can be filtered to find branches which begin with a certain string.

name ~ "release/"

When converted to unicode using escape characters to form the URL, this results in the following URL which can be queried with the GET method to provide this list for you:

https://api.bitbucket.org/2.0/repositories/{workspace}/{reposlug}/refs/branches/?q=name%20~%20%22release%2F%22

More information/instructions can be found here: https://developer.atlassian.com/cloud/bitbucket/rest/intro/#filtering

Hope this helps.

Cheers!

- Ben (Bitbucket Cloud Support)

Asha
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!
July 24, 2022

Hey Ben,

 

I tried the API by filtering the branches by name as you suggested. API is fetching all branches starting with name release instead of active branches.

for ex., I have 50 branches out of which 10 branches are active. The API is getting a total of 50 branches instead of 10. Can you suggest an API/ filtering for this.

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

Hi Asha,

Unfortunately - as the API does not expose ahead/behind information, it is not possible to determine active branches based on the API call.

This has been logged with our development team accordingly, feel free to "Watch" this issue to receive updates related to it and "Vote" on this issue to improve the visibility of customer demand:
https://jira.atlassian.com/browse/BCLOUD-12592

You can run a git for-each-ref command to sort the committer date for each branch and pipe a grep command to only show release branches, this way you can cross-reference the API results and determine which branches should be considered and which should be ignored:

git for-each-ref --sort=-committerdate --format='%(committerdate:short) %(refname)' refs/heads refs/remotes | grep 'release/'

You can further filter this by adding the --count switch to get at most the specified number in your results eg --count=10


Hope this helps.

Cheers!

- Ben (Bitbucket Cloud Support)

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events