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

Deleted user February 20, 2013

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 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.
March 5, 2013

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?

Deleted user March 5, 2013

No problem, Jamie.

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.
March 5, 2013

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.

Deleted user March 5, 2013

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

Here are more elements for you :

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.
March 5, 2013

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.

Like 3digits - Desarrollo likes this
Deleted user March 5, 2013

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.

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.
March 5, 2013

Heh, no problem.

0 votes
Deleted user March 5, 2013

Sure, Jamie.

0 votes
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.
March 5, 2013

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.

0 votes
Deleted user March 5, 2013

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 votes
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.
March 2, 2013

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.

0 votes
Deleted user February 28, 2013

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 votes
Deleted user February 28, 2013

Hi Jamie,

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

Cheers!

F.

0 votes
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 27, 2013

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.

0 votes
Deleted user February 27, 2013

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 votes
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 27, 2013

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

def customFieldManager = ComponentAccessor.getCustomFieldManager()

customFieldManager.get...

0 votes
Deleted user February 27, 2013

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 votes
Deleted user February 27, 2013

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.

0 votes
Deleted user February 24, 2013

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.

0 votes
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 21, 2013

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

0 votes
Deleted user February 21, 2013

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 votes
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 20, 2013

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.

0 votes
Deleted user February 20, 2013

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 votes
Deleted user February 20, 2013

Hi Jamie,

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

Cheers.

0 votes
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 20, 2013

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

0 votes
Deleted user February 20, 2013

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.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events