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

How to identify the default branch for a given build key?

Gregory Paciga
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!
February 28, 2020

I'm working on a CI system that uses the current git branch to look up a build key and start a build in Bamboo.

So far, I'm using the /plan/{projectKey}-{buildKey}/branch endpoint to get all the branches and their keys to look up the key for the branch I'm trying to build. The problem is that the default branch doesn't appear in this list.

For example, if I have project key PK and build key BK, I call /plan/PK-BK/branch and get something like this back (leaving out a bunch of keys):

{
"branches": {
"branch": [{
"shortName": "my-feature-branch",
"key": "PK-BK123"
}, {
"shortName": "second-feature-branch",
"key": "PK-BK234"
}]
}

If my CI server is trying to build my-feature-branch or second-feature-branch, this works fine.

But there's nothing I can find that maps the default branch name - the one I see if I just navigate to /browse/PK-BK in the UI - to the PK-BK build key. Nor have I found anything that goes the other way: knowing that PK-BK is the default, look up what branch name that is. I can't hard code the branch name ("if the current branch is develop, use the root build key") because I want this to work in multiple repos that might have different default branches.

Is there something in the API that can give me this info, or is there a better strategy entirely for mapping from git branch to build key?

2 answers

1 vote
Dan Riesman
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 15, 2024

4.5 years after: still a valid question to Atlasian. Google led me to this page. 

The APIs like 

http://myhost.com:8085/bamboo/rest/api/latest/plan/{projectKey}-{buildKey}/branch

or http://myhost.com:8085/bamboo/rest/api/latest/plan/{projectKey}/{buildKey}

do not return which branch is the default one.

 

That is a problem, because if you use the following API to get the build results

http://myhost.com:8085/bamboo/rest/api/latest/result/{projectKey}-{buildKey}/branch/{branchName} 

and the branch you search IS the default one (assume it is named "default"), you get an error:

{"message":"Branch default not found.","status-code":404}

Basically, the API works for non-default branches only. Which would not be a problem, if we were able to find out the name of the default branch, and if confirmed to match, use the following API instead:

http://myhost.com:8085/bamboo/rest/api/latest/result/{projectKey}-{buildKey}

But for that I need to be able to get the default branch name somehow. 

pBogey December 4, 2024

Wasting a lot of time trying to find a way to determine the name of the default branch via API. This is stupid.

EDIT: anyway, found a way to do it. Use following endpoint:

https://{bamboo_server}/rest/api/latest/search/branches.json?masterPlanKey={projectKey-planKey}&includeMasterBranch=true

 

result with "type": "chain" will be the default branch.

 

or for a specific branch:

https://{bamboo_server}/rest/api/latest/search/branches.json?masterPlanKey={projectKey-planKey}&includeMasterBranch=true&branchNameOnly=true&searchTerm={branchName}

 

1 vote
Alex Lynch
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!
September 12, 2022

Help from Atlasian? Is there a way to query the build status of the default branch via the REST API? 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events