How to bulk get list of Jira Users having their user keys

Rafał_Sosnowski January 2, 2018

I want to get list of Jira Users, preferably in one operation, by Jira Java API. I have list of corresponding User Keys. Most important restriction is that API must work both in Jira 6 and Jira 7.

1 answer

1 accepted

1 vote
Answer accepted
Alexey Matveev
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.
January 2, 2018

Hello,

Try something like this

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.security.login.LoginManager

LoginManager loginManager = ComponentAccessor.getComponent(LoginManager.class);

Set<ApplicationUser> appUsers = ComponentAccessor.getUserManager().getAllUsers();
Rafał_Sosnowski January 3, 2018

Thank you for your answer, however I would like to get only users that keys I have, in order to avoid situation of loading all Jira Users to obtain only fraction of them, e.g. 2000 of 20000.

Alexey Matveev
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.
January 3, 2018

Filter the appUsers list as you wish. I do not think your Jira crashes if you get the full list first.

Rafał_Sosnowski January 3, 2018

I will follow your advice. Last question: is there a way to get only active Jira Users?

Alexey Matveev
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.
January 3, 2018

The problem is that you said you want a script for Jira 6 and Jira 7. In Jira 7 you can use UserSearchService which will let you filter users before retrieving them. I am not sure if UserSearchService will work for you Jira 6 version. That is why when you filter the appUsers list check if a user is active by isActive method of ApplicationUser class. If you want to try UserSearchService the code would look like that

import com.atlassian.jira.component.ComponentAccessor

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

import com.atlassian.jira.bc.user.search.UserSearchParamsdef userSearchService = ComponentAccessor.getComponent(UserSearchService.class);UserSearchParams userSearchParams = (new UserSearchParams.Builder()).allowEmptyQuery(true).includeActive(false).includeInactive(true).maxResults(100000).build();userSearchService.findUsers("", userSearchParams).each{ it-> log.error(it.getKey())}

But I am not sure if the code works for Jira 6.

Suggest an answer

Log in or Sign up to answer