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

Add documentation to handle github issue_comment events #200

Closed okainov closed 3 years ago

okainov commented 3 years ago

In this folder https://github.com/jenkinsci/generic-webhook-trigger-plugin/tree/master/src/test/resources/org/jenkinsci/plugins/gwt/bdd/github I see several useful examples. However it misses the case of triggering on issue_comment event.

Especially interesting part is how to make webhook trigger actually work on both pull_request and issue_comment events. The reason is that number which is ID of pull request is placed in different JSON places in the payload of those webhooks. And so far I have seen no any option how can I extract "one of" or "first" value from JSONPath.

What I kind of expect is $..number[0], but this unfortunately doesn't work.

tomasbjerre commented 3 years ago

Can you provide samples of both webhooks? And point out what values it is you are looking for.

okainov commented 3 years ago

Sure https://docs.github.com/en/developers/webhooks-and-events/webhook-events-and-payloads#issue_comment

{
  "issue": {
    "number": 1,
...
  }
...
}

https://docs.github.com/en/developers/webhooks-and-events/webhook-events-and-payloads#pull_request

...
{
  "number": 2,
...
}
tomasbjerre commented 3 years ago

I added a feature here: https://github.com/jenkinsci/generic-webhook-trigger-plugin/blob/master/src/test/resources/org/jenkinsci/plugins/gwt/bdd/github/github-pull-request-and-issue-comment.feature

With that feature you would need to check, in your build script, if pull_request_number or issue_number variable is available and use the one that is available.

Is the issue-comment webhook content really what you are getting? Looks really strange if one opens a pull request on X and the webhook contains a link to an issue:

{
  "action": "created",
  "issue": {
    "url": "https://api.github.com/repos/Codertocat/Hello-World/issues/X",
...

And this is relevant because you may want to use that to filter out issue-comment comments that are not on a pull request.

okainov commented 3 years ago

Welcome to the wonderful GitHub API, yes, it is as dirty and as contrintuitive as hell. That's why I asked :)

With that feature you would need to check, in your build script, if pull_request_number or issue_number variable is available and use the one that is available.

I think you didn't get the whole point :) Correct me if I'm wrong, but typically one uses webhooks to start jobs. Start mean, apart of others, checkout. Checkout Jenkinsfile and then code. Checkout should be done from a branch. Checkout is done before any script/groovy logic is executed. It's not possible by definition in Jenkins to "check in my build script" when one needs to understand what to checkout.

tomasbjerre commented 3 years ago

Are you saying you want to:

Perhaps it helps that if you configure variables like:

      | variable            | expression            | expressionType  | defaultValue | regexpFilter  |
      | issue_number        | $.issue.number        | JSONPath        |              |               |
      | pull_request_number | $.pull_request.number | JSONPath        |              |               |

You the value of $issue_number$pull_request_number will be the number. As one variable will be empty and one will be the number.

I think it should work if you also add the same variables in "this job is parameterized" as described here: https://github.com/jenkinsci/generic-webhook-trigger-plugin#default-values

okainov commented 3 years ago

You the value of $issue_number$pull_request_number will be the number. As one variable will be empty and one will be the number.

Yeah that's kind of the same what I came up with couple of days ago. Just we use $..number as Jsonpath for GITHUBPR variable and then use GITHUBPR_0. But while it works, I find it quite ugly :(