Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-39680

httpRequest method in Pipeline causes weird behavior

XMLWordPrintable

      We're running into an issue with httpRequest in a Groovy script used on a mutlibranch pipeline job.

      I have code that looks something like this:

      /**** Jenkinsfile ****/
      // ...
      notify = load 'notify.groovy'
      // ...
      if (checkResults(...) > 0) {
          println 'Tests failed, stopping build'
          return 1
      }
      
      int checkResults(...) {
          // ...
          notify.buildFailed(...)
      
          return failedTestCount
      }
      
      /**** notify.groovy ****/
      
      void buildFailed(...) {
        // ...
        sendMessage(...)
      }
      
      void sendMessage(...) {
         // ...
        def response = httpRequest acceptType: 'APPLICATION_JSON', contentType: 'APPLICATION_JSON',
          httpMode: 'POST', requestBody: body, consoleLogResponseBody: true,
          url: 'https://example.net/hooks/4ezwZwLYbmYwJmD6MXN6yH3YNdd/C6m5uKJstArcKQyxfBYb5nMW7PkPSriACsXg9w',
          validResponseContent: '"success":true'
      
        println "Sent a notification, got a $response.status response"
      }
      

      Expected Results

      When this code runs, I expect for the httpRequest to return the response, the code to get printed out from my println, then checkResults to return the number of failed tests it detected (code not shown). However, that's not what happens.

      Actual Results

      Instead, sendMessage immediately return without executing my println, and most oddly of all, checkResults ends up returning the HTTP Response from httpRequest! How is this even possible?

      The symptom, other than my println doing nothing, is that the > 0 comparison in the Jenkinsfile doesn't work and this exception is thrown:

      groovy.lang.GroovyRuntimeException: Cannot compare jenkins.plugins.http_request.ResponseContentSupplier with value 'Status: 200, Response: {"success":true}' and java.lang.Integer with value '0'
      

      It's possible that I'm doing something wrong here, but this seems very much like a bug. It's hard for me to imagine what I could do wrong that would cause the stack to get blown apart in such a way.

            janario Janario Oliveira
            tyrelh Tyrel Haveman
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: