Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Jira Service Desk Scriptrunner - change collaborator comment to external

Jon Starbird
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.
February 7, 2019

Using examples from the Scriptrunner site I have the following code, it runs just fine but the comments are still not showing up on the Portal. 
We need to be able to do this otherwise Service Desk is useless for collaborative work within our company. As is, Agents will have to Copy collaborator comments and re-add them as new comments just to make them show up or programatically do it, either way you end up with duplication which is pointless.

 

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.security.roles.ProjectRoleManager
import com.atlassian.jira.util.json.JSONObject
import groovy.json.JsonSlurper
import org.apache.log4j.Logger
import org.apache.log4j.Level

final SD_PUBLIC_COMMENT = "sd.public.comment"

def projectManager = ComponentAccessor.projectManager
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)

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

def event = event as IssueEvent
def user = event.getUser()
def projectRole = projectRoleManager.getProjectRole('Service Desk Collaborators')
if (projectRoleManager.isUserInProjectRole(user,projectRole,event.issue.getProjectObject())) {
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 curAssignee = event.issue.getAssignee()
def agentUser = curAssignee
log.info "Assignee - ${curAssignee}"
log.info "original user - ${originalUser}"
jiraAuthenticationContext.setLoggedInUser(agentUser)

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

}
} finally {
jiraAuthenticationContext.setLoggedInUser(originalUser)
}
}

 

2 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Alejandro Suárez - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
February 11, 2019

Hi @Jon Starbird i did a few testing about this. It seems that the public comment are only allowed to user having Service Desk Team role or Administrator role and only if the user have an Jira Service Desk licenses. That means that if an Service Desk Agent o Project Admin  (who have this kind of license) tries to update comments from users who dont have this roles in the project, even manually, it wont change to Public.

The only work around I found, is deleting the comment added by the Collaborator, and create new one with the agent user.

This is the code:

import com.atlassian.jira.bc.issue.comment.property.CommentPropertyService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.entity.property.EntityProperty
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.security.roles.ProjectRole
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.util.json.JSONObject
import groovy.json.JsonSlurper

ProjectRoleManager projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)

IssueEvent event = event as IssueEvent
ApplicationUser user = event.getUser()
ProjectRole projectRole = projectRoleManager.getProjectRole('Service Desk Collaborators')

if (projectRoleManager.isUserInProjectRole(user, projectRole, event.issue.getProjectObject())) {

final String SD_PUBLIC_COMMENT = "sd.public.comment"

CommentPropertyService commentPropertyService = ComponentAccessor.getComponent(CommentPropertyService)
CommentManager commentManager = ComponentAccessor.getCommentManager()
Comment comment = event.getComment()
ApplicationUser curAssignee = event.issue.getAssignee(),
agentUser = curAssignee

Closure isInternal = { Comment c ->
EntityProperty 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)) {

LinkedHashMap<String, JSONObject> properties = [(SD_PUBLIC_COMMENT): new JSONObject(["internal": false])]
commentManager.delete(comment)
String newComment = comment.authorFullName + " wrote: \n" + comment.getBody()
commentManager.create(event.issue, agentUser, newComment, null, null, new Date(), properties, true)

}

}

  I created a new comment adding the original author to have better visibility.

Hope it helps, is the better I could think to solve it.

Regards

Jon Starbird
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.
February 11, 2019

thanks. Appreciate the help.

Wish there was a better solution but this will serve the purpose.

Alejandro Suárez - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
February 11, 2019

Sorry for the edits, but i found that even if the users are in that roles, if they dont have Service Desk license, you cant change it to public.

You can test it trying to manually update a comment with an agent from a non agent.

Jon Starbird
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.
February 11, 2019

sorry for the accept/unaccept/accept again there. Was having an issue with the browser and my mouse and got some clicks unintentionally in there.

Anyway, I ran this and it didn't work. The assignee is definitely an agent and the event user is a collaborator but it just doesn't work.

I don't get any errors, just doesn't do the delete and re-add. 

The agent user has permissions to delete all comments and the collaborators can delete their own comments,  checked that just in case.

Jon Starbird
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.
February 12, 2019

@Alejandro Suárez - TecnoFor  I marked unaccepted in case you had stopped watching the issue. 

As I noted above the code isn't working. I did note an additional oddity besides what I said above. It appears to be triggering the Comment event again as emails go out about the comment being added twice. But in both cases it's the same owner of the comment, the collaborator. In the Jira it doesn't show an extra comment. 

0 votes
Alejandro Suárez - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
February 12, 2019

Hi @Jon Starbird, In my tests I created the role "Service Desk Collaborator" and added a random user to it. This user only have a Jira Software license.

The agent is in the Service Desk Agent Role, having a Service Desk license, and I added him to the assignee field.

With the same script I posted it worked for me adding a comment with the collaborator user. I asume that the agent has the create comment permission.

Jon Starbird
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.
February 12, 2019

yes that is exactly what I have as well and yes I did verify all the permissions. 

The roles already exist but the users are part of them, via Jira groups.

Did you have it triggering on the Issue Comment or were you using the Service Desk Comment event? 
I am using the Issue Comment event.

Alejandro Suárez - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
February 12, 2019

Im using the Issue Comment as well, this is my listener.

Here is my listener: 
https://gyazo.com/5dfa3f4564468c03b763405b6ac42011 

This i how i configured the roles:

https://gyazo.com/dce27210165aab7efdb736516f6543e1

And finally this is my permission scheme:

https://gyazo.com/c9bc3e4e24485482c015212962a11034

And this is the result:

https://gyazo.com/de4f43aac2383043bc3ecb319daa540c

Edit:

This is the ticket in the portal:

https://gyazo.com/99efc174abe64e65e740853f588004b7

Jon Starbird
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.
February 12, 2019

that is exactly how I have it setup as well. I just used the code you gave me, didn't modify it at all.

The only difference is that for the Roles I have groups assigned there not specific users. But the users involved are in the appropriate groups.

The Agent is valid, he works other tickets. The collaborator is valid otherwise he couldn't comment on an issue from within Jira itself which is where I had him doing it from like our devs would.

We are using Jira Server 7.10.2 and Service Desk 3.13.2.

Alejandro Suárez - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
February 12, 2019

Hi @Jon Starbird I just did the same, and added te same users to different groups, and then added the groups to corresponding project roles.

It's working for me. Im using Jira Server 7.12.3 and Service Desk 3.15.3 and I dont know if thats relevant or not in this case. 

I'm sorry I can't help you further :(

Jon Starbird
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.
February 12, 2019

@Alejandro Suárez - TecnoFor understood. Thanks for all your help.

Jon Starbird
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 25, 2019

@Alejandro Suárez - TecnoFor I've finally gotten our test server upgraded and we are now on Jira 8.1.0 and Service Desk 4.1.0 but this code still isn't working.

For some reason it isn't getting the comment properties so it cannot tell if it is Internal. I've added some debug statements and it is at that point it just isn't getting anything back for the properties.

I also tried changing it to use the agentuser to read the properties but that also didn't work.  

I did look at Adaptavist's site and the code is basically what you have so not sure if something has changed in the API's.

Jon Starbird
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 25, 2019

I found the issue. By default Collaborator comments are not marked as Internal so the value is Null preventing your code from working.

What I did was just check for the user to be a collaborator and then also checked to make sure they are not the reporter. Then do the rest skipping the check for internal or not. 

I find it odd that Service Desk doesn't mark Collaborator comments as Internal but perhaps there is a different flag there to be used. 

Overall I find Service Desk to be extremely flawed and it just make it useless to any company wanting to use it internally only, so pretty much any IT/IS desk IMO. I think it may work fine for anyone using it for external uses for customer support but they should quit trying to sell it as an IT/IS service desk solution, it isn't.

I've wasted so much time just hacking around it's inadequacies.

TAGS
AUG Leaders

Atlassian Community Events