HTML Encode In Scripted Field

Paul Tiseo
Contributor
February 11, 2019

I have a scripted field where I display the Summary. If the Summary has a doubel-quote, my display breaks. How do I HTML encode the string? (Sorry, but not sure how to do this in Groovy in this constrained script engine environment.)

2 answers

0 votes
Nir Haimov
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 12, 2019

Hi @Paul Tiseo,

As @Alejandro Suárez García suggested, please post your code.

Also please mention your Jira version and ScriptRunner version.

I tried and everything works fine...

2019-02-12_11-24-17.png

0 votes
Alejandro Suárez García
Atlassian Partner
February 11, 2019

Hi @Paul Tiseo I need more information to help you with this. Could you post the Scripted Field code?

Paul Tiseo
Contributor
February 12, 2019

In the code below, the problem is in the linkedIssue.getSummary() being added to the title tag in an HTML field. If the Summary has a double-quote, it prematurely ends the attribute's value. I should probably just do a replace, but I was wondering how to HTML encode if I needed to.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.user.ApplicationUser

if(issue.getIssueType().getName() != 'Epic') {
    return
}

UserManager userManager = ComponentAccessor.getUserManager()
ApplicationUser user = userManager.getUserByName('sysadmin')

IssueLinkManager issueLinkManager = ComponentAccessor.issueLinkManager
def linkedIssues = issueLinkManager.getLinkCollection(issue, user, false).getAllIssues()

def sb = new StringBuilder()
for (linkedIssue in linkedIssues) {
    if (!linkedIssue.getResolution()) {
        sb <<= '<span style="white-space: nowrap"><a href="https://projects.jtafla.com/browse/'
        sb <<= linkedIssue.getKey()
        sb <<= '" title="Summary: '
        sb <<= linkedIssue.getSummary()
        sb <<= '\nType: '
        sb <<= linkedIssue.getIssueType().getName()
        sb <<= '\nAssignee: '
        sb <<= linkedIssue.getAssignee()
        sb <<= '\nResolution: '
        sb <<= linkedIssue.getResolution()
        sb <<= '">'
        sb <<= linkedIssue.getKey()
        sb <<= '</a> ('
        sb <<= linkedIssue.getStatus().getName()
        sb <<= ')</span><br />'
    }
}

return sb
Alejandro Suárez García
Atlassian Partner
February 12, 2019

Hi @Paul Tiseo, you can escape that quotes in java with the "org.apache.commons.lang.StringEscapeUtils.escapeHtml" class. 

Here is the code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.user.ApplicationUser
import static org.apache.commons.lang.StringEscapeUtils.escapeHtml

if (issue.getIssueType().getName() != 'Epic') {
return
}

UserManager userManager = ComponentAccessor.getUserManager()
ApplicationUser user = userManager.getUserByName('sysadmin')

IssueLinkManager issueLinkManager = ComponentAccessor.issueLinkManager
Collection<Issue> linkedIssues = issueLinkManager.getLinkCollection(issue, user, false).getAllIssues()

StringBuilder sb = new StringBuilder()
linkedIssues.findAll { !it.getResolution() }.each {
Issue linkedIssue ->

sb <<= '<span style="white-space: nowrap"><a href="https://projects.jtafla.com/browse/'
sb <<= linkedIssue.getKey()
sb <<= '" title="Summary: '
sb <<= escapeHtml(linkedIssue.getSummary())
sb <<= '\nType: '
sb <<= linkedIssue.getIssueType().getName()
sb <<= '\nAssignee: '
sb <<= linkedIssue.getAssignee()
sb <<= '\nResolution: '
sb <<= linkedIssue.getResolution()
sb <<= '">'
sb <<= linkedIssue.getKey()
sb <<= '</a> ('
sb <<= linkedIssue.getStatus().getName()
sb <<= ')</span><br />'

}

return sb

 

Like pemontto likes this
Alejandro Suárez García
Atlassian Partner
February 12, 2019

@Paul TiseoAditionally if you want to display only the assignee "Display Name" you have to replace

linkedIssue.getAssignee()

for:

linkedIssue.getAssignee()?.getDisplayName()

 This way if the variable returns null it'll display null but if assignee have an user it will display only the Full Name.

Hope it helps!

Regards

Suggest an answer

Log in or Sign up to answer