How can I pass parameters to a scriptrunner script executed remotely through a CURL?

Ricardo Martinez June 11, 2021

As stated here:
https://scriptrunner.adaptavist.com/4.2.0.2/jira/builtin-scripts.html#_executing_built_in_scripts_remotely

To execute a file that already exists under a script root:

 

> curl -u admin:admin -X POST "http://<jira>/jira/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 "scriptFile=foo/bar.groovy"

I can already execute a script fine, but now I need to send a parameter, through curl to my script. And how can i receive and read it in my script?

I cant seem to find a way to do it. 

1 answer

1 accepted

0 votes
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 15, 2021

This is equivalent to pasting the content on foo/bar.groovy into the console and hitting run.

The only way to pass parameters to such a script is to use the "dynamic form" feature

Looking at browser network call when you hit run, you can see that the payload includes 2 elements. A "parameters" json and the script string or scriptPath.
2021-06-15 20_08_38-DevTools - projects-dev.qad.com_plugins_servlet_scriptrunner_admin_console.png

I haven't tried with curl, but you can try something like this

curl -u admin:admin -X POST "http://<jira>/jira/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 "scriptFile=foo/bar.groovy" 
--data-urlencode 'parameters={\"param1\":\"value\"}'

Not sure I have the right escaping.

I'm not a curl wizard, so maybe you can pass --data-urlencode "scriptfile=..." and some other type in the same command.

Alternatively, you could have your script file be dynamically pre-loaded with your variables.

I have the following in a shell script I use to automatically port prod data to my test instance:

folderName="TEST"

groovy=$(cat <<-EOM 2>&1
import com.atlassian.jira.component.ComponentAccessor

def mailFetchers = ComponentAccessor.serviceManager.getServices().findAll{it.serviceClass == 'com.atlassian.jira.service.services.mail.MailFetcherService'}
mailFetchers.collect{mailFetcher->
def folderName='$folder'
mailFetcher.properties.setString('foldername', folderName)
"Set foldername for mailFetcher.name to '\$folderName'"
}
EOM
)

result_code=$(echo "$groovy"| curl -s -o /dev/null -w "%{http_code}" -u "$user":"$password"
-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@-")
if [[ "$result_code" == "200" ]];then
echo " Mail Handler update successful"
else
echo " Mail Handler update was not successful. Review mail handler folders in the application"
fi
Ricardo Martinez June 23, 2021

Hi;

Thanks, what you said gave me the clue on how to get the curl call with the correct parameters.

The call is similar to what you were saying.

curl 'https://[jira]/rest/scriptrunner/latest/user/exec/' \
-H 'Accept: application/json' \
-H 'X-Atlassian-token: no-check' \
-H 'Content-Type: application/json' \
-u user:pswd \
--data-raw '{"script":null,"scriptPath":"testscript.groovy","parameters":{"valueToAdd":"10"}}'

 

That should do the trick.

 

Thanks a lot.

Suggest an answer

Log in or Sign up to answer