Skip to content

Commit

Permalink
Fix incorrect split example
Browse files Browse the repository at this point in the history
Also, adds a unit test that combines split and truncate.

Fixes #518

Signed-off-by: Dibyo Mukherjee <[email protected]>
  • Loading branch information
dibyom authored and tekton-robot committed Apr 6, 2020
1 parent e79fe5a commit b85308d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
18 changes: 9 additions & 9 deletions docs/eventlisteners.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ EventListener sink uses to create the Tekton resources. The ServiceAccount needs
a role with the following rules:

<!-- FILE: examples/role-resources/triggerbinding-roles/role.yaml -->

```YAML
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
Expand All @@ -136,6 +135,7 @@ rules:
verbs: ["create"]
```
If your EventListener is using
[`ClusterTriggerBindings`](./clustertriggerbindings.md), you'll need a
ServiceAccount with a
Expand Down Expand Up @@ -290,7 +290,6 @@ if desired. The response body and headers of the last Interceptor is used for
resource binding/templating.

<!-- FILE: examples/eventlisteners/eventlistener-interceptor.yaml -->

```YAML
---
apiVersion: triggers.tekton.dev/v1alpha1
Expand Down Expand Up @@ -321,6 +320,7 @@ spec:
name: pipeline-template
```


### GitHub Interceptors

GitHub Interceptors contain logic to validate and filter webhooks that come from
Expand All @@ -342,7 +342,6 @@ The body/header of the incoming request will be preserved in this Interceptor's
response.

<!-- FILE: examples/eventlisteners/github-eventlistener-interceptor.yaml -->

```YAML
---
apiVersion: triggers.tekton.dev/v1alpha1
Expand All @@ -366,6 +365,7 @@ spec:
name: pipeline-template
```


### GitLab Interceptors

GitLab Interceptors contain logic to validate and filter requests that come from
Expand Down Expand Up @@ -425,7 +425,6 @@ It also modifies the incoming request, adding an extra key to the JSON body,
with a truncated string coming from the hook body.

<!-- FILE: examples/eventlisteners/cel-eventlistener-interceptor.yaml -->

```YAML
apiVersion: triggers.tekton.dev/v1alpha1
kind: EventListener
Expand Down Expand Up @@ -455,6 +454,7 @@ spec:
name: pipeline-template
```


In addition to the standard expressions provided by CEL, Triggers supports some
useful functions for dealing with event data
[CEL expressions](./cel_expressions.md).
Expand All @@ -463,7 +463,6 @@ The body/header of the incoming request will be preserved in this Interceptor's
response.

<!-- FILE: examples/eventlisteners/cel-eventlistener-interceptor.yaml -->

```YAML
apiVersion: triggers.tekton.dev/v1alpha1
kind: EventListener
Expand Down Expand Up @@ -493,13 +492,13 @@ spec:
name: pipeline-template
```


The `filter` expression must return a `true` value if this trigger is to be
processed, and the `overlays` applied.

Optionally, no `filter` expression can be provided, and the `overlays` will be
applied to the incoming body.
<!-- FILE: examples/eventlisteners/cel-eventlistener-no-filter.yaml -->

```YAML
apiVersion: triggers.tekton.dev/v1alpha1
kind: EventListener
Expand All @@ -520,13 +519,13 @@ spec:
name: pipeline-template
```


#### Overlays

The CEL interceptor supports "overlays", these are CEL expressions that are
applied to the body before it's returned to the event-listener.

<!-- FILE: examples/eventlisteners/cel-eventlistener-multiple-overlays.yaml -->

```YAML
apiVersion: triggers.tekton.dev/v1alpha1
kind: EventListener
Expand All @@ -542,13 +541,14 @@ spec:
- key: extensions.truncated_sha
expression: "truncate(body.pull_request.head.sha, 7)"
- key: extensions.branch_name
expression: "truncate(body.ref.split, '/')[2]"
expression: "split(body.ref, '/')[2]"
bindings:
- name: pipeline-binding
template:
name: pipeline-template
```


In this example, the bindings will see two additional fields:

Assuming that the input body looked something like this:
Expand Down Expand Up @@ -611,7 +611,6 @@ the path to an existing value.
Anything that is applied as an overlay can be extracted using a binding e.g.

<!-- FILE: examples/triggerbindings/cel-example-trigger-binding.yaml -->

```YAML
apiVersion: triggers.tekton.dev/v1alpha1
kind: TriggerBinding
Expand All @@ -626,6 +625,7 @@ spec:
```



## Examples

For complete examples, see
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ spec:
- key: extensions.truncated_sha
expression: "truncate(body.pull_request.head.sha, 7)"
- key: extensions.branch_name
expression: "truncate(body.ref.split, '/')[2]"
expression: "split(body.ref, '/')[2]"
bindings:
- name: pipeline-binding
template:
Expand Down
5 changes: 5 additions & 0 deletions pkg/interceptors/cel/cel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,11 @@ func TestExpressionEvaluation(t *testing.T) {
expr: "split(body.value, '/')",
want: types.NewStringList(types.NewRegistry(), []string{"testing"}),
},
{
name: "combine split and truncate",
expr: "truncate(split(body.value, '/')[0], 2)",
want: types.String("te"),
},
{
name: "exact header lookup",
expr: "header.canonical('X-Test-Header')",
Expand Down

0 comments on commit b85308d

Please sign in to comment.