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

JSONPath return string, but plugin returns string with square brackets #260

Closed izenk closed 1 year ago

izenk commented 1 year ago

Pipeline

pipeline {
    agent {
        label "master"
    }
    options {
        buildDiscarder logRotator(artifactDaysToKeepStr: '1', artifactNumToKeepStr: '', daysToKeepStr: '31', numToKeepStr: '')
    }
    triggers {
      GenericTrigger(
        genericVariables: [
            [
                key: 'sentry_issue_str',
                value: '$',
                defaultValue: '',
                expressionType: 'JSONPath',
                regexpFilter: ''
            ],
            [
                key: 'customer',
                value: '$.event.tags[?(@[0] == "customer")][1]',
                defaultValue: '',
                expressionType: 'JSONPath',
                regexpFilter: ''
            ]
        ],
        causeString: 'Test sentry to jira issue',
        printContributedVariables: true,
        printPostContent: true,
        regexpFilterExpression: '',
        regexpFilterText: '',
        shouldNotFlattern: false,
        silentResponse: true,
        token: 'token',
        tokenCredentialId: ''
      )
    }

    stages {
        stage('Hello') {
            steps {
                sh 'echo "DEBUG: !!!!!!!!!!"'
            }
        }
    }
}

Job log

Test sentry to jira issue
GenericWebhookEnvironmentContributor
Received:
{
  "id": "1043",
  "project": "test_sentry_to_jira_issue",
  "project_name": "test_sentry_to_jira_issue",
  "project_slug": "test_sentry_to_jira_issue",
  "logger": null,
  "level": "error",
  "culprit": "",
  "message": "",
  "url": "<some_url>",
  "triggering_rules": [
    "test_sentry_to_jira"
  ],
  "event": {
    "event_id": "a75b0af569e1418083cf4c1ca459b53e",
    "level": "error",
    "version": "7",
    "type": "error",
    "fingerprint": [
      "{{ default }}"
    ],
    "culprit": "",
    "logger": "",
    "modules": {
      "actioncable": "7.0.4.2",
      "actionmailbox": "7.0.4.2",
      "actionmailer": "7.0.4.2",
      "actionpack": "7.0.4.2",
      "actiontext": "7.0.4.2",
      "actionview": "7.0.4.2",
      "activejob": "7.0.4.2",
      "activemodel": "7.0.4.2",
      "activerecord": "7.0.4.2",
      "activestorage": "7.0.4.2",
      "activesupport": "7.0.4.2",
      "bootsnap": "1.16.0",
      "builder": "3.2.4",
      "bundler": "2.4.7",
      "concurrent-ruby": "1.2.2",
      "crass": "1.0.6",
      "date": "3.3.3",
      "debug": "1.7.1",
      "erubi": "1.12.0",
      "faraday": "2.7.4",
      "faraday-net_http": "3.0.2",
      "globalid": "1.1.0",
      "i18n": "1.12.0",
      "io-console": "0.6.0",
      "irb": "1.6.2",
      "loofah": "2.19.1",
      "mail": "2.8.1",
      "marcel": "1.0.2",
      "method_source": "1.0.0",
      "mini_mime": "1.1.2",
      "minitest": "5.17.0",
      "msgpack": "1.6.0",
      "net-imap": "0.3.4",
      "net-pop": "0.1.2",
      "net-protocol": "0.2.1",
      "net-smtp": "0.3.3",
      "nio4r": "2.5.8",
      "nokogiri": "1.14.2",
      "racc": "1.6.2",
      "rack": "2.2.6.3",
      "rack-test": "2.0.2",
      "rails": "7.0.4.2",
      "rails-dom-testing": "2.0.3",
      "rails-html-sanitizer": "1.5.0",
      "railties": "7.0.4.2",
      "rake": "13.0.6",
      "reline": "0.3.2",
      "ruby2_keywords": "0.0.5",
      "sentry-rails": "5.8.0",
      "sentry-ruby": "5.8.0",
      "thor": "1.2.1",
      "timeout": "0.3.2",
      "tzinfo": "2.0.6",
      "websocket-driver": "0.7.5",
      "websocket-extensions": "0.1.5",
      "zeitwerk": "2.6.7"
    },
    "platform": "ruby",
    "timestamp": 1680989144.0,
    "received": 1680989144.447589,
    "release": "0.0.0",
    "environment": "production",
    "contexts": {
      "os": {
        "name": "Linux",
        "version": "#42~20.04.1-Ubuntu SMP Wed Mar 1 19:17:41 UTC 2023",
        "build": "5.15.0-1035-azure",
        "kernel_version": "#42~20.04.1-Ubuntu SMP Wed Mar 1 19:17:41 UTC 2023",
        "type": "os"
      },
      "runtime": {
        "name": "ruby",
        "version": "ruby 3.2.1 (2023-02-08 revision 31819e82c8) +YJIT [x86_64-linux]",
        "type": "runtime"
      }
    },
    "exception": {
      "values": [
        {
          "type": "Exception",
          "value": "test99 (Exception)",
          "module": "",
          "thread_id": 2500
        }
      ]
    },
    "threads": {
      "values": [
        {
          "id": 2500,
          "crashed": true,
          "current": true
        }
      ]
    },
    "tags": [
      [
        "cmp",
        "acronis"
      ],
      [
        "commit",
        "none"
      ],
      [
        "customer",
        "test_sentry_to_jira"
      ],
      [
        "environment",
        "production"
      ],
      [
        "level",
        "error"
      ],
      [
        "os",
        "Linux #42~20.04.1-Ubuntu SMP Wed Mar 1 19:17:41 UTC 2023"
      ],
      [
        "os.name",
        "Linux"
      ],
      [
        "runtime",
        "ruby ruby 3.2.1 (2023-02-08 revision 31819e82c8) +YJIT [x86_64-linux]"
      ],
      [
        "runtime.name",
        "ruby"
      ],
      [
        "sentry:release",
        "0.0.0"
      ],
      [
        "server_name",
        "69d63c307eb5"
      ],
      [
        "service",
        "none"
      ]
    ],
    "sdk": {
      "name": "sentry.ruby",
      "version": "5.8.0"
    },
    "key_id": "46",
    "project": 46,
    "grouping_config": {
      "enhancements": "eJybzDRxY3J-bm5-npWRgaGlroGxrpHxBABcYgcZ",
      "id": "newstyle:2019-10-29"
    },
    "_metrics": {
      "bytes.ingested.event": 2105,
      "bytes.stored.event": 2812
    },
    "_ref": 46,
    "_ref_version": 2,
    "hashes": [
      "ec771899df8d9dbb48764b9f1f72e7e9"
    ],
    "metadata": {
      "display_title_with_tree_label": false,
      "type": "Exception",
      "value": "test99 (Exception)"
    },
    "nodestore_insert": 1680989145.51168,
    "title": "Exception: test99 (Exception)",
    "id": "a75b0af569e1418083cf4c1ca459b53e"
  }
}

 Contributing variables:  
      customer = ["test_sentry_to_jira"]
      customer_0 = test_sentry_to_jira

Plugin returns it as string with brackets, while I expect just a simple value "test_sentry_to_jira":

01:34:39  [Pipeline] echo
01:34:39  class java.lang.String
01:34:39  [Pipeline] echo
01:34:39  ["test_sentry_to_jira"]
tomasbjerre commented 1 year ago

You pipeline does:

sh 'echo "DEBUG: !!!!!!!!!!"'

And you supply build log:

01:34:39  [Pipeline] echo
01:34:39  class java.lang.String
01:34:39  [Pipeline] echo
01:34:39  ["test_sentry_to_jira"]

So hard to say exactly what you are doing here.

The build log states:

 Contributing variables:  
      customer = ["test_sentry_to_jira"]
      customer_0 = test_sentry_to_jira

I would suggest:

izenk commented 1 year ago

@tomasbjerre Ye, I copied build log from another build. Just see "Contributing variables" section. What do you mean under "Change the JSONPath to not match a branch, but an leaf. Not a list, just a single element."

Im using customer_0 right now, but it's just workaround, not fix.

tomasbjerre commented 1 year ago

This is not the place to ask about jsonpath. I would suggest trying this Stackoverflow and/or fiddle with an online evaluator like https://jsonpath.com/ . You will see the result of your jsonpath is a list.

izenk commented 1 year ago

@tomasbjerre I checked with https://jsonpath.com/ for two variables: Both are shown as list

Screenshot 2023-04-11 at 15 25 56 Screenshot 2023-04-11 at 15 26 39

But plugin returns one as simple string, and other one as string with square brackets

15:23:22  customer var type: class java.lang.String
15:23:22  [Pipeline] echo
15:23:22  customer var value: ["test_sentry_to_jira"]
15:23:22  [Pipeline] echo
15:23:22  test_var type: class java.lang.String
15:23:22  [Pipeline] echo
15:23:22  test_var value: test_sentry_to_jira_issue

Here is trigger config

    triggers {
      GenericTrigger(
        genericVariables: [
            [
                key: 'sentry_issue_str',
                value: '$',
                defaultValue: '',
                expressionType: 'JSONPath',
                regexpFilter: ''
            ],
            [
                key: 'customer',
                value: '$.event.tags[?(@[0] == "customer")][1]',
                defaultValue: '',
                expressionType: 'JSONPath',
                regexpFilter: ''
            ],
            [
                key: 'test_var',
                value: '$.project',
                defaultValue: '',
                expressionType: 'JSONPath',
                regexpFilter: ''
            ],
        ],
        causeString: 'Test sentry to jira issue',
        printContributedVariables: true,
        printPostContent: true,
        regexpFilterExpression: '',
        regexpFilterText: '',
        shouldNotFlattern: false,
        silentResponse: true,
        token: 'testsentrytojiratoken',
        tokenCredentialId: ''
      )
    }