Skip to content

Commit 943202c

Browse files
committed
Make cel2sql generic using View abstraction
1 parent e857941 commit 943202c

18 files changed

Lines changed: 541 additions & 269 deletions

pkg/api/server/cel/view/records.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package view
2+
3+
import (
4+
"github.com/google/cel-go/cel"
5+
"github.com/tektoncd/results/pkg/api/server/cel2sql"
6+
)
7+
8+
// NewRecordsView return the set of variables and constants available for CEL
9+
// filters
10+
func NewRecordsView() (*cel2sql.View, error) {
11+
view := &cel2sql.View{
12+
Constants: map[string]cel2sql.Constant{},
13+
Fields: map[string]cel2sql.Field{
14+
"parent": {
15+
CELType: cel.StringType,
16+
SQL: `parent`,
17+
},
18+
"result_name": {
19+
CELType: cel.StringType,
20+
SQL: `result_name`,
21+
},
22+
"name": {
23+
CELType: cel.StringType,
24+
SQL: `name`,
25+
},
26+
"data_type": {
27+
CELType: cel.StringType,
28+
SQL: `type`,
29+
},
30+
"data": {
31+
CELType: cel.AnyType,
32+
SQL: `data`,
33+
},
34+
},
35+
}
36+
for typeName, value := range typeConstants {
37+
view.Constants[typeName] = value
38+
}
39+
return view, nil
40+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package view
2+
3+
import (
4+
"testing"
5+
6+
"github.com/google/go-cmp/cmp"
7+
"github.com/tektoncd/results/pkg/api/server/cel2sql"
8+
"k8s.io/utils/pointer"
9+
)
10+
11+
func TestNewRecordsViewConstants(t *testing.T) {
12+
view, err := NewRecordsView()
13+
if err != nil {
14+
t.Fatal(err)
15+
}
16+
17+
expectedConstants := map[string]cel2sql.Constant{
18+
"PIPELINE_RUN": {StringVal: pointer.String("tekton.dev/v1beta1.PipelineRun")},
19+
"TASK_RUN": {StringVal: pointer.String("tekton.dev/v1beta1.TaskRun")},
20+
}
21+
if diff := cmp.Diff(expectedConstants, view.Constants); diff != "" {
22+
t.Errorf("Invalid constants (-want, +got):\n%s", diff)
23+
}
24+
}

pkg/api/server/cel/view/results.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package view
2+
3+
import (
4+
"github.com/google/cel-go/cel"
5+
"github.com/tektoncd/results/pkg/api/server/cel2sql"
6+
resultspb "github.com/tektoncd/results/proto/v1alpha2/results_go_proto"
7+
)
8+
9+
var (
10+
typePipelineRun = "tekton.dev/v1beta1.PipelineRun"
11+
typeTaskRun = "tekton.dev/v1beta1.TaskRun"
12+
13+
typeConstants = map[string]cel2sql.Constant{
14+
"PIPELINE_RUN": {
15+
StringVal: &typePipelineRun,
16+
},
17+
"TASK_RUN": {
18+
StringVal: &typeTaskRun,
19+
},
20+
}
21+
)
22+
23+
// NewResultsView return the set of variables and constants available for CEL
24+
// filters
25+
func NewResultsView() (*cel2sql.View, error) {
26+
view := &cel2sql.View{
27+
Constants: map[string]cel2sql.Constant{},
28+
Fields: map[string]cel2sql.Field{
29+
"parent": {
30+
CELType: cel.StringType,
31+
SQL: `parent`,
32+
},
33+
"uid": {
34+
CELType: cel.StringType,
35+
SQL: `id`,
36+
},
37+
"create_time": {
38+
CELType: cel2sql.CELTypeTimestamp,
39+
SQL: `created_time`,
40+
},
41+
"update_time": {
42+
CELType: cel2sql.CELTypeTimestamp,
43+
SQL: `updated_time`,
44+
},
45+
"annotations": {
46+
CELType: cel.MapType(cel.StringType, cel.StringType),
47+
SQL: `annotations`,
48+
},
49+
"summary": {
50+
CELType: cel.ObjectType("tekton.results.v1alpha2.RecordSummary"),
51+
ObjectType: &resultspb.RecordSummary{},
52+
SQL: `recordsummary_{{.Field}}`,
53+
},
54+
},
55+
}
56+
for typeName, value := range typeConstants {
57+
view.Constants[typeName] = value
58+
}
59+
for name, value := range resultspb.RecordSummary_Status_value {
60+
v := value
61+
view.Constants[name] = cel2sql.Constant{
62+
Int32Val: &v,
63+
}
64+
}
65+
return view, nil
66+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package view
2+
3+
import (
4+
"testing"
5+
6+
"github.com/google/go-cmp/cmp"
7+
"github.com/tektoncd/results/pkg/api/server/cel2sql"
8+
"k8s.io/utils/pointer"
9+
)
10+
11+
func TestNewResultsViewConstants(t *testing.T) {
12+
view, err := NewResultsView()
13+
if err != nil {
14+
t.Fatal(err)
15+
}
16+
17+
expectedConstants := map[string]cel2sql.Constant{
18+
"PIPELINE_RUN": {StringVal: pointer.String("tekton.dev/v1beta1.PipelineRun")},
19+
"TASK_RUN": {StringVal: pointer.String("tekton.dev/v1beta1.TaskRun")},
20+
"UNKNOWN": {Int32Val: pointer.Int32(0)},
21+
"SUCCESS": {Int32Val: pointer.Int32(1)},
22+
"FAILURE": {Int32Val: pointer.Int32(2)},
23+
"TIMEOUT": {Int32Val: pointer.Int32(3)},
24+
"CANCELLED": {Int32Val: pointer.Int32(4)},
25+
}
26+
if diff := cmp.Diff(expectedConstants, view.Constants); diff != "" {
27+
t.Errorf("Invalid constants (-want, +got):\n%s", diff)
28+
}
29+
}

pkg/api/server/cel2sql/convert.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@ import (
2020
"github.com/google/cel-go/cel"
2121
)
2222

23-
// Convert takes CEL expressions and attempt to convert them into Postgres SQL
24-
// filters.
25-
func Convert(env *cel.Env, filters string) (string, error) {
23+
// Convert takes a View and CEL expressions and attempt to convert them into
24+
// Postgres SQL filters.
25+
func Convert(view *View, filters string) (string, error) {
26+
env, err := view.GetEnv()
27+
if err != nil {
28+
return "", fmt.Errorf("invalid view: %w", err)
29+
}
30+
2631
ast, issues := env.Compile(filters)
2732
if issues != nil && issues.Err() != nil {
2833
return "", fmt.Errorf("error compiling CEL filters: %w", issues.Err())
@@ -32,10 +37,15 @@ func Convert(env *cel.Env, filters string) (string, error) {
3237
return "", fmt.Errorf("expected boolean expression, but got %s", outputType.String())
3338
}
3439

35-
interpreter, err := newInterpreter(ast)
40+
interpreter, err := newInterpreter(ast, view)
3641
if err != nil {
3742
return "", fmt.Errorf("error creating cel2sql interpreter: %w", err)
3843
}
3944

40-
return interpreter.interpret()
45+
sql, err := interpreter.interpret()
46+
if err != nil {
47+
return "", err
48+
}
49+
50+
return sql, nil
4151
}

0 commit comments

Comments
 (0)