-
Notifications
You must be signed in to change notification settings - Fork 342
/
action.go
182 lines (166 loc) · 4.43 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
package actions
import (
"context"
"errors"
"fmt"
"path"
"regexp"
"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 OnEvents `yaml:"on"`
Hooks []ActionHook `yaml:"hooks"`
}
type OnEvents struct {
PreMerge *ActionOn `yaml:"pre-merge"`
PreCommit *ActionOn `yaml:"pre-commit"`
}
type ActionOn struct {
Branches []string `yaml:"branches"`
}
type ActionHook struct {
ID string `yaml:"id"`
Type HookType `yaml:"type"`
Description string `yaml:"description"`
Properties map[string]interface{} `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")
ErrInvalidEventType = errors.New("invalid event type")
)
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 a.On.PreMerge == nil && a.On.PreCommit == nil {
return fmt.Errorf("'on' is required: %w", ErrInvalidAction)
}
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 (a *Action) Match(spec MatchSpec) (bool, error) {
// at least one matched event definition
var actionOn *ActionOn
switch spec.EventType {
case graveler.EventTypePreCommit:
actionOn = a.On.PreCommit
case graveler.EventTypePreMerge:
actionOn = a.On.PreMerge
default:
return false, ErrInvalidEventType
}
// if no action specified - no match
if actionOn == nil {
return false, nil
}
// if no branches spec found - all match
if 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
}