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)
}
}