Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,360,112
Community Members
 
Community Events
168
Community Groups

Jira Cloud ScriptRunner - How to add comments to issues in Escalation Services

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

 

2 answers

1 accepted

1 vote
Answer accepted

Hi @Marianne Lee _Nagarro_ ,

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.

Thanks @brbojorque for sharing the code.  It works great!

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 :-/

Like Andre Junior Richard likes this
1 vote
Damian I'm New Here Aug 16, 2022

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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Software

An update on Jira Software customer feedback – June 2022

Hello Atlassian Community! Feedback from customers like you has helped us shape and improve Jira Software. As Head of Product, Jira Software, I wanted to take this opportunity to share an update on...

4,997 views 18 31
Read article

Atlassian Community Events