-
Notifications
You must be signed in to change notification settings - Fork 351
/
action.go
239 lines (216 loc) · 5.88 KB
/
action.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package actions
import (
"context"
"errors"
"fmt"
"path"
"regexp"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/treeverse/lakefs/pkg/graveler"
"gopkg.in/yaml.v3"
)
type Action struct {
Name string `yaml:"name"`
Description string `yaml:"description"`
On map[graveler.EventType]*ActionOn `yaml:"on"`
Hooks []ActionHook `yaml:"hooks"`
}
type ActionOn struct {
Branches []string `yaml:"branches"`
}
var (
errMissingKey = errors.New("missing key in properties")
errMissingEnvVar = errors.New("missing env var")
errWrongValueType = errors.New("wrong value type")
)
type Properties map[string]interface{}
func (p Properties) getRequiredProperty(key string) (string, error) {
raw, ok := p[key]
if !ok {
return "", fmt.Errorf("key %s: %w", key, errMissingKey)
}
val, ok := raw.(string)
if !ok {
return "", fmt.Errorf("value of %s is not of type string: %w", key, errWrongValueType)
}
if val == "" {
return "", fmt.Errorf("value of %s is empty: %w", key, errMissingKey)
}
return val, nil
}
type ActionHook struct {
ID string `yaml:"id"`
Type HookType `yaml:"type"`
Description string `yaml:"description"`
Properties Properties `yaml:"properties"`
}
type MatchSpec struct {
EventType graveler.EventType
BranchID graveler.BranchID
}
var (
reName = regexp.MustCompile(`^\w[\w\-. ]+$`)
reHookID = regexp.MustCompile(`^[_a-zA-Z][\-_a-zA-Z0-9]{1,255}$`)
ErrInvalidAction = errors.New("invalid action")
ErrInvalidEventParameter = errors.New("invalid event parameter")
)
func isEventSupported(event graveler.EventType) bool {
switch event {
case graveler.EventTypePreCommit,
graveler.EventTypePostMerge,
graveler.EventTypePreMerge,
graveler.EventTypePostCommit,
graveler.EventTypePreCreateBranch,
graveler.EventTypePostCreateBranch,
graveler.EventTypePreDeleteBranch,
graveler.EventTypePostDeleteBranch,
graveler.EventTypePreCreateTag,
graveler.EventTypePostCreateTag,
graveler.EventTypePreDeleteTag,
graveler.EventTypePostDeleteTag:
return true
}
return false
}
func (a *Action) Validate() error {
if a.Name == "" {
return fmt.Errorf("'name' is required: %w", ErrInvalidAction)
}
if !reName.MatchString(a.Name) {
return fmt.Errorf("'name' is invalid: %w", ErrInvalidAction)
}
if len(a.On) == 0 {
return fmt.Errorf("'on' is required: %w", ErrInvalidAction)
}
for k, v := range a.On {
err := validateEvent(k, v)
if err != nil {
return err
}
}
ids := make(map[string]struct{})
for i, hook := range a.Hooks {
if !reHookID.MatchString(hook.ID) {
return fmt.Errorf("hook[%d] missing ID: %w", i, ErrInvalidAction)
}
if _, found := ids[hook.ID]; found {
return fmt.Errorf("hook[%d] duplicate ID '%s': %w", i, hook.ID, ErrInvalidAction)
}
ids[hook.ID] = struct{}{}
if _, found := hooks[hook.Type]; !found {
return fmt.Errorf("hook[%d] type '%s' unknown: %w", i, hook.ID, ErrInvalidAction)
}
}
return nil
}
func validateEvent(event graveler.EventType, on *ActionOn) error {
if !isEventSupported(event) {
return fmt.Errorf("event '%s' is not supported: %w", event, ErrInvalidAction)
}
if on != nil {
switch {
// Add a case for any additional field added to ActionOn struct
case len(on.Branches) > 0:
if strings.HasSuffix(string(event), "-tag") {
return fmt.Errorf("'branches' is not supported in tag event types. %w", ErrInvalidEventParameter)
}
default:
// Nothing to do
}
}
return nil
}
func (a *Action) Match(spec MatchSpec) (bool, error) {
// at least one matched event definition
actionOn, ok := a.On[spec.EventType]
// if no action specified - no match
if !ok {
return false, nil
}
// if no branches spec found - all match
if actionOn == nil || len(actionOn.Branches) == 0 {
return true, nil
}
// find at least one match
branchSpec := spec.BranchID.String()
for _, b := range actionOn.Branches {
matched, err := path.Match(b, branchSpec)
if err != nil {
return false, err
}
if matched {
return true, nil
}
}
return false, nil
}
// ParseAction helper function to read, parse and validate Action from a reader
func ParseAction(data []byte) (*Action, error) {
var act Action
err := yaml.Unmarshal(data, &act)
if err != nil {
return nil, err
}
err = act.Validate()
if err != nil {
return nil, err
}
return &act, nil
}
func LoadActions(ctx context.Context, source Source, record graveler.HookRecord) ([]*Action, error) {
hooksAddresses, err := source.List(ctx, record)
if err != nil {
return nil, fmt.Errorf("list actions from commit: %w", err)
}
actions := make([]*Action, len(hooksAddresses))
var errGroup multierror.Group
for i := range hooksAddresses {
// pin i for embedded func
ii := i
errGroup.Go(func() error {
addr := hooksAddresses[ii]
bytes, err := source.Load(ctx, record, addr)
if err != nil {
return fmt.Errorf("loading file %s: %w", addr, err)
}
action, err := ParseAction(bytes)
if err != nil {
return fmt.Errorf("parsing file %s: %w", addr, err)
}
actions[ii] = action
return nil
})
}
if err := errGroup.Wait(); err != nil {
return nil, err
}
if err := validateActions(actions); err != nil {
return nil, err
}
return actions, nil
}
// validateActions verify we do not two actions with the same name
func validateActions(actions []*Action) error {
actionNames := make(map[string]struct{})
for _, action := range actions {
if _, found := actionNames[action.Name]; found {
return fmt.Errorf("action name '%s' already loaded: %w", action.Name, ErrInvalidAction)
}
actionNames[action.Name] = struct{}{}
}
return nil
}
func MatchedActions(actions []*Action, spec MatchSpec) ([]*Action, error) {
var matched []*Action
for _, act := range actions {
m, err := act.Match(spec)
if err != nil {
return nil, err
}
if m {
matched = append(matched, act)
}
}
return matched, nil
}