Thursday, January 28, 2016

Groovy script to remove not active users from JIRA who has not logged into JIRA since 2016

import com.atlassian.jira.bc.security.login.LoginService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.user.util.UserUtil

def result = ""

UserManager userManager = ComponentAccessor.userManager
UserUtil userUtil = ComponentAccessor.userUtil
LoginService loginService = ComponentAccessor.getComponent(LoginService)

def myUser = ComponentAccessor.jiraAuthenticationContext?.getUser()

if (myUser) {
    userManager.getAllApplicationUsers().findAll{ user ->
        loginService.getLoginInfo(user.getName()).getLastLoginTime() != null
    }.each{ user ->
            def val = loginService.getLoginInfo(user.getName()).getLastLoginTime()
            def fulllogindate = new Date(val)
def year = fulllogindate[Calendar.YEAR]
        if (year < 2016) {
              if (userUtil.getNumberOfAssignedIssuesIgnoreSecurity(myUser,user)) {
              result += "User $user could not be removed because there are assigned issues!\r\n"
} else if (userUtil.getNumberOfReportedIssuesIgnoreSecurity(myUser,user)) {
            result += "User $user could not be removed because there are reported issues!\r\n"
        } else if (userUtil.getComponentsUserLeads(user)) {
           result += "User $user could not be removed because he leads a component!\r\n"
        } else {
            //userUtil.removeUser(myUser,user)
            result += "User $user removed.\r\n"
        }
} else {
result += "User $user used JIRA in 2016.\r\n"
}
}
}
result



I have commented the removeUser action.. You can do a dry run first and then choose to uncomment it and execure it