/* * This script listiner which should be configured on the issue updated event and shows how you can update the value of the story points field on the epic to show a sum of the values from the story points field for all stories linked to the epic, each time the epic issue is updated. * "All right, title and interest in this code snippet shall remain the exclusive intellectual property of Adaptavist Group Ltd and its affiliates. Customers with a valid ScriptRunner * license shall be granted a non-exclusive, non-transferable, freely revocable right to use this code snippet only within their own instance of Atlassian products. This licensing notice cannot be removed * or amended and must be included in any circumstances where the code snippet is shared by You or a third party." */if(issue.fields.issuetype.name.toString()=="Epic"){// Get the Epic Issue KeydefepicIssueKey=issue.key.toString()logger.info("Epic Key = "+epicIssueKey)// Get the field idsdeffields=get('/rest/api/2/field').asObject(List).bodyasList<Map>// Get the story points custom field to use in the scriptdefstoryPointsField=fields.find{it.name=="Story Points"}.idlogger.info("The id of the story points field is: $storyPointsField")// Note: The search API is limited that to only be able to return a maximum of 50 resultsdefallStories=get("/rest/api/2/search").queryString("jql","parentEpic =${epicIssueKey} and issuetype = 'Story'").queryString("fields","parent,$storyPointsField").asObject(Map).body.issuesasList<Map>logger.info("Total stories for ${epicIssueKey}: ${allStories.size()}")// Sum the storyPointsdefestimate=allStories.collect{Mapstory->story.fields[storyPointsField]?:0}.sum()logger.info("Summed Story Points: ${estimate}")// Store the summed story points on the Story Points field of the epic issuedefsummedEstimateField=fields.find{it.name=="Story Points"}.idlogger.info("Custom field ID to update: ${summedEstimateField}")// Now update the Epic issuedefresult=put("/rest/api/2/issue/${epicIssueKey}").header('Content-Type','application/json').body([fields:[(summedEstimateField):estimate]]).asString()// check that updating the Epic issue workedassertresult.status>=200&&result.status<300}else{logger.info("Not an Epic issue so do nothing")}
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.