Starting with version 1.1.1-AC, JMWE for Jira Cloud provides a section to store shared Nunjucks templates in the JMWE administration section. This document details on how to create and import them into other support for shared Nunjucks templates. This is very useful to share macros across post-functions, as well as to split lengthy templates when you run into this issue: MWEC-159.
This page explains how to create shared Nunjucks templates and reuse them from other Nunjucks templates written in JMWE post-functions or Nunjucks scripts tester (in JMWE administration page). These templates are very useful to share macros across post-functions and also avoid lengthy scripts that are not supported by the Groovy editor.functions or the Nunjucks tester.
Panel | |
---|---|
On this page:
|
Creating a shared Nunjucks template
- Log in to your Jira Cloud instance as an administrator.
- Go to the Administration icon and click on it.
- Locate Add-ons from the menu and click on it.
- Locate JIRA MISC WORKFLOW EXTENSIONS on the left panel.
- Click on Shared Nunjucks templates.
- Provide a name for the Nunjucks template in
Name
. - You can optionally provide a description for the Nunjucks template in
Description
. - Click on
Add
. The Nunjucks template editor is displayed. - Write a template in the editor.
- You can optionally test it against any issue using the Nunjucks script tester.
- Click on
Save
.
Editing a shared Nunjucks template
- Log in to your Jira Cloud instance as an administrator.
- Go to the Administration icon and click on it.
- Locate Add-ons from the menu and click on it.
- Locate JIRA MISC WORKFLOW EXTENSIONS on the left panel.
- Click on Shared Nunjucks templates.
- Click on
Edit
for the template you wish to modify. - Modify it and click on
Save
to save your changes.
Deleting a shared Nunjucks template
- Log in to your Jira Cloud instance as an administrator.
- Go to the Administration icon and click on it.
- Locate Add-ons from the menu and click on it.
- Locate JIRA MISC WORKFLOW EXTENSIONS on the left panel.
- Click on Shared Nunjucks templates.
- Click on
Delete
for the template you wish to delete.
Import/Include a Nunjucks template
You can import or include a shared Nunjucks template into your post-functions or Script tester using the respective tags available in Nunjucks.
Warning | ||
---|---|---|
| ||
Nunjucks macros have two limitations that you need to be aware of:
|
Example 1: To set the Priority of the issue based on its impact.
- Go to Shared Nunjucks templates in the JMWE administration pages.
- Name the template as
Mappings
. Provide the following template in the Nunjucks editor.
Code Block language js linenumbers true {% macro priorityFromImpact(impact) %} {%- if impact == "Company wide" %} Highest {% elseif impact == "More than one project" %} High {% elseif impact == "Single project" or impact == "Individual" %} Medium {% else %} Low {% endif %} {% endmacro -%}
- To access the template in a post-function, go to the configuration of the post-function.
Write the following template in the editor.
Code Block language js linenumbers true {% import "Mappings" as Mappings %} {{Mappings.priorityFromImpact(issue.fields.Impact)}}
- You can test the written template against any issue using the Groovy script Nunjucks tester and verify the result.
Example 2: To provide a resolution date excluding the weekends and based on the priority of the issue:
- Go to Shared Nunjucks templates in JMWE administration pages.
- Name the template as
addDateExcludingWeekends
. Provide the following template in the Nunjucks editor.
Code Block language js linenumbers true {% macro addDays(from, nod) %} {% set weekspriority = nod//5issue.fields.priortiy.name %} {% setif remDayspriority == nod%5"Highest" %} {#- Adding the whole weeks #} {%- {% set fromnod = from | date('add', weeks,'weeks') %} {#- Run a loop and check each day to skip a weekend #} {%- for i in range(0,remDays) %} 2%} {% elseif priority == "High"%} {% set fromnod = from | date('add',1,'d') | date %} 4%} {% if from | date('day')elseif priority == "6Medium" %} {% set fromnod = from | date('add',1,'d') | date %} 6%} {% endifelseif %} {% if from | date('day') priority == "0Low" %} {% set fromnod = from | date('add',1,'d') | date %} {% endif %} 8%} {% endforelse %} {#- Skipping the ending weekend #} {%- if from | date('day') == "6" %} {% set from = from | date('add',1,'d') %} set nod = 6%} {% endif %} {% if from | date('day') == "0" %} {% set from = from | date('addbusinessAdd',1,'d') -%} {% endif -%} {#-Return the Momentjs#} {{-from | date}} {% endmacro -%} {% macro daysFromPriority(priority) %} {% if priority == "Highest" %} 2 {% elseif priority == "High"%} 3 {% elseif priority == "Medium"%} 4 {% elseif priority == "Low"%} 5 {% else %} 5 {% endif %} {% endmacro -% nod ) | date() }}
- To access the template in a post-function, go to the configuration of the post-function.
Write the following template in the editor.
Code Block language js linenumbers true Hi {{issue.fields.reporter.displayName }}, This is in response to the ticket : {{issue.key}} that you created with us on {{issue.fields.created | date("DD/MM/YYYY") }}. Your issue will be resolved on or before : {%- importset "addDateExcludingWeekends"from as AD= issue.fields.created %} {%- set days = AD.daysFromPriority(issue.fields.priority.name) %} {{-AD.addDays(issue.fields.created,days)}}include "addDateExcludingWeekends" %} Regards, {{ issue | projectInfo | field("lead.displayName")}}
Example 3: To include a shared email body template in an Email Issue post-function:
- Go to Shared Nunjucks templates in JMWE administration pages.
- Create a new shared template to hold the HTML body of an email and name it
emailHtmlBody
In the HTML Body field of the Email Issue post-function, write the following template:
Code Block {% include "emailHtmlBody" %}