How to use a JIRA custom listener to copy comments from the parent issue to subtasks and vice versa?

3 minute read

You can copy the comment added in the parent issue to all the associated sub tasks and also copy the comment entered in the subtask to the parent issue using the JIRA custom listener and grovvy script. All you need to do is to setup the custom listener and add the below grovvy script, which will take care of copying the comment from the parent issue to the subtask and vice versa.

If you’re looking to just perform any one of the functionality like parent to subtask or subtask to parent issue, then you can refer to the below previous posts.

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

Post 2: How do I use a JIRA custom listener to copy comments from the subtask to the parent issue?

This post has a groovy script which performs the same operation as mentioned in the above post and additionally handles the logic to avoid retriggering once the comment is copied to the parent or sub task.

Example: Comment added in the parent issue->copy-> All subtasks -> block the trigger to copy the comment back to the parent issue.

Groovy script:

// Namespaces

import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor

def issue = event.issue as Issue 

// Comment manager and the ticket user
def commentManager = ComponentAccessor.getCommentManager();
ApplicationUser currentUser = event.getUser();

// Read comment from the issue
def comment = event.getComment();

// Sub task to parent
if(issue.isSubTask()){

    // Loop through each parent object associated with the sub task
    issue.getParentObject().each { it ->

	Issue sourceIssue = (Issue)it;        
        def parentComment = commentManager.getComments(sourceIssue).find{c -> c.body.equals(comment.getBody())};
        if(parentComment != null){
            return;
        }
        
        // Create comment in the parent from sub task comment
        commentManager.create(sourceIssue, currentUser, comment.getBody(), true);
     }
}

// Parent to Sub tasks
if(!issue.isSubTask()){
    def IsSubTaskComment = false;
    // Loop through each parent object associated with the sub task
    issue.getSubTaskObjects().each { it ->
	Issue sourceIssue = (Issue)it;
        def childComment = commentManager.getComments(sourceIssue).find{c -> c.body.equals(comment.getBody())};
        if(childComment != null){
            IsSubTaskComment = true;
        }
    }

    if(IsSubTaskComment){
        return;
    }
    
    issue.getSubTaskObjects().each { it ->
	Issue sourceIssue = (Issue)it;
        def childComment = commentManager.getComments(sourceIssue).find{c -> c.body.equals(comment.getBody())};
        if(childComment != null){
            return;
        }
        // Create comment in the parent from sub task comment
        commentManager.create(sourceIssue, currentUser, comment.getBody(), true);
     }
}

Leave a comment