Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Help with listener validation script

Pedro J. Acosta April 22, 2020

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.

 

1 answer

0 votes
WW
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 24, 2021

I know it's a bit late, but I don't see which events the Listener is firing on.  Maybe it's just firing on IssueCreated?

The Events are selected in the UI of the Listener itself (not the script code).

That's the first thing I'd check, anyhow.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events