How to Automatically deactivate inactive JIRA users

Mahbub Rahman October 3, 2016

I am trying Automatically deactivate inactive JIRA (7.1.8 ) users  by using code provided link at https://www.adaptavist.com/doco/display/SFJ/Automatically+deactivate+inactive+JIRA+users 

Error

No signature of method: com.atlassian.jira.bc.user.DefaultUserService.validateUpdateUser() is applicable for argument types: (com.atlassian.crowd.embedded.impl.ImmutableUser) values: [com.atlassian.crowd.embedded.impl.ImmutableUser@bec3c60c] Possible solutions: validateUpdateUser(com.atlassian.jira.user.ApplicationUser), validateCreateUser(com.atlassian.jira.bc.user.UserService$CreateUserRequest)

 

groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.bc.user.DefaultUserService.validateUpdateUser() is applicable for argument types: (com.atlassian.crowd.embedded.impl.ImmutableUser) values: [com.atlassian.crowd.embedded.impl.ImmutableUser@bec3c60c] Possible solutions: validateUpdateUser(com.atlassian.jira.user.ApplicationUser), validateCreateUser(com.atlassian.jira.bc.user.UserService$CreateUserRequest) at Script1$_run_closure2.doCall(Script1.groovy:32) at Script1.run(Script1.groovy:25)


Any idea what is missing  or wron in this code

 

import com.atlassian.crowd.embedded.api.CrowdService

 

import com.atlassian.crowd.embedded.api.User

 

import com.atlassian.crowd.embedded.api.UserWithAttributes

 

import com.atlassian.crowd.embedded.impl.ImmutableUser

 

import com.atlassian.jira.bc.user.UserService

 

import com.atlassian.jira.component.ComponentAccessor

 

import com.atlassian.jira.user.util.UserUtil

 

import org.apache.log4j.Level

 

import org.apache.log4j.Logger

 

  

 

Logger log = log

 

log.setLevel(Level.INFO)

 

  

 

int numOfDays = 100 // Number of days the user was not logged in

 

Date dateLimit = (new Date())- numOfDays

 

  

 

UserUtil userUtil = ComponentAccessor.userUtil

 

CrowdService crowdService = ComponentAccessor.crowdService

 

UserService userService = ComponentAccessor.getComponent(UserService.class)

 

User updateUser

 

UserService.UpdateUserValidationResult updateUserValidationResult

 

  

 

long count = 0

 

  

 

userUtil.getUsers().findAll{it.isActive()}.each {

 

    UserWithAttributes user = crowdService.getUserWithAttributes(it.getName())

 

    String lastLoginMillis = user.getValue('login.lastLoginMillis')

 

    if (lastLoginMillis?.isNumber()) {  

 

        Date d = new Date(Long.parseLong(lastLoginMillis))

 

        if (d.before(dateLimit)) {

 

            updateUser = ImmutableUser.newUser(user).active(false).toUser()
// this line causing error above

 

            updateUserValidationResult = userService.validateUpdateUser(updateUser)

 

            if (updateUserValidationResult.isValid()) {

 

                userService.updateUser(updateUserValidationResult)

 

                log.info "Deactivated ${updateUser.name}"

 

                count++

 

            } else {

 

                log.error "Update of ${user.name} failed: ${updateUserValidationResult.getErrorCollection().getErrors().entrySet().join(',')}"

 

            }

 

        }

 

    }

 

}

 

"${count} users deactivated.\n"





1 answer

0 votes
Thanos Batagiannis _Adaptavist_
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.
October 3, 2016

Hi Muhammad

The above example is for JIRA v6, for JIRA v7 there are few changes, therefore the script comp for JIRA 7 should be

import com.atlassian.crowd.embedded.api.CrowdService
import com.atlassian.crowd.embedded.api.UserWithAttributes
import com.atlassian.crowd.embedded.impl.ImmutableUser
import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.ApplicationUsers
import com.atlassian.jira.user.util.UserUtil

int numOfDays = 100 // Number of days the user was not logged in
Date dateLimit = (new Date())- numOfDays

UserUtil userUtil = ComponentAccessor.userUtil
CrowdService crowdService = ComponentAccessor.crowdService
UserService userService = ComponentAccessor.getComponent(UserService)
ApplicationUser updateUser
UserService.UpdateUserValidationResult updateUserValidationResult

long count = 0

userUtil.getUsers().findAll{it.isActive()}.each {
    UserWithAttributes user = crowdService.getUserWithAttributes(it.getName())
        String lastLoginMillis = user.getValue('login.lastLoginMillis')
        if (lastLoginMillis?.isNumber()) {
            Date d = new Date(Long.parseLong(lastLoginMillis))
            if (d.before(dateLimit)) {
                updateUser = ApplicationUsers.from(ImmutableUser.newUser(user).active(false).toUser())
                updateUserValidationResult = userService.validateUpdateUser(updateUser)
                if (updateUserValidationResult.isValid()) {
                    userService.updateUser(updateUserValidationResult)
                    log.info "Deactivated ${updateUser.name}"
                    count++
                } else {
                    log.error "Update of ${user.name} failed: ${updateUserValidationResult.getErrorCollection().getErrors().entrySet().join(',')}"
                }
            }
        }
}

"${count} users deactivated.\n"

regards, Thanos

Jeanne Howe
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 1, 2016

Thanos,

Thank you for this. Very helpful.

Was wondering if there is a way to expand this script to exclude certain accounts. We have service accounts used for notifications only. These accounts never actually log into JIRA. I am hoping that if we use a standard naming convention, preference the user name with SYS-, could this script be modified to exclude accounts that begin with SYS- ? We do not want these accounts deactivated.

 

Thank you

Jeanne

Thanos Batagiannis _Adaptavist_
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 1, 2016

Hi Jeanne,

You can update the condition to be something like 

//if has not logged in for x days AND it's username does not start with the "SYS-" prefix 
if (d.before(dateLimit) && !it.username.startsWith("SYS-")) {
	//rest of the code that deactivates user
}
Utkarsh Agarwal February 2, 2017

Silly question. But can you let me know how can i add Escalation service in script runner to perform this job daily as mentioned in the link:  https://www.adaptavist.com/doco/display/SFJ/Automatically+deactivate+inactive+JIRA+users

I mean what should be the contents for: 

JQL Query:

Userkey:

cron expression:

additional issue action:

JQL query to select issues with. For example "issuetype = Task and updated < -7d"
Enter the key of the user that the service will run as
How often the service should run, in minutes. If not a number, CRON expression is expected
Optionally set an action...
 
1
 
Thanos Batagiannis _Adaptavist_
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.
February 3, 2017

Hi Utkarsh,

Probably you will need a ScriptRunner Service

Mark McCormack _Adaptavist_
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.
February 3, 2017

Hi Utkarsh,

Apologies but the document you viewed was an old draft and incorrect. @Jamie Echlin (Adaptavist) alerted me of this discrepancy and I've since reviewed and updated it to reflect how to set this up as a JIRA Service.

I've also created an issue to update the ScriptRunner docs to include this script there too.

Regards, Mark

Utkarsh Agarwal February 5, 2017

Thanks Mark.

Jeanne Howe
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.
February 6, 2017

I am looking to add one more parameter to the script. For the JIRA 7 comp, how do I limit the users to those in the jira-users group? We have several accounts used for notification purposes only. These accounts are not in the jira-users group, thus preventing end-users from trying to log in with these accounts. The accounts will never have a "last login" but should not be deactivated. 

이경언 February 9, 2017

Hi. I want to use this script. 
But It doesn't work. 
I was login use admin id. And  Go Admin page -> Addon -> Script console.

And Run this code. It work.

 

But I go system-> Services and Run this code.

error like this

Error Messages: [You do not have the permission to update users.]

Help me ^^

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events