How can I create a new branch in Bitbucket repository using REST API?
I'm using Postman client.
How can I create a new branch in Bitbucket repository using REST API?
I'm using Postman client.
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) ;
}
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
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.
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
© 2022 - 2024 — McMap. All rights reserved.