Manipulate webhook data in notification templates

 

Jira sends a JSON webhook payload to JEMHC for each issue event that occurs. JEMHC uses the event content to generate notifications. This guide shows how to extract custom data from an event and render it in your Notification Templates. Notification Template use Velocity markup for logic and variables, see the https://velocity.apache.org/engine/1.7/user-guide.html for general syntax guidance.

Preview contexts

A Preview Context contains webhook event data for a previously processed event. This context allows previews of notification Templates at edit time. In this example, we'll add custom content for the resolution of an issue. By default there are only Created, Updated and Deleted event templates, so this will be a modification to the Updated event template.

  1. Convert a received webhooks from JEMHC > Auditing > Events, into a Preview Context that can be used with Template Sets.

  2. Create Custom Macro to provide detection logic and rendering of required content

You will access this context using Velocity through $context, which represents ‘all’ the data, see below for more.

Creating Preview Contexts

Go to Auditing > Events and locate an issue event that has been processed.

An event entry will look something like this:

Press (…) > View Event Data to show the Webhook data:

Clicking the + Create Preview Context icon converts that data into a Preview Context:

Using Preview Contexts

Simplified, the webhook structure can be seen below. Full details on the webhook data structure can be found in this Atlassian documentation.

{ "timestamp", "webhookEvent", "issue_event_type_name", "user": { --> See User shape in linked documentation }, "issue": { --> See Issue shape in linked documentation }, "changelog" : { --> See Changelog shape in linked documentation } }

The $context reference used in all templates represents this webhook data structure. It important to know that it is a reference to an object of type ObjectNode.

Post function preview contexts

Please see Workflow Post Function Notifications for more information regarding post function event preview context behaviour.

 

Working with Preview Context values

The values in all nodes like $context.issue.id result in the value shown in the webhook data. For example "40301".  The quotes normally need removing, and this can be done with $context.issue.id.asText().

All ‘asText()’ values are effectively a java String object, so all related methods can then be used, eg .toLowerCase()

Most properties lie under the $context.issue.fields prefix, as shown:

 

Example changelog content:

"changelog": { "id": "351426", "items": [ { "field": "assignee", "fieldtype": "jira", "fieldId": "assignee", "tmpFromAccountId": "557058:b74e9574-1a83-4485-bbaf-96910d8d34fe", "tmpToAccountId": "5f4e16969aa96500463c40ad" } ],

field

expression

Issue Key

$context.issue.key.asText()

Project Key

$context.issue.fields.project.key.asText()

Description (as Text)

$fields.description.asText()

Description (as HTML)

$jemhUtils.wikiToHtml($fields.description.asText())

Custom Field Value (Text)

$context.issue.fields.customfield_10610.asText()

Custom Field Value (Single Choice)

$context.issue.fields.customfield_13900.value.asText()

Custom Field Value (Multiple Choice)

#foreach ($item in $context.issue.fields.customfield_13600) $item.value.asText() #end

Reporter Display Name

$context.issue.fields.reporter.displayName.asText()

Most recent comment

All Comments (if present)

Recent 10 comments

Access individual comments

Display comments, most recent first

Display comments, most recent last

Labels

#foreach ($aLabel in $context.issue.fields.labels) $aLabel #end

Changelog items

Example webhook content:

Example, detect if “assignee” changed

Filtering JSM private comments is covered on Detect that a Service Desk comment is private/internal

 

Working with dates

Date timestamps

Date (translate ISO dates)

Issue Created (create event only)

Create a custom macro

The System Macros show macros defined within JEMHC and inherited by all instances:

Here is a “hello world” macro which takes two parameters, $param1 and $param2 which can then be used inside the macro body.

You can define references inside macros that are then available afterwards to the caller.  This is useful for flagging specific situations, which is what we will do with the issue resolved status.

The Custom Macros  are user editable, the following text contains the render method that will be called from our template, as well as the workflow transition detector 'helper' method.

Testing

Below the Edit area, the Preview Context captured earlier can be selected.  This means that data will be available via the $context Velocity reference in templates. The Scratchpad allows you to enter template content and execute it in the Preview Context defined with the edited Macro. It won’t be saved and is just for validating expressions accessing data from the preview context:

This means that the preview icon (Eye Icon) will dynamically do the rendering and show the result, demonstrating (a) The changelog data contained an issue status change matching what was expected, and (b) The content was rendered via macro invocation:

Create and modify a notification template

Create a template:

Then set a name, make the type Issue Updated (as issue resolutions will be 'in' the Issue Updated event), and pick a theme, e.g. Jira System):

Use the Preview Context to understand how to access data

Expanded view

For example, when working over $fields.comment.comments in a loop, each comments data can then be accessed as $currentComment.author.displayName.asText().

Here is an example template:

Using the text preview again, we get to see what the rendered output for issue resolution looks like.

To further test the template, change the Preview Context to be an event that is not an issue resolution. Doing so will automatically re-render the content, and you'll find it isn't on the Resolved screen any more.

With the Preview Context and Template rendering features you can validate correct behaviour up front, saving time!

Example templates

Identifying recent attachments

Adding all attachments from an issue to a template could be problematic if a large quantity of attachments are present. The following script can be used to identify if attachments were recently added to the issue.

Template

How it looks

Related articles