-
Notifications
You must be signed in to change notification settings - Fork 78
/
templating.go
132 lines (126 loc) · 4.35 KB
/
templating.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package templates
import (
"encoding/json"
"net/http"
"reflect"
"strings"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/traits"
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys"
customparams "github.com/openshift-pipelines/pipelines-as-code/pkg/cel"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/structpb"
)
var (
structType = reflect.TypeOf(&structpb.Value{})
listType = reflect.TypeOf(&structpb.ListValue{})
mapType = reflect.TypeOf(&structpb.Struct{})
)
// ReplacePlaceHoldersVariables is a function that replaces placeholders in a
// given string template with their corresponding values. The placeholders are
// expected to be in the format `{{key}}`, where `key` is the identifier for a
// value.
//
// The function first checks if the key in the placeholder has a prefix of
// "body", "headers", or "files". If it does and both `rawEvent` and `headers`
// are not nil, it attempts to retrieve the value for the key using the
// `customparams.CelValue` function and returns the corresponding string
// representation. If the key does not have any of the mentioned prefixes, the
// function checks if the key exists in the `dico` map. If it does, the
// function replaces the placeholder with the corresponding value from the
// `dico` map.
//
// Parameters:
// - template (string): The input string that may contain placeholders in the
// format `{{key}}`.
// - dico (map[string]string): A dictionary mapping keys to their corresponding
// string values. If a placeholder's key is found in this dictionary, it will
// be replaced with the corresponding value.
// - rawEvent (any): The raw event data that may be used to retrieve values for
// placeholders with keys that have a prefix of "body", "headers", or "files".
// - headers (http.Header): The HTTP headers that may be used to retrieve
// values for placeholders with keys that have a prefix of "headers".
// - changedFiles (map[string]interface{}): A map of changed files that may be
// used to retrieve values for placeholders with keys that have a prefix of
// "files".
func ReplacePlaceHoldersVariables(template string, dico map[string]string, rawEvent any, headers http.Header, changedFiles map[string]interface{}) string {
return keys.ParamsRe.ReplaceAllStringFunc(template, func(s string) string {
parts := keys.ParamsRe.FindStringSubmatch(s)
key := strings.TrimSpace(parts[1])
if strings.HasPrefix(key, "body") || strings.HasPrefix(key, "headers") || strings.HasPrefix(key, "files") {
if rawEvent != nil && headers != nil {
// convert headers to map[string]string
headerMap := make(map[string]string)
for k, v := range headers {
headerMap[k] = v[0]
}
val, err := customparams.CelValue(key, rawEvent, headerMap, map[string]string{}, changedFiles)
if err != nil {
return s
}
var raw interface{}
var b []byte
switch val.(type) {
case types.String:
if v, ok := val.Value().(string); ok {
b = []byte(v)
}
case types.Bytes:
raw, err = val.ConvertToNative(structType)
if err == nil {
b, err = raw.(*structpb.Value).MarshalJSON()
if err != nil {
b = []byte{}
}
}
case types.Double, types.Int:
raw, err = val.ConvertToNative(structType)
if err == nil {
b, err = raw.(*structpb.Value).MarshalJSON()
if err != nil {
b = []byte{}
}
}
case traits.Lister:
raw, err = val.ConvertToNative(listType)
if err == nil {
s, err := protojson.Marshal(raw.(proto.Message))
if err == nil {
b = s
}
}
case traits.Mapper:
raw, err = val.ConvertToNative(mapType)
if err == nil {
s, err := protojson.Marshal(raw.(proto.Message))
if err == nil {
b = s
}
}
case types.Bool:
raw, err = val.ConvertToNative(structType)
if err == nil {
b, err = json.Marshal(raw.(*structpb.Value).GetBoolValue())
if err != nil {
b = []byte{}
}
}
default:
raw, err = val.ConvertToNative(reflect.TypeOf([]byte{}))
if err == nil {
if v, ok := raw.([]byte); ok {
b = v
}
}
}
return string(b)
}
return s
}
if _, ok := dico[key]; !ok {
return s
}
return dico[key]
})
}