ScriptRunner to get all instance users in the Jira Internal Directory

David Harkins
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.
September 15, 2023

For user management we need a list of all users within the 'Jita Internal Directory' only.

I have a working script, but would prefer to apply a filter when obtaining the users instead of afterwards.

 

import com.atlassian.jira.bc.JiraServiceContextImpl

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

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

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.user.ApplicationUser

UserSearchParams.Builder paramBuilder = UserSearchParams.builder()

        .allowEmptyQuery(true)

        .includeActive(true)

        .includeInactive(true)

        .limitResults(20000)

ApplicationUser loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

JiraServiceContextImpl jiraServiceContext = new JiraServiceContextImpl(loggedInUser)

Collection<ApplicationUser> users = ComponentAccessor.getComponent(UserSearchService).findUsers(jiraServiceContext, "", paramBuilder.build())

log.info(users.size())

Collection<ApplicationUser> externalUsers = []

for (user in users) {

    if (user.directoryId == 1) {

        externalUsers.add(user)

    }

}

log.info(externalUsers.size())

 

2023-09-15 09:38:31,851 INFO [runner.ScriptBindingsManager]: 11318

2023-09-15 09:38:31,851 INFO [runner.ScriptBindingsManager]: 10249

3 answers

1 accepted

2 votes
Answer accepted
David Harkins
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.
September 19, 2023

Thanks to @Ram Kumar Aravindakshan _Adaptavist_ 

My updated script now no longer needs to iterate through each user to filter out the non-Internal Directory users, and the directory name can be used as a bonus.

Will just need to monitor the value for limit results.

 

import com.atlassian.crowd.manager.directory.DirectoryManager
import com.atlassian.jira.bc.user.search.UserSearchParams
import com.atlassian.jira.bc.user.search.UserSearchService
import com.atlassian.jira.component.ComponentAccessor

final
directoryToCheck = 'Jira Internal Directory'

def directoryManager = ComponentAccessor.getComponent(DirectoryManager)
def userSearchService = ComponentAccessor.getComponent(UserSearchService)
def internalDirectoryId = directoryManager.findAllDirectories()?.find { it.name.toString().toLowerCase() == directoryToCheck.toLowerCase() }?.id

UserSearchParams.Builder paramBuilder = UserSearchParams.builder()
        .allowEmptyQuery(true)
        .includeActive(true)
        .includeInactive(true)
        .limitResults(20000)

def allInternalDirectoryUsers = userSearchService.findUsers('', paramBuilder.build()).findAll { it.directoryId == internalDirectoryId }

log.info("Users in Jira Internal Directory: ${allInternalDirectoryUsers?.size()}")

0 votes
Lisa Sun January 5, 2024

.

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 18, 2023

Hi @David Harkins

For your requirement, I suggest looking at this Adaptavist Library script.

You will need to modify it slightly as it looks specifically at inactive users. 

I hope this helps answer your question. :-)

Thank you and Kind regards,

Ram

David Harkins
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.
September 19, 2023

@Ram Kumar Aravindakshan _Adaptavist_ Thank you for the pointer ;-)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events