Skip to content

Commit

Permalink
Add XML payload support
Browse files Browse the repository at this point in the history
  • Loading branch information
moorereason committed Dec 24, 2019
1 parent 3f4520d commit 3463804
Show file tree
Hide file tree
Showing 42 changed files with 5,174 additions and 4 deletions.
50 changes: 50 additions & 0 deletions docs/Hook-Examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -424,3 +424,53 @@ Travis sends webhooks as `payload=<JSON_STRING>`, so the payload needs to be par
}
]
```

## XML Payload

Given the following payload:

```xml
<app>
<users>
<user id="1" name="Jeff" />
<user id="2" name="Sally" />
</users>
<messages>
<message id="1" from_user="1" to_user="2">Hello!!</message>
</messages>
</app>
```

```json
[
{
"id": "deploy",
"execute-command": "/root/my-server/deployment.sh",
"command-working-directory": "/root/my-server",
"trigger-rule": {
"and": [
{
"match": {
"type": "value",
"parameter": {
"source": "payload",
"name": "app.users.user.0.-name"
},
"value": "Jeff"
}
},
{
"match": {
"type": "value",
"parameter": {
"source": "payload",
"name": "app.messages.message.#text"
},
"value": "Hello!!"
}
},
],
}
}
]
```
27 changes: 26 additions & 1 deletion docs/Referencing-Request-Values.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,31 @@ There are three types of request values:

If the payload contains a key with the specified name "commits.0.commit.id", then the value of that key has priority over the dot-notation referencing.

3. XML Payload

Referencing XML payload parameters is much like the JSON examples above, but XML is more complex.
Take the following XML payload:

```xml
<app>
<users>
<user id="1" name="Jeff" />
<user id="2" name="Sally" />
</users>
<messages>
<message id="1" from_user="1" to_user="2">Hello!!</message>
</messages>
</app>
```

To access a given `user` tag, you must treat them as an array.
So `app.users.user.0.name` yields `Jeff`.

Since there's only one `message` tag, it's not treated as an array.
So `app.messages.message.id` yields `1`.

To access the text within the `message` tag, you would use: `app.messages.message.#text`.

If you are referencing values for environment, you can use `envname` property to set the name of the environment variable like so
```json
{
Expand Down Expand Up @@ -87,4 +112,4 @@ and for query variables you can use
{
"source": "entire-query"
}
```
```
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/adnanh/webhook
go 1.13

require (
github.com/clbanning/mxj v1.8.4
github.com/dustin/go-humanize v1.0.0
github.com/fsnotify/fsnotify v1.4.7 // indirect
github.com/ghodss/yaml v1.0.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/clbanning/mxj v1.8.4 h1:HuhwZtbyvyOw+3Z1AowPkU87JkJUSv751ELWaiTpj8I=
github.com/clbanning/mxj v1.8.4/go.mod h1:BVjHeAH+rl9rs6f+QIpeRl0tfu10SXn1pUSa5PVGJng=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
Expand Down
30 changes: 30 additions & 0 deletions test/hooks.json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,36 @@
}
}
},
{
"id": "xml",
"execute-command": "{{ .Hookecho }}",
"command-working-directory": "/",
"response-message": "success",
"trigger-rule": {
"and": [
{
"match": {
"type": "value",
"parameter": {
"source": "payload",
"name": "app.users.user.0.-name"
},
"value": "Jeff"
}
},
{
"match": {
"type": "value",
"parameter": {
"source": "payload",
"name": "app.messages.message.#text"
},
"value": "Hello!!"
}
},
],
}
},
{
"id": "capture-command-output-on-success-not-by-default",
"pass-arguments-to-command": [
Expand Down
21 changes: 20 additions & 1 deletion test/hooks.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,25 @@
include-command-output-in-response: true
command-working-directory: /

- id: xml
execute-command: '{{ .Hookecho }}'
command-working-directory: /
response-message: success
trigger-rule:
and:
- match:
type: value
parameter:
source: payload
name: app.users.user.0.-name
value: Jeff
- match:
type: value
parameter:
source: payload
name: "app.messages.message.#text"
value: "Hello!!"

- id: capture-command-output-on-success-not-by-default
pass-arguments-to-command:
- source: string
Expand Down Expand Up @@ -113,4 +132,4 @@

- id: warn-on-space
execute-command: '{{ .Hookecho }} foo'
include-command-output-in-response: true
include-command-output-in-response: true
55 changes: 55 additions & 0 deletions vendor/github.com/clbanning/mxj/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3463804

Please sign in to comment.