Hi,
I am trying to create a sheduled job to send emails to assignees of certain tickets.
Therefore I wrote the following code. I used a jql Search in order to get the issues and retrieve their informations. With this informations I will send emails to the assignees of the issues. But I receive error messages, that the variable [issue] is undeclared.
I dont know how to solve this, i thought I already defined issues. Any suggestions are very welcome!
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchException
import com.atlassian.jira.web.bean.PagerFilter
import org.apache.log4j.Level
// Set log level to INFO
log.setLevel(Level.INFO)
// The JQL query you want to search with
final jqlSearch = "issuetype = 'SUD Modul' and 'Sommersemester aktiv?' = aktiv"
// Some components
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def searchService = ComponentAccessor.getComponentOfType(SearchService)
// Parse the query
def parseResult = searchService.parseQuery(user, jqlSearch)
if (!parseResult.valid) {
log.error('Invalid query')
return null
}
try {
// Perform the query to get the issues
def results = searchService.search(user, parseResult.query, PagerFilter.unlimitedFilter)
def issues = results.results
issues.each {
log.info(it.key)
}
issues*.key
} catch (SearchException e) {
e.printStackTrace()
null
}
import com.atlassian.jira.mail.Email
import com.atlassian.jira.mail.settings.MailSettings
import com.atlassian.mail.MailException
import com.atlassian.mail.server.SMTPMailServer
import com.atlassian.plugin.util.ContextClassLoaderSwitchingUtil
import org.apache.log4j.Level
import org.apache.log4j.Logger
String sendEmail(String emailAddr, String subject, String body) {
def logger = Logger.getLogger(getClass())
logger.setLevel(Level.DEBUG)
// Stop emails being sent if the outgoing mail server gets disabled (useful if you start a script sending emails and need to stop it)
def mailSettings = ComponentAccessor.getComponent(MailSettings)
if (mailSettings?.send()?.disabled) {
return 'Your outgoing mail server has been disabled'
}
def mailServer = ComponentAccessor.mailServerManager.defaultSMTPMailServer
if (!mailServer) {
logger.debug('Your mail server Object is Null, make sure to set the SMTP Mail Server Settings Correctly on your Server')
return 'Failed to Send Mail. No SMTP Mail Server Defined'
}
def email = new Email(emailAddr)
email.setMimeType('text/html')
email.setSubject(subject)
email.setBody(body)
try {
// This is needed to avoid the exception about IMAPProvider
ContextClassLoaderSwitchingUtil.runInContext(SMTPMailServer.classLoader) {
mailServer.send(email)
}
logger.debug('Mail sent')
'Success'
} catch (MailException e) {
logger.debug("Send mail failed with error: ${e.message}")
'Failed to Send Mail, Check Logs for error'
}
}
def emailAddr = issue.getAssignee().getEmailAdress
def emailSubject = "Verantwortlichkeit im Modul ${issue.modulname}"
def emailBody = '<Your body here>'
sendEmail(emailAddr, emailSubject, emailBody)
I am not a Groovy expert so sorry but I can't help there. However, if you want to try this without writing code, we support building emails exactly like this in our app, Notification Assistant for Jira.
Thanks @Boris Berenberg - Atlas Authority for your support, but I want to understand how to write the codes :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.