Here at Isos Technology, we've seen use cases for pretty much everything. One use case that has come up for both my clients and myself as an admin has been to identify a way to contact a large group of users. We may know the users' usernames in Jira, or have exported a list of said users, but perhaps now we need to get their email addresses. Going through each user one-by-one to gather this information would take a considerable amount of time, so in this blog post I will show you how to quickly identify the email addresses for a list of users using a simple Groovy script!
To run the script, just pull up a Groovy console in Jira (note that a Groovy console comes with some typical apps, like JMWE, and may not be available out-of-the-box).
Let's jump right in:
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.bc.user.search.UserSearchService def sb = new StringBuffer() def userManager = ComponentAccessor.getUserManager() def user = userManager.getUserByName( "amanda.kirk" ) sb. append (user.getEmailAddress()) return sb.toString() |
As you can see from the above, we use the UserManager from the Jira API to get the Application User object by username (as a string), we then get the email address of that Application User object and output it to the console. Sample output from the above script is below:
Now, the real magic happens here when we need to do the above for a large set of users. So to do that, just throw the usernames into a list in the script and then iterate over the list to output the associated emails of each user. An example script is shown below:
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.bc.user.search.UserSearchService def sb = new StringBuffer() def userManager = ComponentAccessor.getUserManager() def userList = [ "amanda.kirk" , "nich.hogue" ] userList. each { userName -> def user = userManager.getUserByName(userName) sb. append (userName + " : " + user.getEmailAddress() + "\n" ) } return sb.toString() |
From this output, you can copy and paste into a sheet, separating by colon to easily get a nice table of username/email columns.
There's one more trick that I will show you as a BONUS. Given you export a large list of usernames into a sheet, how can you easily transform this into an array list of usernames for your Groovy script?
First, copy the column of usernames and paste it into a text editor, as shown below (this example is with TextMate but any text editor with find and replace should work...):
Now, COMMAND-F to find and replace (your text editor may differ slightly but most have this same shortcut). Make sure regular expressions are enabled and enter the following:
Now, select Replace All and you will have the following updated text in your editor:
No matter the size of your list of users, all you need to do now is add the beginning Open bracket and quote at the beginning of the string and a closing quote and bracket at the end of the string like this:
And voila! It is as simple as that! I hope these script snippets can help speed your admin tasks or client needs. And as always, happy automating!