Forked from KenDUemura/UpdateAssigneeOnComponentChangeListener.groovy
Created
July 7, 2017 15:48
-
-
Save Qijiang60/2cd216e2fc7bf1d5addcf7f60edc699d to your computer and use it in GitHub Desktop.
Script Runner Listener for Re-assigning to Component Lead when Component is changed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Ken D: Uemura <[email protected]> on 6/1/2016. | |
* | |
* On Issue Update Event | |
* If Component is changed and only one component is assigned | |
* then re-assigns the issue to the Component Lead | |
* | |
* Tested with JIRA 6.4.11 | |
* Set this in Add-On -> Script Lisner -> Custom Listener | |
* | |
* References: | |
* https://answers.atlassian.com/questions/223177/answers/2588801 | |
* | |
*/ | |
import com.atlassian.crowd.embedded.api.User | |
import com.atlassian.jira.bc.project.component.ProjectComponent | |
import com.atlassian.jira.component.ComponentAccessor | |
import com.atlassian.jira.event.type.EventDispatchOption | |
import com.atlassian.jira.issue.Issue | |
import com.atlassian.jira.issue.IssueManager | |
import com.atlassian.jira.issue.MutableIssue | |
import com.atlassian.jira.util.collect.MapBuilder; | |
import java.util.List; | |
import org.ofbiz.core.entity.GenericValue; | |
import org.ofbiz.core.entity.GenericEntityException; | |
List<GenericValue> changeItems = null; | |
def delegator = ComponentAccessor.getOfBizDelegator() | |
def user = event.getUser(); | |
def changeAssignee(Issue issue, User user) { | |
String currentAssignee = issue.getAssigneeId(); | |
String componentName = null; | |
String componentLead = null; | |
Collection<ProjectComponent> components = issue.getComponentObjects(); | |
//log.info("current assignee: {}", currentAssignee); | |
for (Iterator<ProjectComponent> iterator = components.iterator(); iterator.hasNext();){ | |
ProjectComponent component = (ProjectComponent) iterator.next(); | |
componentName = component.getName(); | |
componentLead = component.getLead(); | |
log.warn "component name: " << componentName; | |
log.warn "component lead: " << componentLead; | |
} | |
if (currentAssignee != componentLead && components.size() == 1){ | |
MutableIssue mutableIssue = issue | |
IssueManager issueManager = ComponentAccessor.getIssueManager() | |
mutableIssue.setAssigneeId(componentLead) | |
issueManager.updateIssue(user, mutableIssue, EventDispatchOption.DO_NOT_DISPATCH, false) | |
} | |
} | |
log.warn "\n\nEvent: ${event.getEventTypeId()} fired for ${event.issue} and caught by Component Update Check Listener\n\n" | |
try { | |
GenericValue changeLog = event.getChangeLog(); | |
if(changeLog != null){ | |
log.warn("ChangeLog: " + changeLog) | |
changeItems = delegator.findByAnd("ChangeItem", MapBuilder.build("group", changeLog.getLong("id"))); | |
} | |
} catch (GenericEntityException e){ | |
log.warn "\n\nException: " << e.getMessage() | |
} | |
log.warn "number of changes" << changeItems.size() | |
def componentChanged = false; | |
for (Iterator<GenericValue> iterator = changeItems.iterator(); iterator.hasNext();){ | |
GenericValue changetemp = (GenericValue) iterator.next(); | |
String field = changetemp.getString("field"); | |
if (field == "Component") { | |
String oldstring = changetemp.getString("oldstring"); | |
String newstring = changetemp.getString("newstring"); | |
StringBuilder fullstring = new StringBuilder(); | |
fullstring.append("Issue "); | |
fullstring.append(issue.getKey()); | |
fullstring.append(" field "); | |
fullstring.append(field); | |
fullstring.append(" has been updated from "); | |
fullstring.append(oldstring); | |
fullstring.append(" to "); | |
fullstring.append(newstring); | |
log.warn "changes: " << fullstring.toString(); | |
componentChanged = true; | |
} | |
if (field ==~ /[aA]ssignee/ ) { | |
componentChanged = false; | |
break | |
} | |
} | |
if (componentChanged) { | |
changeAssignee(issue, user); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment