Skip to content

Instantly share code, notes, and snippets.

@chmouel
Created June 14, 2022 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chmouel/e2051f42cbe9c4e5f822355c130ca6d4 to your computer and use it in GitHub Desktop.
Save chmouel/e2051f42cbe9c4e5f822355c130ca6d4 to your computer and use it in GitHub Desktop.
diff --git a/pkg/matcher/cel.go b/pkg/matcher/cel.go
index 822d8812..c0a9a416 100644
--- a/pkg/matcher/cel.go
+++ b/pkg/matcher/cel.go
@@ -5,8 +5,11 @@ import (
"github.com/google/cel-go/cel"
"github.com/google/cel-go/checker/decls"
+ "github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
+ "github.com/google/cel-go/interpreter/functions"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
+ exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
)
func celEvaluate(expr string, event *info.Event) (ref.Val, error) {
@@ -17,6 +20,7 @@ func celEvaluate(expr string, event *info.Event) (ref.Val, error) {
}
env, err := cel.NewEnv(
+ cel.Lib(celPac{}),
cel.Declarations(
decls.NewVar("event", decls.String),
decls.NewVar("target_branch", decls.String),
@@ -46,3 +50,31 @@ func celEvaluate(expr string, event *info.Event) (ref.Val, error) {
}
return out, nil
}
+
+type celPac struct{}
+
+func Triggers() cel.EnvOption {
+ return cel.Lib(celPac{})
+}
+
+func (t celPac) ProgramOptions() []cel.ProgramOption {
+ return []cel.ProgramOption{
+ cel.Functions(
+ &functions.Overload{
+ Operator: "fileChanged",
+ Function: fileChanged},
+ )}
+}
+
+func fileChanged(vals ...ref.Val) ref.Val {
+ return types.Bool(true)
+}
+
+func (celPac) CompileOptions() []cel.EnvOption {
+ mapStrDyn := decls.NewMapType(decls.String, decls.Dyn)
+ return []cel.EnvOption{cel.Declarations(
+ decls.NewFunction("fileChanged",
+ decls.NewInstanceOverload("file_changed",
+ []*exprpb.Type{mapStrDyn, decls.String, decls.String}, decls.Bool)),
+ )}
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment