I have a script listener on issue updates that copies the latest value in the Sprint field to another read-only custom field called 'Current Sprint.' So anytime the Sprint field is updated, the Current Sprint field is updated as well.
It mostly works well, but I'm having a problem when issues aren't completed in a sprint & are rolled over to the next sprint. Ex. The Sprint field shows an issue is in Canopy Sprint 15, but Current Sprint says Canopy Sprint 14, the previous sprint. If I try to update this manually, either through a bulk edit or the Scriptrunner script console, it works but then my script listener immediately runs & sets it back to the previous, incorrect sprint. Any ideas on what's causing this, or ways to improve my script? I'm newer to scripting so I'm sure this could be improved a bit.
def issueTypes = ['Story', 'Epic']
def thing
if (!issue.fields.issuetype.name in issueTypes) {
return
}
def issueKey = issue.key
def returnLatestSprintName(issueSprints) {
def latestSprint
if (issue.fields.customfield_10115) {
def sprints = []
issueSprints.each { sprint ->
def sprintMap = sprint.substring(sprint.indexOf('[')+1, sprint.indexOf(']')).tokenize(',').collectEntries { [(it.tokenize('=')[0]): it.tokenize('=')[1] ]}
sprints.add(sprintMap)
//sprints.sort { it.name }
}
latestSprint = sprints.sort { it.id }[-1].name
} else {
latestSprint = ''
}
return latestSprint
}
def latestSprint = returnLatestSprintName(issue.fields.customfield_10115)
if (issue.fields.customfield_10752 != latestSprint && latestSprint != '') {
put("/rest/api/2/issue/${issueKey}")
.queryString("overrideScreenSecurity", Boolean.TRUE)
.header('Content-Type', 'application/json')
.body([
fields: [
customfield_10752: latestSprint
]
])
.asString()
}
Thanks!
Keri