Help with using Scriptrunner to send email Edited

Hi 

After reading this question, I have created the following test script as an Escalation Service using Scriptrunner. But I am not receiving any emails when I run the service. Can anyone tell me where I'm going wrong? (I use my real email address/JQL in the live script of course).

I am trying to set up something so that if an issue is in a certain status and not updated for a certain amount of time, the assignee receives an email prompting them to update/transition the issue. Any help would be extremely welcome.

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.mail.server.MailServerManager
import com.atlassian.mail.server.SMTPMailServer
import com.atlassian.mail.Email

def issueManager = ComponentAccessor.getIssueManager()
def searchService = ComponentAccessor.getComponent(SearchService)
def jqlSearch = '"project = TTT AND issuetype in ("Test Change", "Test2 Change") AND status = Closed AND updated <= -1m"'
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// eventually this array will contain all the issues that returned from the JQL
def issues = []

SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch)
if (parseResult.isValid()) {
def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
issues = searchResult.issues.collect { issueManager.getIssueObject(it.id) }
}

// Create an email
def assignee = userUtil.getUserByName(issue.assignee.name);
def assigneeEmail = assignee.getEmailAddress(); 
def assigneeFirstName = assignee.displayName; 
def sendEmail(String emailAddr, String subject, String body) {
SMTPMailServer mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()
if (mailServer) {
Email email = new Email(assigneeEmail)
email.setSubject("Test")
email.setBody("This is a test")
mailServer.send(email)
log.debug("Mail sent")
} else {
log.warn("Please make sure that a valid mailServer is configured")
}
}

 Best wishes

2 answers

1 accepted

Hi everyone

Thanks for all of your help. I am just writing to update you that in the end, we are using the following script to send an email, just in case anyone else wants to use it:

 

//the first five lines are for logging purposes and are not related to the code that fires the email
import org.apache.log4j.Logger
import org.apache.log4j.Level
def log = Logger.getLogger("com.onresolve.jira.groovy")
log.setLevel(Level.INFO)
log.info "AutoEmail error log"

import com.atlassian.mail.Email;
import com.atlassian.mail.server.MailServerManager;
import com.atlassian.mail.server.SMTPMailServer;
import com.atlassian.jira.component.ComponentAccessor;

def subject = "Insert text here for ${issue.key}"
def body = "Hi ${issue.assignee.displayName} \n\nNow that XXXXX has been released, please complete XXXXX for the following issue: https://XXXXXXX/${issue.key} - ${issue.summary}. \n\n"
def emailAddr = issue.getAssignee().getEmailAddress()

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);
} else {
// Problem getting the mail server from JIRA configuration, log this error
}
}

sendEmail (emailAddr, subject, body)
0 vote
Alexey Matveev Community Champion Jan 04, 2018

Did you see the logs? Did you find  your debug message "Mail Sent" in the logs? If you did not see the message did you enable Debug logging level for your script?

Hi Alex

Thanks so much for your response. I can see this in the results:

Number of issues returned by query: 5
Issue TTT-9 updated (no action defined)
Issue TTT-8 updated (no action defined)
Issue TTT-7 updated (no action defined)
Issue TTT-3 updated (no action defined)
Issue TTT-1 updated (no action defined)

Does this mean it's working but that I need to define it so that for each issue returned it sends the email? Do you know how I'd do that?

I have also slightly modified the script so that the JQL query is correctly phrased. And used "import com.atlassian.mail.Email" at the top. I have edited my OP to reflect this.

When I click on the Logs tab there is no information. I have had a look in the System tab under admin, but much of the debugging/logging options are disabled. Do you know which ones I'd need to turn on to activate the appropriate logging?

Best wishes

Adam

Alexey Matveev Community Champion Jan 04, 2018

The easiest way is to change your log.debug to log.error.

Also put log.error more in your code for example like this

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.mail.server.SMTPMailServer

log.error("script started")
def issueManager = ComponentAccessor.getIssueManager()
def searchService = ComponentAccessor.getComponent(SearchService)
def jqlSearch = "project = TTT AND issuetype in ("Test Change", "Test2 Change") AND status = Closed AND updated <= -1m"
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// eventually this array will contain all the issues that returned from the JQL
def issues = []
 
SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch)
log.error("jql parsed")
if (parseResult.isValid()) {
 def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
 issues = searchResult.issues.collect { issueManager.getIssueObject(it.id) }
}
log.error("creating email")

// Create an email
def assignee = userUtil.getUserByName(issue.assignee.name);
def assigneeEmail = assignee.getEmailAddress();
def assigneeFirstName = assignee.displayName;
def sendEmail(String emailAddr, String subject, String body) {
 SMTPMailServer mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()
 if (mailServer) {
 Email email = new Email(assigneeEmail)
 email.setSubject("Test")
 email.setBody("This is a test")
 mailServer.send(email)
 log.error("Mail sent")
 } else {
 log.error("Please make sure that a valid mailServer is configured")
 }
}

Then have a look at the logs again. There must be something.

Thanks Alex, I will do and come back to you. I will have to liaise with another team to get the logs probably.

From initial inspection then, does this code look ok?

I am not a compiler but it looks ok.;)

Hey Adam, 

Something does not look right, but of course as Alexey said, logs will tell you the truth. 

I would have something like 

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.mail.server.SMTPMailServer
import com.atlassian.mail.Email

def issueManager = ComponentAccessor.getIssueManager()
def searchService = ComponentAccessor.getComponent(SearchService)
def jqlSearch = """project = TTT AND issuetype in ("Test Change", "Test2 Change") AND status = Closed AND updated <= -1m"""
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// eventually this array will contain all the issues that returned from the JQL
def issues = []

SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch)
if (parseResult.isValid()) {
def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
issues = searchResult.issues.collect { issueManager.getIssueObject(it.id) }
}

// for each issue returned from the above JQL send an email to the assignee
issues?.each { issue ->
issue.assignee ? sendEmail(issue.assignee.emailAddress, "Subject", "Hey ${issue.assignee.displayName} this is an email for you")
: log.debug("Email not sent because assignee is null for issue ${issue.key}")
}

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.debug("Mail sent")
} else {
log.warn("Please make sure that a valid mailServer is configured")
}
}
Alexey Matveev Community Champion Jan 04, 2018

That is right. I guess sendEmail procedure was never called. Adam, you should try the code of Thanos. Just add there log.error in some parts of the code so that if the code does not work you could at least know where your script stopped. Just in case. So that you do not do things twice

Hi Thanos and Alexey

Thanks so much for your help again.

I have just run Thanos's code and it looks pretty good but just seeing one issue, which I will attach:Screenshot 2018-01-04 at 2.30.19 PM.pngIs there anything I'm missing here?

Best wishes

Adam

Adam, 

Those "errors" you see in the UI, are because of the Static Type Checking in a dynamic language (groovy). So probably are false alarms. 

Again the logs will tell you the truth :) 

PS. You can avoid them be declaring the types. For example 

line 13 : 

def issues = [] as List <MutableIssue>

and  in the beginning 

import com.atlassian.jira.issue.MutableIssue

One more thing, I will completely agree with Alexey to add log.debug messages and make sure that every debug message has the expected value. 

To be honest I would avoid the log.warn and log.error since will create unnecessary noice in your logs. I would say log.debug or log.info will be just fine, and then you can remove them. I can see the reason that Alexey suggested warn and errors, has to do with the configured debug level. In any case this link will be helpful -Set logging to help debug your scripts

Thanks so much Alexey and Thanos. I have made that tweak and that error message is no longer appearing.

I am still not getting any emails after running the Escalation Service, but I will put in the logging that you advised and try to see what's happening.

Again, you have both been brilliant help. As soon as I get it working, I will post the working script.

Best wishes

Adam

Hi guys

I have been trying to get logging set up on our system that can debug this, but as I am a beginner I am finding it quite hard. I have followed this article, and have added the 5 lines of script it says (changing "com.acme.CreateSubtask" to "com.onresolve.jira.groovy", is this right?).

I have also added com.onresolve.jira.groovy to the packages in the Logging and Profiling section, with logging set to ERROR level.

But after running the script we can't seem to find anything in the logs relating to this script. 

Is there anywhere I am going wrong?

Best wishes

Adam

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published May 30, 2018 in Marketplace Apps

Three tips for boosting your board's efficiency with Story Maps

Trello is one of the most effective tools for driving your sprints. It's customizable for every Agile team and product owners and Scrum masters (SM) love it. However, Agile teams often struggle with:...

823 views 2 9
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you