Get JIRA username from email

Chris Kast June 13, 2017

I'm trying to write a groovy script that will get the username of a user given the specified email. Here's what I have:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.user.search.UserSearchService

def userSearchService = ComponentAccessor.getComponent(UserSearchService)
def user = userSearchService.findUsersByEmail("joe.user@mycompany.com")

How can I pull the the username out of the "user" object? If I do:

String user_key = user.first()
log.info user_key

It'll print out in the log: "joe.user(juser)" but I gotta think there's a better way to pull that info than breaking apart that string.

1 answer

1 accepted

2 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 14, 2017

Hello Chris.

As you can see here(the official atlassian documenation), the function getUsersByEmail returns an array of <ApplicationUsers>. 

In the link you will find that if you have a variable ApplicationUser. You can simply call the method .getName()

Hope that you found this useful. If you need anymore help don't hessitate to ask.

Cheers

Dyelamos

Chris Kast June 14, 2017

Perfect! Exactly what I was looking for. Just had to add:

ApplicationUser my_user = user[0]

log.info "The username is: " + my_user.getName()
Tomáš Vrabec September 23, 2020

Will just make a note in here, maybe even my future me will return here.

You can also use methods:

.getKey()

.getUsername()

.getDirectoryId()

.isActive()

.getEmailAddress()

.getDisplayName()

Like # people like this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events