Forums

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

Cannot synchronize Jira's crowd directory with groovy post-function

Can Yılmaz August 9, 2019

Hi everyone,

Jira Version : 7.9.2

ScriptRunner : 5.4.12

I have a post-function custom groovy script which i try to start synchronization of crowd directory. But when transition is over (post-function seems to be working), synchronization of directory didn't starts. Here's the weird part. If i run the same code at the script-runner console ; synchronization starts and finished. (I check the jira user directory page to see sync is started or not.) 

import com.atlassian.crowd.embedded.api.CrowdDirectoryService
import com.atlassian.crowd.embedded.api.Directory
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Logger

def className = "tr.com.foo.Scripts.sync_crowd"
def Logger log = Logger.getLogger(className)
log.debug("Script " + className + " started")

try{
CrowdDirectoryService crowdDirectoryService = ComponentAccessor.getComponent(CrowdDirectoryService.class);
if (crowdDirectoryService != null){
List<Directory> directories = crowdDirectoryService.findAllDirectories();
for (Directory directory : directories){
if (directory.getType().name().equals("CROWD")){
if (crowdDirectoryService.isDirectorySynchronisable(directory.getId())){
crowdDirectoryService.synchroniseDirectory(directory.getId());
log.debug(directory.getId() + " in sync")
}
}
}
}
else{
log.debug ("Directories not found")
}
} catch (Exception e){
log.debug("exception: " + e)
}

This code  doesn't work when it is used as a post-function but works if i run it from the script runner console.

What could be the reason for this ? 

3 answers

0 votes
Mariusz Piotrowski April 16, 2025

I know it's 5 years late, but I got the same issue and managed to resolved it. I used different class, but it should work also with above example. There are two functions to synchronize user directories. Use the one with parameter for "run in background" set to false and it starts to work in postfunction. Mine code is like this:

import com.atlassian.crowd.manager.directory.DirectoryManager
import com.atlassian.jira.component.ComponentAccessor
import static com.atlassian.crowd.manager.directory.SynchronisationMode.INCREMENTAL

def directoryManager = ComponentAccessor.getComponent(DirectoryManager)
def did= directoryManager.findDirectoryByName("JIRA-TST").getId()
if (directoryManager.isSynchronisable(did) && !directoryManager.isSynchronising(did))
{
    directoryManager.synchroniseCache(did, INCREMENTAL, false)
}
0 votes
Dan C
Contributor
April 20, 2020

I am having the same issue - did you ever find the solution?

 

I tried setting the user with @PD Sheehan 's example above, but that did not work either.

 

My transition is being initiated by Automation For Jira, and the "actor" is the same admin account that is able to do this with no problem in the scriptrunner console.

0 votes
PD Sheehan
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 9, 2019

I don't have a way to verify, but I wonder if it might be a permission issue.

In the console, you are obviously an admin.
But in the post-function, you might expect a non-admin user to kick that off. I guess if you perform the workflow function instead of a non-admin user... does it work? Or does it not work regardless of the user?

Either way, maybe you try to change the current user before executing that part of the script with:

def adminUser = ComponentAccessor.userManager.getUserByName('admin')
ComponentAccessor.jiraAuthenticationContext.setLoggedInUser(adminUser)

Are any of the logs message found when running as post function?

Suggest an answer

Log in or Sign up to answer