From 74c21b5a1f660ad479c4f73a5ed266de2b3ceb40 Mon Sep 17 00:00:00 2001 From: Bob Carroll Date: Mon, 30 May 2016 16:17:04 -0700 Subject: [PATCH] implemented comment updating --- README.md | 5 ++ .../java/net/rcarz/jiraclient/Comment.java | 58 ++++++++++++++++++- src/main/java/net/rcarz/jiraclient/Field.java | 57 ++++++++++++++++-- src/main/java/net/rcarz/jiraclient/Issue.java | 18 ++++-- 4 files changed, 126 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index d4e5b4c..404c6ae 100644 --- a/README.md +++ b/README.md @@ -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"); diff --git a/src/main/java/net/rcarz/jiraclient/Comment.java b/src/main/java/net/rcarz/jiraclient/Comment.java index 7d92824..ef53a73 100644 --- a/src/main/java/net/rcarz/jiraclient/Comment.java +++ b/src/main/java/net/rcarz/jiraclient/Comment.java @@ -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 diff --git a/src/main/java/net/rcarz/jiraclient/Field.java b/src/main/java/net/rcarz/jiraclient/Field.java index c0e3700..0ab62f2 100644 --- a/src/main/java/net/rcarz/jiraclient/Field.java +++ b/src/main/java/net/rcarz/jiraclient/Field.java @@ -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 getComments(Object c, RestClient restclient) { + public static List getComments(Object c, RestClient restclient, + String issueKey) { List results = new ArrayList(); - 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 getResource( Class 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 getResource( + Class 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 List getResourceArray( Class 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 List getResourceArray( + Class type, Object ra, RestClient restclient, String parentId) { + List results = new ArrayList(); 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); } diff --git a/src/main/java/net/rcarz/jiraclient/Issue.java b/src/main/java/net/rcarz/jiraclient/Issue.java index d5154a5..1cb586a 100644 --- a/src/main/java/net/rcarz/jiraclient/Issue.java +++ b/src/main/java/net/rcarz/jiraclient/Issue.java @@ -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); } /**