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

This widget could not be displayed.
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
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

491 views 3 18
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