Script Runner: Story point calculations

Neeta Dubey June 9, 2024

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])

2 answers

0 votes
Marcelo Ulloa June 25, 2024
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
}


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}.")
}
0 votes
Neeta Dubey June 11, 2024

Someone got chance to review my query. I really need help on this one.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events