Groovy Validator: reporter not in specific group or issue type not in list

Hans-Hermann Hunfeld October 8, 2015

Hi folks,

i just tried to get a scripted validator up&running, but it´s not working as expected:

 

import com.atlassian.jira.component.ComponentAccessor

def groupManager = ComponentAccessor.getGroupManager()
groupManager.isUserInGroup(issue.reporter?.name, 'Technical User')
||
(
    issue.issueTypeObject.name != ('Incident')||
    issue.issueTypeObject.name != ('Service Call')||
    issue.issueTypeObject.name != ('Change Request')||
    issue.issueTypeObject.name != ('Problem')||
    issue.issueTypeObject.name != ('Workorder')
)

The idea is to limit the creation of  some specific issue types to specific user group only, so the reporter has to be part of the group "Technical User" or the to be created issue type is not in "Incident, Service Call, Change Request, Problem, Workorder".

Any ideas what i´m doing wrong? Unfortunately my programming skills are very limited... sad

Thanks & regards,
Hans-Hermann

4 answers

1 accepted

2 votes
Answer accepted
JamieA
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.
October 8, 2015

Try:

import com.atlassian.jira.component.ComponentAccessor
def groupManager = ComponentAccessor.getGroupManager()
groupManager.isUserInGroup(issue.reporter?.name, 'Technical User') ||
    ! (issue.issueTypeObject.name in ['Incident', 'Service Call', 'etc'])

if it's still not working tell us how it's not working. 

Beware of the formatting... you can't begin a newline with || if the previous line is a valid groovy statement. There are perils to not using semi-colons.

 

Josh Costella February 12, 2016

@Jamie Echlin [Adaptavist]

Hi Jamie, I'm trying to do the opposite of the solution you showed above but I'm getting the "Use one of the other isUserInGroup methods that takes a concrete user object instead. Since v6.4.8." error. We are running JIRA 7.0. Any thoughts?

 

import com.atlassian.jira.component.ComponentAccessor

def groupManager = ComponentAccessor.getGroupManager()

groupManager.isUserInGroup(issue.reporter?.name, 'jira-administrators') || (issue.getIssueType() in ['Goal'])
Like Matthew Gaffney likes this
Jeremy Gaudet
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.
February 12, 2016

Just from the wording of the error, it sounds like they want you to use "issue.reporter" instead of "issue.reporter?.name".

Like # people like this
JamieA
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.
February 12, 2016

yep, JIRA 7 has some changes to the API. Thanks @Jeremy Gaudet

Like Lebedkov likes this
0 votes
SasaHodzic November 23, 2017

Hi,

I would like to have on my custom transition script validator that reporter has to be part of the project role "Developers". 

Thanks!

BR

P.S. JIRA 7.6

0 votes
Jeremy Gaudet
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.
October 8, 2015

You may know this, but I'm calling it out because it's not clear.  You need to do something like:

import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.component.ComponentAccessor
 
def groupManager = ComponentAccessor.getGroupManager()
if (!(groupManager.isUserInGroup(issue.reporter?.name, 'Technical User')
|| (issue.issueTypeObject.name != 'Incident' &&issue.issueTypeObject.name != 'Service Call' && issue.issueTypeObject.name != 'Change Request' && issue.issueTypeObject.name != 'Problem' &&issue.issueTypeObject.name != 'Workorder'))) {
	invalidInputException = new InvalidInputException("Only users in the \"Technical User\" group can open issues of type \"" + issue.getIssueType() +"\"")
}

I have the same validator on "Create Issue", except it's based on the Developers role, to give you a complete working example:

import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.security.roles.ProjectRoleActors
import com.atlassian.jira.security.roles.ProjectRole
import com.opensymphony.workflow.InvalidInputException
import org.apache.log4j.Category

def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction");
ComponentManager componentManager = ComponentManager.getInstance();
ProjectRoleManager projectRoleManager = ComponentManager.getComponentInstanceOfType(ProjectRoleManager.class) as ProjectRoleManager;


ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getUser();
ProjectRole devsRole = projectRoleManager.getProjectRole("Developers");
ProjectRoleActors devs = projectRoleManager.getProjectRoleActors(devsRole, issue.getProjectObject());
Boolean isDev = devs.contains(currentUser);
String issueType = issue.getIssueTypeObject().getName();

if (!isDev) {
	if (issueType != "Bug" && issueType != "Improvement") {
        invalidInputException = new InvalidInputException("You are not a member of this project's \"Developers\" role; the only issue types available to non-developers are \"Bug\" and \"Improvement\".");
    }
}

(I removed some other exception blocks to keep it simple, but it should still work as-is).

JamieA
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.
October 8, 2015

You need to throw a new InvalidInputException if you're using a custom validator... from the way the code was written by the OP I assume it was a "simple scripted validator". In that case you just return true/false, and set the message in the UI. It's slightly simpler.

Jeremy Gaudet
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.
October 8, 2015

Interesting. Is his return method correct in that case? It's a free-form expression evaluation without a "return", so it still seems odd, but I've never used a "simple scripted validator".

JamieA
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.
October 8, 2015

The return statement is implicit for the last line of a block. So it's correct not to use that, but there were other errors. I prefer not to use "return" if it's obvious.

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 8, 2015

I think your block of OR statements on the issue type is a problem - it's always going to be true.  If an issuetype is an Incident, then it is not any of the others, so all the other clauses become true.  Try AND instead (if issuetype is not A and it's not B and it's not C then...)

JamieA
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.
October 8, 2015

yes, Nic is right. I updated my answer below to take account of that.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 8, 2015

Sorry, typing at the same time :-)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events