I'm trying to fill a custom field with the total amount of work spent on the issue and it's subtaks

Frank Schalkwijk April 26, 2017

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.

1 answer

0 votes
Alex Christensen
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 26, 2017

I'm not sure that you need a custom field to capture this information. In the Time Tracking section on an issue with sub-tasks, there should be a checkbox that says, "Include sub-tasks."

time_tracking_with_subtasks.PNG

The "Logged" bar shows all worklogs for the parent issue and its subtasks. You can also view this information in the Issue Navigator using the Σ Time Spent column.

Frank Schalkwijk April 30, 2017

We used this solution when we used Jira Server, but we migrated to Jira Cloud and there I can't find the checkbox. 

The Σ Time Spent field only shows all time I logged on this issue, not the time my teammates logged.

Frank Schalkwijk May 1, 2017

Thank you for your response you pointed me in the right direction. The Tempo Timesheets plugin removes this checkbox and breaks the Σ Time Spent field. I wil report a bug with them to get this fixed.

Not a resolution to my question so I'm not marking it as answered.

Suggest an answer

Log in or Sign up to answer