Velocity template to include all comments cannot access $c.GetGroupLevel() or $c.getRoleLevel() (v5.2.6)

Im extending the velocity email template to add all comments (PITA) to particular event emails (is working via JETI) except for

1) a couple fields which will not return data. Example output:

ID: 13430,Role: $c.getRoleLevel(), Group Level: $c.getGroupLevel(), Author: taber loveless, Thu Feb 28 21:29:13 MST 2013
fsdfsg
ID: 13429,Role: $c.getRoleLevel(), Group Level: $c.getGroupLevel(), Author: taber loveless, Thu Feb 28 21:27:22 MST 2013
xgbxgfh
ID: 13428,Role: $c.getRoleLevel(), Group Level: $c.getGroupLevel(), Author: taber loveless, Thu Feb 28 21:16:38 MST 2013
dfda asdf

2) cannot seem to access com.atlassian.jira.component.ComponentAccessor instead of ComponentManager (deprecated). Im not sure but suspect this may be the cause for #1

VELOCITY TEMPLATE:

#disable_html_escaping()
#parse("templates/email/html/includes/emailconstants.vm")
#parse("templates/email/html/includes/header.vm")
<tr valign="top">
    <td id="email-banner" style="padding:32px 32px 0 32px;">

        #if ($commentauthor)
            #set ($changelogauthorLink = "#authorlink2($commentauthor.name $linkstyle)")
        #else
            #set ($changelogauthorLink = "#authorlink2($remoteUser.name $linkstyle)")
        #end

        #set ($issueType = $issue.getIssueTypeObject())

        #set ($issueLink = "#renderIcon(${issueType.iconUrlHtml} ${issueType.getNameTranslation($i18n)}) <a style='color:${textLinkColour};text-decoration:none;' href='${baseurl}/browse/${issue.getKey()}'>$issue.getKey()</a>")

        #emailbanner($commentauthor "email.event.activity.comment" $changelogauthorLink $issueLink "")
    </td>
</tr>
#if ($comment)
<tr valign="top">
    <td id="email-fields" style="padding:0 32px 32px 32px;">
        <table border="0" cellpadding="0" cellspacing="0" style="padding:0;text-align:left;width:100%;" width="100%">
            <tr valign="top">
                <td id="email-gutter" style="width:64px;white-space:nowrap;"></td>
                <td>
                    <table border="0" cellpadding="0" cellspacing="0" width="100%">
                        #parse("templates/email/html/includes/fields/comment.vm")
                    </table>
                </td>
            </tr>
        </table>
    </td>
</tr>
#end

##https://confluence.atlassian.com/display/JIRACOM/Display+All+Comments+%28Historical+Log%29+in+JIRA+Emails
##http://docs.atlassian.com/jira/latest/index.html?com/atlassian/jira/ComponentManager.html

#set ($componentManagerClass = $constantsManager.getClass().getClassLoader().findClass('com.atlassian.jira.ComponentManager'))
#set ($method = $componentManagerClass.getDeclaredMethod('getInstance', null))
#set ($componentManager = $method.invoke(null, null))

#set ($allComments = $componentManager.getCommentManager())

#set ( $comms = ${allComments.getComments($issue)}  )
#set ( $rSorter = $comms.size() - 2  ) ##dont include latest comment as this is for comment event notifications which will put latest comment at the top top top
#if($rSorter >= 0)
<tr valign="top">
    <td  id="email-fields" style="padding:0 32px 32px 32px;">
        <table border="0" cellpadding="0" cellspacing="0" style="padding:0;text-align:left;width:100%;" width="100%">
	    <tr valign="top">
                <td style="width:64px;white-space:nowrap;"></td>
                <td>
                    <p>================== 	Discussion History 	==================</p>
                </td>
            </tr>

			#foreach($d in [$rSorter .. 0])
				#set ( $c = $comms.get($d) )
				
				<tr valign="top">
					<td colspan="2" style="color:${textColour};font-family:${textFontFamily};font-size:${textSize};padding:0 0 10px 0;width:100%;">
						<div>ID: $c.getId(),Role: $c.getRoleLevel(), Group Level: $c.getGroupLevel(), Author: $c.getAuthorFullName(), $c.getCreated()</div>
						<div style="background-color:${commentBackground};border:1px solid #dddddd;color:${textColour};padding:5px;">
							$c.getBody()
						</div>
						<div style="color:${textSubtleColour};padding:4px 0 0 0;">#visibility()</div>
					</td>
				</tr>

			#end
			</table>
		</td>
	</tr>
#end


        

#parse("templates/email/html/includes/footer.vm")


4 answers

You mean so HTML formatting of comments are preserved?

The previous was used on 5.2.x, here is one of the templates I am using for 6.x (note - some items may be specific to the JETI plugin or custom fields).

The main parts are the html_escaping and compiling the comments:

<table border="0" cellpadding="0" cellspacing="0" style="padding:0;text-align:left;width:100%;" width="100%">
					<tr valign="top">
						<td id="email-gutter"></td>
						<td style="color:${textColour};font-family:${textFontFamily};font-size:${textSize};padding:0 0 10px 0;width:100%;">
							#foreach($c in $!publicComments)
								<div style="border-bottom: 1px solid #DDD;">
										<b>$!{c.authorFullName}</b> - $c.getCreated()</div>
								<p>$!{c.body}</p>
							#end
						</td>
					</tr>
				</table>

Full:

#disable_html_escaping()
#parse("templates/email/html/includes/emailconstants.vm")
#parse("templates/email/html/includes/header.vm")

<tr valign="top">
	<td id="email-banner" style="padding:16px 16px 0 16px;">
		#if ($changelogauthor)
			#set ($changelogauthorLink = "#authorlink2($changelogauthor.name $linkstyle)")
		#else
			#set ($changelogauthorLink = "#authorlink2($remoteUser.name $linkstyle)")
		#end
		#set ($resolution = "<strong>$textutils.htmlEncode(${issue.getResolutionObject().getNameTranslation($i18n)}, false)</strong>")
		#set ($issueType = $issue.getIssueTypeObject())
		#set ($issueLink = "<a style='color:${textLinkColour};text-decoration:none;' href='${baseurl}/browse/${issue.getKey()}'>${issueType.getNameTranslation($i18n)} $issue.getKey()</a>")
		#emailbanner($changelogauthor "email.event.activity.updated" $changelogauthorLink $issueLink $resolution)
	</td>
</tr>

#if ($comment || $changelog)
<tr valign="top">
    <td id="email-fields" style="padding:0 32px 32px 32px;">
        <table border="0" cellpadding="0" cellspacing="0" style="padding:0;text-align:left;width:100%;" width="100%">
            <tr valign="top">
                <td id="email-gutter" style="width:64px;white-space:nowrap;"></td>
                <td>
                    <table border="0" cellpadding="0" cellspacing="0" width="100%">
                        #parse("templates/email/html/includes/fields/comment.vm")
                        #parse("templates/email/html/includes/fields/changelog.vm")
                    </table>
                </td>
            </tr>
        </table>
    </td>
</tr>
#end

<tr valign="top">
    <td id="email-fields" style="padding:0 16px 16px 16px;">
        <table border="0" cellpadding="0" cellspacing="0" style="padding:0;text-align:left;width:100%;" width="100%">
            <tr valign="top">
		<td id="email-gutter" style="width:64px;white-space:nowrap;"></td>
                <td>
                    <table border="0" cellpadding="0" cellspacing="0" width="100%">
						#parse("templates/email/html/includes/fields/issuetype.vm")
						#parse("templates/email/html/includes/fields/affectsversions.vm")
						#parse("templates/email/html/includes/fields/components.vm")
						#parse("templates/email/html/includes/fields/createddate.vm")
						#parse("templates/email/html/includes/fields/duedate.vm")
						#parse("templates/email/html/includes/fields/priority.vm")
						<tr valign="top">
							<td style="color:${textColour};font-family:${textFontFamily};font-size:${textSize};padding:0 10px 10px 0;white-space:nowrap;">
								<strong style="font-weight:normal;color:${textSubtleColour};">External Reporter:</strong>
							</td>
							<td style="color:${textColour};font-family:${textFontFamily};font-size:${textSize};padding:0 0 10px 0;width:100%;">
								$!jetiFieldRenderer.renderCustomField($issue, 'customfield_11100')
							</td>
						</tr>
                                                <tr valign="top">
							<td style="color:${textColour};font-family:${textFontFamily};font-size:${textSize};padding:0 10px 10px 0;white-space:nowrap;">
								<strong style="font-weight:normal;color:${textSubtleColour};">External Watchers:</strong>
							</td>
							<td style="color:${textColour};font-family:${textFontFamily};font-size:${textSize};padding:0 0 10px 0;width:100%;">
								$!jetiFieldRenderer.renderCustomField($issue, 'customfield_11400')
							</td>
						</tr>
						#parse("templates/email/html/includes/fields/reporter.vm")
						#parse("templates/email/html/includes/fields/assignee.vm")
						#parse("templates/email/html/includes/fields/attachments.vm")
						#parse("templates/email/html/includes/fields/fixversions.vm")
						#parse("templates/email/html/includes/fields/project.vm")
						#parse("templates/email/html/includes/fields/labels.vm")
						#parse("templates/email/html/includes/fields/timetracking.vm")
						#parse("templates/email/html/includes/fields/description.vm")
                    </table>
                </td>
            </tr>
        </table>
    </td>
</tr>

#if ($!emailDef.emailOptions.addComments)
		<tr valign="top">
			<td id="email-fields" style="padding:0 16px 16px 16px;">
				<table border="0" cellpadding="0" cellspacing="0" style="padding:0;text-align:left;width:100%;" width="100%">
					<tr valign="top">
						<td id="email-gutter"></td>
						<td style="color:${textColour};font-family:${textFontFamily};font-size:${textSize};padding:0 0 10px 0;width:100%;">
							#foreach($c in $!publicComments)
								<div style="border-bottom: 1px solid #DDD;">
										<b>$!{c.authorFullName}</b> - $c.getCreated()</div>
								<p>$!{c.body}</p>
							#end
						</td>
					</tr>
				</table>
			</td>
		</tr>
#end


            </table>
        </td><!-- End #email-page -->
    </tr>
    <tr valign="top">
        <td style="color:${textSubtleColour};font-family:${textFontFamily};font-size:11px;text-align:center;">-- <b>To add Comments or Attachments</b> simply reply to this email without changing subject line --<br /> <i>when replying, any content following "From: " will be omitted</i><br /></td>
    </tr>
</table><!-- End #email-wrap -->
</div><!-- End #email-body -->

Yes, HTML format of the initial comment. I tried to use your code, but it works the same. I will attach image to make myself more clear. I ve added a comment to the story, but the format was broken in the history (red arrows on the bottom)

Is there any way to save the HTML format?

Thanks,

Alexander Bogin

JETI provides a list of templates which includes all the default JIRA email templates and ONE template which JETI created. This 'Defalut' JETI template incldued the code for accessing comments:

#foreach ($comment in $!publicComments)
													       <p style="border-bottom: 1px solid #DDD;>
													       	<p style="font-weight: bold;"><a href="$!baseurl/secure/ViewProfile.jspa?name=$!{comment.author}"><b>$!{comment.authorFullName}</b></a> - <b>$!{dateformatter.format($comment.created)}</b></p>
																	<p>$!{rendererManager.getRenderedContent("atlassian-wiki-renderer", $!comment.body, $issue.issueRenderContext)}</p>       	
													       </p>
												     #end

Which I used to create some customized templates such as this one which provides newest comment at the top and all other comments in descending order below:

#disable_html_escaping()
#parse("templates/email/html/includes/emailconstants.vm")
#parse("templates/email/html/includes/header.vm")
<tr valign="top">
    <td id="email-banner" style="padding:32px 32px 0 32px;">

        #if ($commentauthor)
             #set ($changelogauthorLink = "#authorlink2($commentauthor.name $linkstyle)")
        #else
            #set ($changelogauthorLink = "#authorlink2($remoteUser.name $linkstyle)")
        #end

        #set ($issueType = $issue.getIssueTypeObject())

        ##set ($issueLink = "#renderIcon(${issueType.iconUrlHtml} ${issueType.getNameTranslation($i18n)}) <a style='color:${textLinkColour};text-decoration:none;' href='${baseurl}/browse/${issue.getKey()}'>$issue.getKey()</a>")
        #set ($issueLink = "<a style='color:${textLinkColour};text-decoration:none;' href='${baseurl}/browse/${issue.getKey()}'>${issueType.getNameTranslation($i18n)} $issue.getKey()</a>")

        #emailbanner($commentauthor "email.event.activity.comment" $changelogauthorLink $issueLink "")
    </td>
</tr>

#if ($changelog || $comment)
<tr valign="top">
    <td id="email-fields" style="padding:0 32px 32px 32px;">
        <table border="0" cellpadding="0" cellspacing="0" style="padding:0;text-align:left;width:100%;" width="100%">
            <tr valign="top">
                <td id="email-gutter" style="width:64px;white-space:nowrap;"></td>
                <td>
                    <table border="0" cellpadding="0" cellspacing="0" width="100%">
                        #parse("templates/email/html/includes/fields/comment.vm")
                        #parse("templates/email/html/includes/fields/changelog.vm")
                    </table>
                </td>
            </tr>
        </table>
    </td>
</tr>
#end
												
#if ($!emailDef.emailOptions.addComments)
#set ( $rSorter = $!publicComments.size() - 2 ) ##2 = dont include latest comment in history as it is highlighted at the very top.
	#if($rSorter >= 0)
		<tr valign="top">
			<td id="email-fields" style="padding:0 32px 32px 32px;">
				<table border="0" cellpadding="0" cellspacing="0" style="padding:0;text-align:left;width:100%;" width="100%">
					<tr valign="top">
						<td id="email-gutter"></td>
						<td style="color:${textColour};font-family:${textFontFamily};font-size:${textSize};padding:0 0 10px 0;width:100%;">
							#foreach($d in [$rSorter .. 0])
								#set ( $c = $!publicComments.get($d) )
								<div style="border-bottom: 1px solid #DDD;">
										<b>$!{c.authorFullName}</b> - $c.getCreated()</div>
								<p>$!{c.body}</p>
							#end
						</td>
					</tr>
				</table>
			</td>
		</tr>
	#end
#end


            </table>
        </td><!-- End #email-page -->
    </tr>
    <tr valign="top">
        <td style="color:${textSubtleColour};font-family:${textFontFamily};font-size:10px;line-height:14px;padding: 0 16px 16px 16px;text-align:center;">
		-! To add detail (comments or file attachements) simply reply to this email without changing subject line !-<br />
		!! Any content following "From: " will be omitted !!<br />
		this message was automatically generated</td>
    </tr>
</table><!-- End #email-wrap -->
</div><!-- End #email-body -->

Hello Taber,

Thanks a lot for your post! I was looking a lot the solution to add all comments to the email! Unfortunatelly, I am not experienced at Velocity template engine, could you tell me how to save the format of initial comment? Now it looks like: "Problem is solved Kind regards..." , when initial comment was made with separation for lines.

Thanks in advance!

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Oct 31, 2018 in Marketplace Apps

Marketplace Spotlight: Zephyr

Hello Atlassian Community! Each month, we run a series of Spotlights to highlight Marketplace vendors and apps that our team thinks this Community would find valuable. In last month's Spotlig...

346 views 0 1
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