Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Having issue parsing github request #330

Open
lubronzhan opened this issue Nov 20, 2024 · 2 comments
Open

Having issue parsing github request #330

lubronzhan opened this issue Nov 20, 2024 · 2 comments
Labels

Comments

@lubronzhan
Copy link

lubronzhan commented Nov 20, 2024

Jenkins and plugins versions report

Plugin version: 2.2.5
Jenkins verison: 2.452.2
I was trying to use example

here to parse the github post body using jsonPath
But github post body is wrapped in a payload element now. for example

{
    "payload": [
        {"action":"created"}
    ]
}

Then using $.action doesn't return anything for action.
Example jenkins config file I have

pipeline {
    agent {
        node {
        }
    }
  triggers {
    GenericTrigger(
     genericVariables: [
      [key: 'payload', value: '$.payload[0]'],
      [key: 'action', value: '$.action']
     ],

     causeString: 'Triggered on $action',

     token: 'token1',

     printContributedVariables: true,
     printPostContent: true,

     silentResponse: false,

     shouldNotFlatten: false,
    )
  }
  stages {
    stage('Some step') {
      steps {
        sh "echo $payload"
        sh "echo $action"
      }
    }
  }
}

Running the pipeline got below error

14:47:25  Also:   org.jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: f89c7f77-e85b-40d7-a209-583ddb326aa7
14:47:25  groovy.lang.MissingPropertyException: No such property: action for class: groovy.lang.Binding
14:47:25  	at groovy.lang.Binding.getVariable(Binding.java:63)
14:47:25  	at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:285)
14:47:25  	at org.kohsuke.groovy.sandbox.impl.Checker$7.call(Checker.java:375)

Even if I change from $.action to $.payload[0].action, still nothing returns. I can only get the output of $.payload[0]

Does anyone also run into this?

@lubronzhan lubronzhan added the bug label Nov 20, 2024
@lubronzhan lubronzhan changed the title Issue Parsing github response Having Issue parsing github response Nov 20, 2024
@lubronzhan lubronzhan changed the title Having Issue parsing github response Having issue parsing github request Nov 20, 2024
@tomasbjerre
Copy link
Contributor

Works on my machine.

I created a pipeline like this:


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

     causeString: 'Triggered on $action',

     token: 'token1',

     printContributedVariables: true,
     printPostContent: true,

     silentResponse: false,

     shouldNotFlatten: false,
   ]
  ])
 ])

 stage("build") {
  sh "echo $payload"
 }
}

I run that, manually, and I get your error. And that is totally ok, as long as the properties are applied and they are.

I trigger it with:

curl -v \
 -H "Content-Type: application/json" \
 -X POST \
 -d '{ "payload": [ {"action":"created"} ] }' \
 "http://localhost:8080/jenkins/generic-webhook-trigger/invoke?token=token1"

It triggers and logs:

 Received:

{ "payload": [ {"action":"created"} ] }


Contributing variables:

    payload = {"action":"created"}
    payload_action = created


[Pipeline] node
Running on [Jenkins](http://localhost:8080/jenkins/computer/(built-in)/) in /home/bjerre/workspace/generic-webhook-trigger-plugin/work/workspace/gh
[Pipeline] {
[Pipeline] properties
[Pipeline] stage
[Pipeline] { (build)
[Pipeline] sh
+ echo {action:created}
{action:created}
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

@lubronzhan
Copy link
Author

Sorry forgot to put the jenkins version 2.452.2. Which jenkins version is yours? I guess it shouldn't be related to jenkins version?
I also tried your scripted jenkins file, I only saw payload got contributed, not payload_action

Contributing variables:
10:05:57  
10:05:57      payload = {"action":...

I directly use github to send the request, since curling our jenkins requires a crumb.

Right now the workaround I have is to parse that payload in the script to get information I need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants