1
0
Fork 0

Merge pull request #11 from chaplinkyle/list-of-transitions

List of possible transitions #68
master
Kyle Chaplin 2015-03-15 20:05:03 -05:00
commit 676d16c7ea
5 changed files with 255 additions and 32 deletions

View File

@ -148,6 +148,7 @@ public final class Field {
public static final String TIME_SPENT = "timespent";
public static final String CREATED_DATE = "created";
public static final String UPDATED_DATE = "updated";
public static final String TRANSITION_TO_STATUS = "to";
public static final String DATE_FORMAT = "yyyy-MM-dd";
public static final String DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
@ -362,6 +363,8 @@ public final class Field {
result = (T)new Resolution(restclient, (JSONObject)r);
else if (type == Status.class)
result = (T)new Status(restclient, (JSONObject)r);
else if (type == Transition.class)
result = (T)new Transition(restclient, (JSONObject)r);
else if (type == User.class)
result = (T)new User(restclient, (JSONObject)r);
else if (type == Version.class)

View File

@ -21,11 +21,7 @@ package net.rcarz.jiraclient;
import java.io.File;
import java.net.URI;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
@ -278,47 +274,37 @@ public class Issue extends Resource {
public final class FluentTransition {
Map<String, Object> fields = new HashMap<String, Object>();
JSONArray transitions = null;
List<Transition> transitions = null;
private FluentTransition(JSONArray transitions) {
private FluentTransition(List<Transition> transitions) {
this.transitions = transitions;
}
private JSONObject getTransition(String id, boolean name) throws JiraException {
JSONObject result = null;
private Transition getTransition(String id, boolean name) throws JiraException {
Transition result = null;
for (Object item : transitions) {
if (!(item instanceof JSONObject) || !((JSONObject)item).containsKey("id"))
throw new JiraException("Transition metadata is malformed");
JSONObject t = (JSONObject)item;
if ((!name && Field.getString(t.get("id")).equals(id)) ||
(name && Field.getString(t.get("name")).equals(id))) {
result = t;
break;
for (Transition transition : transitions) {
if((name && id.equals(transition.getName())
|| (!name && id.equals(transition.getId())))){
result = transition;
}
}
if (result == null)
throw new JiraException("Transition was not found in metadata");
throw new JiraException("Transition was not found.");
return result;
}
private void realExecute(JSONObject trans) throws JiraException {
private void realExecute(Transition trans) throws JiraException {
if (trans.isNullObject() || !trans.containsKey("fields") ||
!(trans.get("fields") instanceof JSONObject))
throw new JiraException("Transition metadata is missing fields");
if (trans == null || trans.getFields() == null)
throw new JiraException("Transition is missing fields");
JSONObject editmeta = (JSONObject)trans.get("fields");
JSONObject fieldmap = new JSONObject();
for (Map.Entry<String, Object> ent : fields.entrySet()) {
Object newval = Field.toJson(ent.getKey(), ent.getValue(), editmeta);
fieldmap.put(ent.getKey(), newval);
fieldmap.put(ent.getKey(), ent.getValue());
}
JSONObject req = new JSONObject();
@ -327,7 +313,7 @@ public class Issue extends Resource {
req.put("fields", fieldmap);
JSONObject t = new JSONObject();
t.put("id", Field.getString(trans.get("id")));
t.put("id", Field.getString(trans.getId()));
req.put("transition", t);
@ -349,6 +335,17 @@ public class Issue extends Resource {
realExecute(getTransition(Integer.toString(id), false));
}
/**
* Executes the transition action.
*
* @param transition Transition
*
* @throws JiraException when the transition fails
*/
public void execute(Transition transition) throws JiraException {
realExecute(transition);
}
/**
* Executes the transition action.
*
@ -537,7 +534,7 @@ public class Issue extends Resource {
return (JSONObject)jo.get("fields");
}
private JSONArray getTransitions() throws JiraException {
public List<Transition> getTransitions() throws JiraException {
JSON result = null;
try {
@ -555,9 +552,17 @@ public class Issue extends Resource {
if (jo.isNullObject() || !jo.containsKey("transitions") ||
!(jo.get("transitions") instanceof JSONArray))
throw new JiraException("Transition metadata is missing from jos");
throw new JiraException("Transition metadata is missing.");
return (JSONArray)jo.get("transitions");
JSONArray transitions = (JSONArray) jo.get("transitions");
List<Transition> trans = new ArrayList<Transition>();
for(Object obj: transitions){
JSONObject ob = (JSONObject) obj;
trans.add(new Transition(restclient, ob));
}
return trans;
}
/**

View File

@ -0,0 +1,79 @@
/**
* 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;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import java.util.List;
import java.util.Map;
/**
* Represents an issue priority.
*/
public class Transition extends Resource {
private String name = null;
private Status toStatus = null;
private Map fields = null;
/**
* Creates a priority from a JSON payload.
*
* @param restclient REST client instance
* @param json JSON payload
*/
protected Transition(RestClient restclient, JSONObject json) {
super(restclient);
if (json != null)
deserialise(json);
}
private void deserialise(JSONObject json) {
Map map = json;
self = Field.getString(map.get("self"));
id = Field.getString(map.get("id"));
name = Field.getString(map.get("name"));
toStatus = Field.getResource(Status.class, map.get(Field.TRANSITION_TO_STATUS), restclient);
fields = (Map)map.get("fields");
}
@Override
public String toString() {
return getName();
}
public String getName() {
return name;
}
public Status getToStatus() {
return toStatus;
}
public Map getFields() {
return fields;
}
}

View File

@ -0,0 +1,135 @@
package net.rcarz.jiraclient;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.Assert;
import org.junit.Test;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
public class TransitionTest {
@Test
public void TransitionInit(){
Transition transition = new Transition(null, getTestJson());
}
@Test
public void testDeserializeJSON(){
Transition transition = new Transition(null, getTestJson());
assertEquals("21", transition.getId());
assertEquals("Done", transition.getName());
}
@Test
public void testToStatus() {
Transition transition = new Transition(null, getTestJson());
Status toStatus = transition.getToStatus();
assertEquals("Done", toStatus.getName());
assertEquals("A description of great importance.", toStatus.getDescription());
}
@Test
public void testTransitionToString() throws URISyntaxException {
Transition transition = new Transition(new RestClient(null, new URI("/123/asd")), getTestJson());
assertEquals("Done", transition.toString());
}
public static JSONObject getTestJson() {
JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(
"{\n" +
" \"id\": \"21\",\n" +
" \"name\": \"Done\",\n" +
" \"to\": {\n" +
" \"self\": \"https://jira-client.atlassian.net/rest/api/2/status/10001\",\n" +
" \"description\": \"A description of great importance.\",\n" +
" \"iconUrl\": \"https://jira-client.atlassian.net/images/icons/statuses/closed.png\",\n" +
" \"name\": \"Done\",\n" +
" \"id\": \"10001\",\n" +
" \"statusCategory\": {\n" +
" \"self\": \"https://jira-client.atlassian.net/rest/api/2/statuscategory/3\",\n" +
" \"id\": 3,\n" +
" \"key\": \"done\",\n" +
" \"colorName\": \"green\",\n" +
" \"name\": \"Done\"\n" +
" }\n" +
" },\n" +
" \"fields\": {\n" +
" \"issuelinks\": {\n" +
" \"required\": false,\n" +
" \"schema\": {\n" +
" \"type\": \"array\",\n" +
" \"items\": \"issuelinks\",\n" +
" \"system\": \"issuelinks\"\n" +
" },\n" +
" \"name\": \"Linked Issues\",\n" +
" \"autoCompleteUrl\": \"https://jira-client.atlassian.net/rest/api/2/issue/picker?currentProjectId\\u003d\\u0026showSubTaskParent\\u003dtrue\\u0026showSubTasks\\u003dtrue\\u0026currentIssueKey\\u003dJIR-1\\u0026query\\u003d\",\n" +
" \"operations\": [\n" +
" \"add\"\n" +
" ]\n" +
" },\n" +
" \"resolution\": {\n" +
" \"required\": true,\n" +
" \"schema\": {\n" +
" \"type\": \"resolution\",\n" +
" \"system\": \"resolution\"\n" +
" },\n" +
" \"name\": \"Resolution\",\n" +
" \"operations\": [\n" +
" \"set\"\n" +
" ],\n" +
" \"allowedValues\": [\n" +
" {\n" +
" \"self\": \"https://jira-client.atlassian.net/rest/api/latest/resolution/1\",\n" +
" \"name\": \"Fixed\",\n" +
" \"id\": \"1\"\n" +
" },\n" +
" {\n" +
" \"self\": \"https://jira-client.atlassian.net/rest/api/latest/resolution/2\",\n" +
" \"name\": \"Won\\u0027t Fix\",\n" +
" \"id\": \"2\"\n" +
" },\n" +
" {\n" +
" \"self\": \"https://jira-client.atlassian.net/rest/api/latest/resolution/3\",\n" +
" \"name\": \"Duplicate\",\n" +
" \"id\": \"3\"\n" +
" },\n" +
" {\n" +
" \"self\": \"https://jira-client.atlassian.net/rest/api/latest/resolution/4\",\n" +
" \"name\": \"Incomplete\",\n" +
" \"id\": \"4\"\n" +
" },\n" +
" {\n" +
" \"self\": \"https://jira-client.atlassian.net/rest/api/latest/resolution/5\",\n" +
" \"name\": \"Cannot Reproduce\",\n" +
" \"id\": \"5\"\n" +
" },\n" +
" {\n" +
" \"self\": \"https://jira-client.atlassian.net/rest/api/latest/resolution/10000\",\n" +
" \"name\": \"Done\",\n" +
" \"id\": \"10000\"\n" +
" },\n" +
" {\n" +
" \"self\": \"https://jira-client.atlassian.net/rest/api/latest/resolution/10001\",\n" +
" \"name\": \"Won\\u0027t Do\",\n" +
" \"id\": \"10001\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
"}");
return jsonObject;
}
}

View File

@ -201,4 +201,5 @@ public class Utils {
return jsonObject;
}
}