Time Between Statuses Script Field

Trying to fix a really old scripted field for time between statuses. I'm not at this level of complexity (I just did an Age field, but it took a lot of trial and error). Any ideas where line 16 went wrong? I am getting Static Type Checking error on L16,C17 for cannot cast java.sql.Timestamp to double and doulbe#minus(java.sql.Timestamp)

 

import com.atlassian.core.util.DateUtils
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.history.ChangeItemBean
import com.atlassian.jira.issue.Issue

def componentAccessor = ComponentAccessor
def changeHistoryManager = componentAccessor.getChangeHistoryManager()

def timediff = 0.0
ChangeItemBean item = changeHistoryManager.getChangeItemsForField(issue, "status").find {it.toString == "Analysis Complete"}
if( null != item ) {
    def To_Trans_time =  changeHistoryManager.getChangeItemsForField(issue, "status").find {it.toString == "Analysis Complete"}?.getCreated()

    ChangeItemBean item1 = changeHistoryManager.getChangeItemsForField(issue, "status").find {it.fromString == "In Progress"}
    if( null != item1 ) {
     def from_Trans_time =  changeHistoryManager.getChangeItemsForField(issue, "status").find {it.fromString == "In Progress"}?.getCreated()
     timediff = (double) To_Trans_time - from_Trans_time
   }   
}

1 answer

0 vote
Vasiliy Zverev Community Champion Jan 17, 2017

Here is a draft for script to start. Main question is to define what to do if issue was into requred status more that once.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager;
import com.atlassian.jira.issue.history.ChangeItemBean
import com.atlassian.jira.issue.Issue


ChangeHistoryManager changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

List<ChangeItemBean> items = changeHistoryManager.getChangeItemsForField(issue, "status").sort(
        //sort all
        new Comparator<ChangeItemBean>() {
            @Override
            int compare(ChangeItemBean o1, ChangeItemBean o2) {
                return o1.getCreated().after(o2.getCreated()) ? 1 :-1;
            }
        }
)
if(items.size() == 0) //there were no status change at all
    return null
//now we have all status chages sorted by time
//some logic for all cases

//code example to return value. It is requared to define do we return worjing or calendar days. It return total time in milliseconds
return items.get(0).getCreated().getTime() - items.get(1).getCreated().getTime() //returns milliseconds

Thank you! We actually rarely have a return to Analysis Complete before In Progress, but I see what you're saying. I'll see what I can do with this and convert to answer if I can get it puzzled out.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Jul 10, 2018 in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

180 views 0 4
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you