func MakeCELPredicate()

in lib/notifiers/notifiers.go [466:495]


func MakeCELPredicate(filter string) (*CELPredicate, error) {
	env, err := cel.NewEnv(
		// Declare the `build` variable for useage in CEL programs.
		cel.Declarations(decls.NewIdent("build", decls.NewObjectType(cloudBuildProtoPkg+".Build"), nil)),
		// Register the `Build` type in the environment.
		cel.Types(new(cbpb.Build)),
		// `Container` is necessary for better (enum) scoping
		// (i.e with this, we don't need to use the fully qualified proto path in our programs).
		cel.Container(cloudBuildProtoPkg),
	)
	if err != nil {
		return nil, fmt.Errorf("failed to create a CEL env: %w", err)
	}

	ast, issues := env.Compile(filter)
	if issues != nil && issues.Err() != nil {
		return nil, fmt.Errorf("failed to compile CEL filter %q: %w", filter, issues.Err())
	}

	if !proto.Equal(ast.ResultType(), decls.Bool) {
		return nil, fmt.Errorf("expected CEL filter %q to have a boolean result type, but was %v", filter, ast.ResultType())
	}

	prg, err := env.Program(ast, cel.EvalOptions(cel.OptOptimize))
	if err != nil {
		return nil, fmt.Errorf("failed to create CEL program from filter %q: %w", filter, err)
	}

	return &CELPredicate{prg}, nil
}