Script Runner: Story point calculations
Asked by Neeta Dubey:

Hi Team,

I need help in script runner to get script for

1) "Sum of Feature story point" =Story calculation at Feature level(Sum of Story point Story,task and subtak)

2)"Sum of Epic Story point" = Story calculation at Epic level(Sum of Story point Story,task and subtak + Sum of Feature Story point \[Story point calculated above\])


Answers:
author: Marcelo Ulloa
body: I have this for the StoryPoints, I hope it helps you get started  
You would have to make the ID changes and modify it if you need something extra  
What it does is calculate the Story Points of all the tasks that the EPIC has, add them and place them in the Story Points field of the EPIC  
---------------------------------------------------------------------  
import org.slf4j.LoggerFactory  
import groovy.json.JsonSlurper

def logger = LoggerFactory.getLogger(this.class)  
def issueKey = issue.key  
def getEpicIssueKey = { issueId -\>  
def response = get("/rest/api/3/issue/${issueId}?fields=customfield_10014")  
.header('Content-Type', 'application/json')  
.asString()  
if (response.status == 200) {  
def jsonResponse = new JsonSlurper().parseText(response.body)  
return jsonResponse.fields.customfield_10014 ?: issueId  
}  
null  
}

def updateEpicCustomField = { epicKey, value -\>  
def body = \[fields: \[customfield_10028: value\]\]  
def response = put("/rest/api/3/issue/${epicKey}")  
.header('Content-Type', 'application/json')  
.body(body)  
.asString()

if (response.status == 204) {  
logger.info("Custom field updated successfully for epic ${epicKey} with value ${value}.")  
} else {  
logger.error("Error updating custom field for epic ${epicKey}. Status: ${response.status}, Message: ${response.body}")  
}  
}

def fetchAndSumCustomFieldValues = { epicKey -\>  
double totalValue = 0.0  
def response = get("/rest/api/3/issue/${epicKey}?expand=changelog")  
.header('Content-Type', 'application/json')  
.asString()  
if (response.status == 200) {  
def jsonResponse = new JsonSlurper().parseText(response.body)  
def changelog = jsonResponse.changelog.histories

if (changelog) {  
changelog.each { entry -\>  
entry.items.each { item -\>  
if (item.field == "Epic Child" \&\& item.toString) {  
def childIssueUrl = "/rest/api/3/issue/${item.toString}?fields=customfield_10028"  
def childResponse = get(childIssueUrl)  
.header('Content-Type', 'application/json')  
.asString()

if (childResponse.status == 200) {  
def childJsonResponse = new JsonSlurper().parseText(childResponse.body)  
def fieldValue = childJsonResponse.fields.customfield_10028 as Double  
if (fieldValue) {  
totalValue += fieldValue  
}  
}  
}  
}  
}  
logger.info("Total value of customfield_10028 for all child issues of ${epicKey}: ${totalValue}")  
updateEpicCustomField(epicKey, totalValue)  
} else {  
logger.info("No changelog entries found for epic ${epicKey}.")  
}  
} else {  
logger.error("Error retrieving epic details. Status: ${response.status}, Message: ${response.body}")  
}  
totalValue  
}

<br />

def epicIssueKey = getEpicIssueKey(issueKey)  
if (epicIssueKey) {  
def totalValue = fetchAndSumCustomFieldValues(epicIssueKey)  
logger.info("Total value of customfield_10028 from epic ${epicIssueKey}: ${totalValue}")  
} else {  
logger.error("Failed to determine the epic association for issue ${issueKey}.")  
}


author: Neeta Dubey
body: Someone got chance to review my query. I really need help on this one.


Tags: adaptavist

calculation

cloud

jira-cloud

scriptrunner

story-points