We're using this script to set up a custom field called Last comment (script field):
/**
* Field name: Last Comment
* Template: Text Field (multi-line)
*
* Description:
* Returns the last comment on an issue.
*
*/
import com.atlassian.jira.component.ComponentAccessor
// get the latest comment
def commentManager = ComponentAccessor.getCommentManager()
def comment = commentManager.getLastComment(issue)
// check to see if comments exist - return comment body if so
if (comment != null) {
return comment.body
}
return null
This returns the last comment, whether it's internal or external.
We need this script to return ONLY the last EXTERNAL comment.
I found this link; https://community.atlassian.com/t5/Jira-Service-Management/Scriptrunner-Latest-public-comment-in-custom-email/qaq-p/1406868
But we get an error:
groovy.lang.MissingPropertyException: No such property: transientVars for class: Script8473 at Script8473.run(Script8473.groovy:7)
How can we set transientVars as a script field?
For your requirement, it would be best to use the ScriptRunner Listener with the Issue Commented event.
Below is a working sample code for your reference:-
import com.atlassian.jira.bc.issue.comment.property.CommentPropertyService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.comments.Comment
import groovy.json.JsonSlurper
final SD_PUBLIC_COMMENT = 'sd.public.comment'
def issue = event.issue as MutableIssue
def issueEvent = event as IssueEvent
def user = issueEvent.user
def comment = issueEvent.comment
def customFieldManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def commentPropertyService = ComponentAccessor.getComponent(CommentPropertyService)
def sampleMultiLine = customFieldManager.getCustomFieldObjectsByName('Sample Multi Line Text').first()
def isInternal = { Comment c ->
def commentProperty = commentPropertyService.getProperty(user, c.id, SD_PUBLIC_COMMENT).entityProperty.orNull
if (commentProperty) {
def props = new JsonSlurper().parseText(commentProperty.value) as Map
props['internal'].toString().toBoolean()
}
else {
null
}
}
if (comment && !isInternal(comment)) {
issue.setCustomFieldValue(sampleMultiLine, comment.body)
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}
return false
Please note that the sample code provided is not 100% exact to your environment. Hence, you will need to make the required modifications.
Below is a screenshot of the Listener Configuration for your reference:-
Below are a few test screenshots for your reference:-
1. When I first add a new public comment, the comment is copied to the text field as shown below:-
2. If I try to include a private comment, the text field is not updated as expected
3. If I add a new public comment, the text field is updated as expected.
I hope this helps to answer your question. :)
Thank you and Kind Regards,
Ram
Hi Ram,
I've implemented this code as a listener and it seems to do exactly as we require.
Thanks so much for your help!
Kind regards,
Sam
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.