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

Multiple regex filtering using json path. #245

Closed aytemuryakup closed 1 year ago

aytemuryakup commented 1 year ago

Hi everyone, I'm using the generic webhook trigger plugin when triggering jenkins via gitlab. I am using the path based generic webhook trigger feature. In addition, I want to use the user_name based trigger feature. I haven't seen more than one filtering like this, is this usage possible?

My Default trigger pipeline script like this:

pipeline {
    agent any
    triggers {
        GenericTrigger(
        genericVariables: [
            [key: "changed_files", value: "\$.commits[*].['modified','added','removed'][*]"]
        ],
        genericRequestVariables: [
        [key: 'requestWithNumber', regexpFilter: ''],
        [key: 'requestWithString', regexpFilter: '$changed_files']
        ],
        causeString: 'Triggered on $changed_files',
        tokenCredentialId: 'gitlab-trigger-token',
        printContributedVariables: true,
        printPostContent: true,
        silentResponse: false,
        regexpFilterText: '$changed_files',
        regexpFilterExpression: "wheater-forecast-app/PrivateHub-Test/[^\"]+?"
        )
    }

Is it possible to use it like this? Or is the configuration correct?

What I want to do: If the committing user is DevOps, the pipeline is not triggered.

pipeline {
    agent any
    triggers {
        GenericTrigger(
        genericVariables: [
            [key: "changed_files", value: "\$.commits[*].['modified','added','removed'][*]"],
            //user_name filter
            [key: "commit_user", value: "\$.user_name"]
        ],
        genericRequestVariables: [
        [key: 'requestWithNumber', regexpFilter: ''],
        [key: 'requestWithString', regexpFilter: '$changed_files'],
        //user_name filter
        [key: 'requestWithNumber2', regexpFilter: ''],
        [key: 'requestWithString2', regexpFilter: '$commit_user'],
        ],
        causeString: 'Triggered on $changed_files',
        tokenCredentialId: 'gitlab-trigger-token',
        printContributedVariables: true,
        printPostContent: true,
        silentResponse: false,
        regexpFilterText: '$changed_files $commit_user',
        regexpFilterExpression: "wheater-forecast-app/PrivateHub-Test/[^\"]+?\\^(?!DevOps\$)"
        )
    }

What I want to do in the trigger block above: regexpFilterText: I'm calling _changesfiles and commit_user regexes.

regexpFilterExpression:

gitlab json request output:

{
  "object_kind": "push",
  "event_name": "push",
  "before": "***",
  "after": "***",
  "ref": "refs/heads/main",
  "checkout_sha": "***",
  "message": null,
  "user_id": 42,
  "user_name": "Yakup***",
  "user_username": "***",
  "user_email": "",
  "user_avatar": "***",
  "project_id": 11,
  "project": {
    "id": 11,
    "name": "sampleApi",
    "description": "",
    "web_url": "***",
    "avatar_url": null,
    "git_ssh_url": "***",
    "git_http_url": "***",
    "namespace": "DevOpsPoc",
    "visibility_level": 0,
    "path_with_namespace": "***",
    "default_branch": "main",
    "ci_config_path": null,
    "homepage": "***",
    "url": "***",
    "ssh_url": "***",
    "http_url": "***"
  },
  "commits": [
    {
      "id": "***",
      "message": "generic webhook trigger add.\n",
      "title": "generic webhook trigger add.",
      "timestamp": "2022-08-09T14:48:56+03:00",
      "url": "****",
      "author": {
        "name": "Yakup***",
        "email": "***"
      },
      "added": [

      ],
      "modified": [
        "wheater-forecast-app/PrivateHub-Test/Program.cs"
      ],
      "removed": [

      ]
    }
  ],
  "total_commits_count": 1,
  "push_options": {
  },
  "repository": {
    "name": "sampleApi",
    "url": "*",
    "description": "",
    "homepage": "***",
    "git_http_url": "***",
    "git_ssh_url": "***",
    "visibility_level": 0
  }
}
tomasbjerre commented 1 year ago

I thnk this regexp might work:

regexpFilterExpression: ".*"wheater-forecast-app/PrivateHub-Test/[^\"]+?\".*\\s(?!DevOps\$)"

https://jex.im/regulex/#!flags=&re=.*%22wheater-forecast-app%2FPrivateHub-Test%2F%5B%5E%22%5D%2B%3F%22.*%5Cs(%3F!DevOps%24)

aytemuryakup commented 1 year ago

After opening Issue, I tried combining 2 regex but triggering did not work properly. When I tried the regex you sent, it worked fine. Thanks for your support.

        GenericTrigger(
        genericVariables: [
            [key: "changed_files", value: "\$.commits[*].['modified','added','removed'][*]"],
            [key: "commit_user", value: "\$.user_name"] 
        ],
        causeString: 'Triggered on $changed_files',
        tokenCredentialId: 'gitlab-trigger-token',
        printContributedVariables: true,
        printPostContent: true,
        silentResponse: false,
        regexpFilterText: '$changed_files $commit_user',
        regexpFilterExpression: ".*\"wheater-forecast-app/PrivateHub-Test/[^\"]+?\".*\\s(?!DevOps\$)"
        )