Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
function getStatusesTimeSpent(string issueKey) {
    // function that returns a map with key status and value total time spent
    stringinterval[] result;
    string[] statusHistory = fieldHistory(issueKey, "status");
    
    date oldDate = %issueKey%.created;
    if(!size(statusHistory) > 0) {
        //status never changed
        result[%issueKey%.status] = currentDate() - oldDate;
        return result;
    }
    
    string oldStatus = statusHistory[1];
    interval timeSpent = 0;
    
    for(number i = 3; i < size(statusHistory); i += 2) {
        string newStatus = statusHistory[i];
        date newDate = statusHistory[i-1];
        timeSpent += newDate - oldDate;
        if(isNotNull(result[oldStatus])) {
            // if issue already was in this status add the time interval to the time spent
            result[oldStatus] = result[oldStatus] + timeSpent;
        } else {
            // otherwise set the time spent for this status
            result[oldStatus] = timeSpent;
        }
        oldStatus = newStatus;
        oldDate = newDate;
    }
    //add also last status time interval
    interval result[oldStatus]lastTime = currentDate() - oldDate; 
    if(isNotNull(result[oldStatus])) {
        result[oldStatus] = result[oldStatus] + lastTime;
    } else {
        result[oldStatus] = lastTime;
    }
    
    return result;
}

You then just have to call getStatusesTimeSpent function, giving the desired issue key as parameter and you will get an array containing all statuses in which the issue was, as well as the total time spent for each status:

...