Hi,
I have an curl command to trigger an external url. Need some help on script runner groovy script for below command format.
curl --location --request POST 'https://XXXX.XXXX.com:443/api/1/rest/slsched/feed/XXXXXXXXXX/projects/SXXXXXX%20xxxxx/JIRA-Access-TEST%20Task' \ --header 'Authorization: Bearer xxxxxxxxxxxxxxxxxxxx' \ --header 'Content-Type: application/json' \ --data-raw '[ { "Request": { "issueKey": "JJIM-26", "issueId": "This is passthorugh response" } } ]'
Thanks in advance.
Regards,
Aditya.D
Hi
You could execute the curl command as a shell command using the "execute()" method available on groovy strings.
def cmd = """curl --location --request POST 'https://XXXX.XXXX.com:443/api/1/rest/slsched/feed/XXXXXXXXXX/projects/SXXXXXX xxxxx/JIRA-Access-TEST Task' \ --header 'Authorization: Bearer xxxxxxxxxxxxxxxxxxxx' \ --header 'Content-Type: application/json' \ --data-raw '[ { "Request": { "issueKey": "JJIM-26", "issueId": "This is passthorugh response" } } ]'"""def process = command.execute()def sOut = new StringBuilder()def sErr = new StringBuilder()process.consumeProcessOutput(sOut,sErr)process.waitForOrKill(60000) //milliseconds... timeout
Then you could process the sOut strings
Or you can use some native html processing classes.
import groovyx.net.http.HTTPBuilderimport groovyx.net.http.Methodimport groovyx.net.http.ContentTypedef httpBuilder = new HTTPBuilder(https://XXXX.XXXX.com:443)def response = httpBuilder.request(Method.POST, ContentType.JSON){ uri.path = "/api/1/rest/slsched/feed/XXXXXXXXXX/projects/SXXXXXX%20xxxxx/JIRA-Access-TEST%20Task" headers.Authorization = "Bearer xxxxxxxxxxxxxxxxxxxx" body = [Request:[issueKey:"JJIM-26", issueId:""]] response.success = { response, json -> return json } response.failure = { resp, data -> log.error " httprequest failed: $data" return null }}
This will give you some native parsing of the response and response error handling etc.
Hi Peter-Dave,
Thanks for the script.
I tried to run the first script, throwing below error:
You can either escape the \ or change the type of groovy string. The dollar-slashy string is perfect for this use case:
(also, I had an error in the script where I called the command string "cmd" but then tried to execute as if I had called it "command"
Here it is fully corrected
def cmd = $/curl --location --request POST 'https://XXXX.XXXX.com:443/api/1/rest/slsched/feed/XXXXXXXXXX/projects/SXXXXXX xxxxx/JIRA-Access-TEST Task' \ --header 'Authorization: Bearer xxxxxxxxxxxxxxxxxxxx' \ --header 'Content-Type: application/json' \ --data-raw '[ { "Request": { "issueKey": "JJIM-26", "issueId": "This is passthorugh response" } } ]'/$def process = cmd.execute()def sOut = new StringBuilder()def sErr = new StringBuilder()process.consumeProcessOutput(sOut,sErr)process.waitForOrKill(60000) //milliseconds... timeout
Thanks again Peter-Dave for valuable script. Could you help me out with two more improvements.
1. First : "Issue key" & "issue id" has to come dynamically. I'm placing this script in post function. As soon status changed, script should pick the "issuekey" and "issuekey id" of that issue.
2. Second : No transaction success information on issue page. Is it possible to print transaction success code/ log info on issue page like activity tab/history/worklog. Some where we can refer as soon as transition is completed.
BR,
I think given your use case, I would strongly recommend using the httpbuilder approach rather than the curl approach.
Then, with post function, there are a couple of things you can do for telling the user this took place.
1) You can display a flag 2) You can generate a fake history record
Here is a full example:
import com.atlassian.jira.component.ComponentAccessorimport groovyx.net.http.HTTPBuilderimport groovyx.net.http.Methodimport groovyx.net.http.ContentTypeimport com.onresolve.scriptrunner.runner.util.UserMessageUtilimport com.atlassian.jira.issue.util.DefaultIssueChangeHolderimport com.atlassian.jira.issue.history.ChangeItemBeanimport com.atlassian.jira.issue.history.ChangeLogUtilsdef apiBase = "https://XXXX.XXXX.com:443"def apiPath = "/api/1/rest/slsched/feed/XXXXXXXXXX/projects/SXXXXXX%20xxxxx/JIRA-Access-TEST%20Task"def httpBuilder = new HTTPBuilder(apiBase)def response = httpBuilder.request(Method.POST, ContentType.JSON){ uri.path = apiPath headers.Authorization = "Bearer xxxxxxxxxxxxxxxxxxxx" body = [Request:[issueKey:issue.key, issueId:"this is passed through response"]] response.success = { response, json -> return json } response.failure = { resp, data -> log.error " httprequest failed: $data" return null }}def flag = [type:'success',close:'auto']def message = ""if(response){ //here you can incorporate some data from the response into the message message = "Succesfully posted to external api ($apiBase) with response: $response.issueId"} else { message = "There was an error posting data to external API. Please consult the Jira log" flag.type = 'error' flag.close = 'manual'}//use the next two line to display a flag to the userflag.body = messageUserMessageUtil.flag(flag)//use the following lines to create the fake issue history//such fake issue history need to be given a "field name". That name doesn't have to exist or match an existing fielddef historyFieldName = "POST RESULT" // <-change this to whatever makes sense to youdef currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUserdef changeHolder = new DefaultIssueChangeHolder();def changeItemBean = new ChangeItemBean(ChangeItemBean.CUSTOM_FIELD, historyFieldName , "", "", "", message)changeHolder.addChangeItem(changeItemBean);def changeGroup = ChangeLogUtils.createChangeGroup(currentUser, issue, issue, changeHolder.getChangeItems(), false);
It looks like you're new here. Sign in or register to get started.