How to find email address from project role member in scriptrunner

Jackson Loong April 5, 2017

I have a scriptrunner custom email post function.  I'd like to setcc based on the email address from Project Role.    I searched the old Q&A area and it seems there is no direct ay to obtain the email addresses directly.  One solution seems to scan through all users and check if match project role.   But I couldn't get that to work either.   Is there any direct function that I can use to obatin the list of email address based on the role?

1 answer

0 votes
adammarkham
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.
April 6, 2017

You will be able to do this with the following code in the "Condition and Configuration" section of the send a custom email script.

def projectRoleService = ComponentAccessor.getComponent(ProjectRoleService)

def roleName = "jira-administrators"

def projectRole = projectRoleService.getProjectRoleByName(roleName, new SimpleErrorCollection("role error", [] as Set))

def actors = projectRoleService.getProjectRoleActors(projectRole, issue.projectObject, new SimpleErrorCollection("actors error", [] as Set))

def ccAddresses = actors?.getApplicationUsers()*.emailAddress.join(",")

if (ccAddresses) { mail.setCc(ccAddresses)
} true // so the script fires as the last statement is evaluated as a condition 

 

You should change roleName to whatever project role you want to use is.

Let us know how that goes.

Jackson Loong April 7, 2017

Thanks for the sample codes.  

I have been trying to resolve some static errors without much success.  Probably because of my lack of knowledge about the java libraries & programming.

Initially, I got an error about SimpleErrorCollection not found.  So I added 

import com.atlassian.jira.util.SimpleErrorCollection
import com.atlassian.jira.bc.projectroles.DefaultProjectRoleService

 


I now have static error complaining not able to find matching method  com.atlassian.jira.component.ComponentAccessor#getComponent

I had already added 
import com.atlassian.jira.component.ComponentAccessor

 


Woukd appreciate additional pointers to help me resolve these static errors

Guido April 22, 2018

I have made this work for my use case:

I need to monitor projects in specific categories for new bugs raised in certain priorities and severities.

Below is what I have added to the field "Condition and Configuration" in my custom listener: 

//DEFINE CUSTOM EMAIL RECIPIENTS
import com.atlassian.jira.bc.projectroles.ProjectRoleService
import com.atlassian.jira.util.SimpleErrorCollection
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.projectroles.DefaultProjectRoleService

def projectRoleService = ComponentAccessor.getComponent(ProjectRoleService)

//Get Producer role email addresses
def roleName1 = "Producers"
def projectRole1 = projectRoleService.getProjectRoleByName(roleName1, new SimpleErrorCollection())
def actors1 = projectRoleService.getProjectRoleActors(projectRole1, issue.projectObject, new SimpleErrorCollection())
def toAddresses1 = actors1?.getApplicationUsers()*.emailAddress.join(",")

//Get Project Manager role email addresses
def roleName2 = "Project Managers"
def projectRole2 = projectRoleService.getProjectRoleByName(roleName2, new SimpleErrorCollection())
def actors2 = projectRoleService.getProjectRoleActors(projectRole2, issue.projectObject, new SimpleErrorCollection())
def toAddresses2 = actors2?.getApplicationUsers()*.emailAddress.join(",")

//Get Account Manager role email addresses
def roleName3 = "Account Managers"
def projectRole3 = projectRoleService.getProjectRoleByName(roleName3, new SimpleErrorCollection())
def actors3 = projectRoleService.getProjectRoleActors(projectRole3, issue.projectObject, new SimpleErrorCollection())
def toAddresses3 = actors3?.getApplicationUsers()*.emailAddress.join(",")

//Put together the recipient string
def recipients = ''
if (toAddresses1){
recipients = toAddresses1 //Add Producer email addresses to string
}
if (toAddresses2){
if (recipients){
recipients = recipients+','+toAddresses2 //Append PM email addresses to string
} else {
recipients = toAddresses2
}
}
if (toAddresses3){
if (recipients){
recipients = recipients+','+toAddresses3 //Append AM email addresses to string
} else {
recipients = toAddresses3
}
}
if (recipients){
mail.setTo(recipients)
}

true //Making sure the rest of the script fires, since the last statement is evaluated as a condition


//ACTUAL CONDITION TESTED HERE:
//Project category is "Projects" OR "Retainers"
(issue.projectObject.projectCategoryObject?.name == 'Projects' ||
issue.projectObject.projectCategoryObject?.name == 'Retainers') &&
//Issue Type is "Bug - Live"
issue.issueType?.name == 'Bug - Live' &&
//Priority is "Urgent" OR "High"
(originalIssue.priority?.name == 'Urgent' ||
originalIssue.priority?.name == 'High') &&
//Severity is "Critical" OR "Blocker" OR "Major"
(cfValues['Severity']?.value == 'Blocker' ||
cfValues['Severity']?.value == 'Critical' ||
cfValues['Severity']?.value == 'Major')

Important with this approach is to check the box "Disable validation" because I leave the "To addresses" blank to have it filled by my script instead.

This listener listens to the event "Issue Created". So every time a live bug with certain parameters in certain projects is created, the respective Producer, PM and AM roles are notified.

Guido April 23, 2018

Not sure what happened to my previous response. It went missing when I tried to edit it... Here another attempt to reply.

I have taken the code example above one step further and here is my solution:

//DEFINE CUSTOM EMAIL RECIPIENTS
import com.atlassian.jira.bc.projectroles.ProjectRoleService
import com.atlassian.jira.util.SimpleErrorCollection
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.projectroles.DefaultProjectRoleService

def projectRoleService = ComponentAccessor.getComponent(ProjectRoleService)

//Get Producer role email addresses
def roleName1 = "Producers"
def projectRole1 = projectRoleService.getProjectRoleByName(roleName1, new SimpleErrorCollection())
def actors1 = projectRoleService.getProjectRoleActors(projectRole1, issue.projectObject, new SimpleErrorCollection())
def toAddresses1 = actors1?.getApplicationUsers()*.emailAddress.join(",")

//Get Project Manager role email addresses
def roleName2 = "Project Managers"
def projectRole2 = projectRoleService.getProjectRoleByName(roleName2, new SimpleErrorCollection())
def actors2 = projectRoleService.getProjectRoleActors(projectRole2, issue.projectObject, new SimpleErrorCollection())
def toAddresses2 = actors2?.getApplicationUsers()*.emailAddress.join(",")

//Get Account Manager role email addresses
def roleName3 = "Account Managers"
def projectRole3 = projectRoleService.getProjectRoleByName(roleName3, new SimpleErrorCollection())
def actors3 = projectRoleService.getProjectRoleActors(projectRole3, issue.projectObject, new SimpleErrorCollection())
def toAddresses3 = actors3?.getApplicationUsers()*.emailAddress.join(",")

//Put together the recipient string
def recipients = ''
if (toAddresses1){
recipients = toAddresses1 //Add Producer email addresses to string
}
if (toAddresses2){
if (recipients){
recipients = recipients+','+toAddresses2 //Append PM email addresses to string
} else {
recipients = toAddresses2
}
}
if (toAddresses3){
if (recipients){
recipients = recipients+','+toAddresses3 //Append AM email addresses to string
} else {
recipients = toAddresses3
}
}
if (recipients){
mail.setTo(recipients)
}

true //Making sure the rest of the script fires, since the last statement is evaluated as a condition


//ACTUAL CONDITION TESTED HERE:
//Project category is "Projects" OR "Retainers"
(issue.projectObject.projectCategoryObject?.name == 'Projects' ||
issue.projectObject.projectCategoryObject?.name == 'Retainers') &&
//Issue Type is "Bug - Live"
issue.issueType?.name == 'Bug - Live' &&
//Priority is "Urgent" OR "High"
(originalIssue.priority?.name == 'Urgent' ||
originalIssue.priority?.name == 'High') &&
//Severity is "Critical" OR "Blocker" OR "Major"
(cfValues['Severity']?.value == 'Blocker' ||
cfValues['Severity']?.value == 'Critical' ||
cfValues['Severity']?.value == 'Major')

This is the code in a custom listener in the field "Condition and Configuration" (listens to the event "Issue Created").

I am listening to all new issue created in projects of a certain category, of type "Bug - Live" and of a certain priority and severity level.

If this condition is true I am putting together a string of email addresses from the project roles Producer, Project Manager and Account Manager.

Important is to leave the "To addresses" field blank and check the box on "Disable validation" (otherwise you wouldn't be able to save the listener).

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events