Hello,
I have written a groovy script to deactivate users. The issue I am running into is that the script will fail if the user is a component lead. What I want to do is check if the user is a component lead and remove them if they are. So far I have been struggling to do so. Any help would be much appreciated.
Script to deactivate user:
def deActivateUser(user, email, fullName, userService, userUtil){
deActivate = ImmutableUser.newUser(user).active(false).emailAddress("DELETE.${email}").displayName("${fullName} - OLD").toUser()
//user project or component lead?
def userProjectLead = userUtil.getProjectsLeadBy(user)
def userComponentLead = ComponentAccessor.projectComponentManager.findComponentsByLead("userName")
//get the request
updateUserValidationResult = userService.validateUpdateUser(deActivate)
if (!userProjectLead && !userComponentLead) {
userService.updateUser(updateUserValidationResult)
userUtil.removeUserFromGroup(group, user)
return "User ${user} deactivated"
}
Hi Justin,
As you said one of the reasons that the script could fail to deactivate the user is because he/she is a project or component lead. So, in this case you have to remove him/her from the lead roles first. This worked for me, hope to work for you as well
/** * Groovy Script to remove user from all component lead roles * and deactivate him. */ import com.atlassian.crowd.embedded.impl.ImmutableUser import com.atlassian.jira.bc.project.component.MutableProjectComponent import com.atlassian.jira.bc.user.UserService import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.user.DelegatingApplicationUser def userService = ComponentAccessor.getComponent(UserService.class) def userName = "bob" // get the project components where user leads def componentsLeadedBy = ComponentAccessor.projectComponentManager.findComponentsByLead(userName) def userManager = ComponentAccessor.getUserManager(); // Get the projects where user leads def applicationUser = userManager.getUserByName(userName); def projectLeadedBy = ComponentAccessor.userUtil.getProjectsLeadBy(applicationUser); def projectManager = ComponentAccessor.getProjectManager(); def projectComponentManager = ComponentAccessor.getProjectComponentManager(); if (!componentsLeadedBy && !projectLeadedBy) log.info "userName doesn't lead any component or project" else { //remove userName from project lead roles if (projectLeadedBy.size() > 0) for (project in projectLeadedBy) { projectManager.updateProject(project, project.name, project.description, "", project.url, project.assigneeType); } //remove userName from component lead roles if (componentsLeadedBy.size() > 0) for (component in componentsLeadedBy) { MutableProjectComponent newComponent = MutableProjectComponent.copy(component) newComponent.setLead("") projectComponentManager.update(newComponent) } } def listUsers = userManager.getUsers().findAll{user -> user.name.equals(userName)} if (listUsers.size() <= 0) return "User userName does not exist" ImmutableUser.Builder builder = ImmutableUser.newUser(listUsers[0]); builder.active(false); def updatedUser = new DelegatingApplicationUser(applicationUser.key, builder.toUser()) UserService.UpdateUserValidationResult updateUserValidationResult = userService.validateUpdateUser(updatedUser) if (updateUserValidationResult.isValid()) { userService.updateUser(updateUserValidationResult) return "User ${applicationUser.name} deactivated"; } else { return updateUserValidationResult.getErrorCollection() }
Hi Justin,
Thanks for script, but i am getting below error message while executing through script console in jira 7.0.11
kindly help in fixing this issue.
If possible please provide any URL for learning Groovy with intention of Jira since i have basic nowledge of Java.
Thanks,
Balvant
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
error message
2017-04-21 08:13:29,996 WARN [common.UserScriptEndpoint]: Script console script failed: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script269.groovy: 24: expecting ')', found ';' @ line 24, column 29. if (!componentsLeadedBy && !projectLeadedBy) ^ 1 error
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes the Atlassian Community is fairly new and has some issues to fix, one of them is how the code is rendered. So in your case
line 24: if (!componentsLeadedBy && !projectLeadedBy)
line 28: if (projectLeadedBy.size() > 0)
line 34: if (componentsLeadedBy.size() > 0)
line 42: def listUsers = userManager.getUsers().findAll{user -> user.name.equals(userName)}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Thanos,
Apprciate your quick help and now i am getting below error
2017-04-21 09:33:32,033 WARN [common.UserScriptEndpoint]: Script console script failed: java.lang.NullPointerException at com.atlassian.crowd.embedded.impl.ImmutableUser.newUser(ImmutableUser.java:91) at com.atlassian.crowd.embedded.impl.ImmutableUser$newUser.call(Unknown Source) at Script311.run(Script311.groovy:45)
line numbers are 45,47.
Please suggest any editor or something for fixing rendering related issue in code.
I want to learn this Groovy but understanding complete syntex itself is confusing so kindly provide any tutorial link to learn from scratch to advance
Thanks,
Balvant
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I solved myself after learning some concepts in Groovy
Thanks,
Balvant
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.