scripted field total time a issue has been in open state

Ulrich Schreck April 15, 2014

I m trying to show th total time a issue has been in open state. For that I m using a scripted field

and a modified script :

import com.atlassian.core.util.DateUtils
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.history.ChangeItemBean
 
def componentManager = ComponentManager.getInstance()
def changeHistoryManager = componentManager.getChangeHistoryManager()
 
def inProgressName = "Open"
 
def rt = [0]
changeHistoryManager.getChangeItemsForField (issue, "status").reverse().each {ChangeItemBean item ->
 
    def timeDiff = System.currentTimeMillis() - item.created.getTime()
    if (item.fromString == inProgressName) {
        rt << -timeDiff
    }
    if (item.toString == inProgressName){
        rt << timeDiff
    }
}
 
// NOTE: doesn't show anything if less than 60 seconds
DateUtils.getDurationString(Math.round(rt.sum() / 1000))

Problem is the field allways show 0m. Can someone help?

5 answers

1 accepted

0 votes
Answer accepted
JamieA
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.
April 16, 2014

That's just example code, not really production quality. It doesn't work if the status you are interested in is the first one.

I just made an edit - https://gist.github.com/jechlin/10928320

0 votes
Kevin Dalton October 8, 2015

Is there a way to get total time since creation but not closed?  I really wouldn't want to have to add up all the time between transitions as I would not know each transition it goes through.

0 votes
Andreas Ebert
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.
April 15, 2014

Isn't "<<" a bit-operator? I don't think that works with collections/arrays. Try this instead:

rt.add(timeDiff)

JamieA
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.
April 16, 2014

<< is overridden for collections in groovy.

0 votes
Ulrich Schreck April 15, 2014

Hi Boris,

no that doesn t help scripted field still shows 0m.

0 votes
Boris Georgiev _Appfire_
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.
April 15, 2014

I think the problem is that you should use item.created.getTime()

com.atlassian.jira.issue.history.ChangeItemBean.getCreated() returns java.sql.Timestamp so you should call getTime() to get milliseconds.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events