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

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 Champion Apr 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.

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.

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 Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,802 views 11 18
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot