I'm using Jira 8 with scriptrunner to automate some transitions in our ticket system. I have a listener event that needs to transition automatically if an update event is detected and all fields have been updated within a certain amount of time.
I have two fields that I care about how long it's been since they were updated, and three other fields that I don't care when they were updated, just that they are correct. I'm having trouble finding out how to calculate the time since the custom fields were updated last that I can use to determine whether to transition the issue.
Can anybody help me with the right syntax to get the time since the last field update?
I found this answer https://community.atlassian.com/t5/Jira-questions/Change-from-quot-hours-quot-to-quot-Days-Hours-Mins-quot-in-a/qaq-p/803110)
and this question https://community.atlassian.com/t5/Answers-Developer-Questions/Is-there-a-way-to-retrieve-the-date-time-a-field-was-last/qaq-p/549129 that allow me to get access to the date of the last update, but I don't know to turn that into a usable time I can use to transition the issue. Thanks for any help!
Here is a short script (console) to demonstrate how to get epoch millis value as a standard base for comparing created field from the changeHistory against current date/time
import com.atlassian.jira.component.ComponentAccessordef im = ComponentAccessor.issueManagerdef hm = ComponentAccessor.changeHistoryManagerdef issue = im.getIssueObject('JSP-1922')def assigneeChanges = hm.getChangeItemsForField(issue, 'assignee')def lastAssigneeChangeDt = (assigneeChanges) ? assigneeChanges.last().created : nullif(lastAssigneeChangeDt){ log.info lastAssigneeChangeDt log.info new Date().toTimestamp().time log.info new Date().toTimestamp().time - lastAssigneeChangeDt.time} else { log.info "assignee was never changed"}
This will give you a number of milliseconds since the change. Simple arithmetics can get you hours and minutes
Great! That seems to do what I wanted it to. Although there's some weirdness in the static checking in Jira 8, but the code runs just fine.
Yeah, that's because of my sloppy null protection on lastASsigneeChangeDt. The static type checking errors can't know for such what type that's going to be.
Here is an equivalent example without static type checking errors:
import com.atlassian.jira.component.ComponentAccessorimport java.sql.Timestampdef im = ComponentAccessor.issueManagerdef hm = ComponentAccessor.changeHistoryManagerdef issue = im.getIssueObject('JSP-1922')def assigneeChanges = hm.getChangeItemsForField(issue, 'assignee')if(assigneeChanges){ def lastAssigneeChangeDt = assigneeChanges.last().created log.info lastAssigneeChangeDt log.info new Date().toTimestamp().time log.info new Date().toTimestamp().time - lastAssigneeChangeDt.time} else { log.info "assignee was never changed"}
It looks like you're new here. Sign in or register to get started.