Forums

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

Custom field that calculates number of specific comments?

daniellec May 2, 2019

Hello!

Is there a way to use script runner (or other plugins) in order to create a custom field which will calculate the number of comments containing specific text, that were added in a the last week?

 

Thanks!

Danielle

2 answers

1 accepted

0 votes
Answer accepted
Ben Poulson
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.
May 3, 2019

Quick mock up of what the logic would look like for this. Might be a few syntax errors since I haven't tested it, but it should be basically right.

import com.atlassian.jira.issue.comments.CommentManager
import java.util.GregorianCalendar
import java.util.Date
import java.util.Calendar

def comments = CommentManager.getComments(issue)
def count = 0
def textToCheck = "Text you want to check"
Calendar cal = new GregorianCalendar()
cal.add(Calendar.DAY_OF_MONTH, -7)
Date sevenDaysAgo = cal.getTime()

for (comment in comments)
{
def text = comment.getBody()
def created = comment.getCreated()
if (text.contains(textToCheck) && created > sevenDaysAgo)
{
count++
}
}
return count
daniellec May 5, 2019

Wow thank you so much Ben, will try and update!

daniellec June 13, 2019

@Ben Poulson  @Antoine Berry Thanks again!

This is what I did and it's working perfectly (just counting the number of those specific comments):

import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.component.ComponentAccessor

List<String> list = new ArrayList<String>();
def comments = ComponentAccessor.commentManager.getComments(issue)
def count = 0
def textToCheck = "my text"

for (comment in comments)
{
def text = comment.getBody()
if (text.contains(textToCheck) )
{
count++
}
}
return count ? count as Double : null

Now the problem is that I can't seem to use this in a JQL search,

Searcher and Template are set to Number but if I try to filter all issues with this field set to > 4 - it doesn't show correct results.

Any idea what am I doing wrong?

Thanks!

Antoine Berry
Community Champion
June 13, 2019

Hi @daniellec ,

hard to say right off the bat. You should try the JQL for one or two specific issues and determine when it is working and when it is not.

The code looks fine anyway.

Ben Poulson
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.
June 14, 2019

@daniellec Could you try reindexing your jira instance and check again?

daniellec July 1, 2019

Reindexing worked! thank you!

Ben Poulson
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.
July 8, 2019

@daniellec Perfect!

0 votes
Antoine Berry
Community Champion
May 2, 2019

Hi @daniellec , Welcome to the community ! 

This is most probably doable with script runner with calculated fields.

You will be using the getComments method from the CommentManager to retrieve the comments of the issue.

Antoine

daniellec May 5, 2019

Thank you!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events