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 ?
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:-
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:-
2) The test is done using the MOCK-4 issue. An issue link to MOCK-1 is created, as shown in the screenshot below:-
3) Once the issue has been linked, it will display the Story points from the completed issue MOCK-1
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:-
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:-
6) Finally, the issue MOCK-2, which has already been completed, will be linked as shown below:-
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.
I hope this helps to answer your question. :-)
Thank you and Kind regards,
Ram
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 ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Alok Kumar
Has your question been answered?
If yes, please accept the solution.
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Ram Kumar Aravindakshan _Adaptavist_ ,
Sorry for the delayed reply.
I tried the same script, but it is giving type error.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:-
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Scripted field configuration:
all linked storied have story points value. I have changed the value of custom field value in the script.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Even if i remove epic issue type from issue type section, It still gives same error.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.