Script Runner Send Custom Email with Issue Activity History

Hello,

I am attempting to use Script Runner to send a Custom Email that includes part of the Issues History.

The part I need to send in the email is the User who made the modification to the Issue. (See Sample image)

I am unable to get the formatting of the Email Template set properly. I keep getting several errors about the Import line and binding when I remove the Import line.

Here is the setup -

JIRA Version 6.4.12
Adaptavist ScriptRunner for JIRA Standard Edition Version: 3.1.4
Script Listeners
- Send Custom Email
-- Email Template

Error

Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): startup failed: GStringTemplateScript68.groovy: 3: Unknown type: IMPORT at line: 3 column: 1. File: GStringTemplateScript68.groovy @ line 3, column 1. import com.atlassian.jira.ComponentManager ^ 1 error

Here is my Email Template -

<%
import com.atlassian.jira.ComponentManager
def componentManager = ComponentManager.getInstance()
def changeHistoryManager = componentManager.getChangeHistoryManager()
%>

<b>There has been an update to an Epic in the Claims project<br><br>
<b>Issue:</b> $issue<br><br>
<b>Summary:</b> $issue.summary<br><br>


<% out << changeHistoryManager.getChangeHistoriesForUser($issue,user)%>

Please help me set the Email Template.

Issue-History-Information.png

3 answers

1 accepted

Accepted Answer
2 votes

Hi Dianne,

Just got some time to write a custom listener for your case. So, create a custom listener, add the project you want to 'listen', for the events field choose Issue Updated and for the inline script:

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.mail.Email
import com.atlassian.mail.server.SMTPMailServer

// get the issue that triggers the event (got updated)
Issue issue = event.issue

// get the user that just updated the issue (in JIRA-7.* will be ApplicationUser)
User user = event.user

// If the user updated an epic send an email to the user who made the change
if (issue.issueTypeObject.name == "Epic")
    sendEmail(user.emailAddress, "JIRA msg", "You just edited an Epic and you shouldn't")

// check if the field that got updated is the Rank
def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "Rank"}

// if the issue is type of Epic and the updated field is the Rank send an email to admin@example.com
if (issue.issueTypeObject.name == "Epic" &amp;&amp; change) {
    def emailBody = "User ${user.name} just edited epic ${issue.key} and updated the Rank - ${change.newstring}"
    sendEmail("admin@example.com", "Rank updated", 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.debug("Mail sent")
    } else {
        log.warn("Please make sure that a valid mailServer is configured")
    }
}

Also wrote a couple of comments for what its part of the script does. Hope this will help you.

Kind regards

Thanos

Thank you. Worked perfectly

Hi DIanne,

replace your out with

&lt;% out &lt;&lt; com.atlassian.jira.component.ComponentAccessor.getChangeHistoryManager().getChangeHistoriesForUser(issue,user)%&gt;

for the email templates you need to call the full class name. 

Kind regards

I replaced the Out with your version. However, I am still receiving the error about the part about the Import -

Error

Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): startup failed: GStringTemplateScript72.groovy: 3: Unknown type: IMPORT at line: 3 column: 1. File: GStringTemplateScript72.groovy @ line 3, column 1. import com.atlassian.jira.ComponentManager ^ 1 error

You should also remove the lines:

<%
import com.atlassian.jira.ComponentManager
def componentManager = ComponentManager.getInstance()
def changeHistoryManager = componentManager.getChangeHistoryManager()
%>

I also updated the answer above, I forgot to copy the whole class.

I changed the out part and removed the Import.

The error that I receive now is -

 

Error

No such property: $issue for class: groovy.lang.Binding

groovy.lang.MissingPropertyException: No such property: $issue for class: groovy.lang.Binding

try to remove the $ in front of the issue

Error

No such property: user for class: groovy.lang.Binding

groovy.lang.MissingPropertyException: No such property: user for class: groovy.lang.Binding

Ok let's hope that now I don't miss anything else. You can get the user via the event (is the user who triggered the event), so instead of user add event.user.

 

Sorry,
I am still struggling with the basic construction with how to use these classes to make a complete logical framework in Script Listener.


Is the event item you are referring to - com.atlassian.jira.event.user

Is there some basic reference document on how one would create a working framework?

I don't have any reference material or guidelines to build the basic framework.

The only thing I have are previously created frameworks within Atlassian Answers. From there I try to guess what items should be updated or removed.

It is extremely difficult to learn how to correctly use these classes, since I am unable to locate any materials that can give a basic reference as what and how they should be used.

I have been referencing the following - https://docs.atlassian.com/jira/6.2.1/com/atlassian/jira/event/user/UserEvent.html

Hi Dianne,

If you tell me what you are want to do, I can come with a script and comments on the steps. I suppose that way you can start understanding how SR works.

Hi Thanos,

I am trying to send a custom email with the person who changed (updated) an issue.

The email needs to send the users name who made any modification to an issue.

We have several epics that should never be modified by anybody other than the Scrum Master, however some of the team members tend to modify the epic and the Scrum Master needs to be notified who made the modification.

The Scrum Master only wants to be notified when the Epic has its Rank changed. This is the only notification that they want.

Thanks.

0 votes

Diane,

If you are using a script listener, what you have available is event. The type of event is whatever you selected to listen to. You can get the user doing the transition from event.user.

You get the issue from event.issue. https://scriptrunner.adaptavist.com/latest/jira/listeners.html#_custom_listeners has much more detail.

If you attach a screenshot of the listener configuration, we could advise more.

It is extremely difficult to learn how to correctly use these classes, since I am unable to locate any materials that can give a basic reference as what and how they should be used.

All of the material Atlassian provide, javadocs, tutorials etc, is relevant here, but this is not a straightforward thing for someone that is not a developer.

 

 

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Oct 11, 2018 in Marketplace Apps

You + one app + a desert island...

Hi all! My name is Miles and I work on the Marketplace team. We’re looking for better ways to recommend and suggest apps that are truly crowd favorites, so of course we wanted to poll the Community. ...

3,116 views 6 6
Join discussion

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