Event in groovy - how to?

Rumceisz July 15, 2013

Hi All,

some projects of our Jira instance requested custom email templates. Most of them are already deployed as a custom event added to a workflow.

But there a few events - as you know - which are not related to workflow. For example: comment adding, worklog adding, delete an issue, etc. Now we have this fantastic Script runner plugin where custom conditions can be added. Hence we are not acquinted with groovy can you maybe help how to write them correctly?

For example:

if (eventTypeId.equals(EventType.ISSUE_CREATED_ID))

it's not correct we know but we didn't find a reference for this.

We need these:

Work Logged On Issue

Issue Updated

Issue Worklog Deleted

Issue Commented

Issue Worklog Updated

Issue Comment Edited

Issue Deleted

Issue Moved

Thanks for the support!

2 answers

1 accepted

0 votes
Answer accepted
&(*&)#)_*#@@(*)(@*)(*@
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.
August 6, 2013
Work Logged On Issue
Issue Updated
Issue Worklog Deleted
Issue Commented
Issue Worklog Updated
Issue Comment Edited
Issue Deleted
Issue Moved



You will find those events in the javadoc :

https://docs.atlassian.com/jira/latest/com/atlassian/jira/event/type/EventType.html

as a side note, in case you wanted also the attachements , they get handled with the ISSUE_GENERICEVENT_ID

there are some use case where velocity is not enough, sometime you just wand the attachement to not be send , sometime like your use case , you want the internal url to be send .

for your issue, your proposal is correct :

#if ($changelogauthor)
        #if project==PROJECTKEY))
            #set ($changelogauthorLink = "internalURL")
     #else
            #set ($changelogauthorLink = "externalURL")

you will have to hardcode all yout value in the templates files , it make sense to create some macro.

If you have a lot of custom test or if you find yourself limited by what can be found in the velocity jira context and tested in velocity, then consider writing your own listener. It is well documented , if you look a the source of jira you will see that you can change baseurl before it pass to the velocity context quite easily or using a different smtp server for you external user, almost everything is possible

https://developer.atlassian.com/display/JIRADEV/Writing+JIRA+Event+Listeners+with+the+atlassian-event+Library

here is how to invoque the template render

import com.atlassian.templaterenderer.RenderingException;
import com.atlassian.templaterenderer.TemplateRenderer;

StringWriter mesg = new StringWriter();
Map<String, Object> context = Maps.newHashMap();
		
context.put("customInternalBaseUrl", "http://internal.url.org");

try {
	renderer.render("templates/mail/custom/issuecreated.vm", context, mesg);
} catch (RenderingException e) {
	LOG.error("failed to generate the message content", e);
	throw new MySystemException(e.getMessage(), e);
} catch (IOException e) {
	LOG.error("failed to load the template", e);
	throw new MySystemException(e.getMessage(), e);
}

mail.setCharset("UTF-8");
mail.setMessage(mesg.toString());

here is the java code for the getting the attachements from a issueEvent, my requirement was to have the attachements send with the notification :

private static final String GV_FIELDTYPE = "fieldtype";
private static final String GV_FIELD = "field";
private static final String GV_NEWVALUE = "newvalue";
private static final String GV_JIRA = "jira";

 

    /**
     * Recupere tous les attachements
     *
     * @param issueEvent
     * @return List<String> the absolute file path related to the issueEvent
     */
    public Collection<Attachment> getAllAttachmentsFileAbsolutePath(IssueEvent issueEvent) {
        List<Attachment> attachments = new ArrayList<Attachment>(10);
        try {
            GenericValue changeLog = issueEvent.getChangeLog();

            if ((changeLog == null) && (issueEvent.getIssue().getAttachments() != null)) {
                // issue was just created , get all initial attachement
                attachments.addAll(issueEvent.getIssue().getAttachments());

            } else {
                // issue was updated , add attachement in changeLog
                List<GenericValue> changeItems = changeLog.getRelated("ChildChangeItem");
                for (GenericValue changeItem : changeItems) {

                    if (LOG.isDebugEnabled()) {
                        LOG.debug(" changed field '{}' '{}'", ((String) changeItem.get(GV_FIELDTYPE)), ((String) changeItem.get(GV_FIELD)));
                    }
                    if (((String) changeItem.get(GV_FIELDTYPE)).equalsIgnoreCase(GV_JIRA)
                            && ((String) changeItem.get(GV_FIELD)).equalsIgnoreCase(IssueFieldConstants.ATTACHMENT)) {
                        String id = changeItem.get(GV_NEWVALUE).toString();
                        Attachment attachment = attachmentManager.getAttachment(Long.valueOf(id));
                        attachments.add(attachment);
                    }
                }
            }

        } catch (GenericEntityException e) {
            LOG.error("error in getNewAttachmentFileAbsolutePath", e);
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("{} files attached ", attachments.size());
            int i = 0;
            for (Attachment attachment : attachments) {
                LOG.debug(" {} - {}", ++i, attachment.getFilename());
            }
        }
        return attachments;
    }

here is the documentation for registering a groovy listener https://jamieechlin.atlassian.net/wiki/display/GRV/Listeners

you may notice that the above code, is somehow incomplete , i suggest that you first write a java listener : it is much easier to work with the atlassian sdk than going with the hardway and dealing with import .

you can easily transform your java listener into a groovy listener .

best regards

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 15, 2013

Rumi, I'm sorry, but this is the fourth or fifth time you've asked about these events without actually seeming to phrase a useful question. In this case, you've mentioned custom email templates (as before). Then you've said there are more events. Then you've said you've got the script runner.

But you still haven't told us what you actually WANT from fiddling with these.

IF it helps clarify:

  • The emails for these are sent out when an event of that name is fired.
  • The events are fired when an issue has one of these things done to it.
  • If you want do customise them, you amend the templates (As per other questions)
  • If you want other stuff to happen when they are fired, you need a listener (and the script runner can help you structure a listener)

Maybe you could try to explain what you actually need? In terms of what happens and what the user sees - forget events and scripts, and just tell us what the user gets?

Rumceisz July 15, 2013

Hello Nic,

you're right. I'm sorry, I don't wanna flood this forum.

The cover story:

our JIRA is configured with 2 URLs: there is an internal (like jira.internal.com) URL which is available only within intranet.

Because we have plenty of customer users who cannot access to our internal network we established an external URL. This became the baseURL.

So far all the email templates used the external URL (jira.external.com - the baseURL) as it is by default in Jira velocity templates. Now it comes a project which expressly wanted to have all the email templates using the internal URL links. We configured them:

1. A custom workflow using custom events which trigger custom email templates with these internal URLs (we just simply replaced the baseURL to the internal links). It is OK for those events which are triggered by workflow post functions like issue creation, issue resolving, issue reopening, etc.

But there are several more events which are triggered by event listeners like issue assigning, issue updating or work log adding - this is what I tried to mention in my other issues but in a more obscure way -sorry for that.

So now I see only 1 solution if we put an 'IF' statement in these velocity templates. Like (excerpt from issue_assinged.vm):

#if ($changelogauthor)
		#if project==PROJECTKEY))
            #set ($changelogauthorLink = "internalURL")
 	 #else
            #set ($changelogauthorLink = "externalURL")

Sad, but we are not acquinted in velocity script. In this case only 1 velocity template would remain for each event (issue assign, issue worklogged, etc.) with an if statement in them: if the assign event was occured in project X then the internalURL will be applied in the email.

So this is the story so far. I hope everything is clear now.

Warm regards,
Rumi

Suggest an answer

Log in or Sign up to answer