Scripting Service Desk
Here are some limited examples for scripting JIRA Service Desk. Please let us know what you require…
Getting Comment Visibility Levels
Service Desk uses entity properties for whether the comment is internal or not, amongst other things.
This is fortunate as it means we don’t need to use the JSD API directly.
From a workflow function
The following code will get the visibility of a comment this transition, and is suitable to be used for the condition field of validators and post-function built-in scripts (such as Sending a Custom Email):
import groovy.json.JsonParserType
import groovy.json.JsonSlurper
def commentProperties = transientVars["commentProperty"] as String[]
def isInternalComment = false
if (commentProperties) {
def commentProperty = commentProperties.first()
def props = new JsonSlurper().setType(JsonParserType.LAX).parseText(commentProperty)
isInternalComment = props.find {it.key == "sd.public.comment"}?.get("value")?.get("internal")
}
return isInternalComment.toBoolean()
From an event listener
Given an event listener listening for the Issue Commented event, the following code will yield the visibility level of the comment just added:
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.issue.comments.Comment
import groovy.json.JsonSlurper
final SD_PUBLIC_COMMENT = "sd.public.comment"
def event = event as IssueEvent
def user = event.getUser()
def comment = event.getComment()
def commentPropertyService = ComponentAccessor.getComponent(CommentPropertyService)
def isInternal = { Comment c ->
def commentProperty = commentPropertyService.getProperty(user, c.id, SD_PUBLIC_COMMENT)
.getEntityProperty().getOrNull()
if (commentProperty) {
def props = new JsonSlurper().parseText(commentProperty.getValue())
props['internal'].toBoolean()
}
else {
null
}
}
if (comment) {
return isInternal(comment)
}
return false
Reusing this isInternal
closure above, you can find all internal/external comments etc:
// Example: find all _external_ comments on issue
log.debug commentManager.getComments(issue).findAll { ! isInternal(it) }
// iterate comments and check external property
commentManager.getComments(issue).each {
log.debug("Comment on issue ${issue.key}, id: ${it.id}, internal: ${isInternal(it)}")
}
Creating Internal Comments
You can use the following to create an internal comment. If using a workflow function the user executing the function is passed to the script as currentUser
:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.util.json.JSONObject
final SD_PUBLIC_COMMENT = "sd.public.comment"
def commentManager = ComponentAccessor.getCommentManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def properties = [(SD_PUBLIC_COMMENT): new JSONObject(["internal": true])]
commentManager.create(issue, user, "my internal comment", null, null, new Date(), properties, true)