Forked from KenDUemura/UpdateAssigneeOnComponentChangeListener.groovy
Last active
January 11, 2019 14:58
-
-
Save pineoc/110c37b6b8e8e89ba195b8e2a1118b26 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 hidden or 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 Listner -> 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(); | |
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; | |
def issue = event.issue; | |
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) { | |
String currentAssignee = issue.getAssigneeId(); | |
String componentName = null; | |
String componentLead = null; | |
Collection<ProjectComponent> components = issue.getComponents(); | |
//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){ | |
IssueManager issueManager = ComponentAccessor.getIssueManager() | |
def issueService = ComponentAccessor.getIssueService() | |
def userManager = ComponentAccessor.getUserManager() | |
MutableIssue mutableIssue = issueManager.getIssueObject(issue.id) | |
def updateUser = userManager.getUserByName(componentLead) | |
log.warn "updateUser: " << updateUser | |
mutableIssue.setAssignee(updateUser) | |
issueManager.updateIssue(updateUser, mutableIssue, EventDispatchOption.DO_NOT_DISPATCH, false) | |
//def validateAssignResult = issueService.validateAssign(updateUser, issue.id, issue.reporterId) | |
//issueService.assign(updateUser, validateAssignResult) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment