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
404 stars 159 forks source link

x-www-form-urlencoded receiver is not properly converted in JSON #243

Closed elrandira closed 1 year ago

elrandira commented 2 years ago

{ "payload": [ "{\"ref\":\"refs\/heads\/master\",\"after\":\"ee311c46a7df88f01aa7a8d35984b1fe89bd3d20\",\"before\":\"f03bec6afe3af8de2a75210eaaa6e5a51370773f\",\"repository\":{\"id\":4765,\"name\":\"robotframework\",\"full_name\":\"robotframework\"},\"pusher\":{\"name\":\"lpinson\",\"email\":\"\"},\"sender\":{\"id\":841,\"uri\":\"users\/841\",\"user_url\":\"\/users\/lpinson\",\"real_name\":\"lpinson\",\"display_name\":\"lpinson (lpinson)\",\"username\":\"lpinson\",\"ldap_id\":\"lpinson\",\"avatar_url\":"",\"is_anonymous\":false,\"has_avatar\":true}}" ] }

Contributing variables:

ref = 
ref_payload = 

ref is defined as JSONPath $.ref
ref_payload is defined as JSONPath $.payload[0].ref

 * Post content received: see above
 * Expected result: receiver content available as JSON
 * actual result: "unreadable" receiver content

My employer is working with [Tuleap ](https://www.tuleap.org/) to manage projects and it includes its own Git server. It is possible to configure Jenkins webhooks and custom webhooks. As the Jenkins webhook didn't allow me to trigger a build on a specific branch, I decided to look into generic-webhook-trigger plugin.

After struggling to understand that a token is necessary to properly trigger my job ( as in #234 ), I found out that the payload is not properly decoded as JSON. 
Tuleap uses only _Content-Type: application/x-www-form-urlencoded_, it is not configurable at the moment :(. I feared that is was not handled by this plugin but it seems to be solved since #225. However the payload stays in a string format instead of JSON format...

Any idea how to solve this?

ref: https://tuleap.net/doc/en/user-guide/integration/webhook.html
elrandira commented 2 years ago

I worked around this by parsing the whole payload string. payload is defined as JSONPath $.payload[0]

I compare then $payload to .*\"ref\":\"refs\\/heads\\/master\".*

not pretty but it works

tomasbjerre commented 2 years ago

The problem is not in this plugin, it is on the sender side.

The body:

{
  "payload": [
    "{\"ref\":\"refs\\/heads\\/master\",\"after\":\"ee311c46a7df88f01aa7a8d35984b1fe89bd3d20\",\"before\":\"f03bec6afe3af8de2a75210eaaa6e5a51370773f\",\"repository\":{\"id\":4765,\"name\":\"robotframework\",\"full_name\":\"robotframework\"},\"pusher\":{\"name\":\"lpinson\",\"email\":\"<redacted>\"},\"sender\":{\"id\":841,\"uri\":\"users\\/841\",\"user_url\":\"\\/users\\/lpinson\",\"real_name\":\"lpinson\",\"display_name\":\"lpinson (lpinson)\",\"username\":\"lpinson\",\"ldap_id\":\"lpinson\",\"avatar_url\":"<redacted>",\"is_anonymous\":false,\"has_avatar\":true}}"
  ]
}

I being sent as x-www-form-urlencoded and what you see in the log is the decoded value.

Seems strange to wrap the json within payload attribute, but not much to do about that here on the receiver side.

elrandira commented 2 years ago

That's what I feared. I'll try to dig into the code since Tuleap is open source. Thanks for the feedback Tomas