forked from GoogleCloudPlatform/cloud-build-notifiers
/
jsonpath.go
146 lines (126 loc) · 3.73 KB
/
jsonpath.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package notifiers
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"reflect"
"strings"
"sync"
cbpb "google.golang.org/genproto/googleapis/devtools/cloudbuild/v1"
"k8s.io/client-go/third_party/forked/golang/template"
"k8s.io/client-go/util/jsonpath"
)
// BindingResolver is an object that given a Build and a way to get secrets, returns all bound substitutions from the
// notifier configuration.
type BindingResolver interface {
Resolve(context.Context, SecretGetter, *cbpb.Build) (map[string]string, error)
}
type inputAndJSONPath struct {
j *jsonpath.JSONPath // The JSONPath that parsed that path.
p string // The user-provided path.
}
type jpResolver struct {
mtx sync.RWMutex
jps map[string]*inputAndJSONPath // Map of _SOME_SUBST_NAME => its inputAndJSONPath.
cfg *Config
}
func newResolver(cfg *Config) (BindingResolver, error) {
jps := map[string]*inputAndJSONPath{}
for name, path := range cfg.Spec.Notification.Params {
p, err := makeJSONPath(path)
if err != nil {
return nil, fmt.Errorf("failed to derive substitution path from %q: %v", path, err)
}
j := jsonpath.New(name).AllowMissingKeys(false)
if err := j.Parse(p); err != nil {
return nil, fmt.Errorf("failed to parse JSONPath expression from %q: %v", path, err)
}
jps[name] = &inputAndJSONPath{
j: j,
p: path, // Use the user-provided path so error messages are easier to understand.
}
}
return &jpResolver{
jps: jps,
cfg: cfg,
}, nil
}
func (j *jpResolver) Resolve(ctx context.Context, sg SecretGetter, build *cbpb.Build) (map[string]string, error) {
j.mtx.RLock()
defer j.mtx.RUnlock()
// Use a "JSON" payload here since a struct would have export-field issues
// based on the lowercase names.
pld := map[string]interface{}{
"build": build,
}
ret := map[string]string{}
for name, jp := range j.jps {
fullResults, err := jp.j.FindResults(pld)
if err != nil {
return nil, fmt.Errorf("failed to parse %q with path %q from payload: %v", name, jp.p, err)
}
if len(fullResults) == 0 {
return nil, fmt.Errorf("failed to get JSONPath query results for %q with path %q", name, jp.p)
}
buf := new(bytes.Buffer)
for _, r := range fullResults {
if err := printResults(buf, r); err != nil {
return nil, err
}
}
ret[name] = buf.String()
}
return ret, nil
}
func makeJSONPath(path string) (string, error) {
if !strings.HasPrefix(path, "$(") || !strings.HasSuffix(path, ")") {
return "", fmt.Errorf("expected %q to start with `$(` and end with `)` for a valid JSONPath expression", path)
}
trimmed := strings.TrimSuffix(strings.TrimPrefix(path, "$("), ")")
return fmt.Sprintf("{ .%s }", trimmed), nil
}
/**
NOTE; THE FOLLOWING HAS BEEN SHAMELESSLY COPIED FROM
https://github.com/tektoncd/triggers/blob/master/pkg/template/jsonpath.go
**/
func printResults(wr io.Writer, results []reflect.Value) error {
for i, r := range results {
text, err := textValue(r)
if err != nil {
return err
}
if i != len(results)-1 {
text = append(text, ' ')
}
if _, err := wr.Write(text); err != nil {
return err
}
}
return nil
}
func textValue(v reflect.Value) ([]byte, error) {
t := reflect.TypeOf(v.Interface())
// special case for null values in JSON; evalToText() returns <nil> here
if t == nil {
return []byte("null"), nil
}
switch t.Kind() {
// evalToText() returns <map> ....; return JSON string instead.
case reflect.Map, reflect.Slice:
return json.Marshal(v.Interface())
default:
return evalToText(v)
}
}
func evalToText(v reflect.Value) ([]byte, error) {
iface, ok := template.PrintableValue(v)
if !ok {
// only happens if v is a Chan or a Func
return nil, fmt.Errorf("can't print type %s", v.Type())
}
var buffer bytes.Buffer
fmt.Fprint(&buffer, iface)
return buffer.Bytes(), nil
}