I've created Script Fragment (Web Item) which shows on button on certain issue types for specific project. Now next thing I'm trying to accomplish is pass the issuekey when user clicks on this button.
Documentation (https://docs.adaptavist.com/sr4js/8.15.0/features/script-fragments/custom-fragments/web-item) indicates that we can pass the current issue id as a query parameter when I do redirection to external link such as follows:
https://myexternal-url.com?summary=$[issue.summary]&key=$[issue.key]
But so far I don't have any luck in populating this information in link. Any idea how I can make this work?
Thank you,
Raju
Hi Raju
Looks like you have found a problem with our docs. There is an error in the screenshot that shows how to pass the issue variables to the link. It appears this was added after version 8.8.1 of our docs, so this link here has the correct screenshot.
You need to use curly braces ${issue.summary} and not square braces []
So your web items link should be
https://myexternal-url.com?summary=${issue.summary}&key=${issue.key}
Thanks for bringing this to our attention. I will update this as soon as possible.
Please let me know if you have further issues configuring your web item.
Kind regards
Matthew
Matthew,
Thank you for the update. That definitely helped to populate the issuekey in the URL. But here is next issue I will need help with:
The mentioned webhook is asking the information as POST, so it seems I can call the webhook with query parameters but rather I need to create a small ScriptRunner REST ENDPOINT and from that I've to pass issue details as JSON payload to POST request type.
To make that happen, I created a REST API end point as follows:
But when I make a call to this REST Endpoint, I end up with following error:
No webpage was found for the web address: https://jira-stage.com/rest/scriptrunner/latest/custom/servicenow
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Raju
Looking at your code, it appears your goal is to use a ScriptRunner rest endpoint to make a POST request to an external API and use a web fragment to trigger that endpoint.
If you want to generate a flag then you need to choose the "Run code and display flag" option from the web items "Do what" dropdown
You also need to format it correctly to display it correctly.
Finally, you need to make another call using a rest client library from within the Endpoint.
WARNING: Just wanted to draw your attention to the fact that you should restrict your ScriptRunner endpoint using the group requirement as shown here to prevent the endpoint from being called by anyone.
i.e. this part
doSomething(
httpMethod: "GET", groups: ["jira-administrators"]
)
Have a look at this code and see if it helps you write your logic:
import com.atlassian.jira.component.ComponentAccessor import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate import groovy.json.JsonBuilder import groovy.transform.BaseScript import groovyx.net.http.ContentType import groovyx.net.http.HttpResponseDecorator import groovyx.net.http.RESTClient import javax.ws.rs.core.MultivaluedMap import javax.ws.rs.core.Response @BaseScript CustomEndpointDelegate delegate /** * Use a ScriptRunner web item and choose the "Run code and show flag" from the 'Do what' field */ // GET for the Jira endpoint someEndpoint(httpMethod: "GET") { MultivaluedMap queryParams -> def issueKey = queryParams.getFirst("key") as String def issueManager = ComponentAccessor.getIssueManager() def restClient = new RESTClient("https://someURLtoPostTo.com") restClient.setContentType(ContentType.JSON) def response = restClient.post( path: "/foo/bar", queryString: "token:sometoken", body: [ "foo": issueKey, "bar": "hello", ], ) as HttpResponseDecorator def successFlag = [ type : 'success', title: "Post Successful", close: 'auto', body : 'Service now updated' ] def failedFlag = [ type : 'failure', title: "Post Failed", close: 'auto', body : 'Service now update failed' ] if (response.isSuccess()) { //log.warn("response data: ${response['data']}") return Response.ok(new JsonBuilder(successFlag).toString()).build(); } else { //log.error("response data: ${response['data']}") return Response.ok(new JsonBuilder(failedFlag).toString()).build(); } }
Kind regards
Matthew
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Matthew,
Thank you for providing the detailed example. Unfortunately I was not able to use RESTClient library but ended up using plain HttpURLConnection to get things done. Then I use Responses as you've suggested to updated user with information on the webhook post request.
And it's working just fine as I needed. Thank you very much for your kind help.
I'm posting here my code for history purposes. Cheers!
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonOutput
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response
import com.atlassian.jira.issue.*
import java.nio.charset.StandardCharsets;
@BaseScript CustomEndpointDelegate delegate
//https://medium.com/@archimedes.fagundes/5-ways-to-call-an-api-in-java-b3de65fb2022
rightnow(httpMethod: "GET") { MultivaluedMap queryParams ->
def issueKey = queryParams.getFirst("key") as String
def aIssue = Issues.getByKey(issueKey)
def reporter = aIssue.reporter ? aIssue.reporter.getEmailAddress() : ""
String servicenow_webhook_url = "servicenow_URL_with_queryparams"
URL url = new URL(servicenow_webhook_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Content-Type", "application/json");
def json = JsonOutput.toJson([
"issueKey" : issueKey,
"reporter" : reporter,
])
httpURLConnection.setDoOutput(true);
try (OutputStream outputStream = httpURLConnection.getOutputStream()) {
outputStream.write(json.getBytes(StandardCharsets.UTF_8));
outputStream.flush();
}
def successFlag = [
type : 'success',
title: "Post Successful",
close: 'auto',
body : 'Delivered message to Service Now!'
]
def failedFlag = [
type : 'failure',
title: "Post Failed",
close: 'auto',
body : 'Unable to deliver message to Service Now!'
]
int responseCode = httpURLConnection.getResponseCode();
loger.warn("HTTP status: " + responseCode);
if (responseCode == 200) {
log.warn(json.toString() + " successfully delivered to ServiceNow.")
return Response.ok(new JsonBuilder(successFlag).toString()).build();
} else {
//log.error("response data: ${response['data']}")
return Response.ok(new JsonBuilder(failedFlag).toString()).build();
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.