Using Scriptrunner add-on in Jira Cloud, I'm trying to fill a custom field with the total amount of work spent on this ticket and all of it's subtasks. I added the Scriptrunner add-on to my cloud instance and tried to write some code using the examples on http://scriptrunner-docs.connect.adaptavist.com/jiracloud/script-listeners.html.
I inserted my code as a Script Listener that responds to the Issue Created and Issue Updated events. I think I can't use Worklog * because of the Tempo Timesheets add-on.
The code I have now generates the following logs, but I think there are more things wrong with this code.
2017-04-26 14:18:13.128 INFO - Serializing object into 'interface java.util.List'
2017-04-26 14:18:13.139 INFO - GET /rest/api/2/issue/25909/worklog asObject Request Duration: 205ms
2017-04-26 14:18:13.152 ERROR - Cannot get property 'body' on null object on line 38
2017-04-26 14:18:13.164 ERROR - Class: com.adaptavist.sr.cloud.events.ScriptExecution, Config: null
This is the code I have now:
setTotalTimeSpent(issue.id, !issue.fields.issuetype.subtask)
if (issue.fields.issuetype.subtask) {
setTotalTimeSpent(issue.fields.parent.id, Boolean.TRUE)
}
void setTotalTimeSpent(String issueId, Boolean includeSubtasks) {
def totalTimeSpent = getTotalTimeSpent(issueId)
if (includeSubtasks) {
def allSubtasks = get("/rest/api/2/search")
.queryString("jql", "parent=${issueId}")
.queryString("fields", "[]")
.asObject(Map)
.body
.issues as List<Map>
totalTimeSpent += allSubtasks.collect { Map subtask ->
getTotalTimeSpent(subtask.id)
}.sum()
}
def response = put("/rest/api/2/issue/${issueId}")
.queryString("overrideScreenSecurity", Boolean.TRUE)
.header("Content-Type", "application/json")
.body([
fields:[
"customfield_10701": totalTimeSpent
]
])
.asString()
assert response.status >= 200 && response.status < 300
}
float getTotalTimeSpent(String issueId) {
def worklogs = get("/rest/api/2/issue/${issueId}/worklog")
.asObject(List)
.body
.worklogs as List<Map>
return worklogs.collect { Map worklog -> worklog.timeSpentSeconds / 3600.0 }.sum()
}If anyone has more experience with Scriptrunner, please help me to get working code.