Script Validator (Condition) : check if user is in group

Greetings dear Community,

Using JIRA 4.4.1 and Script Runner (GroovyRunner) 2.0.7, I'm trying to add a Script validator (only with conditions and not a whole script) in my workflow.

This script will check the value of a customfield (TextField < 255) where we put a user name, and see if this value match one in a user group (jira-users).

For now, I was able to extract the value, but encounter some difficulties to check if there is a match in the user group.

Here is the condition I wrote to extract the customfield value :

(customFieldManager.getCustomFieldObject("customfield_13100").getValue(issue))

and here is the function I wrote (but unfortunately doesn't work) :

issue.cfValues['my customfield'].inGroup("jira-users") == true

(error message :
MissingPropertyException: No such property: cfValues for class: com.atlassian.jira.issue.IssueImpl)

or

issue.(customFieldManager.getCustomFieldObject("customfield_13100").getValue(issue)).inGroup("jira-users")

(error message :
MissingPropertyException: No such property: name of the user in the CF for class: com.atlassian.jira.issue.IssueImpl)

Can you tell me what am I missing, at this point (I guess it's obivious, but because I'm a beginner, can't really see what's wrong)

Thanks for your help.

Best Regards

F.

20 answers

1 accepted

0 vote

OK... so a validator won't fire until the user has typed in all the fields... which is less than ideal.

Sorry to ask, but can you click Edit on that validator and attach a screenshot of that?

No problem, Jamie.

That really should work (if you tested the script in condition tester) ... are you sure you have published the workflow, and that the project is using the correct workflow?

I see that the workflow is currently in draft mode.

I know, and that's why I don't understand.

Here are more elements for you :

I just wrote the script below and applied it as a validator on the Create transition and it worked as expected, ie it blocked me when the user in the custom field exists and is in jira-users, and it allowed it when the user didn't exist.

To say again, have you published your draft? Any time you make changes to the groovy script you need to publish your draft.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

def groupManager = ComponentAccessor.getGroupManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

assert issue instanceof Issue

def cf = customFieldManager.getCustomFieldObjects(issue).find { it.name == "Name of your field here, not ID" }
def cfValue = issue.getCustomFieldValue(cf)
log.debug("cfValue: $cfValue")
! groupManager.isUserInGroup(cfValue, "jira-users")

I don't think I can offer anything else... I think it must be a simple config issue.

No, I can assure you, that publish my draft after working on it, is a reflex, and I've triple check it.

Thought, I tested your script...and it works perfectly! (but I don't know what's wrong with mine...)

Many many thanks to you Jamie, it was a real pleasure to work with you!

Fabien.

Heh, no problem.

I need to precise that, with this function, we want to know if the name entered in the CF, is a new user (so not already registered in Jira), or not.

0 vote

You need to use ComponentAccessor.getUserUtil() and use that to see if the user is in a group. User has no "inGroup()" method. There are examples with this on this forum somewhere...

Hi Jamie,

Thanks for your answer, I'll try it right away!

Cheers.

Hi (again) Jamie,

So, I Tried to follow your advice, but for some reason I don't get (for now), it doesn't work.

Am I missing a class import?

import com.atlassian.jira.issue.*
import com.atlassian.jira.issue.customfields.*
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.ComponentManager


MutableIssue issue = issue
componentManager = ComponentManager.instance
customFieldManager = componentManager.getCustomFieldManager()
userUtil = componentManager.getUserUtil()

cfUserInGroup = customFieldManager.getCustomFieldObjectByName("jira-users")
teamUserGroup = issue.getCustomFieldValue(cfUserInGroup)
assigneeUserName = issue.getAssigneeUser().getName()

if (userUtil.getGroupNamesForUser(assigneeUserName).contains("jira-users"))

{
System.out.println("User exists already in Jira" )
}
else
{
System.out.println("User doesn't exists in Jira" )
}

Here is the error I get :

javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: issue for class: Script10

Thanks for you help!

F.

0 vote

Hi F... hrm, no idea. What context are you running this in... is the validator called "simple scripted validator" ?

If so then issue should be defined. You don't need all those imports - in fact the only one you need is ComponentManager.

Hi Jamie,

Thanks for your answer.

I'll try to give you as many details as I can.

In our company, we want human ressources or the futur manager to create new issues when a new employee has arrived, in order to give him the appropriate access software regarding his job.

For that, we have multiples fields to set / fullfill but we also want one where you put the name of this newcomer.

After validating the issue, a script should check the value in it, and tell the human ressources / manager, if this new employee is present in the Active Directory or not.

-> If yes, then display error message inviting the human ressources to check if there isn't another issue created for this name.

-> If no, keep the string value then continue the workflow.

The best thing would be to have a user picker field accepting unknown value as new string value, unless you have other things to propose :)

Hope this will help you to understand what I want to do.

Best regards from Paris, Jamie.

Fabien.

0 vote

I'd suggest you just use a short text and do an AD lookup on that, because you won't be able to use a user picker as you say. I'm away for a bit now, will try to help when I'm back.

jamie

Hi Jamie,

Indeed, that's I wanted to do at first, but no problem to wait for your to be back.

Meanwhile, I'll try to look for more data abtout this.

F.

Hi Jamie,

After having made some tests (with Script Runner / Condition Tester), I figured out it would be something like this (as a workflow validator) :

(isUserMemberOfGroup(user.getName(), "jira-users"))==(customFieldManager.getCustomFieldObject("customfield_13100").getValue(issue))

But, all I get is the following error message : No such property: user for class: Script16

I'm sure I'm close to find the anwser, but something is missing.

I'll keep ou in touch.

Fabien.

I also have this code, but encounter an error :

No signature of method: static com.atlassian.jira.issue.CustomFieldManager.getCustomFieldObject() is applicable for argument types: (java.lang.String) values: [customfield_13100]

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager

GroupManager groupManager = ComponentManager.getComponentInstanceOfType(GroupManager.class)

def componentManager = ComponentManager.getInstance()

if ( groupManager.isUserInGroup((CustomFieldManager.getCustomFieldObject("customfield_13100").getValue(issue)),"jira-users"))
{
System.out.println("User already exists! " )
}
else
{
System.out.println("User doesn't exists" )
}

0 vote

It's not a static method... use:

def customFieldManager = ComponentAccessor.getCustomFieldManager()

customFieldManager.get...

Thanks Jamie.

Almost! No more error message with the following code, but unfortunately a 'No return value' :

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager

GroupManager groupManager = ComponentManager.getComponentInstanceOfType(GroupManager.class)

def customFieldManager = ComponentAccessor.getCustomFieldManager()

if (groupManager.isUserInGroup((customFieldManager.getCustomFieldObject("customfield_13100").getValue(issue)),"jira-users"))
{
System.out.println("User already exists! " )
}
else
{
System.out.println("User doesn't exists" )
}

Is there a modification like this to do?



def cf = (ComponentAccessor.getCustomFieldManager.getCustomFieldObject("customfield_13100").getValue(issue))

if (groupManager.isUserInGroup(cf,"jira-users"))
{
System.out.println("User already exists! " )
}
else
...

0 vote

Hey... println will go to your catalina.out log.

You should just remove your if/else and make the last line:

return groupManager.isUserInGroup((customFieldManager.getCustomFieldObject("customfield_13100").getValue(issue)),"jira-users")

But you also need to check for nulls and stuff... maybe try to break that line into several lines with some variables.

Hi Jamie,

Thanks, I'll make the changes / optimizations and tell you the result.

Cheers!

F.

So,

Things are better. Here is the script (which works when running in script runner) I've got now :

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.crowd.embedded.api.User
import com.atlassian.crowd.embedded.api.Group

def issue = componentManager.getIssueManager().getIssueObject(issue.key)
GroupManager groupManager = ComponentManager.getComponentInstanceOfType(GroupManager.class)

CustomFieldManager cfManager = ComponentManager.getInstance().getCustomFieldManager()
CustomField cf = cfManager.getCustomFieldObject(13500)
Object customFieldValue = issue.getCustomFieldValue(cf)

if (groupManager.isUserInGroup((cfManager.getCustomFieldObject("customfield_13500").getValue(issue)),"jira-users"))
{
return "User already exists! "
}
else
{
return "User doesn't exists"
}

One precision thought, in order to work, the username have to be like this j.echlin and not like Jamie ECHLIN.

Now, I have to integrate it into my workflow, but there are questions remaining :

- When putting it in the workflow as a Condition or a Validator, I have this error message :

java.lang.NullPointerException

Is it related to the getIssueObject(issue.key), because I've putted it in the "create" transition, so there are no issue key yet?

- Should I try to run it with a scripted field?

Again, thank you for your advices and your patience, Jamie.

Have an excellent weekend.

Fabien.

0 vote

Hi...

This line:

def issue = componentManager.getIssueManager().getIssueObject(issue.key)

is redundant... the issue is already provided to the script.

You're returning a string... I thought you were using the "simple scripted condition/validator"? If so you need to return a Boolean, so just return true/false, depending on whether you want the condition to be allowed or not.

Hi Jamie,

You're right, at first, I was looking for a condition validator and still am.

For now, I have the following lines as a condition validator integrated into the create workflow transition (1st step of it) :

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.crowd.embedded.api.User
import com.atlassian.crowd.embedded.api.Group
import com.atlassian.jira.util.SimpleErrorCollection

GroupManager groupManager = ComponentManager.getComponentInstanceOfType(GroupManager.class)

CustomFieldManager cfManager = ComponentManager.getInstance().getCustomFieldManager()
CustomField cf = cfManager.getCustomFieldObject(13700)
Object customFieldValue = issue.getCustomFieldValue(cf)

!groupManager.isUserInGroup((cfManager.getCustomFieldObject("customfield_13700").getValue(issue)),"jira-users")

...but unfortunately, it doesn't work, meaning that in any case (value matches a user in jira-user or not), either it create the issue or not.

What i do not understand, is that this script fully work in the condition tester (giving me a true or false result regarding the value).

I don't know what to do now, do you have any ideas?

Thanks for your help Jamie.

Fabien.

0 vote

Is this a validator or a condition? They are two very different things, but you are talking about a "condition validator". Could you post a screenshot of this appearing the list of conditions or validators in the workflow? ta.

Sure, Jamie.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira

Meet the AUG leaders of Northern Virginia

@Rachel Wright (Jira Genie), @Billy Poggi (AUG NOVA, DC), and @Dana Jansen (Confluence Queen) are just some of the folks that lead one of the world's most active Atlassian User Group (AUG)....

154 views 5 9
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