I am working on a script runner listener script that sums up the values for a field (MT Loaded) in all the issues linked to an epic and places them in the epic MT Loaded field. Working off a script runner example for subtasks I have this:
// Get the parent issue as a Map
def parent = (issue.fields as Map).parent as Map
// Retrieve all the subtasks of this issue's parent
def parentKey = parent.key
// Get the field ids
def fields = get('/rest/api/2/field')
.asObject(List)
.body as List<Map>
// Get the story points custom field to use in the script
def MTLoadedField = fields.find { it.name == "MT Loaded" }.id
logger.info("The id of the MT Loaded field is: $MTLoadedField")
// Note: The search API is limited that to only be able to return a maximum of 50 results
def allSubtasks = get("/rest/api/2/search")
.queryString("jql", "parent=${parentKey}")
.queryString("fields", "parent,$MTLoadedField")
.asObject(Map)
.body
.issues as List<Map>
logger.info("Total subtasks for ${parentKey}: ${allSubtasks.size()}")
// Sum the estimates
def sumMTLoaded = allSubtasks.collect { Map subtask ->
subtask.fields[MTLoadedField] ?: 0
}.sum()
logger.info("Summed MT Loaded: ${sumMTLoaded}")
// Store the summed MT Loaded on the MT Loaded field of the parent issue
def summedMTLoadedField = fields.find { it.name == "MT Loaded" }.id
logger.info("Custom field ID to update: ${summedMTLoadedField}")
// Now update the parent issue
def result = put("/rest/api/2/issue/${parentKey}")
.header('Content-Type', 'application/json')
.body([
fields: [
(summedMTLoadedField): sumMTLoaded
]
])
.asString()
// check that updating the parent issue worked
assert result.status >= 200 && result.status < 300
But I am stuck in modifying the example to look up the linked epic instead of subtask parent.
Can anyone advise?