Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

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

Marianne Lee (Nagarro)
Contributor
December 28, 2019

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
brbojorque
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 28, 2019 edited

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.

Marianne Lee (Nagarro)
Contributor
December 29, 2019

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

BBury April 17, 2020

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
2 votes
Damian
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 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
Alok Kumar October 7, 2022

Thanks @Damian ,

Your code helped me as well.

Cheers!!

Like Damian likes this

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, atlassian government cloud, fedramp, webinar, register for webinar, atlassian cloud webinar, fedramp moderate offering, work faster with cloud

Unlocking the future with Atlassian Government Cloud ☁️

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 Now
AUG Leaders

Atlassian Community Events