1
0
Fork 0

added support for creating a new Version for a project.

master
Alfred Sawatzky 2016-02-01 15:25:11 -07:00
parent 12f11ce2c3
commit 480b7a7a57
1 changed files with 123 additions and 0 deletions

View File

@ -22,6 +22,9 @@ package net.rcarz.jiraclient;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
/**
@ -29,6 +32,109 @@ import java.util.Map;
*/
public class Version extends Resource {
/**
* Used to chain fields to a create action.
*/
public static final class FluentCreate {
/**
* The Jira rest client.
*/
RestClient restclient = null;
/**
* The JSON request that will be built incrementally as fluent methods
* are invoked.
*/
JSONObject req = new JSONObject();
/**
* Creates a new fluent.
* @param restclient the REST client
* @param project the project key
*/
private FluentCreate(RestClient restclient, String project) {
this.restclient = restclient;
req.put("project", project);
}
/**
* Sets the name of the version.
* @param name the name
* @return <code>this</code>
*/
public FluentCreate name(String name) {
req.put("name", name);
return this;
}
/**
* Sets the description of the version.
* @param description the description
* @return <code>this</code>
*/
public FluentCreate description(String description) {
req.put("description", description);
return this;
}
/**
* Sets the archived status of the version.
* @param isArchived archived status
* @return <code>this</code>
*/
public FluentCreate archived(boolean isArchived) {
req.put("archived", isArchived);
return this;
}
/**
* Sets the released status of the version.
* @param isReleased released status
* @return <code>this</code>
*/
public FluentCreate released(boolean isReleased) {
req.put("released", isReleased);
return this;
}
/**
* Sets the release date of the version.
* @param releaseDate release Date
* @return <code>this</code>
*/
public FluentCreate releaseDate(Date releaseDate) {
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
req.put("releaseDate", df.format(releaseDate));
return this;
}
/**
* Executes the create action.
* @return the created Version
*
* @throws JiraException when the create fails
*/
public Version execute() throws JiraException {
JSON result = null;
try {
result = restclient.post(getRestUri(null), req);
} catch (Exception ex) {
throw new JiraException("Failed to create version", ex);
}
if (!(result instanceof JSONObject) || !((JSONObject) result).containsKey("id")
|| !(((JSONObject) result).get("id") instanceof String)) {
throw new JiraException("Unexpected result on create version");
}
return new Version(restclient, (JSONObject) result);
}
}
private String name = null;
private boolean archived = false;
private boolean released = false;
@ -156,5 +262,22 @@ public class Version extends Resource {
return description;
}
private static String getRestUri(String id) {
return getBaseUri() + "version/" + (id != null ? id : "");
}
/**
* Creates a new JIRA Version.
*
* @param restclient REST client instance
* @param project Key of the project to create the version in
*
* @return a fluent create instance
*/
public static FluentCreate create(RestClient restclient, String project) {
FluentCreate fc = new FluentCreate(restclient, project);
return fc;
}
}