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:
- When i call a internal rest url in a listener, do i need to authenticate the user?
- 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;
}
}