Emailing new ComponentWatcher via ScriptRunner

Raynard Rhodes
Contributor
June 4, 2020

I'm attempting to email the users of a component when it is added to the Component/s field. I'm using ComponetWatcher and ScriptRunner.

I can send an email, I just need to ensure that I'm not sending an email to those who are already in the Component/s field.

 

import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.mail.Email
import com.atlassian.mail.server.SMTPMailServer
import org.apache.log4j.Level
import com.atlassian.jira.issue.changehistory.ChangeHistoryItem
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin




log.setLevel(Level.INFO)

Issue issue = event.issue


ApplicationUser user = event.user
def baseUrl = com.atlassian.jira.component.ComponentAccessor.getApplicationProperties().getString("jira.baseurl")

def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find { it.field == "Component" }
log.info(change)
log.info(user)
log.info(issue)
def cW = ComponentAccessor.getCustomFieldManager().getCustomFieldObjects(issue).find{it.name == "ComponentWatcher"}




def changeOldString = change.oldstring
def changeNewString = change.newstring
def changeCurrentString = issue.components*.getName()

log.info("Old Value: "+changeOldString)
log.info("New value: "+changeNewString)
log.info("Current Value: "+changeCurrentString)




def cwValue = issue.getCustomFieldValue(cW)

log.info(cwValue)





if (change){
if(changeNewString!=null && changeOldString==null){

for(cwUsers in cwValue){
log.info("Users: "+cwUsers.getEmailAddress())
def emailBody = """
${user.displayName} updated ${issue.key}:
-------------------------------------------

Component/s field has been updated to \"${issue.components*.getName().toArray().join(", ")}\"

-------------------------------------------
Key: ${issue.key}
URL: ${baseUrl}/browse/${issue.key}
Project: ${issue.getProjectObject().name}
Issue Type: ${issue.getIssueType().name}
Reporter: ${issue.reporter.displayName}
Assignee: ${ -> if(!issue.assignee){'Unassigned'}else{issue.getAssignee().name} }

"""
sendEmail("raynard.rhodes@nih.gov", "(${issue.key}) ${issue.summary}", emailBody)
}
}
}
def sendEmail(String emailAddr, String subject, String body) {
SMTPMailServer mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()
if (mailServer) {
Email email = new Email(emailAddr)
email.setSubject(subject)
email.setBody(body)
mailServer.send(email)
log.info("Mail sent")

} else {
log.info("Please make sure that a valid mailServer is configured")
}

}

 

 

Here is where I'm trying to figure out the logic in order to accomplish this

if (change){
if(changeNewString!=null && changeOldString==null){

for(cwUsers in cwValue){
log.info("Users: "+cwUsers.getEmailAddress())

0 answers

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events