Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

I'm using Postman client.

like image 611
snir.tur Avatar asked Apr 26 '17 07:04

snir.tur


1 Answers

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) ;
    }
like image 136
RishiKesh Pathak Avatar answered Oct 06 '22 04:10

RishiKesh Pathak