One of the projects I work on uses the original Jira Rest Java Client to create issues from their web application, and since it was integrated a long time ago, things on the Atlassian side has deprecated, like Basic Authentication and original API models. So, with that, lets update to the latest client.
Dependencies
Thankfully, going from 1.0 to 5.2.1 does not involve much code change, however there is some tweaking we have to do to get the client working. Previously we only needed one dependency, but now we have a few to get it all working. This is what I switched out.
Original ivy.xml
<dependency org="com.atlassian.jira" name="jira-rest-java-client" rev="1.0" />
Updated ivy.xml
<dependency org="com.atlassian.jira" name="jira-rest-java-client-core" rev="5.2.0" />
<dependency org="com.atlassian.sal" name="sal-api" rev="3.1.1">
<artifact name="sal-api" />
</dependency>
<dependency org="io.atlassian.fugue" name="fugue" rev="4.7.2" />
<dependency org="com.sun.jersey" name="jersey-core" rev="1.19.4" />
jira-rest-java-client-core
The main dependency that has all (most) of what you need, is in the new “jira-rest-java-client-core”.
sal-api
Unfortunately, Ivy has issues handling poms with sub <package> and using “jira-rest-java-client-core” alone, would fail to resolve atlassian-plugin for “sal-api”. So, to get around that, we have to have an explicit dependency on “sal-api” so we can target the artifact that we need.
jersey-core
Jira does not have this declared in their pom and ivy does not seem to pull it in, and because the JiraClient uses a UriBuilder from jrs311 from that package, we have to manually add that one to our ivy.xml too.
Refactoring
Now that we have all the libraries needed, we will need to update the existing usage of the client. Fortunately, there are only a few changes needed.
Update Imports
Packages just about remains the same, except we need to add one more level.
Before
import com.atlassian.jira.rest.client.JiraRestClient;
import com.atlassian.jira.rest.client.domain.BasicProject;
After
import com.atlassian.jira.rest.client.api.JiraRestClient;
import com.atlassian.jira.rest.client.api.domain.BasicProject;
Switch to Async Client
The original JerseyJiraRestClientFactory is now gone and replaced with the AsynchronousJiraRestClientFactory. Also, there is no need for the Progress Monitor, so remove any reference.
Before
private final NullProgressMonitor pm;
public JiraRestClient getJiraClient() {
JerseyJiraRestClientFactory factory = new JerseyJiraRestClientFactory ();
return factory.createWithBasicHttpAuthentication(jiraUri, username, password);
}
After
public JiraRestClient getJiraClient() {
AsynchronousJiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
return factory.createWithBasicHttpAuthentication(jiraUri, username, password);
}
Update Client Usage (Promise/FutureTask)
The client is now async, so we need to handle the request and response a little bit differently, otherwise, it will blow through our code.
Before
JiraRestClient restClient = getJiraClient();
BasicProject project = restClient.getProjectClient().getProject(projectKey, pm);
After
JiraRestClient restClient = getJiraClient();
BasicProject project = restClient.getProjectClient().getProject(projectKey).claim();
“Claiming” will pause the async and wait for a response, rather than it continuing on to the next line.
Finally, since we are using async, we must close the connection after making the request or requests.
restClient.close();
Basic Authentication is Deprecated
Well, kinda. Atlassian says Basic Authentication is no longer supported as an authentication method for the API. This is true when using with the user’s original email and password. However, if you generate an AP Token for that user, the Basic Authentication method will work.
Create an API Token
Follow the link and generate a token. Copy and Save it.
https://confluence.atlassian.com/cloud/api-tokens-938839638.html
Update Password in your Application
With the new API Token, replace your previous stored password with the new Token. Also, make sure the username is using the full email address, rather than the username itself.
Conclusion
Now with these changes, you can fire up your application and your Jira Client *should just work* 🙂 Good Luck!