22
33import com .atlassian .jira .rest .client .api .IssueRestClient ;
44import 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 ;
5+ import com .atlassian .jira .rest .client .api .domain .BasicVotes ;
76import com .atlassian .jira .rest .client .api .domain .Comment ;
87import com .atlassian .jira .rest .client .api .domain .Issue ;
98import com .atlassian .jira .rest .client .api .domain .input .IssueInput ;
1211
1312import java .io .IOException ;
1413import java .net .URI ;
15- import java .net .URISyntaxException ;
16- import java .util .ArrayList ;
1714import java .util .List ;
15+ import java .util .stream .Collectors ;
16+ import java .util .stream .StreamSupport ;
1817
1918public class MyJiraClient {
2019
@@ -23,14 +22,14 @@ public class MyJiraClient {
2322 private String jiraUrl ;
2423 private JiraRestClient restClient ;
2524
26- public MyJiraClient (String username , String password , String jiraUrl ) {
25+ private MyJiraClient (String username , String password , String jiraUrl ) {
2726 this .username = username ;
2827 this .password = password ;
2928 this .jiraUrl = jiraUrl ;
3029 this .restClient = getJiraRestClient ();
3130 }
3231
33- public static void main (String [] args ) {
32+ public static void main (String [] args ) throws IOException {
3433
3534 MyJiraClient myJiraClient = new MyJiraClient ("user.name" , "pass" , "http://jira.company.com" );
3635
@@ -51,42 +50,38 @@ public static void main(String[] args) {
5150
5251 myJiraClient.deleteIssue(issueKey, true);
5352*/
54- myJiraClient .close ();
53+ myJiraClient .restClient . close ();
5554 }
5655
5756 public String createIssue (String projectKey , Long issueType , String issueSummary ) {
5857
5958 IssueRestClient issueClient = restClient .getIssueClient ();
6059
6160 IssueInput newIssue = new IssueInputBuilder (projectKey , issueType , issueSummary ).build ();
62- BasicIssue createdIssue = issueClient .createIssue (newIssue ).claim ();
6361
64- return createdIssue .getKey ();
62+ return issueClient . createIssue ( newIssue ). claim () .getKey ();
6563 }
6664
67- public Issue getIssue (String issueKey ) {
68- Issue issue = restClient .getIssueClient ().getIssue (issueKey ).claim ();
69- return issue ;
65+ private Issue getIssue (String issueKey ) {
66+ return restClient .getIssueClient ().getIssue (issueKey ).claim ();
7067 }
7168
7269 public void voteForAnIssue (Issue issue ) {
7370 restClient .getIssueClient ().vote (issue .getVotesUri ()).claim ();
7471 }
7572
7673 public int getTotalVotesCount (String issueKey ) {
77- Issue updatedIssue = getIssue (issueKey );
78- return updatedIssue . getVotes () .getVotes ();
74+ BasicVotes votes = getIssue (issueKey ). getVotes ( );
75+ return votes == null ? 0 : votes .getVotes ();
7976 }
8077
8178 public void addComment (Issue issue , String commentBody ) {
8279 restClient .getIssueClient ().addComment (issue .getCommentsUri (), Comment .valueOf (commentBody ));
8380 }
8481
8582 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 ;
83+ return StreamSupport .stream (getIssue (issueKey ).getComments ().spliterator (), false )
84+ .collect (Collectors .toList ());
9085 }
9186
9287 public void updateIssueDescription (String issueKey , String newDescription ) {
@@ -99,32 +94,11 @@ public void deleteIssue(String issueKey, boolean deleteSubtasks) {
9994 }
10095
10196 private JiraRestClient getJiraRestClient () {
102- JiraRestClientFactory factory = new AsynchronousJiraRestClientFactory ();
103-
104- URI jiraServerUri = getJiraUri ();
105- return factory
106- .createWithBasicHttpAuthentication (jiraServerUri , this .username , this .password );
97+ return new AsynchronousJiraRestClientFactory ()
98+ .createWithBasicHttpAuthentication (getJiraUri (), this .username , this .password );
10799 }
108100
109101 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 );
102+ return URI .create (this .jiraUrl );
129103 }
130104}
0 commit comments