how a display a hyperlink in a table in script field

jane chen November 14, 2019

I followed the method in this wiki page to show a table in script field: https://scriptrunner.adaptavist.com/latest/jira/recipes/misc/table-custom-field.html

but the table just show text. I also want the issue key in the table is a hyperlink to be clicked. 

so I tried to return a html format of hyperlink, it can be displayed as a hyperlink

def hyperlink = "<a href=\""+browseurl+issue.key+"\">"+issue+"</a>";
log.info(hyperlink)
return hyperlink

but when I input this information into a table (as code below), it displays as text.

def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter())

log.debug("Total issues: ${results.total}")

def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
def browseurl = com.atlassian.jira.component.ComponentAccessor.getApplicationProperties().getString("jira.baseurl")+"/browse/"

if (results.total < 1)
{
xml.string("Nothing required.")
}
else
{
xml.style(type: "text/css",
'''
#scriptField, #scriptField *{
border: 1px solid black;
}

#scriptField{
border-collapse: collapse;
}
''')

xml.table(id: "scriptField") {
tr {
th("Key")
th("Summary")
th("Status")
}
results.getIssues().each { documentIssue ->
log.debug(documentIssue.key)
// if you need a mutable issue you can do:
def issue_i = issueManager.getIssueObject(documentIssue.id)
// do something to the issue...
log.debug(issue_i.key)
//issue_i hyperlink
def ikey = issue_i.key;
def issue_i_link = "<a href=\""+browseurl+ikey+"\" >"+ikey+"</a>";
tr {
td(issue_i_link)
td(issue_i.summary.toString())
td(issue_i.status.getName())
}
}
}
}
return (writer.toString())

 what is displayed: 

<a href="https://xxx/xx">issue</a>

2 answers

1 accepted

1 vote
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 15, 2019

You need to either build the URL using the markup syntax or use mkp.yieldUnescaped to have the html interpreted. Read more about the markup builder: http://groovy-lang.org/processing-xml.html#_markupbuilder 

xml.table(id: "scriptField") {
tr {
th("Key")
th("Summary")
th("Status")
}
results.getIssues().each { documentIssue ->
def issue_i = issueManager.getIssueObject(documentIssue.id )

def ikey = issue_i.key;
tr {
td{
a(href: "/browse/$ikey") ikey
}
td(issue_i.summary.toString())
td(issue_i.status.getName())
}
}
}

or

td( mkp.yieldUnescaped(issue_i_link) )

 BTW, if you want to create a link to jira to be displayed in jira...  and you don't have a context path in your environment, you don't need to lookup the base url, you can just use an absolute path starting at root : "/browse/ABC-123" will work. If you have a context path, you can either hard code that or look it up.

Also, with groovy, you can make strings lots easier to read by using triple quoted strings. With those, you don't need to escape quotes:

def longComplexString = """ this string has unescaped "double quotes" """
jane chen November 17, 2019

Thanks for your answer. that helps a lot!

0 votes
jane chen November 17, 2019

Thanks for your answer. that helps a lot!

Suggest an answer

Log in or Sign up to answer