Hi,
I have setup the ScriptRunner Escalation Services in Jira Cloud and managed to transition the issues for testing. I tried to search of code snippets for Jira Cloud to add comments, but am unable to find any.
I should use the REST API v3 endpoint /rest/api/3/issue/{issueIdOrKey}/comment but I'm not very familiar with ScriptRunner scripting yet, so I'm stuck here and would appreciate your help with formatting the body part of the code. Let me know if any part of the code is incorrect as well.
// The rest call to transition the issue
def result = post("/rest/api/2/issue/${issue.key}/transitions")
.header("Content-Type", "application/json")
.body([transition: [id: transitionID]])
.asObject(Map)
// Check if the issue was transitioned correctly
// Log out the issues updated or which failed to update
if (result.status == 204) {
logger.info("The ${issue.key} issue was transitioned by the escalation service.")
// Write a comment in the issue transitioned
// /rest/api/3/issue/{issueIdOrKey}/comment
// NEED HELP WITH FORMATTING THE BODY
def resultComment = post("/rest/api/3/issue/${issue.key}/comment")
.header("Content-Type", "application/json")
.body([paragraph: [])
.asObject(Map)
Thanks and Best Regards,
Marianne
Here is the working code for commenting in an issue.
I just tested it and it works. Let me know if you have any issues.
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
// The payload definition using the Jackson library
JsonNodeFactory jnf = JsonNodeFactory.instance;
ObjectNode payload = jnf.objectNode();
A:{
ObjectNode visibility = payload.putObject("visibility");
B:{
visibility.put("type", "role");
visibility.put("value", "Administrators");
}
ObjectNode body = payload.putObject("body");
C:{
body.put("type", "doc");
body.put("version", 1);
ArrayNode content = body.putArray("content");
ObjectNode content0 = content.addObject();
D:{
content0.put("type", "paragraph");
content = content0.putArray("content");
content0 = content.addObject();
E:{
content0.put("text", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque eget venenatis elit. Duis eu justo eget augue iaculis fermentum. Sed semper quam laoreet nisi egestas at posuere augue semper.");
content0.put("type", "text");
}
}
}
}
def resultComment = post("/rest/api/3/issue/${issue.key}/comment").header("Content-Type", "application/json").body(payload).asObject(Map)
Please format it accordingly.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
thanks! the code works nicely. however, there has to be a way to implement this a bit leaner/cleaner. It seems a little extensive just to add a comment :-/
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This is the shortest way using the API, which is also recommended by Atlassian:
def
commentResp = post(
"/rest/api/2/issue/${issue.key}/comment"
)
.header(
"Content-Type"
,
"application/json"
)
.body([body:
"COMMENT TEXT"
])
.asObject(Map)
assert
commentResp.status ==
201
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.
Register NowOnline forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.