Nov 8, 2013

JIRA listener script to add assignee into Summary field

A little script to help the life better. This script is a Listener which update the summary of JIRA issue to add assignee info. I'm using JIRA Agile and its board doesn't show the assignee name so I create this script to display it in the summary field.

Code as following, save it with .groovy extension and then put it into atlassian-jira/WEB-INF/classes/com/xxx/listeners and then configure JIRA to use com.xxx.listeners.UpdateAssigneeSummaryListener as class

import com.atlassian.jira.event.issue.AbstractIssueEventListener;
import com.atlassian.jira.event.issue.IssueEvent;
import org.apache.log4j.Category;
import com.atlassian.jira.event.type.EventType;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.component.ComponentAccessor;

class UpdateAssigneeSummaryListener extends AbstractIssueEventListener {
    Category log = Category.getInstance(UpdateAssigneeSummaryListener.class)

    @Override
    void workflowEvent(IssueEvent event) {
        Issue issue = event.getIssue();
        Long eventTypeId = event.getEventTypeId();
        if ((eventTypeId.equals(EventType.ISSUE_ASSIGNED_ID) || eventTypeId.equals(EventType.ISSUE_CREATED_ID)) && issue.getResolutionDate() == null)
        {
            def test = (issue.getSummary() =~ /^\[([A-Za-z]+)\]/)
            def summary
            def assignee = issue.getAssigneeId()
            if (test.getCount() != 0)
            {
                // Replace the user in bracket.
                summary = (issue.getSummary() =~ /\[([A-Za-z]+)\]/).replaceFirst("[${assignee}]")
            }
            else
            {
                summary = "[${assignee}] ${issue.getSummary()}"
            }

            MutableIssue mutableIssue = (MutableIssue)issue;
            mutableIssue.setSummary(summary);
            IssueManager issueManager = ComponentAccessor.getIssueManager();
            UserManager userManager = ComponentAccessor.getUserManager();
            issueManager.updateIssue(event.getUser(), mutableIssue, EventDispatchOption.DO_NOT_DISPATCH, false)
        }
    }
}

Post a Comment