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

Next challenges

Recent achievements

Recognition

  • Give kudos
  • My kudos

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

update epic story points when story in epic updated Edited

Hi. I am trying to create a listener to run whenever the "story point" field for non-epic issues are updated. I would like the listener to get the issue epic link and then run through all the issues in that epic summing up the story points and then write that to the epic story point field. 

I have used a mish mash of cut and pastes from various posts and come up with this .. but it doesnt work .. there is no failure and  no information so i can not see what is going wrong. I dont know how to use preview in listener so can really test it. Any advice or suggestions would be much appreciated:

 

import com.atlassian.jira.component.ComponentAccessor
import groovy.xml.MarkupBuilder
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField;

import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

//get issue
def issue = event.issue as Issue

//now get epic

def customFieldManager = ComponentAccessor.getCustomFieldManager()

//def epicLinkCf = customFieldManager.getCustomFieldObjectsByName("Epic Link")
def epicLinkCf = customFieldManager.getCustomFieldObjects(event.issue).find {it.name == "Epic Link"}

def epicIssue = issue.getCustomFieldValue(epicLinkCf) as Issue


if (epicIssue) {
// (epicIssue.getCustomFieldValue(epicCustomField))

def epicCustomField = customFieldManager.getCustomFieldObjectsByName("Story Points") //change to your Epic custom field's name

//now get story points of issues in epic
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def links = issueLinkManager.getOutwardLinks(issue.id)

def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
def cfManager = ComponentAccessor.getCustomFieldManager()

def cfStoryPoints = cfManager.getCustomFieldObjects().find{it.name == "Story Points"}
def cfStoryPointsTotal = 0

if(links){

links.each {issueLink ->

if (issueLink.issueLinkType.name == "Epic-Story Link") {
def linkedIssue = issueLink.destinationObject

cfStoryPointsTotal = cfStoryPointsTotal += linkedIssue.getCustomFieldValue(cfStoryPoints) ?: 0

 

}

}

// write the total values to the epic
def changeHolder = new DefaultIssueChangeHolder()
// get epic story points field

def tgtEpicField = customFieldManager.getCustomFieldObjects(epicIssue).find {it.name == "Story Points"}

tgtEpicField.updateValue(null, issue, new ModifiedValue(epicIssue.getCustomFieldValue(tgtEpicField), cfStoryPointsTotal ),changeHolder)
}
}

1 answer

Easiest is to add log info in your script...

There is no issue update (save the issue) in your script ? (nor a re-index of the issue ?)...

But... why having a field updated by listeners ? Isn't a scripted field on the Epic much easier (no worry about saving, indexes, ...)?

Code might be something like (did not add the import stuff)

IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

CustomField cfSPts = customFieldManager.getCustomFieldObjectByName("Story Points")
BigDecimal storyPoints
issueLinkManager.getOutwardLinks(issue.getId()).each {epicLink ->
if (epicLink.getIssueLinkType().getName() == "Epic-Story Link") {
Issue epicLinkedIssue = epicLink.getDestinationObject()
if (epicLinkedIssue.getCustomFieldValue(cfSPts)) {
if (!storyPoints) {storyPoints = 0.0}
storyPoints += (BigDecimal) epicLinkedIssue.getCustomFieldValue(cfSPts)
}
}
if (storyPoints) {
return storyPoints.toDouble()
} else {
return null
}

Thanks Marc . I will check out your script tomorrow when back at work but I just wanted to reply to your question : "But... why having a field updated by listeners ? Isn't a scripted field on the Epic much easier (no worry about saving, indexes, ...)?"

The reason I wanted a field update listener is so that the epics story point value will be updated as soon as an issue (in the epic) story point value is updated .. and so can be used in a report or when someone looks at the epic kanban board straight away 

I thought that if i used a scripted field then it would require someone to open / view the epic before the epics story point field was recalculated .. 

is my assumption wrong ? ie if i use a scripted field will the epic story point summation be updated without having to open the epic ? 

thanks again 

We use a lot scripted fields, and they are correctly updated everywhere in Jira (Reports, Boards, other plugins, even in REST calls to Jira) !

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence Cloud

Share your Confluence Cloud experience for a chance to win $500!

Take our 5-minute survey to win a $500 Visa gift card! Are you currently using Confluence Cloud? We want to hear from you! Fill out this quick survey about your Confluence Cloud experience so we ...

104 views 1 5
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you