How can I use the Groovy Runner jira user id rename form the command line?

Sorin Sbarnea (Citrix)
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 7, 2012

How can I use the Groovy Runner jira user id rename form the command line?

I need to run this for 500 users and I do not enjoy using the web interface for this.

/secure/admin/groovy/CannedScriptRunner.jspa?cannedScript=com.onresolve.jira.groovy.canned.admin.RenameUser#parameters

3 answers

1 vote
Scott Dudley [Inactive]
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.
December 19, 2012

Jamie's snippet was a great help, but I found that it didn't work directly for me with Script Runner 2.0.7 on JIRA 4.4. (The ComponentManager wasn't able to get the needed components, presumably because SR is now a versions 2 plugin.)

Here is a complete, working example that I fixed up to work with JIRA 4.4.5 and the above version of Script Runner. Obviously, YMMV with other versions of either product.

If you want to merge users rather than rename them, change the "(renameUser.FIELD_MERGE): 'false'," piece to read 'true' instead. Note that you seemingly need to pass a String containing the text 'true' or 'false', rather than a real boolean.

You may also want to change the log.error() calls to a different log level--I only did this because I was lazy and I didn't bother to lower the logging threshold to debug for just one script run.

import com.atlassian.jira.util.ErrorCollection;
import com.atlassian.jira.ComponentManager;
import com.atlassian.plugin.PluginAccessor;
import org.apache.log4j.Level
import org.apache.log4j.Category;

Category log = Category.getInstance(this.getClass());
ComponentManager componentManager = ComponentManager.getInstance();

PluginAccessor pluginAccessor = componentManager.getPluginAccessor();
Class scriptManagerClass = pluginAccessor.getClassLoader().findClass("com.onresolve.jira.groovy.ScriptManager");
Class cannedScriptRunnerClass = pluginAccessor.getClassLoader().findClass("com.onresolve.jira.groovy.CannedScriptRunner");

def scriptManager = ComponentManager.getOSGiComponentInstanceOfType(scriptManagerClass);

def groovyClassLoader = scriptManager.getGcl();

Map allUsers = new HashMap();
allUsers.put("user1_from", "user1_to");
allUsers.put("user2_from", "user2_to");
// repeat for as many users as necessary

for (Map.Entry entry : allUsers.entrySet()) {
    String fromUser = entry.getKey();
    String toUser = entry.getValue();
    log.error("** Mapping user " + fromUser + " to " + toUser);

    def renameUser = groovyClassLoader.loadClass("com.onresolve.jira.groovy.canned.admin.RenameUser", true, false).newInstance();

    Map<String, Object> inputs = [
            (renameUser.FIELD_FROM_USER_ID) : fromUser,
            (renameUser.FIELD_TO_USER_ID): toUser,
            (renameUser.FIELD_MERGE): 'false',
    ] as Map<String, Object>

    ErrorCollection errorCollection = renameUser.doValidate(inputs, true)
    if (errorCollection.hasAnyErrors()) {
        log.error errorCollection
    }
    else {
        def cannedScriptRunnerConstructor = cannedScriptRunnerClass.getConstructor(scriptManagerClass);
        def cannedScriptRunner = cannedScriptRunnerConstructor.newInstance(scriptManager);

        Map returnVars = cannedScriptRunner.runCannedScript(renameUser.class.getName(), inputs);
    }
}

log.error("** RenameUser All done!");

JamieA
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.
December 19, 2012

> Note that you seemingly need to pass a String containing the text 'true' or 'false', rather than a real boolean.

Yes... this is what the browser would send, iirc. I forgot it doesn't have a no-arg ctor anymore so nice work sussing that out. Would be good if this was a bit more script-friendly.
Rumceisz March 4, 2016

Hi Scott,

 

when I run this script, I get the following error:

java.lang.ClassNotFoundException: com.onresolve.jira.groovy.ScriptManager at com.atlassian.plugin.classloader.PluginsClassLoader.findClass(PluginsClassLoader.java:122) at Script29.run(Script29.groovy:11)

 

I use JIRA 6.3.11. Is there any class changed in the meantime?


Thanks

Scott Dudley [Inactive]
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.
March 7, 2016

I haven't looked at this script in years, but it appears that the JIRA CLI can now rename users on JIRA 6.1+.

1 vote
JamieA
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 22, 2012

When I need to do this I created a script for it... see below. Otherwise you could use wget/curl.

in some loop of users...
	log.debug  "Merge from $mergeFrom to $k"

	ScriptManager scriptManager = (ScriptManager) ComponentManager.getComponentInstanceOfType(ScriptManager.class);
	GroovyClassLoader gcl = scriptManager.getGcl();
	CannedScript renameUser = (CannedScript) gcl.loadClass("com.onresolve.jira.groovy.canned.admin.RenameUser", true, false).newInstance();

	Map<String, Object> inputs = [
		(renameUser.FIELD_FROM_USER_ID) : mergeFrom,
		(renameUser.FIELD_TO_USER_ID): k,
		(renameUser.FIELD_MERGE): true,
	] as Map<String, Object>


	renameUser.log.setLevel(Level.DEBUG)
	ErrorCollection errorCollection = renameUser.doValidate(inputs, true)
	if (errorCollection.hasAnyErrors()) {
		log.error errorCollection
	}
	else {
		runner.run(renameUser.class.getName(), inputs)
	}
end loop

0 votes
Andreas Gounaris
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 22, 2012

Hi,

I'm up to do the same thing, but I need to ensure that user ID rename/merge works correctly with a single user first.

What I'm planning to do is to take the source from the script and insert a new method to iterate through users in chuncks of 10-20, taking backups in the meantime (better be safe than sorry).

Then, save and execute this as a new custom script. I hope this helps you.

However, the rename doesn't work for me yet, it throws a bounch of warnings during preview, something about missing components.

So, the rest of my comment is a little out-of-topic:

The log says (just one of those entries):
Unable to find component with key 'class com.atlassian.jira.issue.search.CachingSearchRequestStore' - eventually found 'com.atlassian.jira.issue.search.CachingSearchRequestStore@358b7a35' the slow way.

When I click run, it goes on with renaming but it doesn't complete it (no issues are transferred but user is renamed, groups and roles are also updated). Then I have to restore everything from backup.

Did you make it with a single user?

JamieA
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 22, 2012

"Unable to find component with key"

This is annoying but expected, I have an unanswered question on this on this forum.

Without seeing the logs I couldn't tell what is going wrong in your case.

Andreas Gounaris
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 23, 2012

Thanks Jamie for reply,

Where can I send you the full log? Should I create a new issue in GRV project @ studio.plugins.atlassian.com?

Thanks,

Andreas Gounaris
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 23, 2012

Suggest an answer

Log in or Sign up to answer