How to Call Internal Jira Rest URL with scriptrunner

Krishnanand Nayak
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.
February 22, 2019

We are trying to implement this solution as part of a listener. This example need me to save the user/password, but we want to use the logged in user when making the call to the rest url. So 2 questions:

  1. When i call a internal rest url in a listener, do i need to authenticate the user?
  2. if yes, how do i use the current logged in user details to authenticate?

 

boolean associateTestsToTestExecution(testExecKey,listOfTestKeys){
def jiraBaseUrl = com.atlassian.jira.component.ComponentAccessor.getApplicationProperties().getString("jira.baseurl")
def endpointUrl = "${jiraBaseUrl}/rest/raven/1.0/api/testexec/${testExecKey}/test"
url = new URL(endpointUrl);
def body_req = [ "add": listOfTestKeys ]

// you should use a specific user for this purpose
username = "admin"
password = "admin"
def authString = "${username}:${password}".bytes.encodeBase64().toString()

URLConnection connection = url.openConnection();
connection.requestMethod = "POST"
connection.doOutput = true
connection.addRequestProperty("Authorization", "Basic ${authString}")
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8")
connection.outputStream.withWriter("UTF-8") { new StreamingJsonBuilder(it, body_req) }
connection.connect();
log.debug(connection.getResponseCode())
log.debug(connection.getResponseMessage())

if (connection.getResponseCode() == 200) {
// OK
return true;
} else {
// error
return false;
}
}

 

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Katy Kelly
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 28, 2019

Hi Krishnanand, 

So that others can benefit from the answer we supplied in your support ticket we are also listing it here: 

As you can see from the example, the XRay team is using hardcoded credentials to interact with the REST API.
They are doing this because for security reason Atlassian does not allow you to get the password from the logged user.

Usually, when interacting with other plugins from Jira you don't need to make a REST call, the plugin exposes some Java API what can be used with the @WithPlugin annotation directly inside the script as explained here . 
Unfortunately, I was not able to find any Java API exposed from XRay.

If XRay needs to get notified by this and there is no Java API available, the only way to achieve this is through the REST API.

If the tracking needs to be done on the Jira side, then we can come up with something to track who is clicking the button. 
If the tracking needs to be done on the XRay side, then there is not much you can do.

We would appreciate if you can accept the answer so that others know about this. 

Regards,

Katy

TAGS
AUG Leaders

Atlassian Community Events