webhook icon indicating copy to clipboard operation
webhook copied to clipboard

Reference a null payload value

Open dmittakarin8 opened this issue 5 years ago • 10 comments

Following the documentation for implementing a NOT rule. I need to check and make sure the head_commit in the payload is NOT null.

Webhook is not accepting my NOT rule and I'd like to know if there is a known issue with using NOT or if anyone has successfully implemented a NOT rule.

{
"not":
  {
    "match":
    {
      "type": "value",
      "value": null,
      "parameter":
      {
        "source": "payload",
        "name": "head_commit"
      }
    }
  }
}

dmittakarin8 avatar Jan 09 '20 17:01 dmittakarin8

Your match rule is asserting that the head_commit value equals the string "null", which I'm assuming is not what you're wanting to do.

I'm assuming this is a JSON payload. Is the head_commit property null? Like this:

{ "head_commit": null }

Or is the property not present at all?

moorereason avatar Jan 09 '20 18:01 moorereason

webhook doesn't currently support testing if a value is actually null since we assume the value property of the Match object will be a string. The closest you can probably get today is this:

{
"not":
  {
    "match":
    {
      "type": "value",
      "value": "",
      "parameter":
      {
        "source": "payload",
        "name": "head_commit"
      }
    }
  }
}

moorereason avatar Jan 09 '20 18:01 moorereason

@moorereason thanks for catching that. I've updated my issue to show it is not the string value "null" but instead null.

I'm getting the following error when trying to create a NOT rule using the following:

"trigger-rule":
{
"not":
  {
    "match":
    {
      "type": "value",
      "value": "",
      "parameter":
      {
        "source": "payload",
        "name": "head_commit"
      }
    }
  }
}

couldn't load hooks from file! error unmarshaling JSON: json: cannot unmarshal array into Go struct field Rules.not of type hook.NotRule

dmittakarin8 avatar Jan 09 '20 18:01 dmittakarin8

I didn't catch that in your original post, but the "not" value is an array of objects. Try this:

"trigger-rule":
{
  "not":
  [
    {
      "match":
      {
        "type": "value",
        "value": "",
        "parameter":
        {
          "source": "payload",
          "name": "head_commit"
        }
      }
    }
  ]
}

PS - To add formatted code blocks in GitHub, read this page. I edited your last two posts for you. :smiley:

moorereason avatar Jan 09 '20 23:01 moorereason

@moorereason thanks again for your response. Unfortunately it is still failing with the same error. Here is my full hook file:

[
  {
    "id": "hook",
    "execute-command": "/home/test.sh",
    "command-working-diretory": "/home",
    "pass-arguments-to-command": [
      {
        "source": "payload",
        "name": "head_commit.id"
      }
    ],
    "trigger-rule": {
      "not": [
        {
          "match": {
            "type": "value",
            "value": "",
            "parameter": {
              "source": "payload",
              "name": "head_commit"
            }
          }
        }
      ]
    }
  }
]

dmittakarin8 avatar Jan 10 '20 13:01 dmittakarin8

Is there any chance to get this implemented? It would help me a lot.

volcano1111 avatar Jan 26 '21 14:01 volcano1111

@volcano1111, I doubt I'll have time to work it in the immediate future. It's not a trivial issue to resolve. I'll add it to the v2.9.0 milestone and hope that we can make it happen.

moorereason avatar Jan 26 '21 15:01 moorereason

@moorereason Check out the feature/new-rules branch, let me know what you think about this approach? Generally, it would be nice if we could replace this rule mechanism with something more scriptable, but still not sure what would be the optimal solution. Lua scripts maybe?

adnanh avatar Jan 26 '21 21:01 adnanh

Haha, I did it guys :) I tried experimenting with different values and value: <nil> for null parameter works just fine. Here is part of my hook rules file, test.sh is just echo "$1" "$2" command:

- id: branch
  execute-command: /home/marker/webhook/test.sh
  pass-arguments-to-command:
  - source: payload
    name: ref
  - source: payload
    name: checkout_sha
  trigger-rule:
    and:
    - match:
        type: value
        value: "0000000000000000000000000000000000000000"
        parameter:
          source: payload
          name: after
    - match:
        type: regex
        value: ^refs\/heads\/
        parameter:
          source: payload
          name: ref
    - match:
        type: value
        value: <nil>
        parameter:
          source: payload
          name: checkout_sha

This is a part of json payload it handles:

{
  "object_kind": "push",
  "event_name": "push",
  "before": "52470e1378e210b52462e45c439e78bba7d3891c",
  "after": "0000000000000000000000000000000000000000",
  "ref": "refs/heads/tardis-1087_test",
  "checkout_sha": null,
...

And here's from log file after executing:

[webhook] 2021/01/27 10:58:57 [0961b4] executing /home/marker/webhook/test.sh (/home/marker/webhook/test.sh) with arguments ["/home/marker/webhook/test.sh" "refs/heads/tardis-1087_test"
 "<nil>"] and environment [] using /home/marker/webhook/ as cwd
[webhook] 2021/01/27 10:58:57 [0961b4] command output: refs/heads/tardis-1087_test <nil>

[webhook] 2021/01/27 10:58:57 [0961b4] finished handling branch

volcano1111 avatar Jan 27 '21 07:01 volcano1111

Yes, it works because we're serializing nil as "<nil>", but keep in mind that same rule will also trigger if the property is a string with contents "<nil>", and that's not really a fully complete solution to the problem at hand, but in your case it might be an OK workaround since "checkout_sha" has a specific format :-)

adnanh avatar Jan 27 '21 09:01 adnanh