Internal Comment count with scriptrunner?

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

This widget could not be displayed.

For a count of the comments, try

comments.size()

Hi Nic,

 

great, it works! 

 

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

 

Thanks ;-)

This widget could not be displayed.
Alexey Matveev Community Champion Nov 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
    }
}

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 Community Champion Nov 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());

 

Great, thanks :) 

 

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

Alexey Matveev Community Champion Nov 29, 2017

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

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 Community Champion Nov 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();

 

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 Community Champion Dec 04, 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();

Hello Alexey, 

 

this code seems to work now. 

 

Thank you for your help! ;-)

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 Community Champion Dec 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;

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

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! 

Is there no possibility to do so? 

This widget could not be displayed.

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
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

148 views 2 0
Join discussion

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