It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Internal Comment count with scriptrunner?

Yousaf Nov 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
Nic Brough [Adaptavist] Community Leader Nov 29, 2017

For a count of the comments, try

comments.size()

Yousaf Nov 29, 2017

Hi Nic,

 

great, it works! 

 

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

 

Thanks ;-)

1 vote
Alexey Matveev [cPrime] Community Leader 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
    }
}
Yousaf Nov 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 [cPrime] Community Leader 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());

 
Yousaf Nov 29, 2017

Great, thanks :) 

 

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

Alexey Matveev [cPrime] Community Leader Nov 29, 2017

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

Yousaf Nov 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 [cPrime] Community Leader 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();

 
Yousaf Dec 04, 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 [cPrime] Community Leader 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();
Yousaf Dec 04, 2017

Hello Alexey, 

 

this code seems to work now. 

 

Thank you for your help! ;-)

Yousaf Dec 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 [cPrime] Community Leader 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;
Yousaf Dec 11, 2017 • edited

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 Dec 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 Dec 12, 2017

Is there no possibility to do so? 

0 votes
Yousaf Nov 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
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Next-gen

Introducing subtasks for breaking down work in next-gen projects

Teams break work down in order to help simplify complex tasks. This is often done iteratively, with tasks being broken down into smaller tasks and so on until the work is accurately captured in well-...

12,322 views 61 58
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you