Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,456,998
Community Members
 
Community Events
176
Community Groups

Help setting User Property dynamically using a groovy script

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

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

 

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

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

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!

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

Cheers

Dyelamos

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

Atlassian Community Events