Add the Last Issue Comment to Custom Notification Templates


You want your issue notifications to include the last comment that was made on an issue, or even the last comment set to a particular Project Role/Group Visibility Level.


Using JEMH and its Custom Notification Templates, this either scenario is possible.  Custom Notification Templates use Velocity Template Language (VTL), so it is best to have a look at the user guide to familiarise yourself with it.

API changes : isPrivateJSDComment(...)

This particular API call has changed recently, historically it just took a Comment and derived whether it 'should' be private.  In 2.7.19 we use the JSD API that requires a contextUser to decide if its private for that user.  In 2.7.23 we add TemplateUser support to solve outbound notification usage that differed slightly from TemplateSet edit time preview.

API changes

Some changes were made to the isPrivateJSDComment method:

<= 2.7.18 and >= 2.7.24

boolean isPrivateJSDComment(Comment c)

2.7.23+ (support TemplateUser)

boolean isPrivateJSDComment(ApplicationUser u, Comment c)

boolean isPrivateJSDComment(TemplateUser tu, Comment c)

Set Up a Velocimacro

Velocimacro's allow a section of a template to be repeatedly used in multiple templates, meaning that the VTL script only has to be written to and maintained in one place.  We will define the macro and then use it in an Ad-Hoc notification template.  You can of course just insert the code directly into the template if you wish.

  1. Go to JEMH>Template Sets > Macros


  2. On the User Macros tab, click Edit (the pencil icon).

  3. Enter the following into the VTL editor window.  The script in its current state will show the last comment on an issue that does not have a Project Role/Group level of visibility (i.e. a "public" comment).  The final section of this wiki page will discuss alterations that can be made to make the comment specific to a role or group visibility level  Note that $lastPublicComment.getBody() used here will output the unrendered wiki markup of the comment, see later for HTMLand TEXT rendering.

  4. The Velocity script to enter:

    #macro (showLastComment) #set ($commentList = $allComments.getComments($issue)) #set ($lastPublicComment = "") #if ($commentList.size()>0) #foreach ($comment in $commentList) #if (!$comment.getRoleLevel() && !$comment.getGroupLevel()) #set ($lastPublicComment = $comment) #end #end This is the last public comment, made by $lastPublicComment.getAuthorApplicationUser():<br> $lastPublicComment.getBody()<br> #else There are no public comments #end #end
  5. Press Submit to save your changes

Add to a Custom Notification Template

Now that the Velocimacro is completed, we can now call this macro from inside our custom template.

  1. Go to JEMH>Template Sets

  2. Add  or Edit  a custom notification template of your choice.  In this example we are looking at an Ad-Hoc Notification

  3. Our template makes use of some HTML formatting, so we will be placing our macro call in the HTML content section (if you are adding to an existing template, make sure you add in the correct place):

  4. Test your template by entering an issue key in the field Preview Context IssueKey

  5. The status "No Preview Context" will change to a blue icon.  Press it to preview your template:

Rendering comments from Wiki mark-up

The velocity context contains $wikiRenderer for this purpose (see the API, its linked in the Velocity Context docs within the JEMH TemplateSet section).  The $lastPublicComment.getBody() can be rendered through the wiki tenderer into TEXT or HTML depending on need:

HTML output

$wikiRenderer.render($lastPublicComment.getBody(), $issueRenderContext)

Example Script rendering recent 10 public comments

#macro (showLastCommentReverse) ##$jemhUtils.log("info", "starting, actionerUser=$actionerUser") ##For Each loop which runs backwards through the issues (youngest to oldest) #set ($comments = $allComments.getComments($issue)) #set ($c = $comments.size()) ##$jemhUtils.log("info", "there are $c comments to render") #set ($a = 1) #set ($c = $c - 1) #foreach($aComment in [0..$c]) ##$jemhUtils.log("info", "looping with c = $c") #if ($c > 0) #set ($c = $c - 1)