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,365,004
Community Members
 
Community Events
168
Community Groups

I want to clone comments with Scriptrunner for Jira Cloud

How can I create a listener that will clone comments along with an issue? Does it matter that a comment author is an external customer?

Sometimes our JSD issues need extra development support, and I need to clone the issue into a Jira Software project. The comments in the JSD issue are valuable feedback and information that the developer needs to help resolve the issue.  

2 answers

0 votes

You don't have an event for issue clone on Jira cloud, and even when you clone a ticket you can't know what was the original ticket because there is no link between them.

Hi @Neta Elyakim

The creation of the link when you clone is optional. It is done by design of our ScriptRunner team. 

If you have any questions about it let me know! 

Hey@Brittany Wispell,

Thank you for informing me.

@Drew Nedderman

If you have a link between them you can try to use this script-

def getComments = get('/rest/api/2/issue/${issue.key}/comment')
.header('Content-Type', 'application/json')
.asObject(Map)

if (getComments.status == 200){
def comments = getComments.body.comments["body"]

logger.info("comments: "+comments)

if(!comments.isEmpty()){

def issuelinks = issue.fields["issuelinks"]
def inwardIssue = issuelinks["outwardIssue"] // or def inwardIssue = issuelinks["inwardIssue"] you need to check this

def cloneIssue
def requestBody
for(int i=0;i<inwardIssue.size();i++){
// check here the like type if is clone type
cloneIssue = inwardIssue[i]
}
if(cloneIssue != null){
for(int i=0;i<comments.size();i++){
def commentResp = post("/rest/api/2/issue/${cloneIssue.key}/comment")
.header('Content-Type', 'application/json')
.body([body: comments[i].toString()])
.asObject(Map)

assert commentResp.status == 201
}
}
}
}
0 votes

Hey @Drew Nedderman

You could modify this code. It is for a parent subtask relation. 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.Comment

Issue subtask = event.issue
Comment comment = event.comment
def commentManager = ComponentAccessor.getCommentManager()

if (subtask.isSubTask() && comment) {
def parentIssue = subtask.getParentObject()
def commentToParent = commentManager.create(
parentIssue, //the parent issue to copy the comment
comment.getAuthorApplicationUser(), //the author of the subtask's comment
comment.getBody(), //the actual comment body
comment.getGroupLevel(), // is the group name to limit comment visibility to, this must be a valid group name.
comment.getRoleLevelId(), // is the id of the the ProjectRole to limit comment visibility to, this must reference a valid project role.
false) //if true then an event of type EventType.ISSUE_COMMENTED_ID will be dispatched and any notifications listening for that event will be triggered. If false no event will be dispatched.
log.info "Comment copied to parent issue ${parentIssue.getKey()}"
}

 

And here's a snippet of how you'd get the linked issues. 

def originIssue
issueLinkManager.getInwardLinks(issue.id)?.each {issueLink ->
issueLink.sourceId
Issue candidateIssue = issueManager.getIssueObject(issueLink.sourceId)
if(candidateIssue.projectObject.key.equals(cloneParentKey)){
originIssue = candidateIssue

 

I hope this helps get you started.  

Hi @Brittany Wispell, does the code that you provided work for cloud instances? When I past it into the console I get static type checking errors.

 

Unable to resolve class for these lines:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.Comment

 Thank you for your assistance!

Hey @Drew Nedderman

Try adding ; to the end of those lines.

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events