Using the "JIRA REST Java Client" (JRJC) library it is possible to read/edit/create JIRA tickets using Java via the REST API. I would like to access information about the JIRA Sprints contained in an issue field. I was able to read out the field (see example code), but that just gives me a JSON array that contains String values. I assume that the JRJC provides capabilities so that it is not necessary to parse the String values by my own.
I would be grateful, if someone could show me how to do that. And what additional dependencies would be needed for that in Maven's pom.xml.
This is my demo code:
import com.atlassian.jira.rest.client.api.JiraRestClient; import com.atlassian.jira.rest.client.api.domain.Issue; import com.atlassian.jira.rest.client.api.domain.IssueField; import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory; import java.io.IOException; import java.net.URI; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; public class JRJC_Demo { public static void main(String[] args) throws IOException { String jiraUrl = "https://domain.com/jira"; String username = "username" String jiraApiToken = "asdf"; // generate API-Token: Profile-Button: 'API Token Authentication' > 'My API Tokens' JiraRestClient client = new AsynchronousJiraRestClientFactory().createWithBasicHttpAuthentication(URI.create(jiraUrl), username, jiraApiToken); String issueKey = "ISSUE-123" Issue issue = client.getIssueClient().getIssue(issueKey).claim(); IssueField field_sprint = issue.getFieldByName("Sprint"); JSONArray jsonArray = (JSONArray) field_sprint.getValue(); for (int i=0; i<jsonArray.length(); i++) { try { System.out.println( String.format("Sprint %d: '%s')", i, jsonArray.get(i) ) ); // output: "... com.atlassian.greenhopper.service.sprint.Sprint@17254e8[id=178412 ..." } catch (JSONException e) { e.printStackTrace(); } } } }
And this is my pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.domain</groupId> <artifactId>jira.robot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>jira.robot</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.13.0</version> <configuration> <source>1.21</source> <target>1.21</target> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>com.atlassian.jira</groupId> <artifactId>jira-rest-java-client-api</artifactId> <version>6.0.1</version> </dependency> <dependency> <groupId>com.atlassian.jira</groupId> <artifactId>jira-rest-java-client-core</artifactId> <version>6.0.1</version> </dependency> <dependency> <groupId>io.atlassian.fugue</groupId> <artifactId>fugue</artifactId> <version>5.0.2</version> </dependency> <dependency> <groupId>com.atlassian.sal</groupId> <artifactId>sal-api</artifactId> <version>6.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>2.0.9</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j2-impl</artifactId> <version>2.24.3</version> <scope>runtime</scope> </dependency> <dependency> <groupId>com.atlassian.event</groupId> <artifactId>atlassian-event</artifactId> <version>6.0.0</version> </dependency> </dependencies> </project>
Hi @maze
I think instead of just handling the raw JSON strings you need to correctly map things to a proper object type.
So instead of..
IssueField field_sprint = issue.getFieldByName("Sprint"); JSONArray jsonArray = (JSONArray) field_sprint.getValue(); for (int i=0; i<jsonArray.length(); i++) { try { System.out.println( String.format("Sprint %d: '%s')", i, jsonArray.get(i) ) ); // output: "... com.atlassian.greenhopper.service.sprint.Sprint@17254e8[id=178412 ..." } catch (JSONException e) { e.printStackTrace(); }
..maybe this would be better..
// Get the Sprint field
IssueField field_sprint = issue.getFieldByName("Sprint");
List<Sprint> sprints = (List<Sprint>) field_sprint.getValue();
if (sprints != null) {
for (int i = 0; i < sprints.size(); i++) {
Sprint sprint = sprints.get(i);
System.out.println(String.format("Sprint %d: '%s' (ID: %d)", i, sprint.getName(), sprint.getId()));
}
} else {
System.out.println("No Sprint info");
}
and of course Sprint import class need to be added
import com.atlassian.greenhopper.service.sprint.Sprint;
and probably this dependency also..
<dependency>
<groupId>com.atlassian.greenhopper</groupId>
<artifactId>greenhopper-service</artifactId>
<version>?.?.?</version>
</dependency>
Just please make sure that version above is correct :)
Other than that.. it looks fine so far..
Thank you for your answer. This is going in the direction I was looking for, but it is not working for me, yet.
(1)
I cannot find the mentioned dependency "com.atlassian.greenhopper.greenhopper-service". Where is this library hosted? How would I figure out the correct version to use?
I was looking on mvnrepository.com and found this one which also includes a class com.atlassian.greenhopper.service.sprint.Sprint:
https://mvnrepository.com/artifact/com.atlassian.jira.plugins/jira-greenhopper-api/
I therefore tried to get it running using the latter library.
(2)
This line of code then gives me a compiler warning: "Type safety: Unchecked cast from Object to List<Sprint>"
List<Sprint> sprints = (List<Sprint>) field_sprint.getValue();
Is it possible to rewrite this without using an unchecked cast?
(3)
When I execute the code I get a java.lang.ClassCastException:
"class org.codehaus.jettison.json.JSONArray cannot be cast to class java.util.List"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Spend the day sharpening your skills in Atlassian Cloud Organization Admin or Jira Administration, then take the exam onsite. Already ready? Take one - or more - of 12 different certification exams while you’re in Anaheim at Team' 25.
Learn more
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.