Help setting User Property dynamically using a groovy script

Blake Jones June 23, 2017

We have 289 users currently, and have been told that for integration purposes we need to now create a User Property for each of them that holds their email alias without the @domain.com piece due to special character issues. Rather than have to update all 289 of them manually one by one, I'm hoping to script something up.

This is what I've gotten so far based on what I've found in forums and online. I believe my issue is related to the fact that I'm not getting an ApplicationUser as I iterate through the userList collection, and then I have an error on the getPropertySet() that I can't decipher. 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.user.util.UserManager
UserUtil userUtil = ComponentAccessor.getUserUtil()

def userPropertyManager = ComponentAccessor.getUserPropertyManager()
def userManager = ComponentAccessor.getUserManager()
def userList=[UserManager.getAllApplicationUsers()]
def email

userList.each { thisUser ->
    email = thisUser.getEmailAddress()
    userPropertyManager.getPropertySet(thisUser.getName()).setString("jira.meta.Alias",email.substring(0, email.indexOf('@')))
}

and the error message: 

2017-06-23 17:33:36,795 WARN [common.UserScriptEndpoint]: Script console script failed: 
groovy.lang.MissingMethodException: No signature of method: static com.atlassian.jira.user.util.UserManager.getAllApplicationUsers() is applicable for argument types: () values: []
	at Script80.run(Script80.groovy:8)

2 answers

1 accepted

0 votes
Answer accepted
Daniel Yelamos [Adaptavist]
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.
June 26, 2017

Hi Blake.

I think you should do this:

def userPropertyManager = ComponentAccessor.getUserPropertyManager()
def userManager = ComponentAccessor.getUserManager()
def userList=UserManager.getAllApplicationUsers()
def email

userList.each { thisUser ->
    email = thisUser.getEmailAddress()
    userPropertyManager.getPropertySet(thisUser.getName()).setString("jira.meta.Alias",email.substring(0, email.indexOf('@')))
}

 Without the brackets around the user manager, the script will give you a red at the start of the closure, but in execution time it should work properly. If you want to avoid the red you could do this:

def userPropertyManager = ComponentAccessor.getUserPropertyManager()
def userManager = ComponentAccessor.getUserManager()
Collection<ApplicationUser> userList=UserManager.getAllApplicationUsers()
def email

userList.each { thisUser ->
    email = thisUser.getEmailAddress()
    userPropertyManager.getPropertySet(thisUser.getName()).setString("jira.meta.Alias",email.substring(0, email.indexOf('@')))
}

Hope this helps!

Cheers!

Dyelamos

 

Blake Jones June 26, 2017

Thanks Dyelamos! I've tried both methods, but unfortunately am still getting the same error (for both methods):

Logs:

2017-06-26 10:32:40,572 WARN [common.UserScriptEndpoint]: Script console script failed: 
groovy.lang.MissingMethodException: No signature of method: static com.atlassian.jira.user.util.UserManager.getAllApplicationUsers() is applicable for argument types: () values: []
 at Script95.run(Script95.groovy:8)

Result: 

Result error.jpg

Daniel Yelamos [Adaptavist]
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.
June 27, 2017

Oh boy :D

I just realised, that you are calling

UserManager, (first letter is a capital, which is a global variable)

insted of 

userManager, (first letter undercase) which is the variable you get line 3.

Apologies for missing that. Your script should look like this:

def userPropertyManager = ComponentAccessor.getUserPropertyManager()
def userManager = ComponentAccessor.getUserManager()
def userList=userManager.getAllApplicationUsers()
def email

userList.each { thisUser ->
    email = thisUser.getEmailAddress()
    userPropertyManager.getPropertySet(thisUser.getName()).setString("jira.meta.Alias",email.substring(0, email.indexOf('@')))
}

Cheers!

Dyelamos

Blake Jones June 27, 2017

Thanks Dyelamos - that would have driven me crazy!

I was able to finally get it to work with your script by changing

userPropertyManager.getPropertySet(thisUser.getName()).

 to

userPropertyManager.getPropertySet(thisUser)

 

thanks again for your help!

Daniel Yelamos [Adaptavist]
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.
June 27, 2017

No problem. Please accept my answer so that people know this has been answered!

Cheers

Dyelamos

0 votes
Jarrod July 23, 2021

Thanks @Daniel Yelamos [Adaptavist]

This helped me.

I think though that there've been some changes since you submitted that code, so here is the latest code that I used.  However, my requirement was to remove the users "autowatch" preferences and let it inherit the settings:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserUtil
UserUtil userUtil = ComponentAccessor.getUserUtil()
def userPropertyManager = ComponentAccessor.getUserPropertyManager()
def userManager = ComponentAccessor.getUserManager()
def userList= userUtil.getUsers() //UserManager.getAllApplicationUsers()
def email
def username
userList.each { thisUser ->
def propertySet = userPropertyManager.getPropertySet(thisUser)
propertySet.remove("user.autowatch.disabled")
}

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events