Change email template to show comment history

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

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

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

Thanks for the tip!

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?

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.

Any tips for doing this on the HTML templates?

0 vote

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 

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?

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?

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



Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published 2 hours ago in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

13 views 0 3
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you