Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

ScriptRunner for Jira: accessing bindings from a validator or post-function in a standalone script?

Janek Schumann December 18, 2024

Hey everyone,

Since the Adaptavist forum seems kind of dead and most Google searches end up in this forum anyways, I am trying my luck here.

We have tons of inline groovy scripts in our workflows: validators, post-functions, etc.
(Jira DC 9.x, not Cloud)

In the admin UI of ScriptRunner, it allows to create a folder structure and create groovy scripts there.
In validators etc. it allows for inline script OR referencing a file from said folder structure.

Now here is my issue / question:

In workflow, I do have access to certain bindings, e.g. issue, cfValues and such.
How do I use the file reference and be able to access those bindings there?

The same bindings aren't readily available in the standalone scripts. Obviously they don't know the context they will be run in.

I find it odd that I couldn't find documentation for this use-case. Why would I take a validator from a file if said validator has no clue about the issue as a context? This seems to massively limit the usefulness.

Or do I have to some weird stuff like have an inline script which imports the file, calls a function and passes the bindings over to the script? I didn't even find documentation for that...

There is a "binding" variable readily available. Yet again, no documentation to be found which tells me how the bindings are named. ¯\_(ツ)_/¯

In the end, we want to put the folder under version control and reuse the same validator code in multiple workflows.

 

Thx.

2 answers

Suggest an answer

Log in or Sign up to answer
0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
December 18, 2024

Hi @Janek Schumann

If you have fully gone through the ScriptRunner documentation, you should come across the  Script Binding section which explains the Binding feature in the editors.

They do not specify exactly how the Binding variables are being named because this differs between editors.

For example, the Listener editor has different binding variables, and the Post-Function editor has different binding variables. Hence, only generic documentation has been created to explain the usage of the Binding variable.

In your last comment, you mentioned:

In the end, we want to put the folder under version control and reuse the same validator code in multiple workflows.

From what you have mentioned, I assume you trying to find a simple approach to migrate your code to the Git repo, including the code you have used in the Inline editors. If yes, unfortunately, there is no simple approach. You need to manually migrate the code you have added in the Inline Editors to Groovy files and use the File tab to access them. 

Once you have converted all your inline code to Groovy files, you can add and manage them via the Git repo. The process of migration will from Inline Editor to Groovy be tedious the first time.

 

Thank you and Kind regards,
Ram

Janek Schumann December 19, 2024

yes I did.

I am still playing around with it. I might have overlooked one thing:
just ignore the syntax checking and errors it spills and all is fine.

In the context of the script editor it doesn't have the bindings as explained in the documentation. Hence me wondering how to access them if I call it as a file in a validator or post-function etc.

I took hours yesterday to figure out, that the syntax checker is broken in more ways than one. I would love a feature to just deactivate it, because it throws around 600 errors across the system...
... 550+ are false positives.

So I bet - this is what I wanted to check today - if I just access the binding variables in a groovy file and execute it as file reference in a workflow, it might actually just work fine.

Janek Schumann December 19, 2024

also yes, I know I have to touch every single inline script and migrate it by hand. 😅

actually, I could automate that task. But it is a good time to refactor most of the 10 year code anyways. 😁

afterwards, the files live in a directory on the server I could easily put under version control...
... once that is done, I bet we migrate to cloud and everything was for nothing. ¯\_(ツ)_/¯

Janek Schumann December 19, 2024

I wrote a nice wrapper around the payload parsing...
... but guess what? imports of your own class from another file is buggy as hell. Syntax checker is having a field day. Runs perfectly fine though.


I use ScriptRunner for a very long time now. I am super annoyed this still is a thing. Very high quality app with the most useless syntax checker of all times. 🙄

0 votes
Janek Schumann December 18, 2024

Okay, I got creative:

there is the binding variable in scripts. It contains key-value pairs. I just created a post-function to use the script and this is the content of that variable:

{
"canned-script": "com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CustomScriptFunction (java.lang.String)",
"class.name": "com.onresolve.jira.groovy.GroovyFunctionPlugin (java.lang.String)",
"full.module.key": "com.onresolve.jira.groovy.groovyrunnerscriptrunner-workflow-function-com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CustomScriptFunction (java.lang.String)",
"issue": "... (com.atlassian.jira.issue.IssueImpl)",
"transientVars": {
"issue": "... (com.atlassian.jira.issue.IssueImpl)",
"configuration": "com.opensymphony.workflow.config.DefaultConfiguration@6242b93a",
"proj": "Project: ... (com.atlassian.jira.project.ProjectImpl)",
"project": "[GenericEntity:Project][name,...][assigneetype,...][description,...][projecttype,...][id,...][counter,...][avatar,...][originalkey,...][url,...][lead,...][key,...] (org.ofbiz.core.entity.GenericValue)",
"currentSteps": "[SimpleStep@33[owner=..., actionId=..., status=...]] (java.util.ArrayList)",
"store": "com.opensymphony.workflow.spi.ofbiz.OfbizWorkflowStore@21d2098f",
"descriptor": "com.atlassian.jira.workflow.ImmutableWorkflowDescriptor@357686d0",
"userKey": "... (java.lang.String)",
"originalAssigneeId": "... (java.lang.String)",
"entry": "com.opensymphony.workflow.spi.SimpleWorkflowEntry@5714562c",
"context": "com.opensymphony.workflow.basic.BasicWorkflowContext@6c3e4ea9",
"createdStep": "SimpleStep@33[owner=..., actionId=..., status=...] (com.opensymphony.workflow.spi.SimpleStep)",
"originalissueobject": "... (com.atlassian.jira.issue.IssueImpl)",
"actionId": "... (java.lang.Integer)",
"pkey": "... (java.lang.String)",
"transaction": "com.atlassian.jira.transaction.TransactionSupportImpl$TransactionImpl@773f9c42"
},
"log": "org.apache.log4j.Logger@7f459f51",
"\u00a3beanContext": "com.onresolve.scriptrunner.beans.BeanFactoryBackedBeanContext@15e1a8a8"
}

so yes, seems possible. However, it is super akward to handle string keys and cast objects to get what I want.

If anyone from Adaptavist is reading here: documentation would be nice, a good helper utility even nicer. *sigh*

At least it is possible after all. Once small step.

TAGS
AUG Leaders

Atlassian Community Events