1
0
Fork 0

implemented comment updating

master
Bob Carroll 2016-05-30 16:17:04 -07:00
parent dc5ed2e3f1
commit 74c21b5a1f
4 changed files with 126 additions and 12 deletions

View File

@ -127,6 +127,11 @@ public class Example {
/* Now let's start progress on this issue. */
issue.transition().execute("Start Progress");
/* Add the first comment and update it */
Comment comment = issue.addComment("I am a comment!");
comment.update("I am the first comment!");
issue.getComments().get(0).update("this works too!");
/* Pretend customfield_1234 is a text field. Get the raw field value... */
Object cfvalue = issue.getField("customfield_1234");

View File

@ -30,6 +30,7 @@ import java.util.Map;
*/
public class Comment extends Resource {
private String issueKey = null;
private User author = null;
private String body = null;
private Date created = null;
@ -42,9 +43,10 @@ public class Comment extends Resource {
* @param restclient REST client instance
* @param json JSON payload
*/
protected Comment(RestClient restclient, JSONObject json) {
protected Comment(RestClient restclient, JSONObject json, String issueKey) {
super(restclient);
this.issueKey = issueKey;
if (json != null)
deserialise(json);
}
@ -86,7 +88,59 @@ public class Comment extends Resource {
if (!(result instanceof JSONObject))
throw new JiraException("JSON payload is malformed");
return new Comment(restclient, (JSONObject)result);
return new Comment(restclient, (JSONObject)result, issue);
}
/**
* Updates the comment body.
*
* @param issue associated issue record
* @param body Comment text
*
* @throws JiraException when the comment update fails
*/
public void update(String body) throws JiraException {
update(body, null, null);
}
/**
* Updates the comment body with limited visibility.
*
* @param issue associated issue record
* @param body Comment text
* @param visType Target audience type (role or group)
* @param visName Name of the role or group to limit visibility to
*
* @throws JiraException when the comment update fails
*/
public void update(String body, String visType, String visName)
throws JiraException {
JSONObject req = new JSONObject();
req.put("body", body);
if (visType != null && visName != null) {
JSONObject vis = new JSONObject();
vis.put("type", visType);
vis.put("value", visName);
req.put("visibility", vis);
}
JSON result = null;
try {
String issueUri = getBaseUri() + "issue/" + issueKey;
result = restclient.put(issueUri + "/comment/" + id, req);
} catch (Exception ex) {
throw new JiraException("Failed add update comment " + id, ex);
}
if (!(result instanceof JSONObject)) {
throw new JiraException("JSON payload is malformed");
}
deserialise((JSONObject) result);
}
@Override

View File

@ -176,14 +176,22 @@ public final class Field {
*
* @param c a JSONObject instance
* @param restclient REST client instance
* @param issueKey key of the parent issue
*
* @return a list of comments found in c
*/
public static List<Comment> getComments(Object c, RestClient restclient) {
public static List<Comment> getComments(Object c, RestClient restclient,
String issueKey) {
List<Comment> results = new ArrayList<Comment>();
if (c instanceof JSONObject && !((JSONObject)c).isNullObject())
results = getResourceArray(Comment.class, ((Map)c).get("comments"), restclient);
if (c instanceof JSONObject && !((JSONObject)c).isNullObject()) {
results = getResourceArray(
Comment.class,
((Map)c).get("comments"),
restclient,
issueKey
);
}
return results;
}
@ -328,6 +336,22 @@ public final class Field {
public static <T extends Resource> T getResource(
Class<T> type, Object r, RestClient restclient) {
return getResource(type, r, restclient, null);
}
/**
* Gets a JIRA resource from the given object.
*
* @param type Resource data type
* @param r a JSONObject instance
* @param restclient REST client instance
* @param parentId id/key of the parent resource
*
* @return a Resource instance or null if r isn't a JSONObject instance
*/
public static <T extends Resource> T getResource(
Class<T> type, Object r, RestClient restclient, String parentId) {
T result = null;
if (r instanceof JSONObject && !((JSONObject)r).isNullObject()) {
@ -340,7 +364,7 @@ public final class Field {
else if (type == ChangeLogItem.class)
result = (T)new ChangeLogItem(restclient, (JSONObject)r);
else if (type == Comment.class)
result = (T)new Comment(restclient, (JSONObject)r);
result = (T)new Comment(restclient, (JSONObject)r, parentId);
else if (type == Component.class)
result = (T)new Component(restclient, (JSONObject)r);
else if (type == CustomFieldOption.class)
@ -430,11 +454,34 @@ public final class Field {
public static <T extends Resource> List<T> getResourceArray(
Class<T> type, Object ra, RestClient restclient) {
return getResourceArray(type, ra, restclient, null);
}
/**
* Gets a list of JIRA resources from the given object.
*
* @param type Resource data type
* @param ra a JSONArray instance
* @param restclient REST client instance
* @param parentId id/key of the parent resource
*
* @return a list of Resources found in ra
*/
public static <T extends Resource> List<T> getResourceArray(
Class<T> type, Object ra, RestClient restclient, String parentId) {
List<T> results = new ArrayList<T>();
if (ra instanceof JSONArray) {
for (Object v : (JSONArray)ra) {
T item = getResource(type, v, restclient);
T item = null;
if (parentId != null) {
item = getResource(type, v, restclient, parentId);
} else {
item = getResource(type, v, restclient);
}
if (item != null)
results.add(item);
}

View File

@ -818,7 +818,7 @@ public class Issue extends Resource {
assignee = Field.getResource(User.class, fields.get(Field.ASSIGNEE), restclient);
attachments = Field.getResourceArray(Attachment.class, fields.get(Field.ATTACHMENT), restclient);
changeLog = Field.getResource(ChangeLog.class, map.get(Field.CHANGE_LOG), restclient);
comments = Field.getComments(fields.get(Field.COMMENT), restclient);
comments = Field.getComments(fields.get(Field.COMMENT), restclient, key);
components = Field.getResourceArray(Component.class, fields.get(Field.COMPONENTS), restclient);
description = Field.getString(fields.get(Field.DESCRIPTION));
dueDate = Field.getDate(fields.get(Field.DUE_DATE));
@ -1033,8 +1033,8 @@ public class Issue extends Resource {
*
* @throws JiraException when the comment creation fails
*/
public void addComment(String body) throws JiraException {
addComment(body, null, null);
public Comment addComment(String body) throws JiraException {
return addComment(body, null, null);
}
/**
@ -1046,7 +1046,7 @@ public class Issue extends Resource {
*
* @throws JiraException when the comment creation fails
*/
public void addComment(String body, String visType, String visName)
public Comment addComment(String body, String visType, String visName)
throws JiraException {
JSONObject req = new JSONObject();
@ -1060,11 +1060,19 @@ public class Issue extends Resource {
req.put("visibility", vis);
}
JSON result = null;
try {
restclient.post(getRestUri(key) + "/comment", req);
result = restclient.post(getRestUri(key) + "/comment", req);
} catch (Exception ex) {
throw new JiraException("Failed add comment to issue " + key, ex);
}
if (!(result instanceof JSONObject)) {
throw new JiraException("JSON payload is malformed");
}
return new Comment(restclient, (JSONObject) result, key);
}
/**