View Javadoc

1   package org.lsst.ccs.release.management;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.text.MessageFormat;
6   import java.text.SimpleDateFormat;
7   import java.util.ArrayList;
8   import java.util.Date;
9   import java.util.List;
10  import java.util.logging.Level;
11  import java.util.logging.Logger;
12  import javax.ws.rs.client.Client;
13  import javax.ws.rs.client.ClientBuilder;
14  import javax.ws.rs.client.Entity;
15  import javax.ws.rs.client.WebTarget;
16  import javax.ws.rs.core.MediaType;
17  import javax.ws.rs.core.MultivaluedHashMap;
18  import javax.ws.rs.core.MultivaluedMap;
19  import javax.ws.rs.core.Response;
20  import javax.xml.parsers.DocumentBuilder;
21  import javax.xml.parsers.DocumentBuilderFactory;
22  import org.glassfish.jersey.client.ClientConfig;
23  import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
24  import org.json.JSONArray;
25  import org.json.JSONException;
26  import org.json.JSONObject;
27  import org.w3c.dom.Document;
28  
29  /**
30   *
31   * @author turri
32   */
33  public class JiraVersionManagement {
34  
35      private static String jiraIssuesRequestParameter = "fixVersion=\"{0}\"&project={1}";
36  
37      private static WebTarget jiraRestWebTarget = null;
38  
39      private static MultivaluedMap<String, Object> jiraHeaders = new MultivaluedHashMap<>();
40      static {
41          jiraHeaders.add("Content-Type", "application/json");
42          jiraHeaders.add("Authorization", "Bearer Mzc0NTg4MzcwOTMzOuv1pr+yDq2MeZL0o23LcPZ+tEXB");
43      }
44      
45  
46      private static WebTarget getJiraRestWebTarget() {
47          if (jiraRestWebTarget == null) {
48              Client client = ClientBuilder.newBuilder().newClient(new ClientConfig());
49  //            client.register(new LoggingFilter());
50  
51              jiraRestWebTarget = client.target("https://jira.slac.stanford.edu/rest/api/2/");
52          }
53          return jiraRestWebTarget;
54      }
55  
56      public static List<JiraIssue> getJiraIssues(String jiraProject, String jiraVersion) throws IOException {
57  
58          ArrayList<JiraIssue> jiraIssues = new ArrayList<>();
59          WebTarget search = getJiraRestWebTarget().path("search");
60          Response r = search.queryParam("jql", MessageFormat.format(jiraIssuesRequestParameter, jiraVersion, jiraProject)).request().headers(jiraHeaders).get();
61          String sResult = r.readEntity(String.class);
62          try {
63              JSONObject result = new JSONObject(sResult);
64              JSONArray issues = (JSONArray) result.get("issues");
65              for (int i = 0; i < issues.length(); i++) {
66                  JSONObject issue = issues.getJSONObject(i);
67                  jiraIssues.add(new JiraIssue(issue));
68              }
69          } catch (JSONException ex) {
70              Logger.getLogger(JiraVersionManagement.class.getName()).log(Level.SEVERE, null, ex);
71          }
72          return jiraIssues;
73  
74      }
75  
76      private static String getNewJiraVersionPayload(String jiraVersionName, String projectId) {
77          return "{\"name\": \"" + jiraVersionName + "\",\"archived\": false,\"released\": false,\"projectId\": \"" + projectId + "\" }";
78      }
79  
80      private static String getReleaseJiraVersionPayload(String jiraVersionName, String projectId, String newJiraId) {
81          return "{\"name\": \"" + jiraVersionName + "\",\"archived\": false,\"released\": true,\"projectId\": \"" + projectId
82                  + "\",\"moveUnfixedIssuesTo\": \"" + newJiraId + "\",\"userReleaseDate\": \"" + new SimpleDateFormat("dd/MMM/yy").format(new Date()) + "\"  }";
83      }
84  
85      public static void updateJiraVersions(String jiraProject, String releasedJiraVersion, String snapshotJiraVersion) throws JSONException {
86  
87          if (jiraProject != null && !jiraProject.equals("")) {
88              //First get Id for version and project        
89              WebTarget project = getJiraRestWebTarget().path("project/" + jiraProject);
90              Response r = project.request().headers(jiraHeaders).get();
91              String sResult = r.readEntity(String.class);
92              String projectId = null;
93              JSONArray versions = null;
94              try {
95                  JSONObject result = new JSONObject(sResult);
96                  projectId = result.getString("id");
97                  versions = (JSONArray) result.get("versions");
98              } catch (JSONException ex) {
99                  Logger.getLogger(JiraVersionManagement.class.getName()).log(Level.SEVERE, null, ex);
100             }
101 
102             if (projectId == null) {
103                 throw new RuntimeException("Could not find ID for project " + jiraProject);
104             }
105 
106             JSONObject snapshotVersionJiraJson = null;
107             if (snapshotJiraVersion != null && !snapshotJiraVersion.equals("")) {
108                 snapshotJiraVersion = snapshotJiraVersion.replace("-SNAPSHOT", "");
109                 WebTarget newVersion = getJiraRestWebTarget().path("version");
110                 snapshotVersionJiraJson = getVersionJson(versions, snapshotJiraVersion);
111 
112                 //If the SNAPSHOT Jira version does not exist, creat it.
113                 if (snapshotVersionJiraJson == null) {
114                     Response rNewVersion = newVersion.request().headers(jiraHeaders).accept(MediaType.APPLICATION_JSON)
115                             .post(Entity.entity(getNewJiraVersionPayload(snapshotJiraVersion, projectId), MediaType.APPLICATION_JSON), Response.class);
116                     String newVersionResult = rNewVersion.readEntity(String.class);
117                     snapshotVersionJiraJson = new JSONObject(newVersionResult);
118                 }
119             }
120 
121             if (releasedJiraVersion != null && !releasedJiraVersion.equals("")) {
122 
123                 JSONObject releasedVersionJiraJson = getVersionJson(versions, releasedJiraVersion);
124                 // Do something only if the Jira version exists:
125                 if (releasedVersionJiraJson != null) {
126 
127                     //Check if the old version has already been released:
128                     if (!releasedVersionJiraJson.getString("released").equals("true")) {
129 
130                         WebTarget oldVersion = getJiraRestWebTarget().path("version/" + releasedVersionJiraJson.getString("id"));
131 
132                         Response rOldVersion = oldVersion.request().headers(jiraHeaders).accept(MediaType.APPLICATION_JSON)
133                                 .put(Entity.json(getReleaseJiraVersionPayload(releasedJiraVersion, projectId, snapshotVersionJiraJson.getString("id"))));
134                     }
135                 }
136             }
137 
138         }
139     }
140 
141     private static JSONObject getVersionJson(JSONArray versions, String versionName) throws JSONException {
142         for (int i = 0; i < versions.length(); i++) {
143             JSONObject version = versions.getJSONObject(i);
144             if (versionName.equals(version.getString("name"))) {
145                 return version;
146             }
147         }
148         return null;
149     }
150 
151     public static void main(String[] args) throws Exception {
152 
153 //        initialized = true;
154 //        username = "";
155 //        password = "";
156         
157         List<JiraIssue> issues = getJiraIssues("LSSTCCS", "core-4.5.0");
158         System.out.println("Issues for core 3.0.2");
159         for ( JiraIssue issue : issues ) {
160             System.out.println(issue.getKey()+" "+issue.getResolution());
161         }
162         
163         updateJiraVersions("LSSTCCS", "core-3.0.2", "core-3.1.0");
164 
165         issues = getJiraIssues("LSSTCCS", "core-3.1.0");
166         System.out.println("Issues for core 3.1.0");
167         for ( JiraIssue issue : issues ) {
168             System.out.println(issue.getKey()+" "+issue.getResolution());
169         }
170 
171     }
172 
173 }