Change email template to show comment history

Judy Schneider February 2, 2015

I would like to include the full comment history for an issue when I send out an email update, with comments from youngest to oldest, like a typical email conversation.  I'm thinking I'd have to run a foreach loop to print everything out, but I'm still a bit unclear as to how to reference the subclasses of the issue in order to retrieve the comments.

I'm reading through the Velocity and Jira docs to figure things out, but would would love to hear from anyone who has done this before and can point me in the right direction.

5 answers

1 accepted

4 votes
Answer accepted
Judy Schneider February 2, 2015

Ok, finally found out what to do.  Posting the relevant code here for posterity.  I'm not done modifying it for my own needs, but the basic functionality is there.

##Import the ComponentAccessor class
#set ($componentAccessorClass        = $constantsManager.getClass().getClassLoader().findClass('com.atlassian.jira.component.ComponentAccessor'))
#set ($componentAccessorConstructor  = $componentAccessorClass.getConstructor())
#set ($componentAccessor             = $componentAccessorConstructor.newInstance())
#set($commentManager = $componentAccessor.getCommentManager() )

##For Each loop which runs backwards through the issues (youngest to oldest)
#set ($comments=$commentManager.getComments($issue))
#set ($c=$comments.size())

#foreach($comment in $commentManager.getComments($issue))
#set ($c = $c - 1)
## If statement filters out comments with a RoleLevel other than "all users"
#if(!$comments.get($c).getRoleLevel())
- - - - - - - - - - -
$comments.get($c).getBody()
#end
#end
MattS
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 3, 2015

Glad you got it working. From experience I recommend you only add the latest fifty comments and only the first 4096 characters to keep you email size smallish. Large emails can do bad things to JIRA performance

Judy Schneider February 3, 2015

Thanks for the tip!

Rekha Bhaskaran February 1, 2016

Hello, this is working fine. My question if the user has to add a username in the comment, the format is shows is [~userName] but i would like to show it like "UserName,Testing

how to add it that way?

3 votes
Dominik Budde November 9, 2015

Like @JudyS wrote, we were using the following to access the ComponentAccessor Instance in the email template velocity context:

#set ($componentAccessorClass        = $constantsManager.getClass().getClassLoader().findClass('com.atlassian.jira.component.ComponentAccessor'))
#set ($componentAccessorConstructor  = $componentAccessorClass.getConstructor())
#set ($componentAccessor             = $componentAccessorConstructor.newInstance())
#set ($commentManager 				 = $componentAccessor.getCommentManager() )

 

In JIRA 6.4 the following issue was occuring using this:

duplicate class definition for name: "com/atlassian/jira/component/ComponentAccessor"

 

To get it back to work, we had replace it with the following:

#set ($componentAccessor = $constantsManager.getClass().forName('com.atlassian.jira.component.ComponentAccessor').newInstance())
#set ($commentManager    = $componentAccessor.getCommentManager() )

 

So the example for appending the entire mail history will be:

#set ($componentAccessor = $constantsManager.getClass().forName('com.atlassian.jira.component.ComponentAccessor').newInstance())
#set ($commentManager    = $componentAccessor.getCommentManager() ) 
##For Each loop which runs backwards through the issues (youngest to oldest)
#set ($comments			 = $commentManager.getComments($issue))
#set ($c				 = $comments.size()) 
#foreach($comment in $commentManager.getComments($issue))
#set ($c = $c - 1)
## If statement filters out comments with a RoleLevel other than "all users"
#if(!$comments.get($c).getRoleLevel())
- - - - - - - - - - -
$comments.get($c).getCreated() - $comments.get($c).getAuthorFullName()
$comments.get($c).getBody()
#end
#end

 

Note that the standard template will show the comment on the event on top of the mail body. So with this addition it will be shown a second time.

Andrew Laden
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 3, 2017

Any tips for doing this on the HTML templates?

0 votes
Gaël NEUEZ October 5, 2015

Hi,

I got this code working up to JIRA6.2. Now I'm migrating to JIRA6.4, and this always produces this error with the first line of code:

An error occurred whilst rendering this message. Please contact the administrators, and inform them of this bug. Details: ------- org.apache.velocity.exception.MethodInvocationException: Invocation of method 'findClass' in class org.apache.catalina.loader.WebappClassLoader threw exception java.lang.LinkageError: loader (instance of org/apache/catalina/loader/WebappClassLoader): attempted duplicate class definition for name: "com/atlassian/jira/component/ComponentAccessor" at templates/email/html/includes/fields/comments-history.vm[line 12, column 86] at org.apache.velocity.runtime.parser.node.ASTMethod.handleInvocationException(ASTMethod.java:351) at ...


Have anyone tried with JIRA6.4? If yes, did you get the same error and did you fix it?

Regards



0 votes
Judy Schneider February 2, 2015

Ok, I've come a little further with this issue. The comments are printing out in the order I want. However, I can't seem to enforce the security settings per comment, so they all get added, even the private ones. I am trying to retrieve the Group Level settings via: $comment.getGroupLevel(), but no value is ever returned. $comment.getBody() works just fine. Any idea why this could be?

0 votes
Paula Silveira
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 2, 2015

Hi Judy,

I believe that if you change the mail handler for adding comments to "Add a comment with the entire mail body" you'll achieve the result you want. Please check the following link for further information: 

Thanks and regards,
Paula Silveira
Atlassian Support | Cloud 

Judy Schneider February 2, 2015

Hi Paula,

I'm actually trying to change the template for an outgoing email.  Basically, I want an outgoing email summary to be created on a "comment added" event that looks something like this:

Current Comment
-----
Comment 3
-----
Comment 2
-----
Comment 1
-----
Description


Then when someone replies to the email, then only their reply should be taken in as the next comment (that part should be easy with the right mail handler settings).

Make sense?

Suggest an answer

Log in or Sign up to answer