Forums

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

How to copy a comment from parent issue to sub-task

Wojciech Woytynowski February 18, 2022

I have Script Runner and I would like to use Scirpt Listener so that every time I add a comment to the main ticket it moves to the sub-task

2 answers

1 vote
Code to live blog October 1, 2022

You can achieve the same using the listener as well. I have explained the steps briefly in the below blog post for your reference,

https://codetolive.in/jira/How-to-use-a-JIRA-custom-listener-to-copy-comments-from-the-parent-issue-to-all-sub-tasks/

0 votes
Helmy Ibrahim _Adaptavist_
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 18, 2022

Hi Wojciech,

Do you wish to 'copy' the comment added on the parent issue to all its children? Could you elaborate?

Cheers,
Helmy

wojciechwoytynowski February 20, 2022

Not really. I would like to move comments both ways, from parent issue to subtask but also the other way around. If someone adds comment in parent issue then listener copies it to subtask, if someone adds comment to subtask then it copies it to parent.

I did it two ways. The first way works even though it is in two separate listener.

First way is:

-----------------------------------------------------------------
1. first listener "Copy Story to subtask"

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.watchers.WatcherManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.comments.Comment;

String[] taskInSecondLine = ["5"]

Issue issue = event.getIssue()
CommentManager cm = ComponentAccessor.getCommentManager();
WatcherManager wm = ComponentAccessor.getWatcherManager()
ApplicationUser currentUser = event.getUser()
ApplicationUser epmjraplugin = ComponentAccessor.getUserManager().getUserByName("epmjraplugin")

if(!currentUser.equals(epmjraplugin)) {
def comment = event.getComment()
IssueLinkManager ilm = ComponentAccessor.getIssueLinkManager()

issue.getSubTaskObjects().each{
Issue sourceIssue = it
log.error(sourceIssue)
log.error(sourceIssue.getIssueTypeId())
log.error("status źródła " + sourceIssue.getStatusId())
String zamknietyStatus = "6"
if(sourceIssue.id!=issue.id && taskInSecondLine.contains(sourceIssue.getIssueTypeId()) && sourceIssue.getStatusId()!=zamknietyStatus )
cm.create(sourceIssue,epmjraplugin, "Komentarz dodany przez: "+event.getUser().getDisplayName()+" \nTreść komentarza: " +comment.getBody(), true)
}
}

2. Second listener "Copy subtask to Story"

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.watchers.WatcherManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.comments.Comment;

String[] taskInSecondLine = ["7"]

Issue issue = event.getIssue()
CommentManager cm = ComponentAccessor.getCommentManager();
WatcherManager wm = ComponentAccessor.getWatcherManager()
ApplicationUser currentUser = event.getUser()
ApplicationUser epmjraplugin = ComponentAccessor.getUserManager().getUserByName("epmjraplugin")

if(!currentUser.equals(epmjraplugin)) {
def comment = event.getComment()
IssueLinkManager ilm = ComponentAccessor.getIssueLinkManager()
def parent = event.issue.parentObject
issue.getParentObject().each{
Issue sourceIssue = it
log.error(sourceIssue)
log.error(sourceIssue.getIssueTypeId())
log.error("status źródła " + sourceIssue.getStatusId())
String zamknietyStatus = "6"
if(sourceIssue.id!=issue.id && taskInSecondLine.contains(sourceIssue.getIssueTypeId()) && sourceIssue.getStatusId()!=zamknietyStatus )
cm.create(sourceIssue,epmjraplugin, "Komentarz dodany przez: "+event.getUser().getDisplayName()+" \nTreść komentarza: " +comment.getBody(), true)
}
}

 

-----------------------------------------------------------------------------
The second way is:

1.Copy comment from Story to sub-task 2

import com.atlassian.jira.component.ComponentAccessor

def commentManager = ComponentAccessor.commentManager
if(event.issue.subTaskObjects && event.comment){
event.issue.subTaskObjects.each { subtask ->
def newCommnet = commentManager.create(
subtask,
event.user,
"Comment from parent: $event.issue.key:\n{quote} $event.comment.body {quote}",
event.comment.groupLevel,
event.comment.roleLevelId,
true
)
}
}

Copy comment from subtask to Story 2

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
def commentManager = ComponentAccessor.commentManager
ApplicationUser currentUser = event.getUser()
ApplicationUser epmjraplugin = ComponentAccessor.getUserManager().getUserByName("w.woytynowsk")


if(event.issue.isSubTask() && event.comment){
def parent = event.issue.parentObject
def newCommnet = commentManager.create(
parent,
event.user,
"Comment from sub-task: $event.issue.key:\n{quote} $event.comment.body {quote}",
event.comment.groupLevel,
event.comment.roleLevelId,
true
)
}

----------------------------------------------------------------------------------

Only the second way when I turn on both listeners it does a loop and adds comments until I turn off the listener.

What I would like to achieve is to combine the first way with the second because in the first one you can see that the user automatically inserts a comment and that's great but you can't see that it goes from a subtask with a specific issue number.

I hope I did not confuse you too much.
Thank you. very much that you want to help me
If it was possible to insert screenshots I would show what the first way does and what the second way does

Suggest an answer

Log in or Sign up to answer