(Scriptrunner) Get last comment for link in Web UI

Jordan Berry October 14, 2019

page link

In the last field on this screenshot, you can see ${issue.summary}, and I have been able to do things like ${issue.description} but I am looking to get the last comment made. Is this possible, or is there a list of all fields accessible this way?

 

Alternatively, when an agent is making a comment in service desk, is there a way to automatically append a link at the end of the comment? Is this possible with either built in workflows, Automation for Jira, or ScriptRunner?

2 answers

1 accepted

0 votes
Answer accepted
Ravi Sagar _Sparxsys_
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.
October 15, 2019

Hi @Jordan Berry 

Here is what you can do.

First create a REST Endpoint with ScriptRunner. 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.sal.api.ApplicationProperties
import com.atlassian.sal.api.UrlMode
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import groovy.transform.BaseScript
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.user.ApplicationUser
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response
import java.net.URLEncoder

@BaseScript CustomEndpointDelegate delegate

def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.jiraAuthenticationContext?.loggedInUser

searchGoogle(httpMethod: "GET") { MultivaluedMap queryParams ->

def issueId = queryParams.getFirst("issueId") as Long
def issue = issueManager.getIssueObject(issueId)

CommentManager commentMgr = ComponentAccessor.getCommentManager()
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().loggedInUser

def lastComment = commentMgr.getComments(issue)?commentMgr.getComments(issue).last().body: ""
def encodedComment = java.net.URLEncoder.encode(lastComment, "UTF-8")
Response.temporaryRedirect(URI.create("https://google.com/search?q=${encodedComment}")).build()


}

 Then in your Custom web item use the following in the Link field.

/rest/scriptrunner/latest/custom/searchGoogle?issueId=${issue.id}

The purpose of the custom REST end point is to do further processing like fetching the last comment and then when you call the rest url with the issue id as a parameter then the last comment is fetched, encoded and redirected to Google. 

I hope it was helpful. Let me know.

Ravi

Jordan Berry October 16, 2019

This worked like a charm. Thank you!

Like Ravi Sagar _Sparxsys_ likes this
0 votes
Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 14, 2019

Hi @Jordan Berry 

Those properties that can be accessed via issue.* are the issue fields. However comment is not an issue field. As you know, there might be multiple comments and you can't access the latest one without code or a third party app which gives you the last comment as a custom field.

You can write your own Last comment scripted custom field via Script runner, or you can use one of the comment custom field apps in Atlassian marketplace.

Since I am one of the folks behind Snapbytes, I can recommend Enhancer Plugin's Comment custom field so that you can define Last Comment custom field easily, then use it in script runner as $issue.getCustomFieldValue($customFieldManager.getCustomFieldObject("...."))

But, I don't think it is a good idea to use comment content in url because comment might contain illegal characters which should be Url encoded first. Also it can be too long for the Url that might be truncated in some systems.

I hope I was clear

Tuncay

Jordan Berry October 14, 2019

Hey Tuncay,

While I appreciate the answer, I feel this should be possible with the add-ons that we have already purchased

Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 15, 2019

Great :)

Suggest an answer

Log in or Sign up to answer