Hello
I am completely new to JIRA administration, we are using script runner to schedule different jobs, and one of the requirement is, based on the certain filter condition results, a job needs to be scheduled on certain time frame to send email with the customized body, JIRA ticket number etc,.
What i have tested so far is just simple email sending code which is as follows..
import com.atlassian.mail.Email
import com.atlassian.mail.server.SMTPMailServer
import com.atlassian.jira.component.ComponentAccessor
def emailSubject = "JIRA CHECK"
def emailBody = "Test CHECK"
def emailAddress = "<emaild id>"
SMTPMailServer mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()
if (mailServer) {
Email email = new Email(emailAddress)
email.setSubject(emailSubject)
email.setBody(emailBody)
mailServer.send(email)
} else {}
Hello Helmy
Sorry about the late reply, i would try your option and let you know whether it helps.
I already have an flter_id eg 123, i want to see whether 123 returns me any result, if yes, then with the content of the Customized email message, i want to send to the respective recipients.
Thank You.
Hi Nagppan,
Thank you for using ScriptRunner.
Can you clarify what you meant by "Filter Condition"? Providing examples would help too!
As for the scheduled script, you could use Script Jobs by ScriptRunner. It accepts cron expression to define the interval a job is run at.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Helmy
For example
issuetype = Issue AND status in ("In Progress", Open) and created < -10w
I want to scan the results from the above filer (have saved the filter too and have the filter id with me), and if the filter returns any result, then would like to sent Email to certain email id.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Nagppan,
You can execute a JQL query in your code and then do a simple checking:
if (search.results) {
// send email
}
I hope this helps!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Helmy
Thanks for the link, it really helped and i was able to send the custom email.
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.
@Vladislav sorry for the late reply, i wasnt working few weeks now. Please find the script below.
import groovy.sql.Sql
import java.sql.Driver
import com.atlassian.mail.Email
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.web.bean.PagerFilter
import org.ofbiz.core.entity.ConnectionFactory
import org.ofbiz.core.entity.DelegatorInterface
import java.sql.Connection
import java.util.Date
import java.time.*
import java.lang.String
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.text.ParseException;
def delegator = (DelegatorInterface) ComponentAccessor.getComponent(DelegatorInterface)
String helperName = delegator.getGroupHelperName("default")
def issueManager = ComponentAccessor.getIssueManager()
def searchService = ComponentAccessor.getComponent(SearchService)
def jqlSearch = "issuetype = Issue AND status in ('In Progress', 'Closed') AND (created < -7w AND priority = Medium OR created < -2w AND priority = High OR created < -0.5w AND priority = Highest) ORDER BY priority, status DESC"
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueKeyIns
def SummaryIns
def prioIns
def createdIns
def emailIns
def idIns
def updateCount
def assigneeEmail
def assigneeId
// eventually this array will contain all the issues that returned from the JQL
def issues = []
// hashmap to store username and associated issue(s)
def map = [:].withDefault {
[]
} //The default value will be created every time a non-existing key is accessed.
// Need this to be able to dynamically expand/update map
SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch)
if (parseResult.isValid()) {
def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
issues = searchResult.getResults()
//log.warn(issues)
}
// breaking out the issues per assignee. Will use later when we email the assignees
issues.each {
def assignee = it.assigneeId
map[assignee].add(it)
}
// following section creates separate emails for each assignee from the map, including all related issues in the same email.
map.each { key, value ->
def assignee = key
def theirIssues = value as List
String cc = ""
String bcc = ""
theirIssues.each {
assigneeEmail = ("<to email id here>")
idIns = it.id
issueKeyIns = it.key
SummaryIns = it.getSummary()
prioIns = it.getPriorityObject().getId()
createdIns = it.getCreated()
emailIns = it.getAssignee().getEmailAddress()
assigneeId = it.getAssignee().getName()
TimeZone.getTimeZone('UTC')
def body2 = "Email body"
sendEmail(assigneeEmail, cc, bcc, SummaryIns, body2,issueKeyIns)
}
}
// Create an email method
def sendEmail(String emailAddr, String cc, String bcc, String subjectIns, String body,String issueKeyIns )
{
def mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()
if (mailServer) {
def email = new Email(emailAddr)
email.setMimeType("text/html")
email.setFrom ("<from email id here>")
email.setSubject(subjectIns)
email.setBody(body)
email.setCc(cc)
email.setBcc(bcc)
mailServer.send(email)
log.warn("Email sent")
} else {
log.warn("Please make sure that a valid mailServer is configured")
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you very much! I really appreciate it!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Nagappan,
We don't yet support customized bodies, but we support everything else you said out of the box with Notification Assistant for Jira (a Staff Pick). And we have customized notification templates coming in the next month!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.