Hello everyone,
I am trying to create a listener script that validates that the reporter user of an issue belongs to a certain role and if not, it should display a message informing the user that it is not possible to set that user as a reporter and will return the field to its previous value, the script seems to work correctly but the message is only shown the first time I update the field, the rest of the time it doesn't show anything, could you give me a hand with the script and tell me what I'm doing wrong?
Script:
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.security.JiraAuthenticationContext
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import com.atlassian.jira.bc.user.search.UserSearchService
def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)
def issueManager = ComponentAccessor.getIssueManager();
UserSearchService userSearchService = ComponentAccessor.getComponent(UserSearchService.class);
JiraAuthenticationContext jiraAuthenticationContext = ComponentManager.getComponentInstanceOfType(JiraAuthenticationContext.class);
ApplicationUser loggedUser = jiraAuthenticationContext.getLoggedInUser();
MutableIssue issue = (MutableIssue)event.issue
def change = event?.getChangeLog()?.getRelated("ChildChangeItem").find {it.field == "reporter"};
if (change) {
def user = userSearchService.findUsersByFullName((String)change.newstring)
def projectManager = ComponentAccessor.projectManager
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def project = issue.getProjectObject()
def projectRoles = projectRoleManager.getProjectRoles(user.first(), project)
if(!projectRoles.find(){it.getName() == "TIC"}){
def flag = [
type: "error", //Otra posible opción sería: "info" y "success"
body: "El usuario seleccionado como informador no es válido para este Producto.",
close: "manual"
]
UserMessageUtil.flag(flag)
def usuario = change.oldstring
issue.reporter = userSearchService.findUsersByFullName((String)change.oldstring).first()
issueManager.updateIssue(loggedUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false);
//REINDEXAMOS LAS SOLICITUDES
boolean wasIndexing = ImportUtils.isIndexIssues();
ImportUtils.setIndexIssues(true);
issueIndexingService.reIndex(issueManager.getIssueObject(issue.id));
ImportUtils.setIndexIssues(wasIndexing);
}
}
Regards.