View Javadoc

1   /*
2    * Copyright (C) 2010 Atlassian
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package samples;
18  
19  import com.atlassian.jira.rest.client.api.JiraRestClient;
20  import com.atlassian.jira.rest.client.api.domain.BasicIssue;
21  import com.atlassian.jira.rest.client.api.domain.BasicProject;
22  import com.atlassian.jira.rest.client.api.domain.BasicWatchers;
23  import com.atlassian.jira.rest.client.api.domain.Comment;
24  import com.atlassian.jira.rest.client.api.domain.Issue;
25  import com.atlassian.jira.rest.client.api.domain.SearchResult;
26  import com.atlassian.jira.rest.client.api.domain.Transition;
27  import com.atlassian.jira.rest.client.api.domain.input.ComplexIssueInputFieldValue;
28  import com.atlassian.jira.rest.client.api.domain.input.FieldInput;
29  import com.atlassian.jira.rest.client.api.domain.input.TransitionInput;
30  import com.atlassian.jira.rest.client.internal.ServerVersionConstants;
31  import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory;
32  import com.google.common.collect.Lists;
33  import org.codehaus.jettison.json.JSONException;
34  
35  import java.io.IOException;
36  import java.net.URI;
37  import java.net.URISyntaxException;
38  import java.util.Arrays;
39  import java.util.Collection;
40  import java.util.List;
41  
42  /**
43   * A sample code how to use JRJC library
44   *
45   * @since v0.1
46   */
47  public class Example1 {
48  
49      private static URI jiraServerUri = URI.create("http://localhost:2990/jira");
50      private static boolean quiet = false;
51  
52      public static void main(String[] args) throws URISyntaxException, JSONException, IOException {
53          parseArgs(args);
54  
55          final AsynchronousJiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
56          final JiraRestClient restClient = factory.createWithBasicHttpAuthentication(jiraServerUri, "admin", "admin");
57          try {
58              final int buildNumber = restClient.getMetadataClient().getServerInfo().claim().getBuildNumber();
59  
60              // first let's get and print all visible projects (only jira4.3+)
61              if (buildNumber >= ServerVersionConstants.BN_JIRA_4_3) {
62                  final Iterable<BasicProject> allProjects = restClient.getProjectClient().getAllProjects().claim();
63                  for (BasicProject project : allProjects) {
64                      println(project);
65                  }
66              }
67  
68              // let's now print all issues matching a JQL string (here: all assigned issues)
69              if (buildNumber >= ServerVersionConstants.BN_JIRA_4_3) {
70                  final SearchResult searchResult = restClient.getSearchClient().searchJql("assignee is not EMPTY").claim();
71                  for (BasicIssue issue : searchResult.getIssues()) {
72                      println(issue.getKey());
73                  }
74              }
75  
76              final Issue issue = restClient.getIssueClient().getIssue("TST-7").claim();
77  
78              println(issue);
79  
80              // now let's vote for it
81              restClient.getIssueClient().vote(issue.getVotesUri()).claim();
82  
83              // now let's watch it
84              final BasicWatchers watchers = issue.getWatchers();
85              if (watchers != null) {
86                  restClient.getIssueClient().watch(watchers.getSelf()).claim();
87              }
88  
89              // now let's start progress on this issue
90              final Iterable<Transition> transitions = restClient.getIssueClient().getTransitions(issue.getTransitionsUri()).claim();
91              final Transition startProgressTransition = getTransitionByName(transitions, "Start Progress");
92              restClient.getIssueClient().transition(issue.getTransitionsUri(), new TransitionInput(startProgressTransition.getId()))
93                      .claim();
94  
95              // and now let's resolve it as Incomplete
96              final Transition resolveIssueTransition = getTransitionByName(transitions, "Resolve Issue");
97              final Collection<FieldInput> fieldInputs;
98  
99              // Starting from JIRA 5, fields are handled in different way -
100             if (buildNumber > ServerVersionConstants.BN_JIRA_5) {
101                 fieldInputs = Arrays.asList(new FieldInput("resolution", ComplexIssueInputFieldValue.with("name", "Incomplete")));
102             } else {
103                 fieldInputs = Arrays.asList(new FieldInput("resolution", "Incomplete"));
104             }
105             final TransitionInput transitionInput = new TransitionInput(resolveIssueTransition.getId(), fieldInputs, Comment
106                     .valueOf("My comment"));
107             restClient.getIssueClient().transition(issue.getTransitionsUri(), transitionInput).claim();
108         } finally {
109             restClient.close();
110         }
111     }
112 
113     private static void println(Object o) {
114         if (!quiet) {
115             System.out.println(o);
116         }
117     }
118 
119     private static void parseArgs(String[] argsArray) throws URISyntaxException {
120         final List<String> args = Lists.newArrayList(argsArray);
121         if (args.contains("-q")) {
122             quiet = true;
123             args.remove(args.indexOf("-q"));
124         }
125 
126         if (!args.isEmpty()) {
127             jiraServerUri = new URI(args.get(0));
128         }
129     }
130 
131     private static Transition getTransitionByName(Iterable<Transition> transitions, String transitionName) {
132         for (Transition transition : transitions) {
133             if (transition.getName().equals(transitionName)) {
134                 return transition;
135             }
136         }
137         return null;
138     }
139 
140 }