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

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

2 votes

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.

 

@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'])

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

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

0 vote

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...)

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

Sorry, typing at the same time :-)

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).

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.

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".

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.

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

Suggest an answer

Log in or Join to answer
Community showcase
Emilee Spencer
Published Friday in Marketplace Apps

Marketplace Spotlight: DeepAffects

Hello Atlassian Community! My name is Emilee, and I’m a Product Marketing Manager for the Marketplace team. Starting with this post, I'm kicking off a monthly series of Spotlights to highlight Ma...

64 views 0 3
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot