It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Programmatically connect to Bitbucket/Bamboo from Jira for REST API use

Hi All,

As far as i saw, many people (include me) use REST API calls from Jira to GET/POST data from/to Bitbucket/Bamboo.

Most people are using hard coded credentials with Base64 Encode.

However, putting your credentials  hard coded (even in Base64 Encode format) is not the best practice, so i will share here a way that many users are not familiar with.

By using the following, you will be able to perform your REST API calls from Jira without putting credentials.

This will use the current login user credentials in Jira, to connect to Bitbucket/Bamboo

import com.atlassian.applinks.api.ApplicationLinkRequestFactory;
import com.atlassian.applinks.api.ApplicationLink;
import com.atlassian.applinks.api.ApplicationLinkService;
import com.atlassian.applinks.api.application.bamboo.BambooApplicationType;
import com.atlassian.applinks.api.application.bitbucket.BitbucketApplicationType;

In order to use this technique you will have to link Bitbucket/Bamboo to your Jira link applications.

And here is an example for how to use it:

/*to get the Bitbucket link*/
private
ApplicationLink getPrimaryBitbucketLinkLink() {
ApplicationLinkService applicationLinkService = ComponentLocator.getComponent(ApplicationLinkService.class);
final ApplicationLink bitbucketLink = applicationLinkService.getPrimaryApplicationLink(BitbucketApplicationType.class);

return bitbucketLink;
}

/*to get the Bamboo link*/
private ApplicationLink getPrimaryBambooLink() {
ApplicationLinkService applicationLinkService = ComponentLocator.getComponent(ApplicationLinkService.class);
final ApplicationLink bambooLink = applicationLinkService.getPrimaryApplicationLink(BambooApplicationType.class);

return bambooLink;
}

/*REST API GET request*/
private JSONObject getJsonData(ApplicationLink applicationLink, String restURL) throws Exception {
ApplicationLink appLink = applicationLink;
ApplicationLinkRequestFactory authenticatedRequestFactory = appLink.createAuthenticatedRequestFactory();

final Response[] res = new Response[1];

authenticatedRequestFactory
.createRequest(Request.MethodType.GET, restURL)
.addHeader("Content-Type", "application/json")
.execute(new ResponseHandler<Response>() {
@Override
public void handle(Response response) {
if (response.getStatusCode() != HttpURLConnection.HTTP_OK) {
try {
throw new Exception(response.getResponseBodyAsString());
} catch (Exception e) {
e.printStackTrace();
}
}
res[0] = response;
}
});

JSONObject jsonObject = new JSONObject(res[0].getResponseBodyAsString());

return jsonObject;
}

/*call the getJsonData function to get data from where we want*/
getJsonData(getPrimaryBitbucketLinkLink(), "/rest/api/latest/....")

 

Hope this will serve you well as it served me.

Feel free to reply my article and ask questions or share your thought with my methods.

 

Thanks,

Nir Haimov

5 comments

Thanks for this - I'd been banging my head against the right way to make rest calls via the applinks for way too long today!

Nir Haimov Community Leader Jan 24, 2019

I was looking days till i figured it out.

That's why i felt like i have to post this article .

Glad it helped you :)

Hi Nir Haimov,

 Thank you for sharing this, if I can ask some more help, 

our requirement is to fill a JIRA custom field by accessing Bitbucket;  compare two tags in Bitbucket using REST API and get the JIRA issues used to commit between those tags. This fill may be required during a workflow transition.

So, how I can add your REST End point to a field behavior or to a Listener which will fill the JIRA custom field?

Thanks in advance

with warm regards

ramki

 

PS: in another use case I have one behavior to a single line text custom field like this

getFieldByName("Product 1 Branch").convertToSingleSelect([
ajaxOptions: [
url : getBaseUrl() + "/rest/scriptrunner/latest/custom/frnversions",
query: true,
formatResponse: "general"
]
])

 

which is accessing own JIRA dB and getting the projectversion based on user input. frnversions is the JIRA REST END Point for me in this case; I am looking for similar use case with Bitbucket access

Nir Haimov Community Leader Wednesday

Hi @Ramakrishnan S ,

As I understand you are using Scriptrunner,
I think it will be better and more simple if you use scripted field.

The method I mentioned in my article simply let you connect to Bitbucket/Bamboo without hard coded credentials, getting data with rest api and compare is pure code that need to be written.

Hi Nir Haimov,

 Thank you for the response, yes I am using script runner.

Let me try and give it a shot with your code as base

 

with warm regards

ramki

Comment

Log in or Sign up to comment
Community showcase
Published Apr 09, 2019 in Portfolio for Jira

Portfolio for Jira 3.0 is here!

The wait is over... Portfolio for Jira Server and Data Center 3.0 is now officially here! Platform releases offer Atlassian an opportunity to shift our strategy, make bold predictions about t...

1,625 views 14 26
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you