Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pkg/cel/cel.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/google/cel-go/common/decls"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
"github.com/google/cel-go/ext"
)

func evaluate(expr string, env *cel.Env, data map[string]any) (ref.Val, error) {
Expand Down Expand Up @@ -90,7 +91,10 @@ func Value(query string, body any, headers, pacParams map[string]string, changed
decls.NewVariable("pull_request_labels", types.StringType),
decls.NewVariable("pull_request_number", types.StringType),
decls.NewVariable("git_auth_secret", types.StringType),
))
),
ext.Strings(),
ext.Lists(),
)
val, err := evaluate(query, celDec, map[string]any{
"body": jsonMap,
"pac": pacParams,
Expand Down
47 changes: 47 additions & 0 deletions pkg/templates/templating_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,53 @@ func TestReplacePlaceHoldersVariables(t *testing.T) {
"hello": "world",
},
},
{
name: "Test CEL with string join",
template: `all files: {{ files.all.join(", ") }}`,
expected: `all files: file1.txt, file2.txt, file3.txt`,
dicto: map[string]string{
"revision": "main",
},
changedFiles: map[string]any{
"all": []string{"file1.txt", "file2.txt", "file3.txt"},
},
headers: http.Header{
"Test": []string{"value"},
},
rawEvent: map[string]any{
"hello": "world",
},
},
{
name: "Test CEL with string replace",
template: `{{ cel: trigger_comment.replace("bad", "good") }}`,
expected: `a good comment!`,
dicto: map[string]string{
"trigger_comment": "a bad comment!",
},
changedFiles: map[string]any{},
headers: http.Header{
"Test": []string{"value"},
},
rawEvent: map[string]any{
"hello": "world",
},
},
{
name: "Test CEL with string substring",
template: `{{ cel: "HELLOWORLD".substring(0, 5) }}`,
expected: `HELLO`,
dicto: map[string]string{
"trigger_comment": "a bad comment!",
},
changedFiles: map[string]any{},
headers: http.Header{
"Test": []string{"value"},
},
rawEvent: map[string]any{
"hello": "world",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
28 changes: 28 additions & 0 deletions test/github_pullrequest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,34 @@ func TestGithubGHEPullRequestCelPrefix(t *testing.T) {
assert.NilError(t, err)
}

func TestGithubGHEPullRequestCELJoin(t *testing.T) {
ctx := context.Background()
g := &tgithub.PRTest{
Label: "Github CEL String Join",
YamlFiles: []string{"testdata/pipelinerun-cel-string-join.yaml"},
GHE: true,
}
g.RunPullRequest(ctx, t)
defer g.TearDown(ctx, t)

prs, err := g.Cnx.Clients.Tekton.TektonV1().PipelineRuns(g.TargetNamespace).List(ctx, metav1.ListOptions{})
assert.NilError(t, err)
assert.Assert(t, len(prs.Items) >= 1, "Expected at least one PipelineRun")

err = twait.RegexpMatchingInPodLog(
ctx,
g.Cnx,
g.TargetNamespace,
fmt.Sprintf("tekton.dev/pipelineRun=%s,tekton.dev/pipelineTask=cel-string-join-test", prs.Items[0].Name),
"step-test-cel-string-join-values",
*regexp.MustCompile(".*tekton/pipelinerun-cel-string-join.yaml"),
"",
2,
nil,
)
assert.NilError(t, err)
}

// Local Variables:
// compile-command: "go test -tags=e2e -v -info TestGithubPullRequest$ ."
// End:
20 changes: 20 additions & 0 deletions test/testdata/pipelinerun-cel-string-join.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
name: "\\ .PipelineName //"
annotations:
pipelinesascode.tekton.dev/target-namespace: "\\ .TargetNamespace //"
pipelinesascode.tekton.dev/on-cel-expression: |
target_branch == "\\ .TargetBranch //" && event == "\\ .TargetEvent //"
spec:
pipelineSpec:
tasks:
- name: cel-string-join-test
taskSpec:
steps:
- name: test-cel-string-join-values
image: registry.access.redhat.com/ubi10/ubi-micro
script: |
echo "{{ cel: files.all.join(", ") }}"
exit 0
86 changes: 86 additions & 0 deletions vendor/github.com/google/cel-go/ext/BUILD.bazel

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

Loading
Loading