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

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

1 vote
Accepted answer

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.

 

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

}


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?

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?

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.

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

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Oct 31, 2018 in Marketplace Apps

Marketplace Spotlight: Zephyr

Hello Atlassian Community! Each month, we run a series of Spotlights to highlight Marketplace vendors and apps that our team thinks this Community would find valuable. In last month's Spotlig...

300 views 0 1
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you