Scriptrunner groovy script not working for email
Asked by Syed Ahmed:

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](/t5/user/viewprofilepage/user-id/1435650)(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)

<br />

// 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)  
}


Answers:
author: Peter-Dave Sheehan
body: 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)
}
```


Tags: confluence-server

groovy

scriptrunner

server