I am trying to develope apapter for BitBucket Cloud using BitBucket REST API 2.0.
My requirement is to create a new branch using API. But I am not able to find any API for that. However there is one API to GET branches but API to PUT a branch is not there. Pls help.
For BB cloud, there are two ways:
1) This one will create a commit though, so your branch will be 1 commit ahead of your master...
curl -X POST -is -u user:pass https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}/src --form "parents=name_of_branch_to_create_from" --form "branch=name_of_branch_to_create"
2) This one will just create the branch:
curl -X POST -is -u user:pass -H 'Content-Type: application/json' https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}/refs/branches -d '{ "name": "name_of_branch_to_create", "target": { "hash": "name_of_branch_to_create_from" } }'
Have fun...
Thank you.
Note - the call shown in 1) is in API docs here: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-source/#api-repositories-workspace-repo-slug-src-post
For anyone coming here for branch deletion, https://stackoverflow.com/questions/50006997/how-to-delete-a-branch-using-bitbucket-rest-api/74281876#74281876
curl --location --request DELETE 'https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}/refs/branches/{branchname}'
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@sergio ramos Unfortunately the APIs aren't the same. Please check out the server documentation here: https://developer.atlassian.com/server/bitbucket/reference/rest-api/
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Tony Carter This issue has been identified and fixed. We appreciate the time you took to report it. The error response is very useful!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
In case anyone else needs this, use the follow for creating a new branch via the API: https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/refs/branches#post
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I tried many variations of the following:
curl -X POST "https://api.bitbucket.org/2.0/repositories/owner/my-repo-test/src/?access_token=${access_token}" -F "branch=tony"
curl -X POST "https://api.bitbucket.org/2.0/repositories/owner/my-repo-test/src/?access_token=${access_token}" -d "branch=tony"
TIA,
Tony
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Tony. Do you mind if I ask what arguments you were passing to the API? You don't have to give me the specific values for repository or for the arguments I'm just wondering which arguments you are passing to the API.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Has the API changed?
I'm getting {"type": "error", "error": {"message": "Something went wrong", "id": "b074e82335e948b897f5525a16c883ac"}} 500 when I post to this endpoint.
Thanks,
Tony
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@blossm-steve-personal - Yes, you can specify a commit message and upload files at the same time.
See the API documentation for examples.
Let me know if the docs are clear enough.
Eric Henry
Development Team Lead, Bitbucket Cloud
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks!
This works for me.. is there a way to modify the empty commit message in the request? It currently reads "Edited with Bitbucket".
Also, you mentioned "unless you also commit files when you create the branch" .. how would you do this via api when creating the branch?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for reply. Actually I was using Bibucket local server instance not the cloud one... but I guess the API would be same except the base url..
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello,
You can create a new branch using the 2.0 API as follows:
curl https://api.bitbucket.org/2.0/repositories/{repo_slug}/src/ --F "parents={parent_hash}" --F "branch={branch_name}"
You don't have to specify parents if you want the branch to be directly off the latest commit of the default branch, otherwise parents should be a hash to the commit that you want to create a branch off of.
Just an FYI: when you create a branch through the API it will create an empty commit (unless you also commit files when you create the branch).
Feel free to jump in to this HipChat room if you want some more "real-time support".
ERIC HENRY
Development Team Lead, Bitbucket Cloud
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.