diff --git a/docs/eventlisteners.md b/docs/eventlisteners.md index 2970d9868..5a20a8b0c 100644 --- a/docs/eventlisteners.md +++ b/docs/eventlisteners.md @@ -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 @@ -136,6 +135,7 @@ rules: verbs: ["create"] ``` + If your EventListener is using [`ClusterTriggerBindings`](./clustertriggerbindings.md), you'll need a ServiceAccount with a @@ -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 @@ -321,6 +320,7 @@ spec: name: pipeline-template ``` + ### GitHub Interceptors GitHub Interceptors contain logic to validate and filter webhooks that come from @@ -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 @@ -366,6 +365,7 @@ spec: name: pipeline-template ``` + ### GitLab Interceptors GitLab Interceptors contain logic to validate and filter requests that come from @@ -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 @@ -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). @@ -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 @@ -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 @@ -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 @@ -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: @@ -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 @@ -626,6 +625,7 @@ spec: ``` + ## Examples For complete examples, see diff --git a/examples/eventlisteners/cel-eventlistener-multiple-overlays.yaml b/examples/eventlisteners/cel-eventlistener-multiple-overlays.yaml index 129fc11c5..7d39ad56d 100644 --- a/examples/eventlisteners/cel-eventlistener-multiple-overlays.yaml +++ b/examples/eventlisteners/cel-eventlistener-multiple-overlays.yaml @@ -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: diff --git a/pkg/interceptors/cel/cel_test.go b/pkg/interceptors/cel/cel_test.go index efb4b3fb7..c140bd199 100644 --- a/pkg/interceptors/cel/cel_test.go +++ b/pkg/interceptors/cel/cel_test.go @@ -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')",