Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,466,320
Community Members
 
Community Events
176
Community Groups

Running groovy from python file

In our project we have one workflow which relies on python numpy matrices to assign appropriate user. There is no issue with calling python from scriptrunner script and getting its value. However, the numpy matrix needs some calculated value to determine which user it will return, and that value is calculated by another script

 

It goes like this

The -> means "calls" in this context 

groovy script -> python User matrix -> python Value calculation script

If it's called like that everything works. But we need to move that value calculation script to groovy and need to call that groovy script from within python matrix script.

 

So it will go like this

groovy script -> python User matrix -> groovy Value calculation script

The issue is, python matrix cannot call groovy script. It just returns an error stating it doesn't have necessary file permissions. All the groovy scripts, and groovy executable on the server were chmod 777 to see if it will help. It didn't. 

 

How can we do this?

 

Code for calling groovy from within python looks like this

amount = subprocess.Popen(["/root/.sdkman/candidates/groovy/4.0.3/bin/groovy",
"/home/jira-home/scripts/valueCalcScript.groovy",
sys.argv[1],
sys.argv[5],
"additionalArg"
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
).communicate()

 

2 answers

1 vote

I'm not sure this is the best forum for your question.

But it got me curious even though I'm not much of a python guy and I had never used groovy command line.

I installed groovy and used the same subprocess.Popen to start a simple script.

While documentation says it should be possible for a groovy script executed with the .../bin/groovy should be able to return a final value, I wasn't able to get that behaviour either from a shell command line of from python Popen.

So I would say your best bet is to make sure that your script doesn't output anything other than your final value.

Here is my groovy script:

println "my return value with ${args.size()} arguments: ${args.join(',')}"
return "not printed"

Here is my python script:

import subprocess
p = subprocess.Popen(["/snap/groovy/current/bin/groovy", "/path/to/script.groovy", "arg1", "arg2"],stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
print out

Here is the output

>>> my return value with 2 arguments: arg1,arg2

 Before looking all this up, my instinct would have been to tell you that you can call the scriptrunner groovy engine running inside jira using curl command:

$ curl -X POST "http://127.0.0.1:8080/rest/scriptrunner/latest/user/exec/ -H "X-Atlassian-token: no-check" -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" -H "Accept: application/json" --data-urlencode "scriptText@/path/to/script.groovy" -u <user>:<password>

Hope this helps.

Hi, I have no issue running groovy script through python locally.

What fails is doing the same thing in scriptrunner workflow because of the aforementioned "lack of permissions" to (i think) groovy excutable.

 

We don't want to run it through REST. Although you say that scriptrunner groovy engine can be accessed through it. Which means it has to be installed with plugin itself, is there any way to call it locally on jira itself? Any specific path to it maybe?

Groovy in jira/scriptrunner is embedded into the scriptrunner plugin via the inclusions of libraries.

There are no executable that you can reach via the command line.

Once you start execution of a python script in a shell, you can't access the JVM components anymove other than through an externalized endpoint. And the only one I am aware of is the rest endpoint.

Here is a quick POC I did (expanding on the example before).

groovy script stored in /apps/tmp/test.groovy: 

println "this output is from groovy script. ${args.size()} arguments: ${args.join(',')}"
return "not printed"

 Python file stored in/apps/tmp/test.py

import subprocess
print "This output is from the python script before calling groovy"
p = subprocess.Popen(["/snap/groovy/current/bin/groovy", "/apps/tmp/test.groovy", "groovy arg1", "groovy arg2"],stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print "This output is from the python script after calling groovy"
out, err = p.communicate()
print out

 Code in scriptrunner groovy console:

def proc = """python2 /apps/tmp/test.py """.execute()
def sout = new StringBuilder()
def serr = new StringBuilder()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(10000)
if(serr) log.error serr
log.info sout

Output

2022-07-04 13:36:23,652 INFO [runner.ScriptBindingsManager]: This output is from the python script before calling groovy 
2022-07-04 13:36:23,653 INFO [runner.ScriptBindingsManager]: This output is from the python script after calling groovy
2022-07-04 13:36:23,653 INFO [runner.ScriptBindingsManager]: this output is from groovy script. 2 arguments: groovy arg1,groovy arg2

Hmm, what's the 'snap' directory? Did you install groovy on jira server via snap package?

0 votes

Like I said. I had never used groovy from the command line. That was just the first result I found when I searched for how to install groovy on RHEL. 
https://snapcraft.io/install/groovy/rhel

Unfortunately we do not use systemd and apparently snap needs it(the solution in provided link for OS without it does not work). Thanks tho

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events