Closed gray-image closed 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.
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
@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.
Version report
Jenkins and plugins versions report:
Reproduction steps
Create a Jenkinsfile with GenericTrigger code as described in the Generic Webhook Trigger documentation. E.g.
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 namedpull_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.