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

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

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

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?

"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.

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,

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!");

> 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.

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

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

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

135 views 1 17
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you