Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Script to change the comment visibility of an issue

Wim Abts
Contributor
April 28, 2021

Hi,

 

Looking for a script to change the comment visibility of an issue in JSD.

The comments need to be hidden from the customer after doing a migration and import via csv file.

 

Thanks!

3 answers

0 votes
Wim Abts
Contributor
April 29, 2021

This one did it for me:

 

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.attachment.Attachment
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.search.SearchQuery
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.jql.parser.JqlQueryParser;
import com.atlassian.jira.issue.search.SearchResults;
import com.atlassian.jira.issue.search.SearchProvider;
import com.atlassian.jira.web.bean.PagerFilter;
import com.atlassian.jira.issue.AttachmentManager;
import com.atlassian.jira.entity.property.JsonEntityPropertyManager

def findIssues(String jqlQuery) {
def issueManager = ComponentAccessor.issueManager
def user = ComponentAccessor.getUserManager().getUserByKey("no-reply")
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser.class)
def searchService = ComponentAccessor.getComponent(SearchService)
def query = jqlQueryParser.parseQuery(jqlQuery)
def results = searchService.search(user, query, PagerFilter.getUnlimitedFilter())
results.getResults()
}
def user = ComponentAccessor.getUserManager().getUserByKey("no-reply")
long INTERNAL_USERS_ROLE_ID = 10000l;
def jqlQuery = "project = sd10 and status = 'In Progress'"
def issues = findIssues(jqlQuery)
def commentManager = ComponentAccessor.getCommentManager()
def jsonManager = ComponentAccessor.getComponent(JsonEntityPropertyManager)


issues.each{issue ->
log.error("Changing comment visibility on $issue.")
// Get all comments
def comments = commentManager.getComments(issue);

for (def comment : comments) {
jsonManager.put(user, "sd.comment.property", comment.getId(), "sd.public.comment", "{ \"internal\" : true}" , (java.util.function.BiFunction) null, false)
}
}

0 votes
Wim Abts
Contributor
April 29, 2021

Hi,

Thanks for you reply, however, my problem is different in this because we need to change already existing comments.

These comments are created when importing issues via the CSV import and there it's not possible to set it comment to internal only.

 

So how can we change already existing comments using a groovy script?

0 votes
Tarik Ljouad April 29, 2021

We run across a very similar issue and the resolution/script was provided by Andy Heinzer in this thread.

Basically: 

  • Is it not possible to edit a comment once it is entered in JSM through a Rest API Call (which is what automations use to operate on JSM). So you need to control the visibility of the comment when entering it. Refer to this thread.
  • You can either enter a comment as a separate action through the automation UI, which allows you to set the comment visibility as follows: 
    comment visibility.png

  • Or use an API call to set your comment visibility for a JSM issue which looks like this:
    {
    "update": {
    "comment": [
    {
    "add": {
    "public": false,
    "body": "Ticket automatically resolved upon creation"
    }
    }
    ]
    }
    }
  • Nevertheless, if you are trying to set the visibility of the comment while entering it during an issue transition, or if you are handling issue in a non-JSM project, then you can't set the comment visibility via API calls. The only workaround is to add a public comment, and reduce it's visibility to a specific Jira Role/Group so other users cannot see it (in my case I reduce it to the Administrators role). The API call would look somehow like this:
    {
    "update": {
    "comment": [
    {
    "add": {
    "body": "Thanks for raising {{issue.key}}.",
    "visibility": {
    "type": "role",
    "value": "Developers"
    }

    }
    }
    ]
    }
    }

    In this case, as Andy mentioned as well, you'd need to update the actor of your automation to a user that belongs to the Role/Group you are restricting the comment visibility to. The reason is that the standard automation user "Automation for Jira" is not part of that role and therefore cannot publish restricted comments to that role.

I hope this helps. Let me know if it doesn't 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events