Snippets

Adaptavist Jira Cloud Script Field to Sum up story points for all stories below an Epic

Created by Kristian Walker last modified
/*
* This example script field script should configured to display on the 'Issue Sidebar' and configured to run for just the *Epic* issue type only and should be set as the Number return type. 
* This example shows how to get all the story points for all story issues insdie the epic and to sum these up and to store these in the script field. 
* "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." 
*/

// Check if the issue is an Epic issue
if (issue.fields.issuetype.name == "Epic") {

    // 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 storyPointsField = fields.find { it.name == "Story Points" }?.id

    // Handle if the Story Points Field does not exist
    if (storyPointsField == null) {
        logger.info("Story Points field does not exist ");
        return;
        }

        // Get all issues below the the Epic Issue
        def allStories = get("/rest/agile/1.0/epic/${issue.key}/issue")
        // 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", "parentEpic =${issue.key} and issuetype = 'Story'")
                .queryString("fields", "parent,$storyPointsField")
                .asObject(Map)
                .body
                .issues as List<Map>

        // Sum the Story Points for all the Story issues returned
        def estimate = allStories.collect { Map story ->
            story.fields[storyPointsField] ?: 0
        }.sum()

        // return the estimate value if it is not null and return 0 if it has no value
        return estimate ?: 0;
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.