Currently working on a listener that listens to issue updated events, when an issue updated event happens, it looks through a list of fields to see if they changed, if they have, it then updates a counter by 1.
What I noticed is whenever I make even one change to an issue, I get at least 15 logs. I was able to see the fields changing, and it's looking like a log per field on the issue, even though none of the fields are actually changed. Is this proper behavior? Is there any way to get the listener to run as many times as there are actual issue updated events?
This is one of the logs I get. I get 15 each time I update one field, difference between each log and the next is the field changing, but they all have fromString null and toString null.
The issue I'm running this script on has three subtasks. the script seems to be running 10 times per issue in the issue hierarchy, so 10 times for each subtask, please twice for the issue where the actual change happens. Not sure why this is happening, or why 10 specifically.
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
// Initial issue setup and dependencies
def issueToCompare = event.issue as Issue
def issueService = ComponentAccessor.getIssueService()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def listenFields = ['List of fields']
def acceptedIssueTypes = ["List of issue types"]
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObjectsByName("Field Updated Counter")[0]
def counter = issueToCompare.getCustomFieldValue(cField) as Double
def counterIncrement = ((counter != null ? counter : 0) + 1).doubleValue()
// Check if issue type is accepted and there are change histories
if(issueToCompare.getIssueType().name in (acceptedIssueTypes)){
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def changeHistories = changeHistoryManager.getChangeHistories(event.issue)
log.warn(changeHistories.last().getChangeItemBeans().find {it.field})
if (changeHistories) {
def changeUser = changeHistories.last().getAuthorDisplayName()
if (changeUser != 'CP - Admin'){
log.warn(changeUser)
def changeItem = changeHistories.last().getChangeItemBeans().find {
// Check if Source field value changed
(it.field in listenFields) && it.fromString != it.toString
}
log.warn(changeItem)
if (changeItem) {
// Use IssueService to update the issue and trigger an issue updated event
def issueInputParameters = issueService.newIssueInputParameters()
if (issueInputParameters) {
issueInputParameters.with {
setSkipScreenCheck(true)
setRetainExistingValuesWhenParameterNotProvided(true)
addCustomFieldValue(cField.idAsLong, counterIncrement.toString())
}
def validationResult = issueService.validateUpdate(user, issueToCompare.id, issueInputParameters)
if (validationResult.isValid()) {
issueService.update(user, validationResult)
} else {
log.warn("Issue update validation failed: ${validationResult.errorCollection}")
}
} else {
log.error("IssueInputParameters could not be instantiated. Cannot proceed with issue update.")
}
}
}
}
}