Snippets

Adaptavist JIRA - Email users with particular role

Created by Jamie Echlin
package examples.answers

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.bc.projectroles.ProjectRoleService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.util.SimpleErrorCollection
import com.atlassian.mail.Email
import com.atlassian.mail.queue.SingleMailQueueItem

def projectRoleService = ComponentAccessor.getComponent(ProjectRoleService)
def projectManager = ComponentAccessor.getProjectManager()

def errorCollection = new SimpleErrorCollection()
def projectRole = projectRoleService.getProjectRoleByName("Developers", errorCollection)
assert ! errorCollection.hasAnyErrors() // failed to get role

def actors = projectRoleService.getProjectRoleActors(projectRole, projectManager.getProjectObjByKey("JRA"), errorCollection)
assert ! errorCollection.hasAnyErrors() // failed to get actors

def users = actors.getRoleActors()*.users.flatten() as List<User>

def email = new Email("", "", users*.emailAddress.join(",")) // bcc everyone
email.setSubject("some subject")
email.setBody("the body...")
// email.setMimeType("text/html") // for html

SingleMailQueueItem item = new SingleMailQueueItem(email);
ComponentAccessor.getMailQueue().addItem(item);

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.