following issue: We are calculating a custom field using a scriptrunner listener that is triggered to all issue events. The outcome of this script depends (besides other fields) on the current date so we need to run the script every night in order to update the value of the custom field.
We can manually trigger the listener with the scriptrunner built-in scripts "Generate Event" by triggering a generic event. This works very well. Now for the automation I found two ideas in this forum which don't work for us yet:
1) Somehow add the built-in script "Generate Events" to the normal JIRA Services. --> Here I don't know how to add this script in "class". Maybe option 2 is better anyway.
2) Use Scriptrunners Escalation Service. The latter I tried with the code below but unfortunately it always fails (see Screenshot below) and there is no entry in the log.
--> Could someone help us with the code? I am sure the username exists and we are running JIRA 8.
Thanks a lot!!
IssueEventManager issueEventM = ComponentAccessor.getIssueEventManager()
IssueEventBundleFactory issueEventFactory = (IssueEventBundleFactory) ComponentAccessor.getComponent(IssueEventBundleFactory.class)
def userManager = ComponentAccessor.getUserManager();
def automationUser = userManager.getUserByName("testuser");
int EVENT_ID = 13
IssueEventBundle eventBundle = issueEventFactory.wrapInBundle(new IssueEvent (issue, null, automationUser, (Long)EVENT_ID, false))
Before I answer and suggest some options, I'd like to point out a potential danger to your approach. As your project grows, you will get more and more issues that need to be recalculated. At the very least, I'd try to limit this to open issues or something like that.
If the calculated field really needs to be that dynamic and will continue to change on all issues forever, then I'd recommend using a Scripted Field. This means that the field will only be recalculated when viewing or when indexing the field. Unfortunately, that might be a problem if you need this for searching/filtering purposes.
To run a generic script as a service, you just have to add the class "com.onresolve.jira.groovy.GroovyService" give it a friendly name and specify the schedule. After you click add service, you will get another screen where you can input the file path of your script.
In that script, you could then use JQL to get your issues and iterate over that.
But the escalation service as you've described should work and give you the ability to manage the JQL directly in the configuration instead of in the code.
When I try your exact code (just changing to a different event id and automation user) I get no errors.
What errors are you seeing?
Have you checked permissions on the automation user?
thanks for taking the time to reply in detail. I took your advise and limited the escalation service to unresolved issues.
Unfortunately, I didn't receive any error messages in the log of the escalation service. But I finally found the issue in this way: When I tried not to generate just an event in the escalation service, but instead to run the actual calculation script from the listener I did get error messages in the log. It turned out that the script failed on some issues because some fields that were set to required in the field configuration for this project were not filled.
This in turn happened because we used an upload via XLS lists to upload previously existing issues via the admin interface. If you upload via the admin interface JIRA accepts issues where not all required fields are filled. Apparently it is no problem to run a listener on these not completely filled issues because a listener is not executed as one specific user. In turn, an escalation service is run by one specific user for which JIRA wants all required fields to be filled.
Of course, the users will fill out the missing fields during the workflow, but we would like to run the script every night already now. So my solution was: Set these fields to "optional" in the field configuration and instead use a behaviour to make these fields required when you create new issues (via a "When" condition and "Workflow Action" on "Create").
Hope this may help other users in the future as well :)
Atlassian's marketplace partners have had a very productive start to 2021! Since our last roundup, our developer community has added over 160 new cloud apps to the Atlassian Marketplace to help you...
Connect with like-minded Atlassian users at free events near you!Find an event
Connect with like-minded Atlassian users at free events near you!
Unfortunately there are no Community Events near you at the moment.Host an event