Missed Team ’24? Catch up on announcements here.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Java code example for bitbucker rest api

Dummy Account
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
December 16, 2018

I want to use an on prem bitbucket server using the bitbucket rest api from an application to create, update, and manage projects, repositories, teams etc while taking the parameters from my UI. And create we hooks from my java application. Can anyone point me in the right direction and a few examples to use the api for these from my spring boot project? What are the prerequisites

PS: I looked at the api docs and tried from postman and i want to call the rest end point from my java application.

Edit: So I am trying to use the JAVA api given

<dependency>
          <groupId>com.atlassian.bitbucket.server</groupId>
          <artifactId>bitbucket-rest</artifactId>
          <version>4.0.0-eap1</version>
          <scope>provided</scope>
      </dependency>

I'm unable can someone point me to the right direction to initialize or configure the BitBucketClient in my Java code. I don't know how to start calling my local bitbucket server

1 answer

0 votes
Mozi
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 23, 2019

I happen to automate the process of creating repository for microserice architecture along with the bitbucket pipeline to be created to for the created repo. 

I did it in two steps, 

1) Authentication: Firstly you need to get the authentication token(JWT) from bit bucket in order to authenticate all the api calls you might do further to it, like create repo, commit , delete etc. For this you will need key and secret , which can be generated as in link below.

https://confluence.atlassian.com/bitbucket/oauth-on-bitbucket-cloud-238027431.html

and futher to this, you need to use following code along with generated key and secret in following way ( I have returned request header along with bearer token). I have used spring boot restTemplate for calling rest api, you may use any other.

 

MultiValueMap<String, String> map= new LinkedMultiValueMap<>();

map.add("grant_type", "client_credentials");

HttpHeaders header = new HttpHeaders();

header.setBasicAuth(<key>,<secret>);

header.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, header);

ResponseEntity<Object> responseEntity = restTemplate.postForEntity("https://bitbucket.org/site/oauth2/access_token", request, Object.class);

LinkedHashMap body = (LinkedHashMap)responseEntity.getBody();

header = new HttpHeaders();

header.setContentType(MediaType.APPLICATION_JSON);

header.set(HttpHeaders.AUTHORIZATION, "Bearer " + requireNonNull(body).get("access_token").toString());

return header;

Now to create repository use the header from above code in following way

 

json request = {    
"scm": "git",
"project": {
"key": "<project key from bitbucket or UUID>"
},
"is_private":true/false,
"fork_policy":"no_forks",
"language":"<programming language>",
"name":"my-repo",

}
HttpEntity<Repository> requestEntity = new HttpEntity<>(<json request>, <header from above>);

ResponseEntity<Object> result = restTemplate.postForEntity("https://api.bitbucket.org/2.0/repositories/{username or team}/{repository name in lowercase}/", requestEntity, Object.class);

System.out.println(result.getStatusCode());

 If you get 200 ok you are done creating a repository. 

 

Similarly you can do it for other CRUD operations using bitbucker developer portal.

https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D#put

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events