feat(eda): add optional playbook action ids and when reference validation
This commit is contained in:
1 parent
a1302da320
commit
7a7fb9f9bb
4 files changed
+248
-3
No files matched your search
@@ -16,7 +16,10 @@
|
||||
package expr
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"cel.dev/cel-go/cel"
|
||||
celast "cel.dev/cel-go/common/ast"
|
||||
)
|
||||
|
||||
// DefaultCostLimit bounds evaluation of every playbook expression (J5 in the
|
||||
@@ -79,12 +82,56 @@ func (e *Env) Compile(src string) (*Program, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Program{prg: prg}, nil
|
||||
return &Program{prg: prg, actionIDs: referencedActionIDs(ast)}, nil
|
||||
}
|
||||
|
||||
// referencedActionIDs walks the compiled AST and returns the sorted, de-
|
||||
// duplicated set of action ids an expression reads through `actions.<id>`.
|
||||
// `actions` is declared map(string,dyn), so CEL type-checking cannot reject
|
||||
// an unknown id; the playbook loader compares this set against the ids of
|
||||
// prior actions to reject unknown references at load (J1 in
|
||||
// docs/prds/playbook-expression-syntax.md). The map-index form
|
||||
// (`actions["notify"]`) is a runtime lookup, not a field selection, and is
|
||||
// intentionally not collected here.
|
||||
func referencedActionIDs(ast *cel.Ast) []string {
|
||||
if ast == nil || ast.NativeRep() == nil {
|
||||
return nil
|
||||
}
|
||||
seen := map[string]struct{}{}
|
||||
visitor := celast.NewExprVisitor(func(e celast.Expr) {
|
||||
if e.Kind() != celast.SelectKind {
|
||||
return
|
||||
}
|
||||
sel := e.AsSelect()
|
||||
operand := sel.Operand()
|
||||
if operand.Kind() != celast.IdentKind || operand.AsIdent() != "actions" {
|
||||
return
|
||||
}
|
||||
seen[sel.FieldName()] = struct{}{}
|
||||
})
|
||||
celast.PreOrderVisit(ast.NativeRep().Expr(), visitor)
|
||||
ids := make([]string, 0, len(seen))
|
||||
for id := range seen {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
sort.Strings(ids)
|
||||
return ids
|
||||
}
|
||||
|
||||
// Program is a compiled, cost-limited playbook expression.
|
||||
type Program struct {
|
||||
prg cel.Program
|
||||
prg cel.Program
|
||||
actionIDs []string
|
||||
}
|
||||
|
||||
// ReferencedActionIDs returns the sorted, de-duplicated action ids the
|
||||
// expression reads through `actions.<id>`. It is empty when the expression
|
||||
// reads no prior-action result.
|
||||
func (p *Program) ReferencedActionIDs() []string {
|
||||
if p == nil {
|
||||
return nil
|
||||
}
|
||||
return p.actionIDs
|
||||
}
|
||||
|
||||
// Eval evaluates the program against a dispatch-time context. A missing
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package expr
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -79,6 +80,51 @@ func TestEvalHasMacroPresence(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestReferencedActionIDs: a compiled program exposes the action ids its
|
||||
// expression reads through actions.<id>, sorted and de-duplicated, so the
|
||||
// playbook loader can reject references to undeclared prior actions.
|
||||
func TestReferencedActionIDs(t *testing.T) {
|
||||
env := NewEnv()
|
||||
tests := []struct {
|
||||
name string
|
||||
src string
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "single action reference",
|
||||
src: `actions.notify.result.delivered == true`,
|
||||
want: []string{"notify"},
|
||||
},
|
||||
{
|
||||
name: "no action reference",
|
||||
src: `event.priority == 3`,
|
||||
want: nil,
|
||||
},
|
||||
{
|
||||
name: "multiple references deduped and sorted",
|
||||
src: `actions.b.result == true || actions.a.result == true || actions.b.result2 == true`,
|
||||
want: []string{"a", "b"},
|
||||
},
|
||||
{
|
||||
name: "has macro over action",
|
||||
src: `has(actions.notify.result.delivered)`,
|
||||
want: []string{"notify"},
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
prg, err := env.Compile(tc.src)
|
||||
if err != nil {
|
||||
t.Fatalf("Compile: %v", err)
|
||||
}
|
||||
got := prg.ReferencedActionIDs()
|
||||
if !slices.Equal(got, tc.want) {
|
||||
t.Fatalf("ReferencedActionIDs() = %v, want %v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestEvalActionsContext: prior-action results read via actions.<id>.result.
|
||||
func TestEvalActionsContext(t *testing.T) {
|
||||
env := NewEnv()
|
||||
|
||||
Reference in new issue
Block a user