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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,456,196
Community Members
 
Community Events
176
Community Groups

Scriptrunner groovy script not working for email

Greetings Everyone!

I am using Conf DC 7.13.7 and just installed scriptrunner. I was given a groovy Script template by adaptavist. I have tried modifying and running. It runs fully then nothing happens.

I was supposed to get an email with the List of Deactivated users who have not logged in 90+ days and also deactivate them in Confluence. I have added the Script below.
ANY HELP WILL GREATLY BE APPRECIATED.

//MY SCRIPT

import com.atlassian.confluence.security.login.LoginManager
import com.atlassian.confluence.user.UserAccessor
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.crowd.embedded.api.Group
import com.atlassian.user.GroupManager
import com.atlassian.confluence.user.ConfluenceUser
import com.onresolve.scriptrunner.parameters.annotation.GroupPicker
import com.onresolve.scriptrunner.parameters.annotation.NumberInput
import groovy.util.logging.Slf4j
import com.atlassian.mail.Email;
import com.atlassian.mail.queue.SingleMailQueueItem;
import com.atlassian.confluence.mail.ConfluenceMailServerManager

@grab(group = 'atlassian.mail', module='mail', version='1.4.7')
import atlassian.mail.*
import atlassian.mail.internet.*
 
@NumberInput(label = 'Maximum users', description = 'The maximum number of users to allow in Confluence')
Integer maximumUsers

@GroupPicker(label = 'User group', description = 'The group of users to check (e.g. "confluence-users")')
Group selectedUserGroup

@GroupPicker(label = 'Deactivated user group', description = 'The group to add the deactivated users to (e.g. "deactivated-users"')
Group selectedDeactivatedGroup

def groupManager = ComponentLocator.getComponent(GroupManager)
def loginManager = ComponentLocator.getComponent(LoginManager)
def userAccessor = ComponentLocator.getComponent(UserAccessor)


// Set defaults in case the fields are not filled out
maximumUsers = maximumUsers ?: 500
def userGroup = selectedUserGroup ? groupManager.getGroup(selectedUserGroup.name) : groupManager.getGroup('confluence-users')
def deactivatedGroup = selectedDeactivatedGroup ? groupManager.getGroup(selectedDeactivatedGroup.name) : groupManager.getGroup('deactivated-users') ?: groupManager.createGroup('deactivated-users')

// Get all users from the selected user group
def users = userAccessor.getMembers(userGroup).asList()

// For the users found in the selected group, sort the users by last login date
Map<ConfluenceUser, Date> userWithLoginDate =
    users.collectEntries {
        [(it): (loginManager.getLoginInfo(it)?.lastSuccessfulLoginDate)]
    }.sort { it -> it.value }

// Get all user keys
def sortedUsers = userWithLoginDate*.key

def confluenceMailServerManager = ComponentLocator.getComponent(ConfluenceMailServerManager)
SMTPMailServer mailServer = confluenceMailServerManager.getDefaultSMTPMailServer();

def sendEmailAboutRemovedUsers(List<String> userIds){
    Email email = new Email("john.doe@company.net");
    email.setFromName("TEST Plugin");
    email.setSubject("Deactivated Users");
    email.setBody("Users deactivated: " + userIds.join(", "));
    email.setMimeType("text/html");
    SingleMailQueueItem smqi = new SingleMailQueueItem(email);
    ComponentAccessor.getMailQueue().addItem(smqi);
}

// Deactivate user accounts and add them to the deactivated group if the user list size is more than the allowed amount
if (sortedUsers.size() > maximumUsers) {
    def deactivated = sortedUsers.subList(0, sortedUsers.size() - maximumUsers)
    deactivated.each { user ->
        groupManager.removeMembership(userGroup, user)
        groupManager.addMembership(deactivatedGroup, user)
        userAccessor.deactivateUser(user)
    }

    sendEmailAboutRemovedUsers(deactivated)
}

1 answer

0 votes

I can't imagine why this script would work. It seems to combine elements from Jira and Confluence API.

Here is a version that should work (provided you don't have too many users (otherwise it might either run very long or crash your instance with an out-of-memory or long GC).

import com.atlassian.confluence.mail.ConfluenceMailServerManager
import com.atlassian.confluence.security.login.LoginManager
import com.atlassian.confluence.user.ConfluenceUser
import com.atlassian.confluence.user.UserAccessor
import com.atlassian.crowd.embedded.api.Group
import com.atlassian.mail.Email
import com.atlassian.mail.server.SMTPMailServer
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.user.GroupManager
import com.onresolve.scriptrunner.parameters.annotation.GroupPicker
import com.onresolve.scriptrunner.parameters.annotation.NumberInput

@NumberInput(label = 'Maximum users', description = 'The maximum number of users to allow in Confluence')
Integer maximumUsers
@GroupPicker(label = 'User group', description = 'The group of users to check (e.g. "confluence-users")')
Group selectedUserGroup
@GroupPicker(label = 'Deactivated user group', description = 'The group to add the deactivated users to (e.g. "deactivated-users"')
Group selectedDeactivatedGroup

def groupManager = ComponentLocator.getComponent(GroupManager)
def loginManager = ComponentLocator.getComponent(LoginManager)
def userAccessor = ComponentLocator.getComponent(UserAccessor)

// Set defaults in case the fields are not filled out
maximumUsers = maximumUsers ?: 500
def userGroup = selectedUserGroup ? groupManager.getGroup(selectedUserGroup.name) : groupManager.getGroup('confluence-users')
def deactivatedGroup = selectedDeactivatedGroup ? groupManager.getGroup(selectedDeactivatedGroup.name) : groupManager.getGroup('deactivated-users') ?: groupManager.createGroup('deactivated-users')

// Get all users from the selected user group
def users = userAccessor.getMembers(userGroup).asList()

// For the users found in the selected group, sort the users by last login date
Map<ConfluenceUser, Date> userWithLoginDate =
users.collectEntries {
[(it): (loginManager.getLoginInfo(it)?.lastSuccessfulLoginDate)]
}.sort { it -> it.value }

// Get all user keys
def sortedUsers = userWithLoginDate*.key

// Deactivate user accounts and add them to the deactivated group if the user list size is more than the allowed amount
if (sortedUsers.size() > maximumUsers) {
def deactivated = sortedUsers.subList(0, sortedUsers.size() - maximumUsers)
deactivated.each { user ->
groupManager.removeMembership(userGroup, user)
groupManager.addMembership(deactivatedGroup, user)
userAccessor.deactivateUser(user)
}
sendEmailAboutRemovedUsers(deactivated*.name)
}

def sendEmailAboutRemovedUsers(List<String> userIds) {
def confluenceMailServerManager = ComponentLocator.getComponent(ConfluenceMailServerManager)
SMTPMailServer mailServer = confluenceMailServerManager.defaultSMTPMailServer
Email email = new Email("john.doe@company.net")
email.setFromName("TEST Plugin")
email.setSubject("Deactivated Users")
email.setBody("Users deactivated: " + userIds.join(", "))
email.setMimeType("text/html")
mailServer.send(email)
}

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events