All time logged on epic and underlying stories and subtasks

Hello there. Wondering if anyone can help as I need a scripted field that stores the total time logged on an epic as well as all children of the epic and all subtasks of those children. I’ve got a script which sums the time logged on the children, btu I don’t know how to adapt it to get the time logged on the epic itself, as well as the subtasks of the child issues I am already getting time for. Any help is greatly appreciated!

Here’s what I have so far:

// Check if the issue is an Epic issue
if (issue.fields.issuetype.name == “Epic”) {

// Get the field ids
def fields = get('/rest/api/2/field')
        .asObject(List)
        .body as List<Map>

// Get the Time Spent field to use in the script
def timespentField = fields.find { it.name == "Time Spent" }?.id

// Handle if the Time Spent Field does not exist
if (timespentField == null) {
    logger.info("Time Spent field does not exist ");
    return;
    }

    // Get all issues below the the Epic Issue
    def allStories = get("/rest/agile/1.0/epic/${issue.key}/issue?maxResults=300")
    // The JQL query to return all stories, modify this if you wish to return other issue types inside of the epic as well.
            .queryString("jql", """'Epic Link' =${issue.key}""")
            .queryString("fields", "parent,$timespentField")
            .asObject(Map)
            .body
            .issues as List<Map>

    // Sum the Time Spent for all the Story issues returned
    def estimate = allStories.collect { Map story ->
        story.fields[timespentField] ?: 0
    }.sum()

    // return the estimate value if it is not null and return 0 if it has no value
    return estimate ?: 0;
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.