Skip to content

Commit e71902d

Browse files
YasinBhojawalamaibin
authored andcommitted
BAEL-1139 added more examples (eugenp#2686)
* BAEL-1139 How to Integration with the JIRA API * BAEL-1139 How to Integration with the JIRA API * BAEL-1139 How to Integration with the JIRA API * BAEL-1139 added more examples * BAEL-1139 How to Integration with the JIRA API * How to Integration with the JIRA API * BAEL-1139 add and delete issue * BAEL-1139 minor improvements * BAEL-1139 comment conflicting dependencies * Update pom.xml
1 parent 75803f1 commit e71902d

3 files changed

Lines changed: 142 additions & 58 deletions

File tree

libraries/pom.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,18 @@
583583
<dependency>
584584
<groupId>com.atlassian.fugue</groupId>
585585
<artifactId>fugue</artifactId>
586-
<version>3.0.0-m007</version>
586+
<version>2.6.1</version>
587+
</dependency>
588+
<!-- Uncomment this in order to use the jira-rest-java-client-core API -->
589+
<!-- <dependency>
590+
<groupId>com.google.guava</groupId>
591+
<artifactId>guava</artifactId>
592+
<version>19.0</version>
593+
</dependency> -->
594+
<dependency>
595+
<groupId>org.jgrapht</groupId>
596+
<artifactId>jgrapht-core</artifactId>
597+
<version>1.0.1</version>
587598
</dependency>
588599
</dependencies>
589600
<repositories>

libraries/src/main/java/com/baeldung/jira/JiraClient.java

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package com.baeldung.jira;
2+
3+
import com.atlassian.jira.rest.client.api.IssueRestClient;
4+
import com.atlassian.jira.rest.client.api.JiraRestClient;
5+
import com.atlassian.jira.rest.client.api.JiraRestClientFactory;
6+
import com.atlassian.jira.rest.client.api.domain.BasicIssue;
7+
import com.atlassian.jira.rest.client.api.domain.Comment;
8+
import com.atlassian.jira.rest.client.api.domain.Issue;
9+
import com.atlassian.jira.rest.client.api.domain.input.IssueInput;
10+
import com.atlassian.jira.rest.client.api.domain.input.IssueInputBuilder;
11+
import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory;
12+
13+
import java.io.IOException;
14+
import java.net.URI;
15+
import java.net.URISyntaxException;
16+
import java.util.ArrayList;
17+
import java.util.List;
18+
19+
public class MyJiraClient {
20+
21+
private String username;
22+
private String password;
23+
private String jiraUrl;
24+
private JiraRestClient restClient;
25+
26+
public MyJiraClient(String username, String password, String jiraUrl) {
27+
this.username = username;
28+
this.password = password;
29+
this.jiraUrl = jiraUrl;
30+
this.restClient = getJiraRestClient();
31+
}
32+
33+
public static void main(String[] args) {
34+
35+
MyJiraClient myJiraClient = new MyJiraClient("user.name", "pass", "http://jira.company.com");
36+
37+
// final String issueKey = myJiraClient.createIssue("ABCD", 1L, "Issue created from JRJC");
38+
final String issueKey = "BAEL-1139";
39+
// myJiraClient.updateIssueDescription(issueKey, "This is description from my Jira Client");
40+
Issue issue = myJiraClient.getIssue(issueKey);
41+
System.out.println(issue.getDescription());
42+
43+
/*myJiraClient.voteForAnIssue(issue);
44+
45+
System.out.println(myJiraClient.getTotalVotesCount(issueKey));
46+
47+
myJiraClient.addComment(issue, "This is comment from my Jira Client");
48+
49+
List<Comment> comments = myJiraClient.getAllComments(issueKey);
50+
comments.forEach(c -> System.out.println(c.getBody()));
51+
52+
myJiraClient.deleteIssue(issueKey, true);
53+
*/
54+
myJiraClient.close();
55+
}
56+
57+
public String createIssue(String projectKey, Long issueType, String issueSummary) {
58+
59+
IssueRestClient issueClient = restClient.getIssueClient();
60+
61+
IssueInput newIssue = new IssueInputBuilder(projectKey, issueType, issueSummary).build();
62+
BasicIssue createdIssue = issueClient.createIssue(newIssue).claim();
63+
64+
return createdIssue.getKey();
65+
}
66+
67+
public Issue getIssue(String issueKey) {
68+
Issue issue = restClient.getIssueClient().getIssue(issueKey).claim();
69+
return issue;
70+
}
71+
72+
public void voteForAnIssue(Issue issue) {
73+
restClient.getIssueClient().vote(issue.getVotesUri()).claim();
74+
}
75+
76+
public int getTotalVotesCount(String issueKey) {
77+
Issue updatedIssue = getIssue(issueKey);
78+
return updatedIssue.getVotes().getVotes();
79+
}
80+
81+
public void addComment(Issue issue, String commentBody) {
82+
restClient.getIssueClient().addComment(issue.getCommentsUri(), Comment.valueOf(commentBody));
83+
}
84+
85+
public List<Comment> getAllComments(String issueKey) {
86+
Issue issue = getIssue(issueKey);
87+
List<Comment> comments = new ArrayList<>();
88+
issue.getComments().forEach(c -> comments.add(c));
89+
return comments;
90+
}
91+
92+
public void updateIssueDescription(String issueKey, String newDescription) {
93+
IssueInput input = new IssueInputBuilder().setDescription(newDescription).build();
94+
restClient.getIssueClient().updateIssue(issueKey, input).claim();
95+
}
96+
97+
public void deleteIssue(String issueKey, boolean deleteSubtasks) {
98+
restClient.getIssueClient().deleteIssue(issueKey, deleteSubtasks).claim();
99+
}
100+
101+
private JiraRestClient getJiraRestClient() {
102+
JiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
103+
104+
URI jiraServerUri = getJiraUri();
105+
return factory
106+
.createWithBasicHttpAuthentication(jiraServerUri, this.username, this.password);
107+
}
108+
109+
private URI getJiraUri() {
110+
URI jiraServerUri = null;
111+
try {
112+
jiraServerUri = new URI(this.jiraUrl);
113+
} catch (URISyntaxException e) {
114+
e.printStackTrace();
115+
}
116+
return jiraServerUri;
117+
}
118+
119+
private void closeRestClient(JiraRestClient restClient) {
120+
try {
121+
restClient.close();
122+
} catch (IOException e) {
123+
e.printStackTrace();
124+
}
125+
}
126+
127+
private void close() {
128+
closeRestClient(restClient);
129+
}
130+
}

0 commit comments

Comments
 (0)