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>