How to use BitBucket Cloud Rest API in Java? Use-Cases with Examples.

This article will guide you on how to use the BitBucket Cloud rest API in Java to perform actions to a repository-
- Generate a bitbucket app password (this step is required to access your repositories)
- Retrieve the contents of a file in the repository using
GET
request. - Create a feature branch
- Create and commit changes to a file in the repository
- Create a pull request from
feature branch --> master
Task 1: Generate a bitbucket app authorization token
To generate an app password (which will be used as part of the credentials) to access your repository via your Java code, log into your bitbucket account and perform the following actions -
- Click on your profile icon on the lower left hand side, and click on personal settings.

2. Click on the ‘App passwords’ option and select ‘Create app password’

3. Select the appropriate permissions you want to give to your app password, give it a label name and click on create.

Task 2: Retrieve the contents of a file in the repository using GET
request.
First, a connection to the bitbucket cloud
API is needed to be established in order to send any REST API requests. The following configuration can be set in the main function.
Below is a JAVA function to establish a connection to your BitBucket repository and GET
a file from the master branch. (the file should exist in the repository a-priori)
The function above will retrieve the contents of the file and save it to the connectionDataStream
variable.
Task 3: Create a feature branch
It is usually advisable to always create a feature branch to add your changes in and after committing, create a pull request (PR) for approval.
Below is the JAVA function to establish a connection to your BitBucket repository and send a POST
request to create a feature branch from source -> feature
. (the source branch should exist in the repository a-priori)
The function above will create a branch from sourceBranch -> featureBranch.
Task 4: Create and commit changes to a file
To create/modify a file in the repository-
Below is the JAVA function to create/modify a file in your bitbucket repository. The file will be created in the repo if it doesn’t exist.
The above function should create the file in the repository and fill it with the data provided in the params. Ensure that the data is in string format.
Task 5: Create a pull request from feature branch --> master
After creating a file/any other modification in the feature branch and to get it merged to master, the following JAVA function needs to be invoked.
The function above should create a pull request from your destination (prFromBranch
) to the source branch (prToBranch
) . (Just ensure the destination/feature branch already exists)
If you have any doubts, just send me a message on LinkedIn or leave a comment here.
Any feedback is appreciated.
Thank You!
👋 Join FAUN today and receive similar stories each week in your inbox! ️ Get your weekly dose of the must-read tech stories, news, and tutorials.
Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬