Hi,
I am trying to use listeners in Script Runner. When a comment is added in child/subtask it should automatically add that comment in Parent Task.
Below is Script:-
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager
import org.apache.log4j.Category
class SubtaskCommentListener extends AbstractIssueEventListener {
Category log = Category.getInstance(SubtaskCommentListener.class)
CommentManager commentManager = ComponentAccessor.getCommentManager()
@Override
void workflowEvent(IssueEvent event) {
// only one central way...
this.customEvent(event)
}
@Override
void customEvent(IssueEvent event) {
// set explicit to debug
log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug "Event: \"${ComponentAccessor.eventTypeManager.eventTypesMap[event.getEventTypeId()].name}\" fired for ${event.issue}"
// Here you should put any subtask based restrictions for this task like
// only for special subtask issue types or similar
Comment comment = event?.getComment()
if (comment && event.issue.isSubTask()) {
log.debug "New commment for subtask found."
Issue parent = event.issue.getParentObject()
// Here you should put any parent task based restrictions like
// only for special issue types or similar
commentManager.create(parent, comment.getAuthorApplicationUser(), comment.getUpdateAuthorApplicationUser(), comment.body, comment.groupLevel, comment.roleLevelId, comment.created, comment.updated, true, true)
log.debug ("Created comment on ${parent.key}")
}
}
}
The script is good but it is not working and jira version is 7.13
You can try below 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);
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.