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

Reference a null payload value #389

Open
dmittakarin8 opened this issue Jan 9, 2020 · 10 comments
Open

Reference a null payload value #389

dmittakarin8 opened this issue Jan 9, 2020 · 10 comments
Milestone

Comments

@dmittakarin8
Copy link

dmittakarin8 commented Jan 9, 2020

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"
      }
    }
  }
}
@moorereason
Copy link
Collaborator

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
Copy link
Collaborator

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"
      }
    }
  }
}

@dmittakarin8
Copy link
Author

dmittakarin8 commented Jan 9, 2020

@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

@moorereason
Copy link
Collaborator

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. 😃

@dmittakarin8
Copy link
Author

@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"
            }
          }
        }
      ]
    }
  }
]

@moorereason moorereason changed the title Hook Rule - NOT Reference a null payload value Sep 21, 2020
@volcano1111
Copy link

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

@moorereason
Copy link
Collaborator

@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 moorereason added this to the 2.9.0 milestone Jan 26, 2021
@adnanh
Copy link
Owner

adnanh commented Jan 26, 2021

@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?

@volcano1111
Copy link

volcano1111 commented Jan 27, 2021

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

@adnanh
Copy link
Owner

adnanh commented Jan 27, 2021

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 :-)

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

No branches or pull requests

4 participants