Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

groovy script to deactivate the last 90 day users in jira .

Kotakonda, Bhargavi July 18, 2023

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 = 90 // 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{user.isActive()}.each {
UserWithAttributes user = crowdService.getUserWithAttributes(user.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++
log.error "Update of ${count} ${user.name} success:"
} else {
//log.error "Update of ${user.name} failed: ${updateUserValidationResult.getErrorCollection().getErrors().entrySet().join(',')}"
//"${count} ${updateUser.name} not deactivated.\n"
}
}
}
}

"${count} users deactivated.\n"

 

this script was working fine for us but since the last time we have an error 

error:  groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.user.util.UserUtilImpl.getUsers() is applicable for argument types: () values: [] Possible solutions: getClass(), every() at Script48.run(Script48.groovy:21)

 

anybody with strong scripting ,can i get help on this as to what is the error signifying. 

2 comments

Comment

Log in or Sign up to comment
Tim Perrault
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.
July 18, 2023

Hi @Kotakonda, Bhargavi 

 

Did you upgrade recently? When I put your script in, the console gives me a couple of errors.

  • This method is removed in Jira 9. Your code will work now but consider updating this to ensure it continues to work in Jira 9
  • [Static type checking] - The variable [user] is undeclared.

The good news is that adaptavist has this exact example in their library .

Like Vikrant Yadav likes this
Kotakonda, Bhargavi July 18, 2023

Hi @Tim Perrault we are on version 9.4.4 LTS can you let me know what has to be added or removed from the script

Tim Perrault
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.
July 18, 2023

@Kotakonda, Bhargavi 

 

Put the script into the script console and it will tell you the issues within your script. With that said, I wouldn't waste time trying to reinvent the wheel. The adaptavist script is exactly what you are trying to do using the latest methods provided.

 

Literally the only change you would need to make to adaptavist's script is changing def numOfDays = 100 to def numOfDays = 90

Kotakonda, Bhargavi July 18, 2023

thanks 

Like Tim Perrault likes this
Kotakonda, Bhargavi July 21, 2023

@Tim Perrault i see an error when i run the query

error: 2023-07-21 14:08:56,051 ERROR [runner.ScriptBindingsManager]: Update of mczerpak failed. Errors: {} Error Messages: [Cannot edit user, as the user's directory is read only.] 2023-07-21 14:08:56,051 ERROR [runner.ScriptBindingsManager]: Update of mdc failed. Errors: {} Error Messages: [Cannot edit user, as the user's directory is read only.] 2023-07-21 14:08:56,051 ERROR [runner.ScriptBindingsManager]: Update of mdelafuenter failed. Errors: {} Error Messages: [Cannot edit user, as the user's directory is read only.] 2023-07-21 14:08:56,051 ERROR [runner.ScriptBindingsManager]: Update of mdg failed. Errors: {} Error Messages: [Cannot edit user, as the user's directory is read only.] 2023-07-21 14:08:56,051 ERROR [runner.ScriptBindingsManager]: Update of mdiaz failed. Errors: {} Error Messages: [Cannot edit user, as the user's directory is read only.] 2023-07-21 14:08:56,051 ERROR [runner.ScriptBindingsManager]: Update of mdonohoe failed. Errors: {} Error Messages: [Cannot edit user, as the user's directory is read only.] 2023-07-21 14:08:56,051 ERROR [runner.ScriptBindingsManager]: Update of mdougherty failed. Errors: {} Error Messages: [Cannot edit user, as the user's directory is read only.] 2023-07-21 14:08:56,051 ERROR [runner.ScriptBindingsManager]: Update of meers failed. Errors: {} Error Messages: [Cannot edit user, as the user's directory is read only.] 

Tim Perrault
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.
July 21, 2023

@Kotakonda, Bhargavi 

 

The error indicates that the user's directory is read only, meaning you can only read information from the directory and you won't be able to edit the account.

 

You could go through the Troubleshooting LDAP User Management page to try and determine what is going on.

I would specifically look at this section:

Unable to add/delete Users or make changes to their Groups, Project Roles or Edit them.The LDAP/AD User Directory is set to Read Only.

Verify the Permissions Settings are correct for the external User Directory.

If you want to make changes, Read/Write will need to be activated. If using Active Directory, please refer to this page on Configuring an SSL Certificate.

Kotakonda, Bhargavi August 21, 2023

@Tim Perrault the script you justed worked fine in test in in production i don see any result

nor any error

Kotakonda, Bhargavi August 21, 2023

The result is blank in jira script console 

TAGS
AUG Leaders

Atlassian Community Events