Add support to create / delete branch via REST API in Bitbucket Cloud

Issue #12295 open
Shannon Cruey created an issue

The Bitbucket cloud REST API does not support branch creation / deletion. However the REST API for Bitbucket server does as found in the link below:

https://developer.atlassian.com/static/rest/bitbucket-server/4.3.1/bitbucket-rest.html#idp2564976

Use case: we want to standardize and automate the branch creation process and would most prefer to do this via the REST API. Delete would be nice too.

Comments (19)

  1. Lee Hull

    We need this to implement automation during out development process, currently we have to stop automation to create the branch manually, we really need a way to automate the creation of the branch

  2. Development Tools Sabre Technology

    This is very important for us, to have this feature also in Bitbucket Server.

  3. KartikG

    This is really important to have in Bitbucket Cloud API. we need this functionality desperately. Does anyone know any alternate solution for this?

  4. Toon Geens

    The web-ui seems to use this:

    Request URL:https://bitbucket.org/!api/1.0/repositories/xenit/fred/_branch/EWONECM-43
    Request Method:DELETE
    
  5. Christopher Dancy

    So there is documentation here:

    https://developer.atlassian.com/static/rest/bitbucket-server/5.0.1/bitbucket-branch-rest.html

    However ... whether or not the branch exists or not you get a 204 returned which is terrible at best. I write the bitbucket-rest client and this has really bitten our users. There is no good way to tell when a branch is actually deleted.

    https://github.com/cdancy/bitbucket-rest/blob/master/src/main/java/com/cdancy/bitbucket/rest/features/BranchApi.java#L93

  6. Zaid

    You can always inspect and use what they use in their web application to create or delete branch.We have been using that way for quiet some time now for our automation work. Also they have a different set of apis for bitbucket cloud and branch. Reach out to me at zqidw21@gmail.com, will surely help .

  7. Luciano Deriu

    Thanks @Vladimir Starkov,

    For everyone's reference, here is an example curl command that creates a branch off of a specific commit:

    curl -i -X POST \
       -H "Authorization:<token>" \
       -d "branch=release/1.3.1" \
       -d "message=Creating Release 1.3.1 Branch" \
       -d "parents=b58e9c1b435c889ea632c691e15fadf9ca39ce28" \
     'https://api.bitbucket.org/2.0/repositories/{username}/{repo_slug}/src'
    
  8. Christopher Dancy

    Or if on java you can just use the aforemetioned bitbucket-rest client and do the below:

    BitbucketClient client = BitbucketClient.builder()
    .endPoint("http://127.0.0.1:7990")
    .credentials("admin:password")
    .build();
    
    CreateBranch createBranch = CreateBranch.create("release/1.3.1", "b58e9c1b435c889ea632c691e15fadf9ca39ce28", "Creating Release 1.3.1 Branch");
    Branch branch = client.api().branchApi().create(createBranch);
    

    #MoreShamelessPlugs

  9. Luciano Deriu

    The delete seems to be working now. Here is a curl example that worked for me:

    curl -i -X DELETE \
       -H "Authorization:{token}" \
     'https://api.bitbucket.org/2.0/repositories/{username}/{repo_slug}/refs/branches/{branchname}'
    

    See the docs

  10. Log in to comment