Internal Comment count with scriptrunner?

Yousaf November 29, 2017

Hello,

 

currently i´m trying to get Jira to show me the number of internal comments in each issue. I tried running scriptrunner and entered a code for the general comment count (internal and external).

 

However it doesn´t work, can someone please point out the mistake and maybe show also the way for internal comments.

 

import com.blablub.jira.component.ComponentAccessor
def commentManager = ComponentAccessor.getCommentManager()
def comments = commentManager.getComments(issue)
if (comments) {
comments.last().updated
}

 

 

Thanks ;)

3 answers

1 vote
Alexey Matveev
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.
November 29, 2017

Hello,

Have a look at the post below. You can see how to define if a comment is internal or not. Then you can count internal comments

https://community.atlassian.com/t5/Marketplace-Apps-questions/Change-an-internal-comment-to-external-shared-with-customer-in/qaq-p/459389

The required line is 

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
    }
}
Yousaf November 29, 2017

Hey Alexey,

 

I just tried the lines you shared but it doesn´t work. 

Following errors appear: unable to resolve class Comment in line 1, column 18. 

and unable to resolve class JsonSlurper in line 6, column 21.

 

Isn´t there a way to get the number of internal comments with the script of Nic? 

For example, instead of comments.size() it would be something like internalcomments.size ().. ? 

Alexey Matveev
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.
November 29, 2017

THe code would be like this. Change IssueKey to your issue key

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.Comment
import groovy.json.JsonSlurper
import com.atlassian.jira.bc.issue.comment.property.CommentPropertyService


final SD_PUBLIC_COMMENT = "sd.public.comment"
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
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())
if (props['internal']) {
true
} else {false}

}
else {
null
}
}

def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("IssueKey");
log.error(ComponentAccessor.getCommentManager().getComments(issue).findAll{isInternal(it)}.size());

 
Like Jacek Wolszczak likes this
Yousaf November 29, 2017

Great, thanks :) 

 

Can you just tell me where to find the issue key? Other than that it should work now..

Alexey Matveev
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.
November 29, 2017

Go to issue detail view and there will be project key then "-" and then number. 

Yousaf November 29, 2017

Hmm i´m not sure why, but this code does not work for me. Jira doesn´t show any error but when I do a search, no results are shown for internal comments.

 

For example when I search for an issue which has 30 comments, it says 30 comments (with the code from Nic), but the internal comments field is empty, not even a 0 is shown.

 

In the Scripts Field it says 15 of 15 failed, including the last one.

Alexey Matveev
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.
November 29, 2017

I guess you have a scripted field. Then try like this

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.Comment
import groovy.json.JsonSlurper
import com.atlassian.jira.bc.issue.comment.property.CommentPropertyService


final SD_PUBLIC_COMMENT = "sd.public.comment"
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
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())
if (props['internal']) {
true
} else {false}

}
else {
null
}
}

return ComponentAccessor.getCommentManager().getComments(issue).findAll{isInternal(it)}.size();

 
Yousaf December 4, 2017

Hey Alexey,

 

thanks for the code. We´re almost there.. It works well but i´m not sure why, but it shows strange results:

 

For example an issue has 20 comments in total. 10 of those are shown as internal. But from those 10 only 6 are really internal (literally written from people in this company and sent with the "Internal Comment" Button). The other 4 "internal" comments are just normal sent into the chat visible for customers and workers.

 

If that final problem would be fixed then the results should be just fine. 

Has it got to do with this line maybe?

findAll{isInternal(it)}

 

Thanks for all your help! ;)

Alexey Matveev
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.
December 4, 2017

Try like this

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.Comment
import groovy.json.JsonSlurper
import com.atlassian.jira.bc.issue.comment.property.CommentPropertyService


final SD_PUBLIC_COMMENT = "sd.public.comment"
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
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())
if (props['internal']) {
true
} else {false}

}
else {
false
}
}

return ComponentAccessor.getCommentManager().getComments(issue).findAll{isInternal(it)}.size();
Yousaf December 4, 2017

Hello Alexey, 

 

this code seems to work now. 

 

Thank you for your help! ;-)

Yousaf December 11, 2017

Hello Alexey, 

 

just one problem I found with this code. For some reason the result is always 1 comment too much. Like 10 comments, 5 are actually marked as internal but the result is shown as 6. 

 

Is there a way to put a "-1" into the code?

 

Thanks

Alexey Matveev
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.
December 11, 2017

It would be better if you tried to define which comment is counted as wrong. But you can substract 1 to the result like this

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.Comment
import groovy.json.JsonSlurper
import com.atlassian.jira.bc.issue.comment.property.CommentPropertyService


final SD_PUBLIC_COMMENT = "sd.public.comment"
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
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())
if (props['internal']) {
true
} else {false}

}
else {
false
}
}

return ComponentAccessor.getCommentManager().getComments(issue).findAll{isInternal(it)}.size() - 1;
Yousaf December 11, 2017

Yes, thats true its better to know the origin. I just tested a few tickets and saw that the first response also creates a comment as internal (even when I click on share with customer).

 

So there must be a way to ignore the first comment when opening a ticket and giving the comment to the customer (as it counts as internal for whatever reason). 

Yousaf December 11, 2017

So basically I need to know if its possible to change the following setting:

 

Where can I change the settings for first response comments? 

At the moment it seems to be set to count as internal although everyone including customers can see it.

I would like to change that setting to external.

 

Thanks! 

Yousaf December 12, 2017

Is there no possibility to do so? 

1 vote
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 29, 2017

For a count of the comments, try

comments.size()

Yousaf November 29, 2017

Hi Nic,

 

great, it works! 

 

Could you also tell me how to get a count for internal comments?

 

Thanks ;-)

0 votes
Yousaf November 29, 2017

I tried this, no errors are given but it still doesn´t work. It shows the normal count of comments of internal and external 

 

import com.atlassian.jira.component.ComponentAccessor
def internalcommentManager = ComponentAccessor.getCommentManager()
def internalcomments = internalcommentManager.getComments(issue)
if (internalcomments) {
internalcomments.size()
}

Suggest an answer

Log in or Sign up to answer