Hello!
I'm using automation rule (Automation for JIRA addon) in my project. Now I need to get the Rule Actor of my automation rule in scriptrunner custom script validator. This is because I do not want to run the validation when the rule actor is on my name.
I tried using the currentUser in my script. But this always returns the current login user and not the Rule Actor.
Is there a solution for this problem?
The correct binding variable to use with ScriptRunner is currentUser (see here for more).
For example:
def ruleActor = currentUser
@Gareth Cantrell, currentUser returns JIRA logged-in user. But not the Rule Actor of my automation user.
From the scriptrunner documentation, I could see the below code. When I try to test this code in my scriptrunner custom script validator, I get the error because ruleContext is not defined.
log.warn ("Initiator of the action was... " + ruleContext.renderSmartValues('{{initiator}}'))
How can I import ruleContext?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@KL Kumar you are trying to use a smart value rather than the script binding variable.
The smart value {{initiator}} is specific to the automation rule and returns the user who initiated the rule, and is not necessarily the rule actor!
ScriptRunner, on the other hand, binds the rule actor to the currentUser variable in the script:
log.warn ("Rule actor is... " + currentUser)
Additionally, if you're getting an error when trying to use ruleContext, you are most likely using an older version of either ScriptRunner or Automation for Jira.
If you're using the latest versions of both apps, the following code should work to show the difference (using a manually triggered rule where the rule actor and the initiating user are different):
log.warn ("Rule actor is... " + currentUser + ", Rule initiating user is... " + ruleContext.renderSmartValues('{{initiator}}') )
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Gareth Cantrell , Thanks for the reply.
I upgraded both the plugins to latest.
Still I get the error as undefined. Am I still making mistake anywhere!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@KL Kumar I got confused by your mention of Automation for Jira in your original question. I assumed you were trying to create an automation rule, rather than a workflow validation.
Automation for Jira rules cannot be used within a workflow. They can respond to workflow events after the fact, but cannot be used to affect the outcome of a transition.
To get the user who initiated a workflow transition in a ScriptRunner scripted validation, you would need to use code similar to the following:
import com.atlassian.jira.component.ComponentAccessor
log.warn "Current user is ... " + ComponentAccessor.userManager.getUserByKey(transientVars.userKey)
What exactly are you trying to accomplish with automation rules?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
transientVars.userKey returns the user who initiated the workflow (ideally it is the person who logged into JIRA). So this isn't working for my scenario.
My project has an automation with rule actor configured as "rule.actor" (automation user). My requirement here is to skip the scriptrunner script validation when the automation rule actor is "rule.actor". So that's the reason I am trying to get the rule actor user in my scriptrunner script validation.
The script is something like...
if (currentUser == "rule.actor") { // unfortunately this always returns JIRA login user
// skip validation.
} else {
// proceed with validation
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@KL Kumar Automation for Jira will set the current user to the user defined as the rule actor when initiating a workflow transition.
Here's what I did:
2021-01-27 14:15:38,563+0000 http-nio-8080-exec-3 WARN rule.actor 855x4063x1 wkh6pz 0:0:0:0:0:0:0:1 /rest/cb-automation/latest/project/10000/rule/149/execute/MAPP-9 [c.o.scriptrunner.runner.ScriptBindingsManager] Workflow transition initiated by rule.actor
2021-01-27 14:15:38,564+0000 http-nio-8080-exec-3 WARN rule.actor 855x4063x1 wkh6pz 0:0:0:0:0:0:0:1 /rest/cb-automation/latest/project/10000/rule/149/execute/MAPP-9 [c.o.scriptrunner.runner.ScriptBindingsManager] Transition initiated by automation rule, skipping validation...
2021-01-27 14:15:41,569+0000 http-nio-8080-exec-6 WARN admin 855x4073x1 wkh6pz 0:0:0:0:0:0:0:1 /secure/WorkflowUIDispatcher.jspa [c.o.scriptrunner.runner.ScriptBindingsManager] Workflow transition initiated by admin
2021-01-27 14:15:41,571+0000 http-nio-8080-exec-6 WARN admin 855x4073x1 wkh6pz 0:0:0:0:0:0:0:1 /secure/WorkflowUIDispatcher.jspa [c.o.scriptrunner.runner.ScriptBindingsManager] Transition initiated by normal user
One thing to note is that the workflow is only aware of the user who initiated the transition. It is not possible to determine which automation rule initiated the transition.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Gareth Cantrell Thank you for the explanation.
The only difference I am seeing here is - in my case I have the automation which is triggered automatically when the transition happens. So this way, I am getting the currentUser as logged-in user.
When you tried with manual trigger, you got the rule.actor
So it seems that there's no possibility to get the rule actor in scriptrunner script validator when the automation rule is triggered automatically.
Thanks a lot for the help!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey there,
I think you should be able to use:
{{rule.actor}}
Hope that helps!
Cheers,
Mark C
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Mark Chaimungkalanont , Thanks for the reply.
I am getting the attached error in my script validator using {{rule.actor}
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.