0

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!

pgundlupet
  • 41
  • 5

1 Answers1

0

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();
    }
     
}
Sérgio
  • 1,777
  • 2
  • 10
  • 12
  • Thanks the inline script error got fixed but I still can't trigger the Jenkins build from Jira. Execution History details is still same and I am not sure about the Link I am using in Custom Web Item. – pgundlupet May 25 '22 at 19:00
  • The Url is based on your jira rest api base url.. e.g. http://xx/rest becomes then just /rest. You may copy the url you have there and try it out in your browser. It seems that is working from the screenshot you have though. – Sérgio May 26 '22 at 08:10
  • Yeah you are right the link is working as I am seeing the Issue Key in Payload. I am also trying to narrow down where the error is and I have a question, from 'trigger_jenkins_build_restapi_endpoint.groovy' file 'password' variable has User API Token which I was able to generate and 'token' variable has Project Authentication Token value different from User API token value. How can I obtain "authentication token"? – pgundlupet Jun 01 '22 at 04:28
  • When I substitute baseURL values and hit from the browser the desired Jenkins build starts. – pgundlupet Jun 01 '22 at 07:55
  • I'm not sure from your last comment if you were able to solve this or not. username and password are the username/password of your jenkins instance. the "token" is the authentication token you define on the Jenkins project/job as detailed here: https://docs.getxray.app/display/XRAY/Integration+with+ScriptRunner#IntegrationwithScriptRunner-TriggeraJenkinsprojectbuildfromaTestPlan – Sérgio Jun 01 '22 at 08:45
  • My bad I was not clear. I have not yet solved it. I have used my Jenkins username and password for login and for 'token' it's my user API token. In Jenkins Authentication Token field I have the same user API Token. Please see images attached in below links. SR REST Endpoint - https://gist.github.com/pgundlupetvenkatesh/365b27ca477d84a8850f00bc1a61ba4c User API Token page - https://gist.github.com/pgundlupetvenkatesh/de381aa444767ee6cac9eb8090d9c74e Jenkins Authentication Token - https://gist.github.com/pgundlupetvenkatesh/32a13c24e6b074037964ee743a86863a – pgundlupet Jun 01 '22 at 19:49
  • I got it working now. I was confused with the token value for Authentication Token under Trigger Build Remotely. I didn't know some random keygen would work for that and the 'password' variable should be User API Token. Thanks Sergio. – pgundlupet Jun 01 '22 at 23:27
  • Great :) If you think it makes sense, I would appreciate if you could mark my answer as correct. – Sérgio Jun 02 '22 at 08:03