Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Power Groovy Script unable to resolve class com.atlassian.jira.user.ApplicationUser

Brian Shumaker May 30, 2023

I am running the following groovy script in Power Groovy:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.ApplicationUsers

def userManager = ComponentAccessor.getUserManager()

// Get the current date
def currentDate = new Date()

// Calculate the date one year ago
def oneYearAgo = Calendar.instance
oneYearAgo.add(Calendar.YEAR, -1)

// Get all Jira users
def allUsers = userManager.getAllUsers().collect { ApplicationUsers.from(userManager.getUserByName(it.name)) }

// Iterate through each user
allUsers.each { user ->
def lastLogin = user?.lastLoginTime

// Check if the user hasn't logged in for over a year
if (lastLogin && lastLogin.before(oneYearAgo.time)) {
// Mark the user as inactive
userUtil.deactivateUser(user)
println "User ${user.name} marked as inactive."
}
}

I am coming up with the following error(s):
startup failed: Script1.groovy: 2: unable to resolve class com.atlassian.jira.user.ApplicationUser @ line 2, column 1. import com.atlassian.jira.user.ApplicationUser ^ Script1.groovy: 3: unable to resolve class com.atlassian.jira.user.ApplicationUsers @ line 3, column 1. import com.atlassian.jira.user.ApplicationUsers ^ 2 errors

I can find the class and verify the path to them, what am I doing wrong?
We run version 8.20.10 of Jira Server

Interface ApplicationUser:
https://docs.atlassian.com/software/jira/docs/api/8.20.10/com/atlassian/jira/user/ApplicationUser.html

Class ApplicationUsers:
https://docs.atlassian.com/software/jira/docs/api/8.20.10/com/atlassian/jira/user/ApplicationUsers.html

1 answer

1 vote
PD Sheehan
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.
June 5, 2023 edited

Power Groovy's implementation is very minimal and unsupported.

I don't think it supports imports outside of the immediate plugin context (which probably only includes the main groovy classes).

You might still be able to use power groovy to achieve what you are trying to do, but it will be more difficult.

For instance, I'm not sure where you found userUtil.deactivateUser()... but that's not a method that exists as far I'm aware.

Here is a script that you can use/adapt that will not need any imports.

 


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

def userUtil = ComponentAccessor.userUtil
def crowdService = ComponentAccessor.crowdService
Class UserService = ComponentAccessor.pluginAccessor.classLoader.findClass('com.atlassian.jira.bc.user.UserService')
def userService = ComponentAccessor.getOSGiComponentInstanceOfType(UserService)
def userSearchService = ComponentAccessor.userSearchService
Class UserSearchParams =ComponentAccessor.pluginAccessor.classLoader.findClass('com.atlassian.jira.bc.user.search.UserSearchParams')
def userSearchParams = UserSearchParams.newInstance(true, true, false);
def userList = userSearchService.findUsers("", userSearchParams);
def updateUser
def updateUserValidationResult

long count = 0

def groupManager = ComponentAccessor.getGroupManager();

userList.findAll{it.isActive()}.each {

if(groupManager.isUserInGroup(it.getName(),"jira-administrators"))
return;

def userWithAttributes = crowdService.getUserWithAttributes(it.getName())

String lastLoginMillis = userWithAttributes.getValue('login.lastLoginMillis')
if (lastLoginMillis?.isNumber()) {
Date d = new Date(Long.parseLong(lastLoginMillis))
if (d.before(dateLimit)) {
updateUser = userService.newUserBuilder(it).active(false).build()
updateUserValidationResult = userService.validateUpdateUser(updateUser)
if (updateUserValidationResult.isValid()) {
userService.updateUser(updateUserValidationResult)
count++
}
}
}
}

"${count} users deactivated.\n"

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
atlassian, jira service management, jsm webinar, ai in jsm, opsgenie integration, incident management, virtual agent, atlassian intelligence, ai-powered service desk, it operations, atlassian learning, service management webinar, team '25 recap

What’s new in Jira Service Management 🤔

Discover how Atlassian is revolutionizing service management with cutting-edge solutions for AI-powered support, HR Service Management, or DevOps connectivity.

Register here ⬇️
AUG Leaders

Atlassian Community Events