Change an internal comment to external/shared with customer in Service Desk Using scriptrunner

Amro Hassaan
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.
April 5, 2017

I need to write a scriptrunner listener that will take internal comments made by certain group and change it to external/shared with customer comment. I used examples from scriptrunner and wrote this listener, but it doesn't work. Can you please help as to what i am need to do /am missing?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.index.IssueIndexingService
//import com.atlassian.jira.util.json.JSONObject
import com.atlassian.jira.entity.property.JsonEntityPropertyManager
import 	com.atlassian.jira.util.json.JSONObject
import org.apache.log4j.Category

def iis = ComponentAccessor.getOSGiComponentInstanceOfType(IssueIndexingService.class)

final SD_PUBLIC_COMMENT = "sd.public.comment"

def Category log = Category.getInstance("com.onresolve.jira.groovy")
log.setLevel(org.apache.log4j.Level.DEBUG)
def gm = ComponentAccessor.groupManager
def issue = event.issue
def comment = event.comment
if (issue.projectObject.key.equalsIgnoreCase("ctt")) {
    def user = event.comment.authorApplicationUser
    if (!gm.isUserInGroup(user, "projects-ctt-agents")){
        def commentManager = ComponentAccessor.getCommentManager()
        def properties = [(SD_PUBLIC_COMMENT): new JSONObject(["internal": false])]
        //def jsonManager = ComponentAccessor.getComponent(JsonEntityPropertyManager)
        //jsonManager.put(user, "sd.comment.property", comment.getId(), properties , (java.util.function.BiFunction) null, false)
        commentManager.update(event.comment, properties, true)
        iis.reIndex(issue)
    }

}




1 answer

1 accepted

2 votes
Answer accepted
adammarkham
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.
April 6, 2017

Nice attempt. I see you have found some of the examples from our documentation.

The code below will work for a custom scripted listener:

 

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 com.atlassian.jira.util.json.JSONObject
import groovy.json.JsonSlurper

final SD_PUBLIC_COMMENT = "sd.public.comment"

def commentPropertyService = ComponentAccessor.getComponent(CommentPropertyService)
def commentManager = ComponentAccessor.getCommentManager()

def event = event as IssueEvent
def user = event.getUser()
def comment = event.getComment()

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 && isInternal(comment)) {
    def properties = [(SD_PUBLIC_COMMENT): new JSONObject(["internal": false])]
    commentManager.update(comment, properties, true)
}

It was a case of putting together the two examples from the documentation here and here. Finally adding false to update to an external comment like you had in your example.

 

Let us know how that goes for you.

 

Amro Hassaan
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.
April 6, 2017

Nope..didn't work..same behaviour as when i ran my code. Internal comment stays internal. So I am trying to find users who don't belong to the agents group (when they comment on the request from the issue view page from within JIRA they only have "internal comment" option due to limitation on Service Desk) and then change their internal comments to external. here is my code with the user checking bit.

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 com.atlassian.jira.util.json.JSONObject
import com.atlassian.jira.issue.index.IssueIndexingService
import groovy.json.JsonSlurper

final SD_PUBLIC_COMMENT = "sd.public.comment"

def commentPropertyService = ComponentAccessor.getComponent(CommentPropertyService)
def commentManager = ComponentAccessor.getCommentManager()
def iis = ComponentAccessor.getOSGiComponentInstanceOfType(IssueIndexingService.class)
def event = event as IssueEvent
def issue = event.getIssue()
def user = event.getUser()
def comment = event.getComment()
def gm = ComponentAccessor.groupManager

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 (issue.projectObject.key.equalsIgnoreCase("ctt")) {    
    if (!gm.isUserInGroup(user, "projects-ctt-agents") && comment && isInternal(comment)){        
        def properties = [(SD_PUBLIC_COMMENT): new JSONObject(["internal": false])]
        commentManager.update(comment, properties, true)
        iis.reIndex(issue)
    }

}


adammarkham
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.
April 7, 2017

Hi Amro,

Could you use just my example for the event "Issue Commented" which needs to be selected for the Service Desk project. You shouldn't need to reindex the issue.

Once that is working then we can try the group check.

If you've already tried that then what version of Service Desk, JIRA and ScriptRunner do you have?

Amro Hassaan
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.
April 7, 2017

tried your code again now, and it works only when the comment author is a one who has Service Desk License or an agent or admin in that project. I have a bunch of users who don't have service desk license and are only customers but need to have internal view of the tickets (not from the customer portal only) in order to verify priority queue (they are client stakeholders and they wanna see their issues from within JIRA to know relative priority to others), these guys have view only permissions to the tickets from within JIRA but they can also comment. ATM, due to limitation of Service Desk, we can only see "Internal Comment" option available to them and this is why i want to override that behaviour and re-route all their comments to the customer portal. This is the story behind this attempt for a workaround. So how can this be done in this case?

adammarkham
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.
April 7, 2017

Thanks for the explanation, that's interesting. I can't think of a good way to do this because of permissions issues. Unless you can somehow change the user that updates the comment to be an agent.

You can try this by using the following script which switches to an agent to do the update: 

 

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 com.atlassian.jira.util.json.JSONObject
import groovy.json.JsonSlurper

final SD_PUBLIC_COMMENT = "sd.public.comment"

def commentPropertyService = ComponentAccessor.getComponent(CommentPropertyService)
def commentManager = ComponentAccessor.getCommentManager()

def event = event as IssueEvent
def user = event.getUser()
def comment = event.getComment()

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
    }
}

def jiraAuthenticationContext = ComponentAccessor.getJiraAuthenticationContext()
def originalUser = event.getUser()
try {
    def userManager = ComponentAccessor.getUserManager()
    def agentUsername = "agent"

    jiraAuthenticationContext.setLoggedInUser(userManager.getUserByName(agentUsername))

    if (comment && isInternal(comment)) {
        def properties = [(SD_PUBLIC_COMMENT): new JSONObject(["internal": false])]
        commentManager.update(comment, properties, true)
    }
} finally {
    jiraAuthenticationContext.setLoggedInUser(originalUser)
}

If that doesn't work then it's likely we can't work around the permissions issue.

Like desarrollo likes this
Amro Hassaan
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.
April 7, 2017

Adam, Thank you very much for confirming my realization it is a permission's issue that cannot be overcome unless with a twist. Thanks for the script you provided. So I have resolved to revoking perimssions for those stakeholders to comment from the internal view so they will only have to do from the customer view. I think your script will do the trick but that won't be worth it :)

They should all learn to go to the customer portal :D

THanks again Adam!!

Amro Hassaan
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.
April 14, 2017

By the way Adam, the sample code you have on scriptrunner wiki to create an internal comment, creates the comment as external always despite internal is set to true!!!...I don't know if someone caught this or not yet.

How can this be fixed to create internal comment??

Thanks in advance

Like Jahir H. likes this
Jahir H. October 4, 2023

Can't change customer comment property using the script, any thought please.

 

import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.bc.issue.comment.property.CommentPropertyService
import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.comments.Comment

import com.atlassian.jira.util.json.JSONObject

import com.atlassian.jira.user.ApplicationUser

import com.atlassian.jira.issue.Issue

import org.apache.log4j.Logger


def
logger = Logger.getLogger("Comment property change")

final SD_PUBLIC_COMMENT = "sd.public.comment"


def
commentPropertyService = ComponentAccessor.getComponent(CommentPropertyService)

def commentManager = ComponentAccessor.getCommentManager()

def issueManager = ComponentAccessor.getIssueManager()

Issue issueObject = issueManager.getIssueObject("Temp-12345")


def
comment = commentManager.getLastComment(issueObject)

ApplicationUser appUser = issueObject.getReporterUser() //getAuthorApplicationUser()

def authorEmail = comment.getAuthorApplicationUser().getEmailAddress()

def commentBody = comment.getBody()

def commentProperty = commentPropertyService.getProperty(appUser, comment.id, SD_PUBLIC_COMMENT).getEntityProperty()

logger.debug ("Comment Property: ${commentProperty.get()}")


if
(comment) {

def properties = [(SD_PUBLIC_COMMENT): new JSONObject(["internal", true])]

commentManager.update(comment, properties, true)

logger.debug("Update End")

}

 

Property value before updating: key=sd.public.comment,value={"internal":true}

Property value before updating : key=sd.public.comment,value={}

Comment has a tag with Internal and the comment is not available in customer portal. So the comment property 'internal' is false but the comment is not share with customer. That's the issue here. 

Jahir H. October 4, 2023

@Amro Hassaan Yes, It tested that .

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events