Wanting to trigger a script upon an issue update and specifically upon changes on specific issue fields is a common use case in Jira. I have learned recently the right way to do it.
Wrong way using ComponentAccessor.getChangeHistoryManager()
This post is wrong Triggering a Script When a Custom Field Changes
(as of 2022-12-08; I have reported the error so it might have been corrected since.)
The proposed solution there was:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.model.ChangeItem
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def changeItems = changeHistoryManager.getChangeItemsForField(issue,'FieldName')
//if field has no change history, this returns false
if (changeItems){
//this gets the previous value of the field
def lastValue = changeItems.last().getFromString()
return true
}
else{
return false
}
The problem with this solution is that the ChangeHistoryManager contains the list of all changes for the issue. It is not restricted to the changes linked to the Update Event.
Right approach using event.getChangeLog().getRelated("ChildChangeItem")
This is the right approach is explained by Adaptavist here Find out if a JIRA update event changes a specific... (and here Update Issue Listener: check if something changed with additional tips)
//if field has no change history, exit
fn = "YourFieldName"
def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == fn}
if (!change){
log.info("${fn} field not changed->exit")
return
}
No comments:
Post a Comment