Jira Groovy Script help for sending email

Vijay Sridhar March 21, 2016

 Hi,

Require to send an email to the reporter , assigner & groups once an issue meets certain criteria specified in a JQL query , ( Tried using Filter but not able to send mails to reporter , assigne of each issues ) , there is no event involved , so no transition is changed, so we were unable to use notification scheme for this , 

can anyone help in providing the Groovy Script for sending emails based on JQL.

also tried  Script from https://answers.atlassian.com/questions/8619332/can-you-execute-a-search-filter-using-script-runner but getting errors while executing script 

JIRA Version = 7.0.9

ScriptRunner Version = 4.2.0.7

Regards

Vijay

4 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

3 votes
Answer accepted
Thanos Batagiannis [Adaptavist]
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.
March 21, 2016

Ok so you want either an escalation service, that will trigger an action (send email) or a service. Below is a sample script that you can use as a service. The first part will collect all the issues that returned from JQL in an array. The second part is a simple sendEmail function. You can modify the script to iterate through the issues and /*forEach do something*/

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.web.bean.PagerFilter
 
def issueManager = ComponentAccessor.getIssueManager()
def searchService = ComponentAccessor.getComponent(SearchService)
def jqlSearch = "project = JIRA"
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 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")
    }
}
Vijay Sridhar March 21, 2016

Hi, 

This Script is working 

it would be very helpful if Reporter, assinge of the tickets can be added while sending the email .

Thanos Batagiannis [Adaptavist]
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.
March 21, 2016

You can get the reporter or the assignee (ApplicationUser) of the issue with 

issue.getAssignee()

and his email address 

issue.getAssignee().getEmailAddress()

same with the reporter or any other custom field which 'holds' users. From the moment you have the array with the issues and you iterate over them you can actually get any information an issue has (reporter, assignee, custom fields, ...).

Vijay Sridhar March 21, 2016

Hi,

Thanks for the script ,it working as expected 

tried adding CC in email , but variable is unable to set for CC &BCC in email

def reporter = userUtil.getUserByName(issue.reporter.name); 
def reporterEmail = reporter.getEmailAddress(); 
def reporterFirstName = reporter.displayName; 
def assignee = userUtil.getUserByName(issue.assignee.name);
def assigneeEmail = assignee.getEmailAddress(); 
def assigneeFirstName = assignee.displayName; 

Email email = new Email(reporterEmail) ; is working 

Email email = new Email(reporterEmail to,assigneeEmail cc) ; is not working 

email.setBody(content)

mailServer.send(email)


Followed the https://jamieechlin.atlassian.net/wiki/display/GRV/Post+Functions 

 

 

Thanos Batagiannis [Adaptavist]
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.
March 21, 2016

Vijay,

Here is the updated documentation for SR. The right way to use the email is :

// Email constructor - Email(String to, String cc, String bcc)
Email email = new Email(reporterEmail, assigneeEmail, "aBcc@exampl.com")
//I want to add more than one cc
email.setCc("aUser@example.com")
Vijay Sridhar March 21, 2016

Hi @Thanos Batagiannis [Adaptavist],

Thanks for the support

email.setCc("aUser@example.com") is working perfectly ,

But  Email email = new Email(reporterEmail, assigneeEmail) is throwing error , Kindly find the attachment 

Error.png

Thanos Batagiannis [Adaptavist]
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.
March 21, 2016

sorry Vijay, I forgot to add the third argument. I updated the code above. the email constructor gets either one argument (to) or three (to, cc, bcc).

Vijay Sridhar March 21, 2016

Thanos Batagiannis [Adaptavist]After adding the 3rd Argument  am able send mail successfully ,

Thanks 

 

rao4533 September 23, 2016

 

Hi , do you guys have solution to send the list of issues in one email.

 

Like Shashank Shekhar Singh likes this
0 votes
Shrikant Maheshwari January 15, 2018

@Thanos Batagiannis [Adaptavist] Where to use this service? I mean we need to confiure few things i checked at the time of adding a service its asking for the Class name, what should i give.

0 votes
Vijay Sridhar March 21, 2016

Hi,

Below is my JQL ,  

For some of the Issues Update itself is not happening for more than some period .

issuetype = Incident AND ((Impact = Medium AND Urgency = HIGH) or(Impact = High and Urgency = Medium)) AND status not in (Closed, resolved) and updated < "-2h" and Email= No
issuetype = Incident AND ((Impact = Medium AND Urgency = HIGH) or(Impact = High and Urgency = Medium)) AND status not in (Closed, resolved) and updated < "-4h" and Email= No
issuetype = Incident AND ((Impact = Medium AND Urgency = HIGH) or(Impact = High and Urgency = Medium)) AND status not in (Closed, resolved) and updated < "-6h" and Email= No
issuetype = Incident AND ((Impact = Medium AND Urgency = HIGH) or(Impact = High and Urgency = Medium)) AND status not in (Closed, resolved) and updated < "-7h 35m" and Email= No

0 votes
Thanos Batagiannis [Adaptavist]
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.
March 21, 2016

Hi Vijay,

How your JQL looks like ? I understand that because you do not transition the issue you can't use a post function. Did you try to use an event listener ? (that listens on issue update events)    

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events