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

1 minute read

You can copy the comment added in the sub task to it’s 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 your subtask to its parent issue.

Groovy script:

// Namespaces
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.component.ComponentAccessor;

// Get the current issue
def issue = event.issue as Issue;

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

// Determine whether the current issue is a subtask.
if(issue.isSubTask()){

    // Get the subtask's current comment
    def comment = event.getComment();
    
    // Get the parent issue associated with the subtask
    issue.getParentObject().each { it ->
      Issue sourceIssue = (Issue)it;        
      
      // Determine whether the comment has already been added in the parent issue.
      def parentComment = commentManager.getComments(sourceIssue).find{c -> c.body.equals(comment.getBody())};
      if(parentComment != null){
          return;
      }
        
      // Add a comment to the parent task from the subtask commen
      commentManager.create(sourceIssue, currentUser, comment.getBody(), true);
    }
}

Leave a comment