Time Between Statuses Script Field

Lacey McDonnell January 17, 2017

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 votes
Vasiliy Zverev
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.
January 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
Lacey McDonnell January 17, 2017

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
TAGS
AUG Leaders

Atlassian Community Events