I want to add a custom field to my epics which sums up the time spent logged across all of the epic child issues plus all of the time spent logged on the sub-issues of those epic child issues.
I figured out the first part (immediate children) but don't know how to extend this to include the sub-issues under the child issues as well.
Here's the script I have for just the immediate children:
// 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", """'parent' =${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;