Define MAIN BRANCH when create repo via REST API 2.0

Yevheniia Kutsak
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 22, 2018

I use documentation 

https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D

to create my repo vi REST API 2.0. 

Sent request and got new repo.

curl -X POST -H "Content-Type: application/json" -u 'user:password' -d '{"scm": "git", "project": {"key": "ABC"}, "fork_policy": "no_public_forks", "is_private": "true", "description": "blabla"}' https://api.bitbucket.org/2.0/repositories/teamname/reponame

But "mainbranch": null in this case. It need it to be "master" by default.

I also sent request, where I specified mainbranch, but still got "mainbranch": null  in result.

curl -X POST -H "Content-Type: application/json" -u 'user:password' -d '{"scm": "git", "project": {"key": "ABC"}, "fork_policy": "no_public_forks", "is_private": "true", "mainbranch": {"type": "branch", "name": "master"}, "description": "blabla"}' https://api.bitbucket.org/2.0/repositories/teamname/reponame

 

What am I doing wrong? How can I specify main branch while creating repo?

8 answers

1 vote
shigapov March 25, 2019

I connect to the question, I need to periodically create new branches and change mainbranch

but I can not appoint mainbranch

this action is not mentioned in the documentation

1 vote
Cori Drew
Contributor
September 26, 2018

SOLVED: Push the branch name you want to be default branch FIRST.

In my case (for scripting out creation of a gitflow repository), I init a local repo, programmatically and add initial files (.gitignore, .gitattributes, etc).

I then add the remote & push to develop first (git push origin master:develop).

THEN I push to master (git push -u origin master).

The develop branch becomes the default branch in this case because it was pushed first :)

0 votes
Andrew_L_Goss
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 28, 2018

Alright, I was approaching this the wrong way.  The CORRECT process for me to follow was to take my 'master' repo that had the code that I wanted to duplicate, and simply fork it using the API.   I used the following API page as a guide:

https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/forks#post

 

Forking a repo copies over all the attributes of the original, although you can override most attributes (including mainbranch) in the api call.  The basic api call that I used was:

checkRepo() {
  echo "Checking to see if ${WEB_NAME} exists on cloud......"
    RESPONSE=$( curl -sS --user ${BIT_USER}:${BIT_PASS} https://api.bitbucket.org/2.0/repositories/${BIT_REPO}/${WEB_NAME} )
 

  if [[ ${RESPONSE} == *'not found'* ]]; then
    echo "No main-branch was found on BitBucket for repository ${WEB_NAME}. Creating Repo......"
    if ( curl -X POST -u ${BIT_USER}:${BIT_PASS} "https://api.bitbucket.org/2.0/repositories/${BIT_REPO}/master/forks" -H 'Content-Type: application/json' -d '{"name"
: "'"${WEB_NAME}"'", "owner": {"username": "'"${BIT_REPO}"'"} }' >/dev/null 2>&1) ; then
      echo "Repo ${WEB_NAME} forked on cloud."
    else
      echo "Repo ${WEB_NAME} failed to fork on cloud."
    fi
  else
    echo "The repository, ${WEB_NAME}, exists on the cloud."
  fi

  echo ''
}

0 votes
Victor Velasquez
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 22, 2018

Hi,

 

I'm creating and managing repositories using API, I want to set the default branch to `develop`, I've tried with:

curl -X PUT \
'https://api.bitbucket.org/2.0/{username}/{repo_slug}' \
-H 'Authorization: Bearer mytoken' \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-d '{
"mainbranch": {
"type": "branch",
"name": "develop"
}
}'

 

With no luck.

There is no a way to set the main branch using API 2.0?

0 votes
Andrew_L_Goss
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 13, 2018

Has anyone found a solution to this?  I am trying to use the clues at https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/properties/%7Bapp_key%7D/%7Bproperty_name%7D, but I keep getting "Resource not found" errors. 

Part of it is that I am unsure what the app key is.  I create a new empty repo with the api, then mirror-push an existing repo into it.  I then try to correctly fix the mainbranch.

Is curl -X PUT -u ${BIT_USER}:${BIT_PASS} "https://api.bitbucket.org/2.0/repositories/{username}/{repo_slug}/properties/{app_key}/{property_name} not the solution?

0 votes
Timothy Harris
Contributor
September 6, 2018

Same struggles here!

0 votes
Tony Carter May 22, 2018

I'm interested in this as well. Any updates?

0 votes
Jan Huijsmans
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!
March 21, 2018

I've been battling with this as well, either a way to set main branch when creating it or changing it after creation would be fine by me. Haven't found a way yet...

Replying to this post to get it back in the list again, hoping for an answer. (while I dive into it myself)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events