Recipe - Autotransitioning subtasks
On this page:
Table of Contents |
---|
We want to autotransition substasks from `Open` status in `In progress`, if the parent task gets `In progress` state.
Adding a new function
Log in as an administrator and navigate to Administration-> Workflows and edit "Start Progress" transition on the default JIRA workflow. Press `Add` a new function:
...
The following screen appears:
Writing code
...
Get time spent in certain status per issue
Table of Contents |
---|
Info | ||
---|---|---|
| ||
Power Scripts™ for Jira (server) Level: BASIC |
Problem
We want to count how much time an issue spent in certain statuses.
Solution
Writing the code
The following SIL™ code does all the magic:
Code Block |
---|
//autotransitioning subtasks in `In progress`
//when parent task is trasitioned in `In progress`
string sk;
for (sk in subtasks(key)) {
//if found subtask that has not been transfered to `In progress`
if (%sk%.status=="Open") {
//then execute the transition to `In Progress` state
autotransition("Start Progress", sk);
}
}
|
Press the 'Save' button to save it on the server side (you can also check its syntax before, use the 'Check' button in the editor's menu).
Info |
---|
All you have to do now is to publish your workflow. |
See Also
...
function getStatusesTimeSpent(string issueKey) {
// function that returns a map with key status and value total time spent
interval[] 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 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:
Code Block |
---|
string[] timeSpentInStatuses = getStatusesTimeSpent("DEMO-1");
return timeSpentInStatuses[1]; // will return a time interval spent in status "Open" for issue "DEMO-1" |
Info |
---|
Note that you can access time spent for each status only by its id. You can see the corresponding ids for your statuses either by looking directly into the jira database, in the issuestatus table, either from the Status administration section in Jira, by hovering over the edit link of each status and see the id parameter in the corresponding url. |