I've written a ScriptRunner Listener to update several fields in an issue when the Epic Link field on an issue is changed.
My listener is triggered by the IssueLinkCreatedEvent — and the code works fine. (Note that as far as I could tell, the Issue Updated event does not contain any information about the Epic Link field being updated.)
But here's the problem:
When a user goes into Edit mode and they update the Epic Link field and save their changes, the listener gets triggered, it updates the issue, but then Jira overwrites the changes the listener made back to the values they were when the user was editing the issue.
This is apparent when you look at the History for the issue:
❶ The Listener triggers first, and properly updates Fix Version/s from "none" to "1.2".
❷ Jira updates the issue and records the change to the Epic Link field
❸ Fix Version/s is reverted back to "none" 😞
As a workaround, I considered adding a Behaviour to the Epic Link field to manually update the fields on the Edit form, but there are downsides to doing that, not the least of which is that adding a Behaviour disables the ability to do the quick inline edit of the Epic Link field (clicking the pencil takes you to the full Edit screen), plus it makes the field inaccessible from the dot (.) menu to quickly modify the Epic Link field using the keyboard.
Using a Listener is the best approach, since it handles other situations where the Epic Link can be updated, such as when issues get dragged-and-dropped on an Epic on a Jira Scrum board.
Has anybody experienced this before? Is there something else going on that I'm missing?
Hi @eddyg
Could you please share your listener code so I can review it, provide some feedback, and double-confirm if this requires further investigation?
Thank you and Kind regards,
Ram
For completeness, I used the Edit button to change the Epic Link field in PROJ-153, and then clicked the Update button.
The History was updated with these three entries:
Going to the Listener and clicking on the ✅ that shows the last run was successful, here's all the data:
2023-08-07 08:28:51,139 DEBUG [runner.ScriptBindingsManager]: Epic Link updated: copied 'fixVersion' ([1.2.0]) from PROJ-125 to PROJ-153
{ "projects": "[PROJ] (java.util.ArrayList)", "@class": "com.onresolve.scriptrunner.canned.jira.workflow.listeners.model.CustomListenerCommand (java.lang.String)", "log": "org.apache.log4j.Logger@103220df", "friendlyEventNames": "IssueLinkCreatedEvent (java.lang.String)", "version": "6 (java.lang.Integer)", "name": "Custom listener (java.lang.String)", "canned-script": "com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener (java.lang.String)", "id": "301493bf-e9f6-4aeb-8c6e-cf60c0eca6f9 (java.lang.String)", "event": "com.atlassian.jira.event.issue.link.IssueLinkCreatedEvent@c0da7a8", "£beanContext": "com.onresolve.scriptrunner.beans.BeanFactoryBackedBeanContext@7932f888", "events": "[com.atlassian.jira.event.issue.link.IssueLinkCreatedEvent] (java.util.ArrayList)" }
Elapsed: 120 ms
CPU time: 53 ms
Here's how the Listener is configured:
And here's the script:
import org.apache.log4j.Level
import com.atlassian.jira.event.issue.link.IssueLinkCreatedEvent
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.index.IssueIndexingService
IssueLinkCreatedEvent linkEvent = event
def sourceIssue = linkEvent.getIssueLink().getSourceObject()
def destinationIssue = linkEvent.getIssueLink().getDestinationObject() as MutableIssue
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
destinationIssue.setFixVersions(sourceIssue.getFixVersions())
ComponentAccessor.issueManager.updateIssue(loggedInUser, destinationIssue, EventDispatchOption.ISSUE_UPDATED, false)
ComponentAccessor.getComponent(IssueIndexingService).reIndex(destinationIssue)
log.setLevel(Level.DEBUG)
log.debug("Epic Link updated: copied 'fixVersion' (${sourceIssue.getFixVersions()}) from ${sourceIssue.key} to ${destinationIssue.key}")
It's worth mentioning that if I "inline edit" the Epic Link field, it works as expected. Any suggestions or workarounds you can provide would be appreciated!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @eddyg
I have reviewed your code and tried replicating the problem in my environment. But I don't seem to be facing the issue.
I have run the test with ScriptRunner's HAPI feature, which works fine.
Please upgrade your ScriptRunner plugin to the latest release, i.e. 8.8.1, so you can use the HAPI feature.
Below is the sample working code that I have tested with:-
import com.adaptavist.hapi.jira.issues.Issues
def sourceIssue = Issues.getByKey(event.issueLink.sourceObject.key)
def destinationIssue = Issues.getByKey(event.issueLink.destinationObject.key)
destinationIssue.update {
sourceIssue.fixVersions.collect {
setFixVersions(it)
}
}
Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.
Below is a screenshot of the Listener configuration:-
I hope this helps to solve your question. :-)
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for looking into this, Ram. It sure seems like it should work!
I was afraid the answer might be "upgrade and try a different approach".
Unfortunately our next upgrade window is still several months out, so I won't be able to try anything else until then.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @eddyg
If your production upgrade is targeted for a later date, I suggest setting up a temporary stand alone instance and test the approach suggested to see if it is able to produce the outcome you are expecting.
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.