From 1ffcdbf47579abc5666a09f6ad74283186e1bc47 Mon Sep 17 00:00:00 2001 From: Dibyo Mukherjee Date: Mon, 6 Apr 2020 17:15:14 -0400 Subject: [PATCH] Fix incorrect split example Also, adds a unit test that combines split and truncate. Fixes #518 Signed-off-by: Dibyo Mukherjee --- docs/eventlisteners.md | 18 +++++++++--------- .../cel-eventlistener-multiple-overlays.yaml | 2 +- pkg/interceptors/cel/cel_test.go | 5 +++++ 3 files changed, 15 insertions(+), 10 deletions(-) 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: - ```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. - ```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. - ```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. - ```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. - ```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. - ```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. - ```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. - ```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')",