1
0
Fork 0

Merge pull request #4 from rcarz/master

Incorporate changes from rcarz master.
master
Kyle Chaplin 2013-09-12 08:58:22 -07:00
commit 01d2e18a5b
21 changed files with 1872 additions and 71 deletions

View File

@ -4,7 +4,7 @@ jira-client is a simple and lightweight JIRA REST client library for Java.
The goal of the project is to provide **simple** and clean English idiomatic expressions for interacting with JIRA. In pursuit of this goal, jira-client lacks the usual verbose and cumbersome contortions often found in Java applications. And since the implementation isn't buried under 57 layers of complicated abstractions, jira-client is easy to extend and debug.
jira-client depends on [Apache HttpComponents](http://hc.apache.org/) and [json-lib](http://json.sourceforge.net/).
jira-client depends on [Apache HttpComponents](http://hc.apache.org/), [json-lib](http://json.sourceforge.net/), and [joda-time](http://www.joda.org/joda-time/).
## Features ##
@ -21,6 +21,7 @@ jira-client is still under heavy development. Here's what works:
* Add and remove issue watchers
* Add and remove issue links
* Create sub-tasks
* Retrieval of Rapid Board backlog and sprints
## Maven Dependency ##
@ -182,3 +183,74 @@ public class Example {
}
}
```
## GreenHopper Example ##
```java
import java.util.List;
import net.rcarz.jiraclient.BasicCredentials;
import net.rcarz.jiraclient.Issue;
import net.rcarz.jiraclient.JiraClient;
import net.rcarz.jiraclient.JiraException;
import net.rcarz.jiraclient.greenhopper.Epic;
import net.rcarz.jiraclient.greenhopper.GreenHopperClient;
import net.rcarz.jiraclient.greenhopper.Marker;
import net.rcarz.jiraclient.greenhopper.RapidView;
import net.rcarz.jiraclient.greenhopper.Sprint;
import net.rcarz.jiraclient.greenhopper.SprintIssue;
import net.rcarz.jiraclient.greenhopper.SprintReport;
public class Example {
public static void main(String[] args) {
BasicCredentials creds = new BasicCredentials("batman", "pow! pow!");
JiraClient jira = new JiraClient("https://jira.example.com/jira", creds);
GreenHopperClient gh = new GreenHopperClient(jira);
try {
/* Retrieve all Rapid Boards */
List<RapidView> allRapidBoards = gh.getRapidViews();
/* Retrieve a specific Rapid Board by ID */
RapidView board = gh.getRapidView(123);
/* Print the name of all current and past sprints */
List<Sprint> sprints = board.getSprints();
for (Sprint s : sprints)
System.out.println(s);
/* Get the sprint report, print the sprint start date
and the number of completed issues */
SprintReport sr = board.getSprintReport();
System.out.println(sr.getSprint().getStartDate());
System.out.println(sr.getCompletedIssues().size());
/* Get backlog data */
Backlog backlog = board.getBacklogData();
/* Print epic names */
for (Epic e : backlog.getEpics())
System.out.println(e);
/* Print all issues in the backlog */
for (SprintIssue si : backlog.getIssues())
System.out.println(si);
/* Print the names of sprints that haven't started yet */
for (Marker m : backlog.getMarkers())
System.out.println(m);
/* Get the first issue on the backlog and add a comment */
SprintIssue firstIssue = backlog.getIssues().get(0);
Issue jiraIssue = firstIssue.getJiraIssue();
jiraIssue.addComment("a comment!");
} catch (JiraException ex) {
System.err.println(ex.getMessage());
if (ex.getCause() != null)
System.err.println(ex.getCause().getMessage());
}
}
```

23
pom.xml
View File

@ -38,20 +38,20 @@
</parent>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.2.5</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
@ -60,5 +60,12 @@
<scope>compile</scope>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.3</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@ -146,6 +146,22 @@ public final class Field {
return result;
}
/**
* Gets an floating-point number from the given object.
*
* @param i an Double instance
*
* @return an floating-point number or null if i isn't a Double instance
*/
public static Double getDouble(Object i) {
Double result = null;
if (i instanceof Double)
result = (Double)i;
return result;
}
/**
* Gets an integer from the given object.
*
@ -293,41 +309,11 @@ public final class Field {
List<T> results = new ArrayList<T>();
if (ra instanceof JSONArray) {
for (Object v : (JSONArray)ra)
if (type == Attachment.class)
results.add((T)new Attachment(restclient, (JSONObject)v));
else if (type == Comment.class)
results.add((T)new Comment(restclient, (JSONObject)v));
else if (type == Component.class)
results.add((T)new Component(restclient, (JSONObject)v));
else if (type == CustomFieldOption.class)
results.add((T)new CustomFieldOption(restclient, (JSONObject)v));
else if (type == Issue.class)
results.add((T)new Issue(restclient, (JSONObject)v));
else if (type == IssueLink.class)
results.add((T)new IssueLink(restclient, (JSONObject)v));
else if (type == IssueType.class)
results.add((T)new IssueType(restclient, (JSONObject)v));
else if (type == LinkType.class)
results.add((T)new LinkType(restclient, (JSONObject)v));
else if (type == Priority.class)
results.add((T)new Priority(restclient, (JSONObject)v));
else if (type == Project.class)
results.add((T)new Project(restclient, (JSONObject)v));
else if (type == Resolution.class)
results.add((T)new Resolution(restclient, (JSONObject)v));
else if (type == Status.class)
results.add((T)new Status(restclient, (JSONObject)v));
else if (type == User.class)
results.add((T)new User(restclient, (JSONObject)v));
else if (type == Version.class)
results.add((T)new Version(restclient, (JSONObject)v));
else if (type == Votes.class)
results.add((T)new Votes(restclient, (JSONObject)v));
else if (type == Watches.class)
results.add((T)new Watches(restclient, (JSONObject)v));
else if (type == WorkLog.class)
results.add((T)new WorkLog(restclient, (JSONObject)v));
for (Object v : (JSONArray)ra) {
T item = getResource(type, v, restclient);
if (item != null)
results.add(item);
}
}
return results;

View File

@ -113,17 +113,17 @@ public class JiraClient {
*
* @throws JiraException when the search fails
*/
public List<CustomFieldOption> getCustomFieldAllowedValues(String field, String project, String issueType) throws JiraException {
JSONObject createMetadata = (JSONObject) Issue.getCreateMetadata(restclient, project, issueType);
JSONObject fieldMetadata = (JSONObject) createMetadata.get(field);
List<CustomFieldOption> customFieldOptions = Field.getResourceArray(
CustomFieldOption.class,
fieldMetadata.get("allowedValues"),
restclient
public List<CustomFieldOption> getCustomFieldAllowedValues(String field, String project, String issueType) throws JiraException {
JSONObject createMetadata = (JSONObject) Issue.getCreateMetadata(restclient, project, issueType);
JSONObject fieldMetadata = (JSONObject) createMetadata.get(field);
List<CustomFieldOption> customFieldOptions = Field.getResourceArray(
CustomFieldOption.class,
fieldMetadata.get("allowedValues"),
restclient
);
return customFieldOptions;
}
return customFieldOptions;
}
/**
* Get a list of options for a components
*
@ -134,16 +134,16 @@ public class JiraClient {
*
* @throws JiraException when the search fails
*/
public List<Component> getComponentsAllowedValues(String project, String issueType) throws JiraException {
JSONObject createMetadata = (JSONObject) Issue.getCreateMetadata(restclient, project, issueType);
JSONObject fieldMetadata = (JSONObject) createMetadata.get(Field.COMPONENTS);
List<Component> componentOptions = Field.getResourceArray(
Component.class,
fieldMetadata.get("allowedValues"),
restclient
public List<Component> getComponentsAllowedValues(String project, String issueType) throws JiraException {
JSONObject createMetadata = (JSONObject) Issue.getCreateMetadata(restclient, project, issueType);
JSONObject fieldMetadata = (JSONObject) createMetadata.get(Field.COMPONENTS);
List<Component> componentOptions = Field.getResourceArray(
Component.class,
fieldMetadata.get("allowedValues"),
restclient
);
return componentOptions;
}
return componentOptions;
}
public RestClient getRestClient() {
return restclient;

View File

@ -19,13 +19,6 @@
package net.rcarz.jiraclient;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
/**
* A base class for JIRA resources.
*/
@ -34,7 +27,6 @@ public abstract class Resource {
protected static final String RESOURCE_URI = "/rest/api/2/";
protected RestClient restclient = null;
protected String id = null;
protected String self = null;

View File

@ -0,0 +1,206 @@
/**
* jira-client - a simple JIRA REST client
* Copyright (c) 2013 Bob Carroll (bob.carroll@alum.rit.edu)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.rcarz.jiraclient.greenhopper;
import net.rcarz.jiraclient.Field;
import net.rcarz.jiraclient.JiraException;
import net.rcarz.jiraclient.RestClient;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* GreenHopper backlog data.
*/
public final class Backlog {
private RestClient restclient = null;
private List<SprintIssue> issues = null;
private int rankCustomFieldId = 0;
private List<Sprint> openSprints = null;
private List<RapidViewProject> projects = null;
private List<Marker> markers = null;
private List<Epic> epics = null;
private boolean canEditEpics = false;
private boolean canManageSprints = false;
private boolean maxIssuesExceeded = false;
private int queryResultLimit = 0;
private Map<String, List<RapidViewVersion>> versionsPerProject = null;
/**
* Creates the backlog from a JSON payload.
*
* @param restclient REST client instance
* @param json JSON payload
*/
protected Backlog(RestClient restclient, JSONObject json) {
this.restclient = restclient;
if (json != null)
deserialise(json);
}
private void deserialise(JSONObject json) {
Map map = json;
issues = GreenHopperField.getResourceArray(
SprintIssue.class,
map.get("issues"),
restclient);
rankCustomFieldId = Field.getInteger(map.get("rankCustomFieldId"));
openSprints = GreenHopperField.getResourceArray(
Sprint.class,
map.get("openSprints"),
restclient);
projects = GreenHopperField.getResourceArray(
RapidViewProject.class,
map.get("projects"),
restclient);
markers = GreenHopperField.getResourceArray(
Marker.class,
map.get("markers"),
restclient);
canManageSprints = Field.getBoolean(map.get("canManageSprints"));
maxIssuesExceeded = Field.getBoolean(map.get("maxIssuesExceeded"));
queryResultLimit = Field.getInteger(map.get("queryResultLimit"));
if (map.containsKey("epicData") && map.get("epicData") instanceof JSONObject) {
Map epicData = (Map)map.get("epicData");
epics = GreenHopperField.getResourceArray(Epic.class, epicData.get("epics"), restclient);
canEditEpics = Field.getBoolean(epicData.get("canEditEpics"));
}
if (map.containsKey("versionData") && map.get("versionData") instanceof JSONObject) {
Map verData = (JSONObject)map.get("versionData");
if (verData.containsKey("versionsPerProject") &&
verData.get("versionsPerProject") instanceof JSONObject) {
Map verMap = (Map)verData.get("versionsPerProject");
versionsPerProject = new HashMap<String, List<RapidViewVersion>>();
for (Map.Entry<String, Object> kvp :
(Iterable<Map.Entry<String, Object>>)verMap.entrySet()) {
if (!(kvp.getValue() instanceof JSONArray))
continue;
List<RapidViewVersion> versions = new ArrayList<RapidViewVersion>();
for (Object item : (JSONArray)kvp.getValue()) {
if (!(item instanceof JSONObject))
continue;
RapidViewVersion ver = new RapidViewVersion(restclient, (JSONObject)item);
versions.add(ver);
}
versionsPerProject.put(kvp.getKey(), versions);
}
}
}
}
/**
* Retrieves the backlog data for the given rapid view.
*
* @param restclient REST client instance
* @param rv Rapid View instance
*
* @return the backlog
*
* @throws JiraException when the retrieval fails
*/
public static Backlog get(RestClient restclient, RapidView rv)
throws JiraException {
final int rvId = rv.getId();
JSON result = null;
try {
URI reporturi = restclient.buildURI(
GreenHopperResource.RESOURCE_URI + "xboard/plan/backlog/data",
new HashMap<String, String>() {{
put("rapidViewId", Integer.toString(rvId));
}});
result = restclient.get(reporturi);
} catch (Exception ex) {
throw new JiraException("Failed to retrieve backlog data", ex);
}
if (!(result instanceof JSONObject))
throw new JiraException("JSON payload is malformed");
return new Backlog(restclient, (JSONObject)result);
}
public List<SprintIssue> getIssues() {
return issues;
}
public int getRankCustomFieldId() {
return rankCustomFieldId;
}
public List<Sprint> getOpenSprints() {
return openSprints;
}
public List<RapidViewProject> getProjects() {
return projects;
}
public List<Marker> getMarkers() {
return markers;
}
public List<Epic> getEpics() {
return epics;
}
public boolean canEditEpics() {
return canEditEpics;
}
public boolean canManageSprints() {
return canManageSprints;
}
public boolean maxIssuesExceeded() {
return maxIssuesExceeded;
}
public int queryResultLimit() {
return queryResultLimit;
}
public Map<String, List<RapidViewVersion>> getVersionsPerProject() {
return versionsPerProject;
}
}

View File

@ -0,0 +1,74 @@
/**
* jira-client - a simple JIRA REST client
* Copyright (c) 2013 Bob Carroll (bob.carroll@alum.rit.edu)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.rcarz.jiraclient.greenhopper;
import net.rcarz.jiraclient.Field;
import net.rcarz.jiraclient.Issue;
import net.rcarz.jiraclient.JiraException;
import net.rcarz.jiraclient.RestClient;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
/**
* Represents a GreenHopper epic issue.
*/
public final class Epic extends GreenHopperIssue {
public String epicLabel = null;
public String epicColour = null;
public EpicStats epicStats = null;
/**
* Creates an epic issue from a JSON payload.
*
* @param restclient REST client instance
* @param json JSON payload
*/
protected Epic(RestClient restclient, JSONObject json) {
super(restclient, json);
if (json != null)
deserialise(json);
}
private void deserialise(JSONObject json) {
Map map = json;
epicLabel = Field.getString(map.get("epicLabel"));
epicColour = Field.getString(map.get("epicColor"));
epicStats = GreenHopperField.getEpicStats(map.get("epicStats"));
}
public String getEpicLabel() {
return epicLabel;
}
public String getEpicColour() {
return epicColour;
}
public EpicStats getEpicStats() {
return epicStats;
}
}

View File

@ -0,0 +1,80 @@
/**
* jira-client - a simple JIRA REST client
* Copyright (c) 2013 Bob Carroll (bob.carroll@alum.rit.edu)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.rcarz.jiraclient.greenhopper;
import net.rcarz.jiraclient.Field;
import java.util.Map;
import net.sf.json.JSONObject;
/**
* GreenHopper epic statistics.
*/
public final class EpicStats {
private Double notDoneEstimate = null;
private Double doneEstimate = null;
private int estimated = 0;
private int notEstimated = 0;
private int notDone = 0;
private int done = 0;
/**
* Creates an estimate sum from a JSON payload.
*
* @param json JSON payload
*/
protected EpicStats(JSONObject json) {
Map map = json;
notDoneEstimate = Field.getDouble(map.get("notDoneEstimate"));
doneEstimate = Field.getDouble(map.get("doneEstimate"));
estimated = Field.getInteger(map.get("estimated"));
notEstimated = Field.getInteger(map.get("notEstimated"));
notDone = Field.getInteger(map.get("notDone"));
done = Field.getInteger(map.get("done"));
}
public Double getNotDoneEstimate() {
return notDoneEstimate;
}
public Double getDoneEstimate() {
return doneEstimate;
}
public int getEstimated() {
return estimated;
}
public int getNotEstimated() {
return notEstimated;
}
public int getNotDone() {
return notDone;
}
public int getDone() {
return done;
}
}

View File

@ -0,0 +1,69 @@
/**
* jira-client - a simple JIRA REST client
* Copyright (c) 2013 Bob Carroll (bob.carroll@alum.rit.edu)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.rcarz.jiraclient.greenhopper;
import net.rcarz.jiraclient.Field;
import java.util.Map;
import net.sf.json.JSONObject;
/**
* GreenHopper estimate statistics for rapid views.
*/
public final class EstimateStatistic {
private String statFieldId = null;
private int statFieldValue = 0;
private String statFieldText = null;
/**
* Creates an estimate statistic from a JSON payload.
*
* @param json JSON payload
*/
protected EstimateStatistic(JSONObject json) {
Map map = json;
statFieldId = Field.getString(map.get("statFieldId"));
if (map.containsKey("statFieldValue") &&
map.get("statFieldValue") instanceof JSONObject) {
Map val = (Map)json.get("statFieldValue");
statFieldValue = Field.getInteger(map.get("value"));
statFieldText = Field.getString(map.get("text"));
}
}
public String getFieldId() {
return statFieldId;
}
public int getFieldValue() {
return statFieldValue;
}
public String getFieldText() {
return statFieldText;
}
}

View File

@ -0,0 +1,56 @@
/**
* jira-client - a simple JIRA REST client
* Copyright (c) 2013 Bob Carroll (bob.carroll@alum.rit.edu)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.rcarz.jiraclient.greenhopper;
import net.rcarz.jiraclient.Field;
import java.util.Map;
import net.sf.json.JSONObject;
/**
* GreenHopper estimate sum for rapid views.
*/
public final class EstimateSum {
private Double value = null;
private String text = null;
/**
* Creates an estimate sum from a JSON payload.
*
* @param json JSON payload
*/
protected EstimateSum(JSONObject json) {
Map map = json;
value = Field.getDouble(map.get("value"));
text = Field.getString(map.get("text"));
}
public Double getValue() {
return value;
}
public String getText() {
return text;
}
}

View File

@ -0,0 +1,68 @@
/**
* jira-client - a simple JIRA REST client
* Copyright (c) 2013 Bob Carroll (bob.carroll@alum.rit.edu)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.rcarz.jiraclient.greenhopper;
import net.rcarz.jiraclient.JiraClient;
import net.rcarz.jiraclient.JiraException;
import net.rcarz.jiraclient.RestClient;
import java.util.List;
/**
* A GreenHopper extension to the JIRA client.
*/
public class GreenHopperClient {
private RestClient restclient = null;
/**
* Creates a GreenHopper client.
*
* @param jira JIRA client
*/
public GreenHopperClient(JiraClient jira) {
restclient = jira.getRestClient();
}
/**
* Retreives the rapid view with the given ID.
*
* @param id Rapid View ID
*
* @return a RapidView instance
*
* @throws JiraException when something goes wrong
*/
public RapidView getRapidView(int id) throws JiraException {
return RapidView.get(restclient, id);
}
/**
* Retreives all rapid views visible to the session user.
*
* @return a list of rapid views
*
* @throws JiraException when something goes wrong
*/
public List<RapidView> getRapidViews() throws JiraException {
return RapidView.getAll(restclient);
}
}

View File

@ -0,0 +1,196 @@
/**
* jira-client - a simple JIRA REST client
* Copyright (c) 2013 Bob Carroll (bob.carroll@alum.rit.edu)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.rcarz.jiraclient.greenhopper;
import net.rcarz.jiraclient.RestClient;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
/**
* Utility functions for translating between JSON and fields.
*/
public final class GreenHopperField {
public static final String DATE_TIME_FORMAT = "d/MMM/yy h:m a";
private GreenHopperField() { }
/**
* Gets a date-time from the given object.
*
* @param dt Date-Time as a string
*
* @return the date-time or null
*/
public static DateTime getDateTime(Object dt) {
return dt != null ?
DateTime.parse((String)dt, DateTimeFormat.forPattern(DATE_TIME_FORMAT)) :
null;
}
/**
* Gets an epic stats object from the given object.
*
* @param es a JSONObject instance
*
* @return a EpicStats instance or null if es isn't a JSONObject instance
*/
public static EpicStats getEpicStats(Object es) {
EpicStats result = null;
if (es instanceof JSONObject && !((JSONObject)es).isNullObject())
result = new EpicStats((JSONObject)es);
return result;
}
/**
* Gets an estimate statistic object from the given object.
*
* @param es a JSONObject instance
*
* @return a EstimateStatistic instance or null if es isn't a JSONObject instance
*/
public static EstimateStatistic getEstimateStatistic(Object es) {
EstimateStatistic result = null;
if (es instanceof JSONObject && !((JSONObject)es).isNullObject())
result = new EstimateStatistic((JSONObject)es);
return result;
}
/**
* Gets an estimate sum object from the given object.
*
* @param es a JSONObject instance
*
* @return a EstimateSum instance or null if es isn't a JSONObject instance
*/
public static EstimateSum getEstimateSum(Object es) {
EstimateSum result = null;
if (es instanceof JSONObject && !((JSONObject)es).isNullObject())
result = new EstimateSum((JSONObject)es);
return result;
}
/**
* Gets a list of integers from the given object.
*
* @param ia a JSONArray instance
*
* @return a list of integers
*/
public static List<Integer> getIntegerArray(Object ia) {
List<Integer> results = new ArrayList<Integer>();
if (ia instanceof JSONArray) {
for (Object v : (JSONArray)ia)
results.add((Integer)v);
}
return results;
}
/**
* Gets a GreenHopper resource from the given object.
*
* @param type Resource data type
* @param r a JSONObject instance
* @param restclient REST client instance
*
* @return a Resource instance or null if r isn't a JSONObject instance
*/
public static <T extends GreenHopperResource> T getResource(
Class<T> type, Object r, RestClient restclient) {
T result = null;
if (r instanceof JSONObject && !((JSONObject)r).isNullObject()) {
if (type == Epic.class)
result = (T)new Epic(restclient, (JSONObject)r);
else if (type == Marker.class)
result = (T)new Marker(restclient, (JSONObject)r);
else if (type == RapidView.class)
result = (T)new RapidView(restclient, (JSONObject)r);
else if (type == RapidViewProject.class)
result = (T)new RapidViewProject(restclient, (JSONObject)r);
else if (type == Sprint.class)
result = (T)new Sprint(restclient, (JSONObject)r);
else if (type == SprintIssue.class)
result = (T)new SprintIssue(restclient, (JSONObject)r);
}
return result;
}
/**
* Gets a list of GreenHopper resources from the given object.
*
* @param type Resource data type
* @param ra a JSONArray instance
* @param restclient REST client instance
*
* @return a list of Resources found in ra
*/
public static <T extends GreenHopperResource> List<T> getResourceArray(
Class<T> type, Object ra, RestClient restclient) {
List<T> results = new ArrayList<T>();
if (ra instanceof JSONArray) {
for (Object v : (JSONArray)ra) {
T item = getResource(type, v, restclient);
if (item != null)
results.add(item);
}
}
return results;
}
/**
* Gets a list of strings from the given object.
*
* @param ia a JSONArray instance
*
* @return a list of strings
*/
public static List<String> getStringArray(Object ia) {
List<String> results = new ArrayList<String>();
if (ia instanceof JSONArray) {
for (Object v : (JSONArray)ia)
results.add((String)v);
}
return results;
}
}

View File

@ -0,0 +1,181 @@
/**
* jira-client - a simple JIRA REST client
* Copyright (c) 2013 Bob Carroll (bob.carroll@alum.rit.edu)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.rcarz.jiraclient.greenhopper;
import net.rcarz.jiraclient.Field;
import net.rcarz.jiraclient.Issue;
import net.rcarz.jiraclient.JiraException;
import net.rcarz.jiraclient.RestClient;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
/**
* A base class for GreenHopper issues.
*/
public abstract class GreenHopperIssue extends GreenHopperResource {
private String key = null;
private boolean hidden = false;
private String summary = null;
private String typeName = null;
private String typeId = null;
private String typeUrl = null;
private String priorityUrl = null;
private String priorityName = null;
private boolean done = false;
private String assignee = null;
private String assigneeName = null;
private String avatarUrl = null;
private String colour = null;
private String statusId = null;
private String statusName = null;
private String statusUrl = null;
private List<Integer> fixVersions = null;
private int projectId = 0;
/**
* Creates an issue from a JSON payload.
*
* @param restclient REST client instance
* @param json JSON payload
*/
protected GreenHopperIssue(RestClient restclient, JSONObject json) {
super(restclient);
if (json != null)
deserialise(json);
}
private void deserialise(JSONObject json) {
Map map = json;
id = Field.getInteger(map.get("id"));
key = Field.getString(map.get("key"));
hidden = Field.getBoolean(map.get("hidden"));
summary = Field.getString(map.get("summary"));
typeName = Field.getString(map.get("key"));
typeId = Field.getString(map.get("typeId"));
typeUrl = Field.getString(map.get("typeUrl"));
priorityUrl = Field.getString(map.get("priorityUrl"));
priorityName = Field.getString(map.get("priorityName"));
done = Field.getBoolean(map.get("done"));
assignee = Field.getString(map.get("assignee"));
assigneeName = Field.getString(map.get("assigneeName"));
avatarUrl = Field.getString(map.get("avatarUrl"));
colour = Field.getString(map.get("color"));
statusId = Field.getString(map.get("statusId"));
statusName = Field.getString(map.get("statusName"));
statusUrl = Field.getString(map.get("statusUrl"));
fixVersions = GreenHopperField.getIntegerArray(map.get("fixVersions"));
projectId = Field.getInteger(map.get("projectId"));
}
/**
* Retrieves the full JIRA issue.
*
* @return an Issue
*
* @throws JiraException when the retrieval fails
*/
public Issue getJiraIssue() throws JiraException {
return Issue.get(restclient, key);
}
@Override
public String toString() {
return key;
}
public String getKey() {
return key;
}
public Boolean isHidden() {
return hidden;
}
public String getSummary() {
return summary;
}
public String getTypeName() {
return typeName;
}
public String getTypeId() {
return typeId;
}
public String getTypeUrl() {
return typeUrl;
}
public String getPriorityUrl() {
return priorityUrl;
}
public String getPriorityName() {
return priorityName;
}
public Boolean isDone() {
return done;
}
public String getAssignee() {
return assignee;
}
public String getAssigneeName() {
return assigneeName;
}
public String getAvatarUrl() {
return avatarUrl;
}
public String getColour() {
return colour;
}
public String getStatusId() {
return statusId;
}
public String getStatusName() {
return statusName;
}
public String getStatusUrl() {
return statusUrl;
}
public List<Integer> getFixVersions() {
return fixVersions;
}
public int getProjectId() {
return projectId;
}
}

View File

@ -0,0 +1,50 @@
/**
* jira-client - a simple JIRA REST client
* Copyright (c) 2013 Bob Carroll (bob.carroll@alum.rit.edu)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.rcarz.jiraclient.greenhopper;
import net.rcarz.jiraclient.RestClient;
/**
* A base class for GreenHopper resources.
*/
public abstract class GreenHopperResource {
protected static final String RESOURCE_URI = "/rest/greenhopper/1.0/";
protected RestClient restclient = null;
protected int id = 0;
/**
* Creates a new GreenHopper resource.
*
* @param restclient REST client instance
*/
public GreenHopperResource(RestClient restclient) {
this.restclient = restclient;
}
/**
* Internal JIRA ID.
*/
public int getId() {
return id;
}
}

View File

@ -0,0 +1,65 @@
/**
* jira-client - a simple JIRA REST client
* Copyright (c) 2013 Bob Carroll (bob.carroll@alum.rit.edu)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.rcarz.jiraclient.greenhopper;
import net.rcarz.jiraclient.Field;
import net.rcarz.jiraclient.RestClient;
import java.util.Map;
import net.sf.json.JSONObject;
/**
* Represents a GreenHopper marker (a sprint that hasn't started).
*/
public final class Marker extends GreenHopperResource {
private String name = null;
/**
* Creates a marker from a JSON payload.
*
* @param restclient REST client instance
* @param json JSON payload
*/
protected Marker(RestClient restclient, JSONObject json) {
super(restclient);
if (json != null)
deserialise(json);
}
private void deserialise(JSONObject json) {
Map map = json;
id = Field.getInteger(map.get("id"));
name = Field.getString(map.get("name"));
}
@Override
public String toString() {
return name;
}
public String getName() {
return name;
}
}

View File

@ -0,0 +1,199 @@
/**
* jira-client - a simple JIRA REST client
* Copyright (c) 2013 Bob Carroll (bob.carroll@alum.rit.edu)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.rcarz.jiraclient.greenhopper;
import net.rcarz.jiraclient.Field;
import net.rcarz.jiraclient.JiraException;
import net.rcarz.jiraclient.RestClient;
import java.util.List;
import java.util.Map;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* Represents a GreenHopper Rapid Board.
*/
public final class RapidView extends GreenHopperResource {
private String name = null;
private boolean canEdit = false;
private boolean sprintSupportEnabled = false;
/**
* Creates a rapid view from a JSON payload.
*
* @param restclient REST client instance
* @param json JSON payload
*/
protected RapidView(RestClient restclient, JSONObject json) {
super(restclient);
if (json != null)
deserialise(json);
}
private void deserialise(JSONObject json) {
Map map = json;
id = Field.getInteger(map.get("id"));
name = Field.getString(map.get("name"));
canEdit = Field.getBoolean(map.get("canEdit"));
sprintSupportEnabled = Field.getBoolean(map.get("sprintSupportEnabled"));
}
/**
* Retrieves the given rapid view.
*
* @param restclient REST client instance
* @param id Internal JIRA ID of the rapid view
*
* @return a rapid view instance
*
* @throws JiraException when the retrieval fails
*/
public static RapidView get(RestClient restclient, int id)
throws JiraException {
JSON result = null;
try {
result = restclient.get(RESOURCE_URI + "rapidview/" + id);
} catch (Exception ex) {
throw new JiraException("Failed to retrieve rapid view " + id, ex);
}
if (!(result instanceof JSONObject))
throw new JiraException("JSON payload is malformed");
return new RapidView(restclient, (JSONObject)result);
}
/**
* Retrieves all rapid views visible to the session user.
*
* @param restclient REST client instance
*
* @return a list of rapid views
*
* @throws JiraException when the retrieval fails
*/
public static List<RapidView> getAll(RestClient restclient)
throws JiraException {
JSON result = null;
try {
result = restclient.get(RESOURCE_URI + "rapidview");
} catch (Exception ex) {
throw new JiraException("Failed to retrieve rapid views", ex);
}
if (!(result instanceof JSONObject))
throw new JiraException("JSON payload is malformed");
JSONObject jo = (JSONObject)result;
if (!jo.containsKey("views") || !(jo.get("views") instanceof JSONArray))
throw new JiraException("Rapid View result is malformed");
return GreenHopperField.getResourceArray(
RapidView.class,
jo.get("views"),
restclient
);
}
/**
* Retrieves all sprints associated with this rapid view.
*
* @return a list of sprints
*
* @throws JiraException when the retrieval fails
*/
public List<Sprint> getSprints() throws JiraException {
JSON result = null;
try {
System.out.println(getId());
result = restclient.get(RESOURCE_URI + "sprints/" + id);
} catch (Exception ex) {
throw new JiraException("Failed to retrieve sprints", ex);
}
if (!(result instanceof JSONObject))
throw new JiraException("JSON payload is malformed");
JSONObject jo = (JSONObject)result;
if (!jo.containsKey("sprints") || !(jo.get("sprints") instanceof JSONArray))
throw new JiraException("Sprints result is malformed");
return GreenHopperField.getResourceArray(
Sprint.class,
jo.get("sprints"),
restclient
);
}
/**
* Retrieves the sprint report for the given sprint.
*
* @param sprint Sprint to lookup
*
* @return the sprint report
*
* @throws JiraException when the retrieval fails
*/
public SprintReport getSprintReport(Sprint sprint) throws JiraException {
return SprintReport.get(restclient, this, sprint);
}
/**
* Retrieves the backlog data for this rapid view.
*
* @return the backlog
*
* @throws JiraException when the retrieval fails
*/
public Backlog getBacklogData() throws JiraException {
return Backlog.get(restclient, this);
}
@Override
public String toString() {
return name;
}
public String getName() {
return name;
}
public Boolean canEdit() {
return canEdit;
}
public Boolean isSprintSupportEnabled() {
return sprintSupportEnabled;
}
}

View File

@ -0,0 +1,84 @@
/**
* jira-client - a simple JIRA REST client
* Copyright (c) 2013 Bob Carroll (bob.carroll@alum.rit.edu)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.rcarz.jiraclient.greenhopper;
import net.rcarz.jiraclient.Field;
import net.rcarz.jiraclient.JiraException;
import net.rcarz.jiraclient.Project;
import net.rcarz.jiraclient.RestClient;
import java.util.Map;
import net.sf.json.JSONObject;
/**
* Represents a GreenHopper JIRA project.
*/
public final class RapidViewProject extends GreenHopperResource {
private String key = null;
private String name = null;
/**
* Creates a project from a JSON payload.
*
* @param restclient REST client instance
* @param json JSON payload
*/
protected RapidViewProject(RestClient restclient, JSONObject json) {
super(restclient);
if (json != null)
deserialise(json);
}
private void deserialise(JSONObject json) {
Map map = json;
id = Field.getInteger(map.get("id"));
key = Field.getString(map.get("key"));
name = Field.getString(map.get("name"));
}
/**
* Retrieves the full JIRA project.
*
* @return a Project
*
* @throws JiraException when the retrieval fails
*/
public Project getJiraProject() throws JiraException {
return Project.get(restclient, key);
}
@Override
public String toString() {
return key;
}
public String getKey() {
return key;
}
public String getName() {
return name;
}
}

View File

@ -0,0 +1,90 @@
/**
* jira-client - a simple JIRA REST client
* Copyright (c) 2013 Bob Carroll (bob.carroll@alum.rit.edu)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.rcarz.jiraclient.greenhopper;
import net.rcarz.jiraclient.Field;
import net.rcarz.jiraclient.JiraException;
import net.rcarz.jiraclient.RestClient;
import net.rcarz.jiraclient.Version;
import java.util.Map;
import net.sf.json.JSONObject;
/**
* Represents a GreenHopper JIRA project version.
*/
public final class RapidViewVersion extends GreenHopperResource {
private String name = null;
private int sequence = 0;
private boolean released = false;
/**
* Creates a version from a JSON payload.
*
* @param restclient REST client instance
* @param json JSON payload
*/
protected RapidViewVersion(RestClient restclient, JSONObject json) {
super(restclient);
if (json != null)
deserialise(json);
}
private void deserialise(JSONObject json) {
Map map = json;
id = Field.getInteger(map.get("id"));
name = Field.getString(map.get("name"));
sequence = Field.getInteger(map.get("sequence"));
released = Field.getBoolean(map.get("released"));
}
/**
* Retrieves the full JIRA version.
*
* @return a Version
*
* @throws JiraException when the retrieval fails
*/
public Version getJiraVersion() throws JiraException {
return Version.get(restclient, Integer.toString(id));
}
@Override
public String toString() {
return name;
}
public String getName() {
return name;
}
public int getSequence() {
return sequence;
}
public boolean isReleased() {
return released;
}
}

View File

@ -0,0 +1,91 @@
/**
* jira-client - a simple JIRA REST client
* Copyright (c) 2013 Bob Carroll (bob.carroll@alum.rit.edu)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.rcarz.jiraclient.greenhopper;
import net.rcarz.jiraclient.Field;
import net.rcarz.jiraclient.RestClient;
import java.util.Map;
import net.sf.json.JSONObject;
import org.joda.time.DateTime;
/**
* Represents a GreenHopper sprint.
*/
public final class Sprint extends GreenHopperResource {
private String name = null;
private boolean closed = false;
private DateTime startDate = null;
private DateTime endDate = null;
private DateTime completeDate = null;
/**
* Creates a sprint from a JSON payload.
*
* @param restclient REST client instance
* @param json JSON payload
*/
protected Sprint(RestClient restclient, JSONObject json) {
super(restclient);
if (json != null)
deserialise(json);
}
private void deserialise(JSONObject json) {
Map map = json;
id = Field.getInteger(map.get("id"));
name = Field.getString(map.get("name"));
closed = Field.getBoolean(map.get("closed"));
startDate = GreenHopperField.getDateTime(map.get("startDate"));
endDate = GreenHopperField.getDateTime(map.get("endDate"));
completeDate = GreenHopperField.getDateTime(map.get("completeDate"));
}
@Override
public String toString() {
return name;
}
public String getName() {
return name;
}
public Boolean isClosed() {
return closed;
}
public DateTime getStartDate() {
return startDate;
}
public DateTime getEndDate() {
return endDate;
}
public DateTime getCompleteDate() {
return completeDate;
}
}

View File

@ -0,0 +1,65 @@
/**
* jira-client - a simple JIRA REST client
* Copyright (c) 2013 Bob Carroll (bob.carroll@alum.rit.edu)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.rcarz.jiraclient.greenhopper;
import net.rcarz.jiraclient.Field;
import net.rcarz.jiraclient.RestClient;
import java.util.Map;
import net.sf.json.JSONObject;
/**
* Represents a GreenHopper sprint issue.
*/
public final class SprintIssue extends GreenHopperIssue {
private String epic = null;
private EstimateStatistic estimateStatistic = null;
/**
* Creates a sprint issue from a JSON payload.
*
* @param restclient REST client instance
* @param json JSON payload
*/
protected SprintIssue(RestClient restclient, JSONObject json) {
super(restclient, json);
if (json != null)
deserialise(json);
}
private void deserialise(JSONObject json) {
Map map = json;
epic = Field.getString(map.get("epic"));
estimateStatistic = GreenHopperField.getEstimateStatistic(map.get("estimateStatistic"));
}
public String getEpic() {
return epic;
}
public EstimateStatistic getEstimateStatistic() {
return estimateStatistic;
}
}

View File

@ -0,0 +1,170 @@
/**
* jira-client - a simple JIRA REST client
* Copyright (c) 2013 Bob Carroll (bob.carroll@alum.rit.edu)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.rcarz.jiraclient.greenhopper;
import net.rcarz.jiraclient.Field;
import net.rcarz.jiraclient.Issue;
import net.rcarz.jiraclient.JiraException;
import net.rcarz.jiraclient.RestClient;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
/**
* GreenHopper sprint statistics.
*/
public final class SprintReport {
private RestClient restclient = null;
private Sprint sprint = null;
private List<SprintIssue> completedIssues = null;
private List<SprintIssue> incompletedIssues = null;
private List<SprintIssue> puntedIssues = null;
private EstimateSum completedIssuesEstimateSum = null;
private EstimateSum incompletedIssuesEstimateSum = null;
private EstimateSum allIssuesEstimateSum = null;
private EstimateSum puntedIssuesEstimateSum = null;
private List<String> issueKeysAddedDuringSprint = null;
/**
* Creates a sprint report from a JSON payload.
*
* @param restclient REST client instance
* @param json JSON payload
*/
protected SprintReport(RestClient restclient, JSONObject json) {
this.restclient = restclient;
if (json != null)
deserialise(json);
}
private void deserialise(JSONObject json) {
Map map = json;
sprint = GreenHopperField.getResource(Sprint.class, map.get("sprint"), restclient);
completedIssues = GreenHopperField.getResourceArray(
SprintIssue.class,
map.get("completedIssues"),
restclient);
incompletedIssues = GreenHopperField.getResourceArray(
SprintIssue.class,
map.get("incompletedIssues"),
restclient);
puntedIssues = GreenHopperField.getResourceArray(
SprintIssue.class,
map.get("puntedIssues"),
restclient);
completedIssuesEstimateSum = GreenHopperField.getEstimateSum(
map.get("completedIssuesEstimateSum"));
incompletedIssuesEstimateSum = GreenHopperField.getEstimateSum(
map.get("incompletedIssuesEstimateSum"));
allIssuesEstimateSum = GreenHopperField.getEstimateSum(
map.get("allIssuesEstimateSum"));
puntedIssuesEstimateSum = GreenHopperField.getEstimateSum(
map.get("puntedIssuesEstimateSum"));
issueKeysAddedDuringSprint = GreenHopperField.getStringArray(
map.get("issueKeysAddedDuringSprint"));
}
/**
* Retrieves the sprint report for the given rapid view and sprint.
*
* @param restclient REST client instance
* @param rv Rapid View instance
* @param sprint Sprint instance
*
* @return the sprint report
*
* @throws JiraException when the retrieval fails
*/
public static SprintReport get(RestClient restclient, RapidView rv, Sprint sprint)
throws JiraException {
final int rvId = rv.getId();
final int sprintId = sprint.getId();
JSON result = null;
try {
URI reporturi = restclient.buildURI(
GreenHopperResource.RESOURCE_URI + "rapid/charts/sprintreport",
new HashMap<String, String>() {{
put("rapidViewId", Integer.toString(rvId));
put("sprintId", Integer.toString(sprintId));
}});
result = restclient.get(reporturi);
} catch (Exception ex) {
throw new JiraException("Failed to retrieve sprint report", ex);
}
if (!(result instanceof JSONObject))
throw new JiraException("JSON payload is malformed");
JSONObject jo = (JSONObject)result;
if (!jo.containsKey("contents") || !(jo.get("contents") instanceof JSONObject))
throw new JiraException("Sprint report content is malformed");
return new SprintReport(restclient, (JSONObject)jo.get("contents"));
}
public Sprint getSprint() {
return sprint;
}
public List<SprintIssue> getCompletedIssues() {
return completedIssues;
}
public List<SprintIssue> getIncompletedIssues() {
return incompletedIssues;
}
public List<SprintIssue> getPuntedIssues() {
return puntedIssues;
}
public EstimateSum getCompletedIssuesEstimateSum() {
return completedIssuesEstimateSum;
}
public EstimateSum getIncompletedIssuesEstimateSum() {
return incompletedIssuesEstimateSum;
}
public EstimateSum getAllIssuesEstimateSum() {
return allIssuesEstimateSum;
}
public EstimateSum getPuntedIssuesEstimateSum() {
return puntedIssuesEstimateSum;
}
public List<String> getIssueKeysAddedDuringSprint() {
return issueKeysAddedDuringSprint;
}
}