1
0
Fork 0

Merge remote-tracking branch 'origin/pr/31'

master
Bob Carroll 2014-05-12 20:19:27 -07:00
commit c15648e30e
2 changed files with 166 additions and 0 deletions

View File

@ -19,8 +19,10 @@
package net.rcarz.jiraclient;
import java.util.HashMap;
import java.util.Map;
import net.rcarz.jiraclient.Issue.FluentCreate;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
@ -28,6 +30,105 @@ import net.sf.json.JSONObject;
* Represents an issue component.
*/
public class Component 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 component.
* @param name the name
* @return <code>this</code>
*/
public FluentCreate name(String name) {
req.put("name", name);
return this;
}
/**
* Sets the description of the component.
* @param description the description
* @return <code>this</code>
*/
public FluentCreate description(String description) {
req.put("description", description);
return this;
}
/**
* Sets the lead user name.
* @param leadUserName the lead user name
* @return <code>this</code>
*/
public FluentCreate leadUserName(String leadUserName) {
req.put("leadUserName", leadUserName);
return this;
}
/**
* Sets the assignee type.
* @param assigneeType the assignee type
* @return <code>this</code>
*/
public FluentCreate assigneeType(String assigneeType) {
req.put("assigneeType", assigneeType);
return this;
}
/**
* Sets whether the assignee type is valid.
* @param assigneeTypeValid is the assignee type valid?
* @return <code>this</code>
*/
public FluentCreate assigneeTypeValue(boolean assigneeTypeValid) {
req.put("isAssigneeTypeValid", assigneeTypeValid);
return this;
}
/**
* Executes the create action.
* @return the created component
*
* @throws JiraException when the create fails
*/
public Component execute() throws JiraException {
JSON result = null;
try {
result = restclient.post(getRestUri(null), req);
} catch (Exception ex) {
throw new JiraException("Failed to create issue", ex);
}
if (!(result instanceof JSONObject) || !((JSONObject) result).containsKey("id")
|| !(((JSONObject) result).get("id") instanceof String)) {
throw new JiraException("Unexpected result on create component");
}
return new Component(restclient, (JSONObject) result);
}
}
private String name = null;
private boolean isAssigneeTypeValid = false;
@ -93,5 +194,22 @@ public class Component extends Resource {
public boolean isAssigneeTypeValid() {
return isAssigneeTypeValid;
}
private static String getRestUri(String id) {
return getBaseUri() + "component/" + (id != null ? id : "");
}
/**
* Creates a new JIRA component.
*
* @param restclient REST client instance
* @param project Key of the project to create the component in
*
* @return a fluent create instance
*/
public static FluentCreate create(RestClient restclient, String project) {
FluentCreate fc = new FluentCreate(restclient, project);
return fc;
}
}

View File

@ -399,4 +399,52 @@ public class JiraClient {
throw new JiraException(ex.getMessage(), ex);
}
}
/**
* Creates a new component in the given project.
*
* @param project Key of the project to create in
*
* @return a fluent create instance
*/
public Component.FluentCreate createComponent(String project) {
return Component.create(restclient, project);
}
/**
* Obtains a component given its ID.
*
* @param id the component ID
*
* @return the component
*
* @throws JiraException failed to obtain the component
*/
public Component getComponent(String id) throws JiraException {
try {
URI uri = restclient.buildURI(Resource.getBaseUri() + "/component/"
+ id);
JSON response = restclient.get(uri);
return new Component(restclient, ((JSONObject) response));
} catch (Exception ex) {
throw new JiraException(ex.getMessage(), ex);
}
}
/**
* Deletes a component from a project.
*
* @param id the component ID
*
* @throws JiraException failed to delete the component
*/
public void deleteComponent(String id) throws JiraException {
try {
URI uri = restclient.buildURI(Resource.getBaseUri() + "component/"
+ id);
JSON response = restclient.delete(uri);
} catch (Exception ex) {
throw new JiraException(ex.getMessage(), ex);
}
}
}