Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

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

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

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

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.

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.

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

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

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

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)

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.

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

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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

🤺 Atlympic Event: Partnerships

Hello Community!  Quick disclaimer: We are running a contest on Community (The Atlympics!) from July 23rd - August 8th of 2021. If you are interested in participating in this contest (prizes! ...

44 views 0 0
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