I have the opportunity to use JIRA's Java API and would like to introduce it.
There were a lot of documents and examples using Curl for REST API, but even if you search on the net for Java API, I found the following official JIRA tutorial, but it wasn't useful because the information was out of date.
Besides that, I found a sample of a strong man who analyzes JSON directly using HttpClient.
Reinventing the wheel, but exporting a summary of JIRA issues to Excel I have created a sample and would like to introduce it.
An example is a Java program that outputs an overview of JIRA issues to MS Excel. It's already provided as a standard feature (*), but I hope it helps anyone who wants to learn JIRA's Java API. \ * Export JIRA search results to Microsoft Excel (https://ja.confluence.atlassian.com/jira063/exporting-search-results-to-microsoft-excel-683542534.html)
When executed with a Java batch program, the title, summary, and description are output to the Excel specified by the first argument. In addition, the code published on GitHub below is just an example, so the part that outputs Excel is not particularly sophisticated.
I will write down the order and questions I checked below in chronological order. If you just want a conclusion, see the next section.
→ There is an API. It is written at the end of the following page. I'm not sure which one to use, JIRA Server, Cloud, Software Server, Service Desk Server. https://developer.atlassian.com/jiradev/jira-apis
From a different point of view, I searched for a working sample because it could be either Maven or gradle. The following page was the closest to the search for "pom java JIRA api" etc. Even this is 2.0, which is quite old. https://community.atlassian.com/t5/Answers-Developer-Questions/JIRA-Rest-Java-Client-libraries/qaq-p/524054
Apparently, I found that the following libraries are keyword-like.
I also found that there is an API called JiraRestClientFactory.
Now that I know the dependent libraries, I wrote pom with that as the key. Looking at "jira-rest-java-client-core maven", I found that version 4.0 is the newest. https://mvnrepository.com/artifact/com.atlassian.jira/jira-rest-java-client-core
Now that the dependent libraries have been resolved, how do you use the API? I checked. Look for "JiraRestClientFactory example" as the key. http://massapi.com/class/ji/JiraRestClientFactory.html Was a hit.
Looking at the code, JiraRestClientFactory (interface) is the entry point, and it used to be the implementation class name Jersey…, but AsynchronousJiraRestClientFactory has a new name.
It has a structure to get JiraRestClient from Factory and search, update, etc.
━ JiraRestClientFactory (entry point) ┗ AsynchronousJiraRestClientFactory (implementation class) ┗ JiraRestClient (Client from the factory) ┣ IssueClient (sub-API group that can be obtained from RestClient) ┣ SearchClient ┗ ProjectClient
You can see it by looking at the JavaDoc of JiraRestClient.
After that, I found that it is the same as the tutorial if I replace it with Jersey → Asynchronous.
https://developer.atlassian.com/jiradev/jira-apis/jira-rest-apis/jira-rest-api-tutorials Looking at this REST tutorial, I found that there are Basic, OAuth, etc. Looking at the sample code, it seems that Basic authentication can be done with the following code.
factory.createWithBasicHttpAuthentication(jiraServerUri, yourUsername, yourPassword);
I searched for "Search Client JIRA example". Anyway, it seems good to add an example. The next page was a hit, which also used the JerseyJiraRestClient for a long time, but it is helpful. http://stackoverflow.com/questions/29206524/how-to-get-all-issues-of-project-via-jira-rest-java-client
From the above example, we can see that we can search with the following code. What is a Promise? I thought, but when I claim (), the iterator is returned. All you have to do is loop as in the example.
Promise<SearchResult> searchJqlPromise = client.getSearchClient().searchJql("project = MYPURRJECT AND status in (Closed, Completed, Resolved) ORDER BY assignee, resolutiondate");
for (Issue issue : searchJqlPromise.claim().getIssues()) {
System.out.println(issue.getSummary());
}
<dependency>
<groupId>com.atlassian.jira</groupId>
<artifactId>jira-rest-java-client-core</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>com.atlassian.jira</groupId>
<artifactId>jira-rest-java-client-api</artifactId>
<version>4.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.atlassian.fugue/fugue -->
<dependency>
<groupId>com.atlassian.fugue</groupId>
<artifactId>fugue</artifactId>
<version>2.6.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
JiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
URI uri = new URI("https://your-project-name.atlassian.net");
jiraRestClient = factory.createWithBasicHttpAuthentication(uri, userName, password);
The API was a little strange, but I got it with the following code.
SearchRestClient searchRestClient = jiraRestClient.getSearchClient();
final Set<String> fields = new HashSet<String>();
String jql = "status!=done"; // Only not done issues.
SearchResult result = searchRestClient.searchJql(jql, max, offset, fields).claim();
Iterable<Issue> issues = result.getIssues();
List<Issue> list = new ArrayList<Issue>();
for (final Issue issue : issues) {
list.add(issue);
}
issue.getKey();
issue.getSummary();
issue.getDescription();
that's all.
Click here for the code.
Recommended Posts