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

how to Run Curl command in Script runner

Pravin Awati March 11, 2022

Hi ,

I want t create github branch from workflow  post function .

I am able to run it through server but I am getting below error while running in Script runner .

 

def url = '{"ref":"refs/heads/Scriptrunner_branch124","sha":"5z12347f551369e1ff3e176c55a8640ee9d7c16b"}'

def command = 'curl -X POST -H "Authorization: token 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" https://api.github.com/repos/login_name/repo_name/git/refs -d "$url"'

 

def proc = command.execute()

proc.waitFor()             

println "Process exit code: ${proc.exitValue()}"

println "Std Err: ${proc.err.text}"

println "Std Out: ${proc.in.text}"

 

I am getting below error 

 

org.codehaus.groovy.runtime.InvokerInvocationException: java.io.IOException: Cannot run program "curl": error=2, No such file or directory at ConsoleScriptExecution1_groovyProxy.run(Unknown Source) Caused by: java.io.IOException: Cannot run program "curl": error=2, No such file or directory at Script1.run(Script1.groovy:7)

 

Could you please help me to resolve this ?

 

3 answers

Suggest an answer

Log in or Sign up to answer
1 vote
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.
March 11, 2022

In scriptrunner for jira server/datacenter, the scriptrunner console has access to the underlying operating system.

With that, you can access files directly, and issue any os commands.

That's what the "string".execute() method does. It rune the string command by the OS.

In cloud, that's not possible for various security reasons.

So you need to use the Unirest client available in scriptrunner for cloud.

I don't have a cloud instance to test with, but it should look something like this:

def payload = [
ref: "refs/heads/Scriptrunner_branch124",
sha: "5z12347f551369e1ff3e176c55a8640ee9d7c16b"
]
ef url = "https://api.github.com/repos/login_name/repo_name/git/refs"
def token = 'token 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'

def postResponse = post(url)
.header('Content-Type', 'application/json')
.header('Authorization', token)
.body(payload)
.asObject().body

Pravin Awati March 11, 2022

Hi Peter ,

Thanks for your reply . 

i tried this script but getting below error .

 

 https://api.github.com/repos/Pravina1/jira_projects/git/refs/ returned an error code: status: 422 - Unprocessable Entity body: java.lang.NullPointerException

 

I am not able to find what exactly wrong in script

 

Can you please help to rectify .

 

Thanks,

Pravin

Pravin Awati March 14, 2022

Hi @Peter-Dave Sheehan 

Additionally in Script it is showing below warning message .

  Can not find matching method io.github.openunirest.request.body.RequestBodyEnrity#asObject()

 

Do I need to import any package for this ?

 

Thanks ,

Pravin

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.
March 14, 2022

As I said before I don't have access to a cloud instance to test or debug this further.

You'll have to refer to the GitHub api documentation and the scriptrunner/unirest documentation.

You can try to change asObject() to asString().

But the 422 error likely means that github has some problem with the payload.

I'm affraid I can't provide any additional value.

0 votes
Oleksandr Melnyk March 24, 2023

Hi @Pravin Awati 

 

You can use Jira Automation for this.

Oleksandr Melnyk March 24, 2023

2023-03-24_09-53.png

0 votes
Ravi Sagar _Sparxsys_
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.
March 11, 2022

Hi @Pravin Awati 

ScriptRunner for Jira on server is different from Cloud where you have to do these automations by using Jira REST API.

Ravi

Pravin Awati March 11, 2022

hi @Ravi Sagar _Sparxsys_ 

Thanks for your reply .

But I am not looking for any API for Jira  , I want to create the branch from workflow postfunction in Jira for that i am using this curl command in script runner . is there any other way we can implement it ?

 

Thanks,

Pravin

TAGS
AUG Leaders

Atlassian Community Events