How to create a branch using Bitbucket REST API [closed]
Asked Answered
W

4

5

How can I create a new branch in Bitbucket repository using REST API?

I'm using Postman client.

Westbrooke answered 26/4, 2017 at 7:46 Comment(2)
Don't know why this was closed. It's specific enough that it's the exact question and answer I was looking forIrreligious
@Irreligious It doesn't specify is it BitBucket Server, or online flavor, which version and few more details that could narrow the answerFurlana
B
7

I will show you how to create a branch in Bitbucket using postman and programmatically.

Using Postman

Select method type as POST

Add url: https://example.com/git/rest/api/1.0/projects/{projectKey}/repos/{repoName}/branches

Add Authorization to Basic Auth.

Username and password.

Select Body as raw

Select JSON(application/json)

add this to the body as JSON

{
    "name": "feature/my-feature-branch",
    "startPoint": "refs/heads/master"
}

Click on Send

Now Same programmatically

String authToken = "xyzxyzabcabcabcxyzxyzabcabcabcxyzxyzabcabcabc";

 public boolean createBranchProgrammatically(String projectKey, String repoName, String branchPrefix,String branchName,
            String headStart) {
        Map branches = new HashMap();
        JSONObject json = new JSONObject();
            try {
                String branch = branchPrefix + "/" + branchName;
                json.put("name", branch);
                json.put("startPoint", headStart);
                branches = restTemplate.exchange(myBitbuketUrl + "git/rest/api/1.0/projects/"
                        + projectKey + "repos" + repoName + "/branches",
                        HttpMethod.POST, postRequestEntityForBitbuket(json.toString()), Map.class).getBody();

                break;
            } catch (RestClientException e) {
                logger.error("Branches could not be created from bitbucket for "  , e);            

                return false;
            }

            } 
        return true;
    }



public HttpEntity<String> postRequestEntityForBitbuket(String jsonAsString) {

        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "Basic " + authToken);
        headers.add("content-type", "application/json");

        return new HttpEntity<String>(jsonAsString, headers) ;
    }
Ba answered 17/11, 2017 at 10:20 Comment(3)
The rest api way of doing is not working on an empty repo, any suggestion about that ?Rhondarhondda
@Rhondarhondda you can not create a branch on an empty repo, try committing something first and then retry the aboveWarplane
@RishiKesh could you give some reference to the API documentation?Furlana
G
5

Via curl

curl -u name:password -H "Content-Type:application/json" https://SERVER_ADDRESS/rest/api/1.0/projects/INF1/repos/mariaivanovatest/branches -X POST --data '{"name": "new_branch","startPoint": "refs/heads/master"}'

or if you have json file for example test.json

curl -u name:password -H "Content-Type:application/json" https://SERVER_ADDRESS/rest/api/1.0/projects/INF1/repos/mariaivanovatest/branches -X POST --data @test.json

Gust answered 8/8, 2018 at 12:48 Comment(2)
Perfect, the curl command is precisely what I was looking for. Thank you!Motteo
is this BitBucket (online) or BitBucket Server (self hosted / on premise) API? Most probably is the not self hosted flavor, but SERVER_ADDRESS in example brought confusion. Any reference to documentation would help. ThanksFurlana
U
2

You can create a branch in specific repo by /rest/branch-utils/1.0/projects/{projectKey}/repos/{repositorySlug}/branches . Please take a look at this document for further information.

Unravel answered 26/4, 2017 at 8:8 Comment(0)
W
0

I think what saleh has shared is for stash not bitbucket.

As far as this issue says, bitbucket yet not support API for creating branch

https://bitbucket.org/site/master/issues/12295/add-support-to-create-delete-branch-via

Weatherwise answered 5/5, 2017 at 8:39 Comment(1)
FYI, Stash was rebranded under the Bitbucket name in September 2015, and these days the products are named Bitbucket Cloud (bitbucket.org) and Bitbucket Server (Stash/self-hosted)Bine

© 2022 - 2024 — McMap. All rights reserved.