Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Reverted from v. 13

JMWE for Jira Cloud provides 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 the Nunjucks scripts tester


Panel

On this page:

Table of Contents


Creating a shared Nunjucks template

  1. Log in to your Jira Cloud instance as an administrator.
  2. Go to the Administration icon  and click on it.
  3. Locate Add-ons from the menu and click on it.
  4. Locate JIRA MISC WORKFLOW EXTENSIONS on the left panel.
  5. Click on Shared Nunjucks templates.
  6. Provide a name for the Nunjucks template in Name.
  7. You can optionally provide a description for the Nunjucks template in Description.
  8. Click on Add. The Nunjucks template editor is displayed.
  9. Write a template in the editor.
  10. You can optionally test it against any issue using the Nunjucks script tester.
  11. Click on Save.

Editing a shared Nunjucks template

  1. Log in to your Jira Cloud instance as an administrator.
  2. Go to the Administration icon  and click on it.
  3. Locate Add-ons from the menu and click on it.
  4. Locate JIRA MISC WORKFLOW EXTENSIONS on the left panel.
  5. Click on Shared Nunjucks templates.
  6. Click on Edit for the template you wish to modify.
  7. Modify it and click on Save to save your changes.

Deleting a shared Nunjucks template

  1. Log in to your Jira Cloud instance as an administrator.
  2. Go to the Administration icon  and click on it.
  3. Locate Add-ons from the menu and click on it.
  4. Locate JIRA MISC WORKFLOW EXTENSIONS on the left panel.
  5. Click on Shared Nunjucks templates.
  6. 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
titleLimitations of macros

Nunjucks macros have two limitations that you need to be aware of:

  1. Macros do not have access to the global variables of the calling Nunjucks template (such as issue, currentUser, etc.) If you need to access these variables from the body of the macro, you need to pass them as parameters to the macro.
  2. Macros cannot execute asynchronous code. This means that any filter that calls the Jira API will fail when used inside a macro, and the failure is silent (no error message). This is a major limitation of Nunjucks macros. The workaround is not to use macros and instead include shared templates.


Example 1: To set the Priority of the issue based on its impact.

  1. Go to Shared Nunjucks templates in the JMWE administration pages.
  2. Name the template as Mappings.
  3. Provide the following template in the Nunjucks editor.

    Code Block
    languagejs
    linenumberstrue
    {% 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 -%}


  4. To access the template in a post-function, go to the configuration of the post-function.
  5. Write the following template in the editor.

    Code Block
    languagejs
    linenumberstrue
    {% import "Mappings" as Mappings %}
    {{Mappings.priorityFromImpact(issue.fields.Impact)}}


  6. You can test the written template against any issue using the Nunjucks Groovy script tester and verify the result.

Example 2: To provide a resolution date excluding the weekends and based on the priority of the issue:

  1. Go to Shared Nunjucks templates in JMWE administration pages.
  2. Name the template as addDateExcludingWeekends.
  3. Provide the following template in the Nunjucks editor.

    Code Block
    languagejs
    linenumberstrue
    {% macro addDays(from, nod) %}
        {% set priorityweeks = issue.fields.priortiy.namenod//5 %}
        {% ifset priorityremDays == "Highest"nod%5 %}
    	     
        {#- Adding the whole weeks #}
        {%- set nodfrom = 2%}
    {% elseif priority == "High"%}
    	 from | date('add', weeks,'weeks') %}
         
        {#- Run a loop and check each day to skip a weekend #}
        {%- for i in range(0,remDays) %}
        {% set nodfrom = 4%} from | date('add',1,'d') | date %}
        {% if from elseif priority| date('day') == "Medium6" %}
     	         {% set nod = 6%}
    {% elseif priorityfrom = from | date('add',1,'d') | date %}
          {% endif %}
          {% if from | date('day') == "Low0" %}
              	{% set nod = 8%}
    {% else %}
    	from = from | date('add',1,'d') | date %}
          {% endif %}
        {% endfor %}
         
        {#- Skipping the ending weekend #}
        {%- if from | date('day') == "6" %}
            {% set nodfrom = 6%}
     from | date('add',1,'d') %}
        {% endif %}
        {% if from | date('day') == "0" %}
            {% set from = from | date('businessAddadd',1, nod ) | date() }}'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 -%}


  4. To access the template in a post-function, go to the configuration of the post-function.
  5. Write the following template in the editor.


    Code Block
    languagejs
    linenumberstrue
    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 :
    {%- import "addDateExcludingWeekends" as AD %}
    {%- set fromdays = AD.daysFromPriority(issue.fields.priority.createdname) %}
    {% include "addDateExcludingWeekends" %}
    
    
    {{-AD.addDays(issue.fields.created,days)}}
    Regards,
    {{ issue | projectInfo | field("lead.displayName")}}


Example 3: To include a shared email body template in an Email Issue post-function:

  1. Go to Shared Nunjucks templates in JMWE administration pages.
  2. Create a new shared template to hold the HTML body of an email and name it emailHtmlBody
  3. In the HTML Body field of the Email Issue post-function, write the following template:

    Code Block
    {% include "emailHtmlBody" %}