Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Script to calculate sum of completed story points.

Alok Kumar April 25, 2023

Hi,

Can someone help me with the script which can calculate completed story points. Which means sum of story points of linked stories which are in closed status linked within an epic issue type. There is an example available to calculate total story points already. But how to calculate completed story points ?

 

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 8, 2023

Hi @Alok Kumar

For your requirement, I would recommend using the Scripted Field.

Below is a sample working code for your reference:-

import com.atlassian.jira.component.ComponentAccessor

def issueLinkManager = ComponentAccessor.issueLinkManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def customFieldManager = ComponentAccessor.customFieldManager

def storyPoints = customFieldManager.getCustomFieldObjectsByName('Story Points').first()
issue.getCustomFieldValue(storyPoints) as Double

def linkCollection = issueLinkManager.getLinkCollection(issue, loggedInUser)
def totalStoryPoints = linkCollection.allIssues.findAll {it.status.name == 'Done'}

totalStoryPoints.collect {
it.getCustomFieldValue(storyPoints) as Double
}.sum()

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a screenshot of the Scripted Field configuration:-

scripted_field_configuration.png

Below are a couple of test screenshots for your reference:-

1) Firstly, the two issues that will be tested are MOCK-1 and MOCK-2. Both of these issues are in the Done status  as shown in the screenshots below:-

mock1.pngmock2.png

2) The test is done using the MOCK-4 issue. An issue link to MOCK-1 is created, as shown in the screenshot below:-

test1.png

3) Once the issue has been linked, it will display the Story points from the completed issue MOCK-1

test2.png

4) The next test is to link an issue that has not been closed. MOCK-3 that is still in the To-Do status will be linked as shown below:-

test3.png

5) Once the MOCK-3 issue is linked, there will be no difference in the total points calculated in the Scripted Field as shown in the screenshot below:-

test4.png

 

6) Finally, the issue MOCK-2, which has already been completed, will be linked as shown below:-test5.png

7) Once it has been linked, as expected, the scripted field will be updated with the total of story points from the closed issues, i.e. MOCK-1 and MOCK-2.test6.png

I hope this helps to answer your question. :-)

Thank you and Kind regards,

Ram 

Alok Kumar May 9, 2023

Hi @Ram Kumar Aravindakshan _Adaptavist_ ,

Thanks for your response. 

I forgot to mention and tag that i was looking this for Jira Cloud.

I have this script running in Jira server already. Will you be able to help me to generate script for scriptrunner cloud ?

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 9, 2023

Hi @Alok Kumar

If you intend to do this for the cloud, you can use the sample working code below:-

def issueKey = issue.key

/*To store the story points from the issues that are in Done status*/
def storyPoints = [] as List<Double>

def result = get("/rest/api/2/issue/${issueKey}").header('Content-Type', 'application/json').asObject(Map)

if (result.status == 200){

/*To extract the status name of the linked issues*/
def statusName = result.body.fields.issuelinks['inwardIssue']['fields']['status']['statusCategory']['name'] as List<String>

/*To extract the list of linked issues linked to the current issue that is being viewed*/
def linkedIssueKey = result.body.fields.issuelinks['inwardIssue']['key']

/*To verify that the status of the linked issue(s) is Done*/
if (statusName.first().toString() == 'Done') {

linkedIssueKey.each {
def linkedIssues = get("/rest/api/2/issue/${it}").header('Content-Type', 'application/json').asObject(Map)

if (linkedIssues.status == 200) {
/*To extract the story points from the linked issues and store in the story points list

Please ensure that the default value for the Story Points is set to 0. This is to avoid
any error if the Story Points is not added to any of the linked issues.
*/
storyPoints << Double.parseDouble(linkedIssues.body.fields.customfield_10030.toString())

} else {
"Failed to find issue: Status: ${result.status} ${result.body}"
}
}
}

/*To calculate and return the total story points of all the linked issues that are done*/
storyPoints.sum()

} else {
"Failed to find issue: Status: ${result.status} ${result.body}"
}

Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

In the sample code above, you will notice a custom field invoked, i.e. customfield_10030. This refers to the Story Points field. You must double-check this custom field id as it may differ in your environment.

Also, please ensure that all the Linked Issues that are already in Done status have a value in the Story Points field. If you do not intend to add any Story Points for a particular issue, ensure that you set the Story Points field to 0, else there will be an error in the calculation.

I hope this helps to answer your question. :-)

Thank you and Kind regards,

Ram

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 11, 2023

Hi @Alok Kumar

Has your question been answered?

If yes, please accept the solution.

Thank you and Kind regards,
Ram

Alok Kumar May 15, 2023

Hi @Ram Kumar Aravindakshan _Adaptavist_ ,

Sorry for the delayed reply. 

I tried the same script, but it is giving type error.

script_error.png

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 15, 2023

Hi @Alok Kumar

Could you please share a full screenshot of your Scripted Field configuration?

Also, please share a screenshot of the issue and linked issues that you are testing with.

I've run the test in the cloud instance and don't seem to be encountering this error.

Have you read through my last comment and ensured that you had added value to all the Story Points fields?

If no Story Point is to be added to a particular issue, you need to set the value to 0 and not blank.

Below is a full screenshot of the Scripted Field configuration for your reference:-

image.png

If you observe the screenshot above, I'm not encountering any issue.

I am looking forward to your feedback.

Thank you and Kind regards,

Ram

Alok Kumar May 15, 2023

Scripted field configuration:

1.png2.png3.png4.png

all linked storied have story points value. I have changed the value of custom field value in the script.

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 15, 2023

Hi @Alok Kumar

Can you please remove the Epic issue type from the Issue Type/s field and retest it once again?

Also ensure that the current issue you are testing with does not have any Epics linked to it.

That seems to be the cause of your issue.

Thank you and Kind regards,

Ram

Alok Kumar May 15, 2023

Even if i remove epic issue type from issue type section, It still gives same error.

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 26, 2023

Hi @Alok Kumar

Please confirm if you have ensured that the issues you are testing with have no Epics linked to them, i.e. Epics added to the linked issues when re-running the test.

Thank you and Kind regards,

Ram

TAGS
AUG Leaders

Atlassian Community Events