jenkinsci / generic-webhook-trigger-plugin

Can receive any HTTP request, extract any values from JSON or XML and trigger a job with those values available as variables. Works with GitHub, GitLab, Bitbucket, Jira and many more.
https://plugins.jenkins.io/generic-webhook-trigger
410 stars 161 forks source link

Unable to assign NULL as default value #205

Closed gray-image closed 3 years ago

gray-image commented 3 years ago

Version report

Jenkins and plugins versions report:

Jenkins 2.222.3
Generic Webhook Trigger 1.67
(Sorry: unable to successfully execute the instructions at ttps://www.jenkins.io/doc/book/system-administration/diagnosing-errors/#how-to-report-a-bug)
Debian 4.9.168-1

Reproduction steps

Results

Expected result:

In cases where the POST content does not include the JSONPath element, I was hoping that pull_req would be a null-valued variable.

Actual result:

I am new to Groovy/scripting in general, so I will try my best to accurately describe the problem as I understand it. It looks as though null-valued variables are distinct from undeclared variables (this seems perfectly reasonable). In cases where the POST content does not include $.pullRequest.links.self[0].href, the job outputs a "No such property: pull_req for class: groovy.lang.Binding" error instead of there being a null-valued variable named pull_req

I'm new to the open source community, so I hope I'm correctly submitting a valid bug report, and have demonstrated due diligence to work around the problem (assigning a null defaultValue). Ultimately, I would like to understand/learn how to create null-valued variables when expected POST content is absent. If there is an existing mechanism within the Generic Webhook Trigger framework to deal with this, please advise -- I spent my best effort trying to find an existing solution, but could not.

gray-image commented 3 years ago

I should note: I tried:

[ key: "pull_req", value: "\$.pullRequest.links.self[0].href", defaultValue: "" ]

...but it did not have the effect of creating either a null-valued variable named pull_req, nor an empty-string-valued variable. The error, as described in the original bug report, is unchanged.

tomasbjerre commented 3 years ago

I tried with this pipeline:

node {
 properties([
  pipelineTriggers([
   [$class: 'GenericTrigger',
    genericVariables: [
     [
      key: 'href',
      value: '$.href'
     ]
    ],

    causeString: 'Triggered on $ref',

    token: 'abc123',
    tokenCredentialId: '',

    printContributedVariables: true,
    printPostContent: true,

    silentResponse: false
   ]
  ])
 ])

 stage("build") {
  sh '''
  echo Variables from shell:
  echo ref $href
  '''

          if (env.href) {
            echo "href is not null"
          } else {
            echo "href is null"
          }
 }
}

This will print href is null:

curl -v -H "Content-Type: application/json" -X POST -d '{  }' http://localhost:8080/jenkins/generic-webhook-trigger/invoke?token=abc123

This will print href is not null:

curl -v -H "Content-Type: application/json" -X POST -d '{ "href":"some value" }' http://localhost:8080/jenkins/generic-webhook-trigger/invoke?token=abc123
gray-image commented 3 years ago

@tomasbjerre - confirmed that I can reproduce what you described - thank you! The relevant difference appears to be referring to the variable as env.pull_req, and not just pull_req.

As an aside: this reveals something I find difficult with Jenkins scripting...there appears to be different "ways" to refer to variables...I don't know what else to call it: sometimes they are in the "global" scope, sometimes in the env scope, and apparently sometimes both.

Anyhow, this appears to be a deficiency in my understanding of the language, and not a bug. Thank you for enlightening me.