Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 21:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

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

How to Call Canned (Builtin) Script Runner Script from a Script?

sysadm01
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 11, 2020

Hi all,

I'm trying to automate the 'copy script' functionality in ScriptRunner.

To be more specific - I want to be able to tell ScriptRunner to use 'copy script' and tell it which template to copy and create a new project.

The post here shows that @Phillip Ponzer [Cprime] was able to write a bash script and execute it: https://community.atlassian.com/t5/Answers-Developer-Questions/How-to-Call-Canned-Builtin-Script-Runner-Scripts-from-a-Script/qaq-p/466583

#!/bin/bash

USERNAME=my_username
PASSWORD=my_password
JIRA_URL=http://my.jira.instance.com
COOKIES=cookies.txt
HEADER="X-Atlassian-Token: no-check"

echo Logging in...
curl -s -c "$COOKIES" -H "$HEADER" -d "os_username=$USERNAME" -d "os_password=$PASSWORD" -d "os_cookie=true" $JIRA_URL/login.jsp --output login.html

echo Authenticating as administrator...
curl -si -c "$COOKIES" -b "$COOKIES" -H "$HEADER" -d "webSudoPassword=$PASSWORD" -d "os_cookie=true" -d "webSudoIsPost=false" -d "authenticate=Confirm" $JIRA_URL/secure/admin/WebSudoAuthenticate.jspa --output auth.html

echo Copying project...
curl -s -b "$COOKIES" -H "$HEADER" -d "os_cookie=true" -d "webSudoIsPost=true" -d "cannedScript=com.onresolve.jira.groovy.canned.admin.CopyProject" -d "cannedScriptArgs_FIELD_SOURCE_PROJECT=OLD_PROJECT_KEY" -d "cannedScriptArgs_FIELD_TARGET_PROJECT=NEW_PROJECT_KEY" -d "cannedScriptArgs_FIELD_TARGET_PROJECT_NAME=New Project Name" -d "RunCanned=Run" --output output.html $JIRA_URL/secure/admin/groovy/CannedScriptRunner.jspa

echo Cleaning up...
rm $COOKIES

 

The problem is, I save this as a *.sh file and try to run it on the same server as Jira server installation that has the ScriptRunner add on and it does not work. I initially get the error

-bash: ./scriptrunner.sh: /bin/bash^M: bad interpreter: No such file or directory

I then remove the #!/bin/bash and the result is below:

[root@TEST-PROJECT1 scripts]# ./scriptrunner.sh
./scriptrunner.sh: line 6: $'\r': command not found
Logging in...
./scriptrunner.sh: line 9: $'\r': command not found
Authenticating as administrator...
./scriptrunner.sh: line 12: $'\r': command not found
Copying project...
./scriptrunner.sh: line 15: $'\r': command not found
Cleaning up...
rm: cannot remove 'cookies.txt'$'\r': No such file or directory

 

I'm not particularly experienced, but I can't figure out what I am doing wrong?

Can anyone help please?

1 answer

0 votes
Phillip Ponzer [Cprime]
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.
June 11, 2020

My guess is that you don't have curl installed and you're in a different shell than bash. Try installing curl (usually either by using "apt-get" or "yum install"). You shouldn't need to install bash but you could try that too if you really wanted.

sysadm01
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 11, 2020

Hi @Phillip Ponzer [Cprime] 

Thank you very much for your reply.

I just attempted to install curl using yum install and it doesn't error anymore, which is great, but bad news is that their is no new project created.

Just a question - is there anything that I'm missing here i.e. should I just be able to run this script after modifying the variables and the 'copy project' in ScriptRunner will directly copy the project that I've selected? 

Phillip Ponzer [Cprime]
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.
June 11, 2020

Whenever I wrote that code (6 years ago!), quite a bit of reverse-engineering went into figuring out these lines specifically:

"cannedScript=com.onresolve.jira.groovy.canned.admin.CopyProject" -d "cannedScriptArgs_FIELD_SOURCE_PROJECT=OLD_PROJECT_KEY" -d "cannedScriptArgs_FIELD_TARGET_PROJECT=NEW_PROJECT_KEY" -d "cannedScriptArgs_FIELD_TARGET_PROJECT_NAME=New Project Name" -d "RunCanned=Run" --output 

My guess is that one/some/all of those parameters have changed. To check the parameters you'll need to open up your network tab and find the request it makes whenever you trigger this same function from the UI.

If you're still having troubles, it might be worth opening up a support ticket with Adaptavist.

If you continue having troubles, I highly recommend using Power Scripts as the command becomes a one-liner:

https://confluence.cprime.io/display/SIL/admCreateProjectWithSharedConfiguration

admCreateProjectWithSharedConfiguration(pkey, pname, description, lead, url, categoryName, defaultIsUnassigned, avatarId, pkeyForScheme, projectTypeKey)

Since you'll still need to run the SIL Script from curl, instructions for how to do so can be found here:

https://confluence.cprime.io/display/SIL/Common+REST+service#CommonRESTservice-CURLExample

You'll specifically want to use the "runScript" REST call after you've created your SIL script in the SIL Manager. Alternatively, you don't even have to create a SIL script in SIL Manager if you don't want to and you can just supply the code to the curl command directly:

curl -H "Content-Type: application/json" -u username:password -d " {\"source\" : { \"type\": \"INLINE\", \"code\": \"admCreateProjectWithSharedConfiguration(pkey, pname, description, lead, url, categoryName, defaultIsUnassigned, avatarId, pkeyForScheme, projectTypeKey);\"}}" your.jira.instance.com:7210/rest/keplerrominfo/refapp/latest/async-script/runScript
Phillip Ponzer [Cprime]
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.
June 11, 2020

should I just be able to run this script after modifying the variables

Yes. My guess, however, is that the parameters have changed since I reverse-engineered this 6 years ago and ScriptRunner has gone through a few changes since then :)

You could try opening up your Network tab and run the canned script from the UI to see if any of these parameters have changed:

"cannedScript=com.onresolve.jira.groovy.canned.admin.CopyProject" -d "cannedScriptArgs_FIELD_SOURCE_PROJECT=OLD_PROJECT_KEY" -d "cannedScriptArgs_FIELD_TARGET_PROJECT=NEW_PROJECT_KEY" -d "cannedScriptArgs_FIELD_TARGET_PROJECT_NAME=New Project Name" -d "RunCanned=Run"

If that doesn't work, you may need to reach out to Adaptavist and create a ticket with them.

If you're still having trouble, I might recommend you give Power Scripts a try, which turns this into a one-liner:

https://confluence.cprime.io/display/SIL/admCreateProjectWithSharedConfiguration

admCreateProjectWithSharedConfiguration(pkey, pname, description, lead, url, categoryName, defaultIsUnassigned, avatarId, pkeyForScheme, projectTypeKey)

You'll still need to call this script from shell using curl, however, and instructions on doing so can be found here:

https://confluence.cprime.io/display/SIL/Common+REST+service#CommonRESTservice-CURLExample

Suggest an answer

Log in or Sign up to answer