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)
}
}
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
We use a lot scripted fields, and they are correctly updated everywhere in Jira (Reports, Boards, other plugins, even in REST calls to Jira) !
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.