[Scriptrunner] When I clone issue I wanna include comments to cloned issue.

Hello, 

The problem we are having is with a cloning issue. From now on, we have a script that will set new description on cloned issue. What we would like to achiev is that comments in original issue would be cloned either. Here is our script. The part with description works fine. Problem is with comments, there are no errors but comments won't show.

import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

def issueManager = ComponentAccessor.getIssueManager()
def linkMgr = ComponentAccessor. getIssueLinkManager()
def result = "Original issue"
def commentManager = ComponentAccessor.getCommentManager()

def commentsList = commentManager.getComments(issue)


for (IssueLink link in linkMgr.getOutwardLinks(issue.id))
{
if( link.issueLinkType.name == "Cloners")
{
def newDescription = ("{color:red}Don't modify this description - edit the original (cloned) issue instead and the description gets copied here. {color}\n\n" + link.getDestinationObject().description) as String
issue.setDescription(newDescription)
issue.store()
result = "Branched issue"

for(def i = 0; i <= commentsList.size()-1; i++)
{
def comment = commentsList.get(i)
commentManager.create(issue,comment.getAuthorApplicationUser(),comment.getBody(),true)
issue.store()
}


}
}
return result

 

 

I am really looking forward for you answer.

Best regards,

Stanislava Caja.

3 answers

0 votes
Joshua Yamdogo Community Champion Jul 17, 2017

Hi Stanislava,

What ScriptRunner version are you running? Writing a script to copy comments to a cloned issue does not seem like something you need to do, because this is already a feature in ScriptRunner. You can add a Script Post-Function for "Clone an Issue and Links." On that page, you can simply check the "Copy Comments" box, which will automatically copy comments from the original issue to the cloned issue. You can put your script to set the description in the "Additional Actions" box. 

Screen Shot 2017-07-17 at 10.27.49 AM.png

Hi, thank you for your reply.

My Script Runner is 5.0.11. 

I do have Clones an issue, and links in my Script Listeners option but what I have more than in your screen is field "events" where I must fill on what event it will "fire" but there is no option like "issue cloned" only like issue created, resolved etc.

 

The better to understand what we are doing is, that we have in "custom fields" scripted field called "Branch Info" and in that Scripted field that script above where we are using "Issue linking" , for better understanding i took screens1.PNG2.PNG3.PNG

0 votes

You could also use this in your script to copy comments from the original issue to the cloned issue:

 

def commentManager = ComponentAccessor.commentManager

doAfterCreate = {

commentManager.getComments(sourceIssue)?.each { comment ->

    commentManager.create(issue, comment.authorApplicationUser, comment.body, comment?.groupLevel, comment?.roleLevel?.id, comment.created, false)
}}

Hi,

this script doesn't work for me, I tried to added to the one above, but only what I get is bunch of errors of not existing methods.

@Joshua Yamdogo [Adaptavist] Is the "copy comments" checkbox available in script listener or just post functions? Also is it JIRA server or cloud? I looked through the release notes but could not determine. 

Thanks, 

Skye

Joshua Yamdogo Community Champion Oct 09, 2017

Hi Syke,

This is for JIRA Sever. Are you on the Cloud version?

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,890 views 12 18
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot