Trigger Jenkins Build from Jira Xray Issue

256 Views Asked by At

I am referring to Trigger a Jenkins project build from a Test Plan from Xray docs to trigger a Jenkins build from a Jira issue. While setting up the Custom Web Item, what link should I point to? I have just copied the link from the document.

enter image description here

On REST Endpoint setup, I got errors on requestMethod, getResponseCode(), and getResponseMessage() methods

enter image description here

enter image description here

I am not sure if ScriptRunner execution history is also not showing full payload info.

enter image description here

Jira DC v8.9.0
ScriptRunner v6.51.0
Xray v6.1.3
Jenkins v2.263.1

Would appreciate any help!

1

There are 1 best solutions below

8
Sérgio On BEST ANSWER

The problem is due to the deprecation of UrlConnection class. You should update the code to use the HttpURLConnection class instead. It could be something like this:

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonOutput
import groovy.transform.BaseScript
import groovy.json.StreamingJsonBuilder;
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response
import java.net.HttpURLConnection
  
@BaseScript CustomEndpointDelegate delegate
  
triggerJenkinsBuild(httpMethod: "GET") { MultivaluedMap queryParams ->
  
    def issueId = queryParams.getFirst("issueId") as String // use the issueId to retrieve this issue
      
    def flag = [
    type : 'success',
    title: "Build scheduled",
    close: 'auto',
    body : "A new build has been scheduled related with "+issueId
    ]
    
 
    URL url;
    def jobName = "java-junit-calc"                         // could come from a CF in the Test Plan
    def jenkinsHostPort = "192.168.56.102:8081"             // could be defined elsewhere
    def token = "iFBDOBhNhaxL4T9ass93HRXun2JF161Z"          // could also come from a CF in the Test Plan
    def username = "admin"                                  // probably, would need to be stored elsewhere
    def password = "fa02840152aa2e4da3d8db933ec708d6"       // probably, would need to be stored elsewhere
    def baseURL = "http://${jenkinsHostPort}/job/${jobName}/buildWithParameters?token=${token}&TESTPLAN=$issueId"
 
    url = new URL(baseURL);
    def body_req = []
 
    def authString = "${username}:${password}".bytes.encodeBase64().toString()
 
    HttpURLConnection connection = (HttpURLConnection) url.openConnection()
    connection.setRequestMethod("POST")
    connection.setDoOutput(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() == 201) {
        Response.ok(JsonOutput.toJson(flag)).build()
    } else {
        //Response.status(Response.Status.NOT_FOUND).entity("Problem scheduling job!").build();
    }
     
}