Hi
I think I found the solution, at least the solution appropriate for me.
Sorry my code, I've never have code with groovy.
I had to decompile the service-desk .jar to know how many methods I could call.
I could NOT find how to know the goal time of every issue so I made a function that get me the goal minutes depending of the issue type and priority. Goal times are hard-coded, (it hurts) but it works. Could anyone help with this???
When I get the Time to Resolution custom field (timeToResolution in the code), in fact I have an SLAValue.class object. From this class we need 2 classes: OngoingSLAData and CompletedSLAData. More info in the comments in the code.
Create a custom field -> Groovy script
Go to Script Fields and put the following code
If you want to know the spent time in an issue when the SLA clock is stopped
We only need to get the elapsed time from the CompleteSLAData class
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def componentManager = ComponentManager.getInstance()
def changeHistoryManager = componentManager.getChangeHistoryManager()
CustomField cf_timeToResolution = customFieldManager.getCustomFieldObject(11704) def timeToResolution = issue.getCustomFieldValue(cf_timeToResolution)
if (timeToResolution != null)
{
long timeToResolutionMillis = timeToResolution.getCompleteSLAData().get(0).getElapsedTime()
double timeToResolutionMinutes = timeToResolutionMillis / (1000*60)
return timeToResolutionMinutes.round().toString();
}
If you want to know the spent time when the SLA clock is stopped or PAUSED
In this case different classes in the SLAValue.class have to been checked CompletedSLAData and OngoingSLAData
The result is only showed when the status are Solved, Resolved, Done or Closed (depending on the type of issue)
In my SLA clock configuration when Solved, Resolved or Done the clock is paused, and when Closed the clock is stopped.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.ComponentManager
long goalMinutesPerPriorityOrUrgency(type, value) {
def valueCopied = value
switch (type) {
case "Incident":
result = goalMinutesPerIncidentPriority(valueCopied)
break
case "Request for Change":
result = goalMinutesPerRfCPriority(valueCopied)
break
case "Task":
result = goalMinutesPerTaskUrgency(valueCopied)
break
default:
result = 0
}
result
}
long goalMinutesPerIncidentPriority(priority) {
switch (priority) {
case "Blocker":
result = 240 break
default:
result = 0
}
result
}
long goalMinutesPerRfCPriority(priority) {
switch (priority) {
case "Blocker":
result = 240 break
default:
result = 0
}
result
}
long goalMinutesPerTaskUrgency(urgency) {
switch (urgency) {
case "Critical":
result = 480 break
default:
result = 0
}
result
}
def statusName = issue.getStatusObject().getSimpleStatus().getName();
if ((statusName == 'Solved') || (statusName == 'Resolved') || (statusName == 'Done') || (statusName == 'Closed'))
{
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def componentManager = ComponentManager.getInstance()
def changeHistoryManager = componentManager.getChangeHistoryManager()
CustomField cf_timeToResolution = customFieldManager.getCustomFieldObject(11704) def timeToResolution = issue.getCustomFieldValue(cf_timeToResolution)
long result = 0
def issueTypeName = issue.getIssueTypeObject().getName() def priorityOrUrgency = ""
if (issueTypeName == "Task")
{
CustomField cf_urgency = customFieldManager.getCustomFieldObject(12502) priorityOrUrgency = issue.getCustomFieldValue(cf_urgency)
}
else
{
priorityOrUrgency = issue.getPriority().getString("name")
}
/*
CompleteSLAData is only filled when the issue has the SLA clock stopped (passed through 'Solved' or 'Resolved' to 'Closed')
OngoingSLAData is only filled when the issue has the SLA clock running or paused.
*/
if (timeToResolution != null)
{
if (timeToResolution.getCompleteSLAData().size() > 0)
{
long timeToResolutionMillis = timeToResolution.getCompleteSLAData().get(0).getElapsedTime()
double timeToResolutionMinutes = timeToResolutionMillis / (1000*60)
return timeToResolutionMinutes.round().toString();
}
else
{
double elapsedTime = 0;
if (timeToResolution.getOngoingSLAData() != null)
{
double remainingTimeMin = (timeToResolution.getOngoingSLAData().getThresholdData().get().getRemainingTime().get()) / (1000*60)
long remainingTimeMinLong = remainingTimeMin.round().longValue()
if (remainingTimeMin < 0) {
elapsedTime = elapsedTime.sum(goalMinutesPerPriorityOrUrgency(issueTypeName, priorityOrUrgency), remainingTimeMinLong.abs())
}
else
{
elapsedTime = goalMinutesPerPriorityOrUrgency(issueTypeName, priorityOrUrgency).doubleValue() - remainingTimeMin
}
}
return elapsedTime.round().toString()
}
}
Hope it helps!
Hector Flores
We already released Toolbox for JIRA Service Desk plugin https://marketplace.atlassian.com/plugins/com.kostebekteknoloji.plugins.jira.jira-service-desk-toolbox/server/overview and you can display SLA elapsed times as a custom field with different formats,
Moreover, You can contact with info@kostebekteknoloji.com
Best regards,