Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Assistance using Jira's DirectoryManager class/library

Garry May August 20, 2024

Hi, I was wondering if someone could assist with the conversion of this simplified directory code?  That is, I'd like to convert the usage of 'findAllDirectories' it to use the new non-deprecated call, i.e., searchDirectories(EntityQuery) instead.

 

 

import com.atlassian.crowd.manager.directory.DirectoryManager
def directoryManager = ComponentAccessor.getComponent(DirectoryManager)


return directoryManager.findAllDirectories().find {
    it.name.toString().toLowerCase() == 'jira internal directory'
}?.getId() ?: -1
I know I need to use com.atlassian.crowd.search.query.entity.DirectoryQuery and a replacement call like "List<Directory> ld = directoryManager.searchDirectories( XXX )" but I can't figure it out?
Any help greatly appreciated!
Garry

 

1 answer

1 accepted

0 votes
Answer accepted
Radek Dostál
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.
August 21, 2024

Something like this works

import com.atlassian.crowd.manager.directory.DirectoryManager
import com.atlassian.crowd.search.query.entity.DirectoryQuery
import com.atlassian.crowd.search.query.entity.restriction.NullRestrictionImpl
import com.atlassian.jira.component.ComponentAccessor

DirectoryManager directoryManager = ComponentAccessor.getComponent(DirectoryManager)
directoryManager.searchDirectories(new DirectoryQuery(NullRestrictionImpl.INSTANCE, 0, 100)).stream().forEach(directory -> {
log.warn(directory.getName() + ":" + directory.getId())
})

 

However, there are many SearchRestriction implementations and unless you really, really care about the method being deprecated, I would say just use the deprecated method, until it's removed. Once you start digging this far you will start seeing magic numbers such as 0 and 100, which are start index and max results.

 

Seeing as you're just looking for internal directory you could just as easily hardcode

directoryManager.findDirectoryById(1L)

because internal dir in Jira is always 1

 

Or you could just search by name

directoryManager.findDirectoryByName("Jira Internal Directory")

internal directory is static, it never changes the id, it never changes the name. It beats me as to why anyone would decide to change it.

 

Garry May August 27, 2024

Thanks Radek - perfect!

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
9.15.2
TAGS
AUG Leaders

Atlassian Community Events