Copy Comment to Linked Issue based on workflow transition

mahmoud atout January 24, 2015

Hello all

we are using JIRA as our internal issue tracking and also we use it as a customer support system, after several configurations we settled on this one

every client has a project in JIRA with a simple workflow, when he submit an issue, the PM receive it, and when the PM opens, JIRA automatically (through a scripted post functions) clone the issue to another JIRA project that is internal we we can work on the issue, then when everything is done and tested internal, JIRA automatically through post functions transition the parent issue to resolved in order for the client to approve or not approving it

 

now my question is that

how can I copy comments to linked issues based on workflow transitions

for example ( when developer transition the cloned issue to "customer feedback" the original issues will be transitioned automatically (this is easy) and the comment of the developer must appear on the original issue ( this is what i am trying to do)

also when the customer transition the issue to open (when providing feedback) the cloned issue will be transitioned to Open with the client comment embedded on it

I am trying the script runner but nothing works, it seems that I can't work with comments

 

please help

7 answers

1 accepted

2 votes
Answer accepted
Cesare Jacopo Corzani
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.
January 28, 2015

You can solve it by using "Script Listeners" section of Script runner. You can easily create a Custom listener triggered by the event "Issue Commented" and add a script similar to this one.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent;

public class CopyComments extends AbstractIssueEventListener {

    @Override
    void workflowEvent(IssueEvent event) {

        def linkedIssue = "PROJECT-1";
        def commentManager = ComponentAccessor.getCommentManager();
        def issueManager = ComponentAccessor.getIssueManager();

        def comment = event.getComment()
        def targetIssue = issueManager.getIssueObject(linkedIssue);

        commentManager.create(targetIssue, comment.authorApplicationUser, comment.body, true);

    }

}

You might use the same logic to sync the status of the ticket.

mahmoud atout January 31, 2015

i am going to try the status syncing and get back to you with my results

LL March 9, 2015

Is it possible to specify the link type? I would like to use this to copy Story comments to an Epic, and ignore any other link types being utilized.

Ravi Sagar _Sparxsys_
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.
May 26, 2016

For some reason the above code is not working for me. I am not getting any error and the Issue Commented event is working and shown as no failure but the comments are not copied to the target linked issue hard coded in the code.

Cristine Kikue Sato February 20, 2017

Hey! how are you?

is it possible to create this listener on cloud?

thanks

Cristine

JamieA
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.
February 21, 2017

AFAIK you can but the answer is so different I recommend you ask a new question and use the relevant tags, so the ScriptRunner cloud people can answer.

Cristine Kikue Sato February 21, 2017

ok. thanks!

1 vote
Mehmet AYDIN November 13, 2018

I solved like this:

Copy comments to linked issues

  1. Click Edit for the workflow that has the transition you wish to add the post-function on.
  2. In the Workflow Designer, select the transition.
  3. Click on Post Functions in the properties panel.
  4. Click on Add post function .
  5. Select Copy comments to linked issues from the list of post-functions.

This is working.

Jessica Madera Gumba December 15, 2018

there is no "copy comment" in jira service desk cloud.  Any idea?

German Lopez Peralta May 9, 2019

is there a "copy comment" possible automation as soon as a comment gets posted?

Piyush Dinde December 27, 2019

There is one app called Automation for Jira. You can try using that.

WasimBuden February 25, 2020

Automation for Jira is not available for cloud instance?

I am not able to download it for my cloud instance!

Pat Ryan February 27, 2021

that is not right.

0 votes
Ananthakrishnan N N August 1, 2019

Hi Everyone, we have two projects and comments should be synced in both the projects. I used the below script for two projects. Multiple comments are getting added in both the tickets. It's going on loop even though I added many conditions.

 

Please help me here:

import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.AttachmentManager
import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.user.ApplicationUser
import org.ofbiz.core.entity.GenericValue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue;

def linker = ComponentAccessor.getIssueLinkManager()
def commentMgr = ComponentAccessor.getCommentManager()
def issue = event.issue as MutableIssue
def event = event as IssueEvent
def newComment = event.getComment()
def originalAuthor = newComment.getAuthorApplicationUser()
def commentBody = newComment.getBody()


linker.getOutwardLinks(issue.getId()).each { 

 if( commentBody != null  && (it.issueLinkType.name == "Relates") && commentBody != commentMgr.getLastComment (it.getSourceObject())  && commentBody != commentMgr.getLastComment (it.getDestinationObject())) 
{
 
commentMgr.create(it.getDestinationObject(), originalAuthor.getName(), commentBody , true)
 

}
 }

linker.getInwardLinks(issue.getId()).each { 


 if( commentBody != null  && (it.issueLinkType.name == "Relates") && commentBody != commentMgr.getLastComment (it.getDestinationObject()) && commentBody != commentMgr.getLastComment (it.getSourceObject()))
 
{
 commentMgr.create(it.getSourceObject(), originalAuthor.getName(), commentBody , true && commentBody != commentMgr.getLastComment (it.getSourceObject())) 

}
}




Paul Mata May 6, 2021

@Ananthakrishnan N N I realize your question was from almost two years ago, but I just stumbled on it while searching for the answer to the same question. The only way I was able to get comments both directions without causing the loop condition was to create two listeners. One for outward links and one for inward links.

There is probably another way to fix your script to do it all in one, but I couldn't figure it out. If you ever solved this problem in some other way, please share.

0 votes
Nina Zolotova September 7, 2018

@Tim Hall@Sean Salvage

This code works for me, but only for outward links

import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.AttachmentManager
import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.user.ApplicationUser
import org.ofbiz.core.entity.GenericValue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue;

def linker = ComponentAccessor.getIssueLinkManager()
def attachmentMgr = ComponentAccessor.getAttachmentManager()
def commentMgr = ComponentAccessor.getCommentManager()
def issue = event.issue as MutableIssue

// gather the original author and comment body from the original issues comment
def newComment = event.getComment()
def originalAuthor = newComment.getAuthorApplicationUser()
def commentBody = newComment.getBody()
// get original issue's linked issues
linker.getOutwardLinks( issue.getId() ).each {

if( commentBody != "" )
{
// create comment on the linked issue and fire the comment event on that issue
commentMgr.create(it.getDestinationObject(), originalAuthor.getName(), commentBody , true)
// get the Source Issue key of the Service Desk Issue
Issue sourceIssue = ComponentAccessor.getIssueManager().getIssueObject(it.getSourceObject().getKey())
// Specify the Base URL of your Service Desk Installation in the string below
def serviceDeskBaseURL = "https://avst-dev519.dyn.adaptavist.com/servicedesk/customershim/secure/attachment/"
// Get the Attachment file id and file path
def file = sourceIssue.getAttachments().last().filename.toString()
def fileId = sourceIssue.getAttachments().last().id
// Construct the comment URL and post a comment with it on the linked issue
def commentText = "The URL for the last attachment = " + serviceDeskBaseURL + fileId + "/" + fileId +"_" + file
def url = serviceDeskBaseURL + fileId + "/" + fileId +"_" + file
commentMgr.create(it.getDestinationObject(), originalAuthor.getName(), commentText.toString() , true)
}
}

 

Tim Hall September 21, 2018

Thanks, I will check out in the next update to our JSD Project. 

0 votes
Tim Hall January 30, 2018

I am trying to get this to work, but my comments aren't syncing. Do you have to put the specific project name in quotes? I am trying to sync comments from one project into our JIRA Service Desk project.

Sean Salvage July 6, 2018

I'm trying to work this one out too.

 

I want comments to show on both ticket.

Nina Zolotova September 7, 2018

This code worked for me

import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.AttachmentManager
import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.user.ApplicationUser
import org.ofbiz.core.entity.GenericValue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue;

def linker = ComponentAccessor.getIssueLinkManager()
def attachmentMgr = ComponentAccessor.getAttachmentManager()
def commentMgr = ComponentAccessor.getCommentManager()
def issue = event.issue as MutableIssue

// gather the original author and comment body from the original issues comment
def newComment = event.getComment()
def originalAuthor = newComment.getAuthorApplicationUser()
def commentBody = newComment.getBody()
// get original issue's linked issues
linker.getOutwardLinks( issue.getId() ).each {

if( commentBody != "" )
{
// create comment on the linked issue and fire the comment event on that issue
commentMgr.create(it.getDestinationObject(), originalAuthor.getName(), commentBody , true)
}
}
0 votes
mahmoud atout January 27, 2015

I am trying to clone the issue an another workflow so the client can't see what is happening behind the scene when any developer ask the client to provide any info I need the last comment of the developer to appear on the client issue and when the client answer, his comment will appear on the internal workflow (linked issue)

0 votes
JamieA
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.
January 25, 2015

What have you tried so far? Do you want all internal comments copied, or just the last one that the developer gave?

WasimBuden March 28, 2020

Hi JamieA,

 

Our requirement is simple, we use Jira service desk to support customers and all the tickets open by clients are routed to the product team by creating a linked ticket. 

 

As the engineering team updates their ticket in Jira we need same.commemts to be copied to Jira service desk ticket so the support team can go back and manage the client.

 

Comment should sync to and fro between Jira and Jira service desk without using any plugin can we achieve this?

Suggest an answer

Log in or Sign up to answer