Refer jira email velocity templates from groovy script

Naren
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.
October 12, 2014

Hi,

I've a written a groovy script which runs as a service in JIRA after every 1 minute which triggers email to group of users, after every periodic interval, sending the issue details qualifying the criteria.

I want to refer the JIRA email velocity templates in the groovy script, located under - <JIRA_INSTALL>/atlassian-jira/WEB-INF/classes/templates/email/html. Instead of embedding the whole html code in the groovy script under email body.

Below is groovy script which checks for the issues qualifying the criteria and and then sends an email - 

 

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser.class)
def searchProvider = ComponentAccessor.getComponent(SearchProvider.class)
def issueManager = ComponentAccessor.getIssueManager()
def userManager = ComponentAccessor.getUserManager()
def query = jqlQueryParser.parseQuery("'Shutdown Time' is not EMPTY and 'Startup Time' is not EMPTY and 'Shutdown Time' &lt;= now() and 'Startup Time' &gt;= now()")
def results = searchProvider.search(query, userManager.getUserByName("admin"), PagerFilter.getUnlimitedFilter())
results.getIssues().each {documentIssue -&gt;
def issue = issueManager.getIssueObject(documentIssue.id)
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
CustomField shutdownTime = customFieldManager.getCustomFieldObjectByName("Shutdown Time")
CustomField startupTime = customFieldManager.getCustomFieldObjectByName("Startup Time")
Date shutdownTimeCFDate = (Date)issue.getCustomFieldValue(shutdownTime )
Date startupTimeCFDate = (Date)issue.getCustomFieldValue(startupTime )



final long TWO_MINS_MILLIS = 2 * 60 * 1000l

long now = System.currentTimeMillis()
long delta = now - downTimeStartCFDate.time

if( delta % TWO_MINS_MILLIS &gt;= TWO_MINS_MILLIS - 60000 &amp;&amp; now &lt; startupTimeCFDate.time )
{
   SMTPMailServer mailServer =    ComponentManager.getInstance().getMailServerManager().getDefaultSMTPMailServer();
if (mailServer) 
{
Email email = new Email("admin@company.com")
email.setMimeType("text/html");
email.setSubject(issue.key + " " + issue.summary)
email.setBody(); // The html code is embedded here listing customfeilds and other issue details
}
}
}

Can I refer the JIRA  custom email templates placed in the corresponding directory through the groovy script.

 

Any help would be much appreciated.

1 answer

1 accepted

2 votes
Answer accepted
JamieA
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.
October 13, 2014

Try using com.atlassian.jira.event.issue.IssueEventManager#dispatchEvent(java.lang.Long, com.atlassian.jira.issue.Issue, com.atlassian.crowd.embedded.api.User, boolean), with a notification scheme that listens for that particular event.

Naren
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.
October 13, 2014

Thanks @Jamie Echlin [Adaptavist] , In my case, there is no particular event triggered. I've implemented the above groovy script as a service which runs after every 1 minute and checks if there are set of issues qualifying the JQL search. If yes, then it takes the value of the 2 date-time picker custom fields - Shutdown Time and Startup Time and then triggers an email at regular intervals (say 2 hrs) after the Shutdown Time until the Startup Time is reached. Ex. Shutdown Time: 14/Oct/14 08:15 PM Startup Time: 15/Oct/14 07:00 AM Then an email should be triggered at - Email Notification1 - 14/Oct/14 10:15 PM Email Notification2 - 15/Oct/14 00:15 AM Email Notification3 - 15/Oct/14 02:15 AM Email Notification4 - 15/Oct/14 04:15 AM Email Notification5 - 15/Oct/14 06:15 AM I wanted to use the jira email templates in the groovy script to keep the email format inline with the default one rather than embedding the jira fields in html. Hope I'm clear. It would be very helpful if you could share your inputs and suggestions on the above approach.

Naren
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.
October 14, 2014

Hi @Jamie Echlin [Adaptavist] I tried using the below method - def currentUser = ComponentAccessor.getJiraAuthenticationContext().getUser() IssueEventManager issueEventManager = ComponentAccessor.getIssueEventManager() issueEventManager.dispatchEvent(10015L, issue, currentUser, true) But running the script throws an error - Caused by: groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.event.issue.DefaultIssueEventManager.dispatchEvent() is applicable for argument types: (java.lang.Long, com.atlassian.jira.issue.IssueImpl, com.atlassian.jira.user.DelegatingApplicationUser, java.lang.Boolean) values: [10015, TEST-2792, Naren_G(nare_g), ...] Possible solutions: dispatchEvent(java.lang.Long, com.atlassian.jira.issue.Issue, com.atlassian.crowd.embedded.api.User, boolean), dispatchEvent(java.lang.Long, com.atlassian.jira.issue.Issue, java.util.Map, com.atlassian.crowd.embedded.api.User), dispatchEvent(java.lang.Long, com.atlassian.jira.issue.Issue, java.util.Map, com.atlassian.crowd.embedded.api.User, boolean), dispatchEvent(java.lang.Long, com.atlassian.jira.issue.Issue, com.atlassian.crowd.embedded.api.User, org.ofbiz.core.entity.GenericValue, boolean, boolean), dispatchEvent(java.lang.Long, com.atlassian.jira.issue.Issue, com.atlassian.crowd.embedded.api.User, com.atlassian.jira.issue.comments.Comment, com.atlassian.jira.issue.worklog.Worklog, org.ofbiz.core.entity.GenericValue), dispatchEvent(java.lang.Long, com.atlassian.jira.issue.Issue, com.atlassian.crowd.embedded.api.User, com.atlassian.jira.issue.comments.Comment, com.atlassian.jira.issue.worklog.Worklog, org.ofbiz.core.entity.GenericValue, boolean) at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55)

JamieA
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.
October 14, 2014

You can see the problem in the error message. You need to convert the user to the type that it wants.

JamieA
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.
October 14, 2014

> In my case, there is no particular event triggered I'm saying to trigger an event...

Naren
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.
October 16, 2014

Thanks @Jamie Echlin [Adaptavist] !

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events