Add Linked Issues to Custom Notification Templates

Summary

@since 1.8.20

The DefaultIssueLinkService class is now exposed for use via Velocity Template Language (VTL).  This allows JEMH's custom templates to make use of this service in order to display the linked issues of an issue in notifications.

Create Custom User Macro

Firstly, we will set up the custom Velocity macro.  The reason for creating a macro, rather than just adding the velocity code directly into a template itself, is that if you wish to make a change, this can be done in one place instead of having to do it for all templates where the code is used.

  • Go to JEMH Configuration >Template Sets >Template Macros


  • Click Edit on the User Macros tab


  • Copy the below macro to the bottom of your user macros list

    The macro provided below is an example, showcasing how the exposed service can be used and can of course be modified to meet the users needs.

#macro (issuelink)
#set ($test = $issueLinkService.getIssueLinks($actionUser,$issue))
#set ($linkCollection = $test.getLinkCollection())
#if ($!linkCollection && $linkCollection.getAllIssues().size()>0)
<tr>
    <th>
        Issue Links:
    </th>
    <td>
        <table>
            #foreach( $linkType in $linkCollection.getLinkTypes() )  
                #set ($inboundIssues = $linkCollection.getInwardIssues($linkType.getName()))
                #set ($outboundIssues = $linkCollection.getOutwardIssues($linkType.getName()))
                #set ($outboundSize = $outboundIssues.size())
                #set ($inboundSize = $inboundIssues.size())
                #if ($inboundSize>0 || $outboundSize>0)
                <tr>
                    #if ($inboundSize>0)
                        <td>
                            $linkType.getInward()
                        </td>
                        #foreach ( $anIssue in $inboundIssues)
                            <td><a href="${baseurl}/browse/${anIssue.getKey()}">$anIssue.getKey()</a>&nbsp;[$anIssue.getStatus().getSimpleStatus().getName()]&nbsp;</td>
                        #end
                    #end
                </tr>
                <tr>
                    #if ($outboundSize>0)
                        <td>
                            $linkType.getOutward()
                        </td>
                        #foreach ( $anIssue in $outboundIssues)
                            <td><a href="${baseurl}/browse/${anIssue.getKey()}">$anIssue.getKey()</a>&nbsp;[$anIssue.getStatus().getSimpleStatus().getName()]&nbsp;</td>
                        #end
                    #end
                </tr>
                #end
            #end
        </table>
    </td>    
</tr>
#end
#end
  • Click Submit to save your changes

Implement Macro with Custom Template

With the custom user macro saved, we can now call the macro in our custom notification template.  In this guided example, we will add the issue links to an Ad-Hoc Notification template.

  • Go to JEMH>Template Sets and click Edit on your chosen template
  • As our macro is making use of HTML tables, we will be looking at the HTML content of the notification
  • Our example notification already has some Issue fields being presented (issue type, affected versions etc.):
  • In order to maintain formatting, we will be calling our macro within the table shown.  To call the custom macro we will use a hash tag followed by our macro name: "#issuelink"
  • Test that your macro is working, by supplying a Preview Context Issue Key.  Enter the issue key of an issue that has one or more linked services:
  • Then, press the blue preview icon to the bottom left of the HTML template window:
  • If everything is set up correctly you will see a preview of the template, including the linked issues of the notification subject issue:

Summary

@since 3.3.40

The Issue Links field can also store external links to webpages, or integrated Confluences instances. The DefaultIssueLinkService  only supports links to Issues in the same Jira instance. To add external  links to an email template the 'remoteIssueLinkService ' should be used.

Example script

The following script can be used to retrieve external links and display them in a notification:

#if($remoteIssueLinkService.getRemoteIssueLinksForIssue($actionerUser, $issue).getRemoteIssueLinks().size() > 0)
True! Links found.

#set($foundLinks = $remoteIssueLinkService.getRemoteIssueLinksForIssue($actionerUser, $issue).getRemoteIssueLinks())

#foreach($remoteLink in $foundLinks)
$wikiRenderer.render($remoteLink.getTitle(),null)
$wikiRenderer.render($remoteLink.getUrl(),null)
#end

#else
False! No links found.
#end

How it looks