Refactor JiraClient (#2710)
This commit is contained in:
parent
c521aa66af
commit
1dbc2c9127
|
@ -2,8 +2,7 @@ package com.baeldung.jira;
|
||||||
|
|
||||||
import com.atlassian.jira.rest.client.api.IssueRestClient;
|
import com.atlassian.jira.rest.client.api.IssueRestClient;
|
||||||
import com.atlassian.jira.rest.client.api.JiraRestClient;
|
import com.atlassian.jira.rest.client.api.JiraRestClient;
|
||||||
import com.atlassian.jira.rest.client.api.JiraRestClientFactory;
|
import com.atlassian.jira.rest.client.api.domain.BasicVotes;
|
||||||
import com.atlassian.jira.rest.client.api.domain.BasicIssue;
|
|
||||||
import com.atlassian.jira.rest.client.api.domain.Comment;
|
import com.atlassian.jira.rest.client.api.domain.Comment;
|
||||||
import com.atlassian.jira.rest.client.api.domain.Issue;
|
import com.atlassian.jira.rest.client.api.domain.Issue;
|
||||||
import com.atlassian.jira.rest.client.api.domain.input.IssueInput;
|
import com.atlassian.jira.rest.client.api.domain.input.IssueInput;
|
||||||
|
@ -12,9 +11,9 @@ import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientF
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.StreamSupport;
|
||||||
|
|
||||||
public class MyJiraClient {
|
public class MyJiraClient {
|
||||||
|
|
||||||
|
@ -23,14 +22,14 @@ public class MyJiraClient {
|
||||||
private String jiraUrl;
|
private String jiraUrl;
|
||||||
private JiraRestClient restClient;
|
private JiraRestClient restClient;
|
||||||
|
|
||||||
public MyJiraClient(String username, String password, String jiraUrl) {
|
private MyJiraClient(String username, String password, String jiraUrl) {
|
||||||
this.username = username;
|
this.username = username;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
this.jiraUrl = jiraUrl;
|
this.jiraUrl = jiraUrl;
|
||||||
this.restClient = getJiraRestClient();
|
this.restClient = getJiraRestClient();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) throws IOException {
|
||||||
|
|
||||||
MyJiraClient myJiraClient = new MyJiraClient("user.name", "pass", "http://jira.company.com");
|
MyJiraClient myJiraClient = new MyJiraClient("user.name", "pass", "http://jira.company.com");
|
||||||
|
|
||||||
|
@ -51,7 +50,7 @@ public class MyJiraClient {
|
||||||
|
|
||||||
myJiraClient.deleteIssue(issueKey, true);
|
myJiraClient.deleteIssue(issueKey, true);
|
||||||
*/
|
*/
|
||||||
myJiraClient.close();
|
myJiraClient.restClient.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String createIssue(String projectKey, Long issueType, String issueSummary) {
|
public String createIssue(String projectKey, Long issueType, String issueSummary) {
|
||||||
|
@ -59,14 +58,12 @@ public class MyJiraClient {
|
||||||
IssueRestClient issueClient = restClient.getIssueClient();
|
IssueRestClient issueClient = restClient.getIssueClient();
|
||||||
|
|
||||||
IssueInput newIssue = new IssueInputBuilder(projectKey, issueType, issueSummary).build();
|
IssueInput newIssue = new IssueInputBuilder(projectKey, issueType, issueSummary).build();
|
||||||
BasicIssue createdIssue = issueClient.createIssue(newIssue).claim();
|
|
||||||
|
|
||||||
return createdIssue.getKey();
|
return issueClient.createIssue(newIssue).claim().getKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Issue getIssue(String issueKey) {
|
private Issue getIssue(String issueKey) {
|
||||||
Issue issue = restClient.getIssueClient().getIssue(issueKey).claim();
|
return restClient.getIssueClient().getIssue(issueKey).claim();
|
||||||
return issue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void voteForAnIssue(Issue issue) {
|
public void voteForAnIssue(Issue issue) {
|
||||||
|
@ -74,8 +71,8 @@ public class MyJiraClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getTotalVotesCount(String issueKey) {
|
public int getTotalVotesCount(String issueKey) {
|
||||||
Issue updatedIssue = getIssue(issueKey);
|
BasicVotes votes = getIssue(issueKey).getVotes();
|
||||||
return updatedIssue.getVotes().getVotes();
|
return votes == null ? 0 : votes.getVotes();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addComment(Issue issue, String commentBody) {
|
public void addComment(Issue issue, String commentBody) {
|
||||||
|
@ -83,10 +80,8 @@ public class MyJiraClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Comment> getAllComments(String issueKey) {
|
public List<Comment> getAllComments(String issueKey) {
|
||||||
Issue issue = getIssue(issueKey);
|
return StreamSupport.stream(getIssue(issueKey).getComments().spliterator(), false)
|
||||||
List<Comment> comments = new ArrayList<>();
|
.collect(Collectors.toList());
|
||||||
issue.getComments().forEach(c -> comments.add(c));
|
|
||||||
return comments;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateIssueDescription(String issueKey, String newDescription) {
|
public void updateIssueDescription(String issueKey, String newDescription) {
|
||||||
|
@ -99,32 +94,11 @@ public class MyJiraClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
private JiraRestClient getJiraRestClient() {
|
private JiraRestClient getJiraRestClient() {
|
||||||
JiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
|
return new AsynchronousJiraRestClientFactory()
|
||||||
|
.createWithBasicHttpAuthentication(getJiraUri(), this.username, this.password);
|
||||||
URI jiraServerUri = getJiraUri();
|
|
||||||
return factory
|
|
||||||
.createWithBasicHttpAuthentication(jiraServerUri, this.username, this.password);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private URI getJiraUri() {
|
private URI getJiraUri() {
|
||||||
URI jiraServerUri = null;
|
return URI.create(this.jiraUrl);
|
||||||
try {
|
|
||||||
jiraServerUri = new URI(this.jiraUrl);
|
|
||||||
} catch (URISyntaxException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return jiraServerUri;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void closeRestClient(JiraRestClient restClient) {
|
|
||||||
try {
|
|
||||||
restClient.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void close() {
|
|
||||||
closeRestClient(restClient);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue