Script to calculate time between Created and first comment in an issue.

hbhardwaj3 January 15, 2020

Hi,

I am trying to implement a scripted field which will calculate time between issue created and first comment added to it. can someone please help with the script. I am not sure how to capture comment time in script. 

2 answers

0 votes
Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 30, 2020

Hi all,

There are multiple solutions for sure. Just another solution might be Enhancer Plugin's Response Time Calculators which covers everything without a line of code.

  • It takes working calendar into account rather than just calculating the time between two dates.
  • You can define your own time formatter (1d 4h, 12h, or just display as numbers in minutes)
  • It is searchable
  • It takes private/public comments into account.

I'm one of the folks behind Enhancer Plugin, pleae let me know if you need further assistance.

Cheers

Tuncay

0 votes
David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 17, 2020

I'm not sure about ScriptRunner, but using JMCF, you would write something like this:

if (!issue.get("comment"))
  return null;
return new Date().time - issue.get("comment").first().created.time

which would return a number of milliseconds.

hbhardwaj3 January 20, 2020

Thank you David!

I have used the following code :

 

import com.atlassian.core.util.DateUtils
import com.atlassian.jira.component.ComponentAccessor

def commentManager = ComponentAccessor.getCommentManager()
def comments = commentManager.getComments(issue)


DateUtils.getDurationString(
((comments[0].created.getTime() - issue.getCreated().getTime()) /1000)as Long)

 

It is giving me the desired result. please suggest if your one is more optimized? i can use that.

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 20, 2020

No. Mine would only work with JMCF or JMWE, not ScriptRunner. 

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 20, 2020

That script looks fine for scriptrunner. The only thing is you will get an error if there are no comments.

You could use the following if you wanted to display the time elapsed so far:

import com.atlassian.core.util.DateUtils
import com.atlassian.jira.component.ComponentAccessor
def comments = ComponentAccessor.commentManager.getComments(issue)
def endTime = new Date().time
if(comments){
endTime = comments.first().created.time
}
DateUtils.getDurationString(
((endTime - issue.created.time )/1000) as Long
)

Or make a small adjustment to return null when there are no comments. 

Like hbhardwaj3 likes this
Mohan October 29, 2020

Hi Peter,

I need to show first comment response time of the Assignee in minutes(Not like 1d 14h 14m). Can you please suggest on this.

 

Regards

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 29, 2020

This part...


(endTime - issue.created.time )/1000

Returns the number of seconds.

So if you want minutes, just divide by 60.

Maybe something like

import com.atlassian.core.util.DateUtils
import com.atlassian.jira.component.ComponentAccessor
def comments = ComponentAccessor.commentManager.getComments(issue)
def endTime = new Date().time
if(comments){
endTime = comments.first().created.time
}
Math.floor((endTime-issue.created.time)/1000/60)
Mohan October 30, 2020

Hi Peter,

Thank you for your immediate response. 

I am getting "Can not find matching method error" but it showing the exact result.

And i need to show the result of Assignee first comment time.

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 30, 2020

That is only a warning that can be ignored. The script editor attempts to warn you that you  MIGHT have some type of error. It tries to guess what methods are valid and help you correct mistakes. But it can't know everything and groovy can have dynamic typing.

If you really insist to make the error go away, you can coerce the type

Math.floor((endTime-issue.created.time)/1000/60 as Integer)

 

To get the first comment from the assignee you can try this:

import com.atlassian.core.util.DateUtils
import com.atlassian.jira.component.ComponentAccessor
def comments = ComponentAccessor.commentManager.getComments(issue)
def endTime = new Date().time
if(comments){
def authorComment = comments.findAll{it.authorApplicationUser == issue.assignee }
if(authorComment){
endTime = authorComment.first().created.time
return Math.floor((endTime-issue.created.time)/1000/60 as Integer)
}
}
return null //in case there are no comment or no comments by the assignee

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events