Forums

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

How can i update the body of a specific comment with Scriptrunner/Groovy?

Markus W_ BENES
Contributor
July 31, 2023
How can i update a specific comment? Until now i have this code that isnt working:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager

String issueKey = "IP-123456"
def commentIDs = ["1234567", "1234689"]

commentIDs.eachWithIndex{ item, index ->  

int commentID = Integer.parseInt(item);
IssueManager issueManager = ComponentAccessor.issueManager
CommentManager commentManager = ComponentAccessor.commentManager
MutableIssue issue = issueManager.getIssueObject(issueKey)
List<Comment> comments = commentManager.getComments(issue)
comments.each {comment ->
    if (comment.id == commentID) {
        commentManager.update(comment, false)
    }
}
}

1 answer

1 accepted

2 votes
Answer accepted
Reece Lander _ScriptRunner - The Adaptavist Group_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 31, 2023

Hey :)

What exactly are you trying to do? The code provided doesn't actually modify the comment body, it just updates the comment with its current content.

Are you on a recent version of ScriptRunner? If you are running ScriptRunner 8.3.0 or later you could use HAPI to simplify this kind of task.

For example with HAPI a script to update specific comment bodies would look something like:

Issues.getByKey('SR-100').comments.each { comment ->
if (comment.id in [10001L, 20003L]) {
comment.update {
body = 'New comment body'
dispatchEvent = false
}
}
}

HAPI - Work with Comments

If you are using an older version of ScriptRunner, you would need to cast the comment to MutableComment, update the body, and then call commentManager.update

Cheers

Markus W_ BENES
Contributor
July 31, 2023

Thank you for your detailed answer; i just need the second option with MutableComment and commentManager.update and your advice gave me a first working solution. Because iam completely new to Scriptrunner/Groovy i actually dont know all methods and commands.

But if i add the line comment.setBody("new body") you indirectly mentioned its working. Thank you :D

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager

String issueKey = "IP-123456"
def commentIDs = ["1234567", "1234689"]

commentIDs.eachWithIndex{ item, index ->  

int commentID = Integer.parseInt(item);
IssueManager issueManager = ComponentAccessor.issueManager
CommentManager commentManager = ComponentAccessor.commentManager
MutableIssue issue = issueManager.getIssueObject(issueKey)
List<Comment> comments = commentManager.getComments(issue)
comments.each {comment ->
    if (comment.id == commentID) {
comment.setBody("new body")
        commentManager.update(comment, false)
    }
}
}

Suggest an answer

Log in or Sign up to answer