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

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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

77 views 0 5
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