Get Rule Actor of a project automation rule in Scriptrunner custom script validator

KL Kumar January 24, 2021

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?

2 answers

0 votes
Gareth Cantrell
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 25, 2021

The correct binding variable to use with ScriptRunner is currentUser (see here for more).

For example:

def ruleActor = currentUser
KL Kumar January 26, 2021

@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?

Gareth Cantrell
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 26, 2021

@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}}') )
KL Kumar January 26, 2021

@Gareth Cantrell , Thanks for the reply.

I upgraded both the plugins to latest.

  • Scriptrunner for Jira - 6.17.0
  • Automation for Jira - 7.2.4

Still I get the error as undefined. Am I still making mistake anywhere!error.png

Gareth Cantrell
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 27, 2021

@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?

KL Kumar January 27, 2021

@Gareth Cantrell

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

}
Gareth Cantrell
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 27, 2021

@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:

  1. Create a new user called Rule Actor with username rule.actor
    Screenshot 2021-01-27 at 14.12.17.png
  2. Created a manually triggered rule to transition an issue to In Progress, and set the rule actor to the Rule Actor user.
    Screenshot 2021-01-27 at 14.12.57.png
  3. Created the following validation on the "In Progress" transition.
    Screenshot 2021-01-27 at 14.17.11.png
  4. Triggered the automation on the issue from the "More" menu.
  5. Transitioned the issue to In Progress as the admin user.
  6. Examined the atlassian-jira.log file:
    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.

KL Kumar January 27, 2021

@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!

0 votes
Mark Chaimungkalanont
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 24, 2021

Hey there,

I think you should be able to use:

{{rule.actor}}

Hope that helps!

Cheers,

Mark C 

KL Kumar January 25, 2021

@Mark Chaimungkalanont , Thanks for the reply.

I am getting the attached error in my script validator using {{rule.actor}CustomScriptValidator-Error.png

Suggest an answer

Log in or Sign up to answer