Groovy Script to remove user from all component lead roles

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

2 answers

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

 

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

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)}

 

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 

I solved myself after learning some concepts in Groovy

Thanks,

Balvant

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,760 views 11 18
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot