-
Notifications
You must be signed in to change notification settings - Fork 28
/
ruleset.go
289 lines (248 loc) · 8.52 KB
/
ruleset.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// SPDX-License-Identifier: Apache-2.0
package pipeline
import (
"fmt"
"path/filepath"
"regexp"
"strings"
"github.com/go-vela/server/constants"
)
type (
// Ruleset is the pipeline representation of
// a ruleset block for a step in a pipeline.
//
// swagger:model PipelineRuleset
Ruleset struct {
If Rules `json:"if,omitempty" yaml:"if,omitempty"`
Unless Rules `json:"unless,omitempty" yaml:"unless,omitempty"`
Matcher string `json:"matcher,omitempty" yaml:"matcher,omitempty"`
Operator string `json:"operator,omitempty" yaml:"operator,omitempty"`
Continue bool `json:"continue,omitempty" yaml:"continue,omitempty"`
}
// Rules is the pipeline representation of the ruletypes
// from a ruleset block for a step in a pipeline.
//
// swagger:model PipelineRules
Rules struct {
Branch Ruletype `json:"branch,omitempty" yaml:"branch,omitempty"`
Comment Ruletype `json:"comment,omitempty" yaml:"comment,omitempty"`
Event Ruletype `json:"event,omitempty" yaml:"event,omitempty"`
Path Ruletype `json:"path,omitempty" yaml:"path,omitempty"`
Repo Ruletype `json:"repo,omitempty" yaml:"repo,omitempty"`
Sender Ruletype `json:"sender,omitempty" yaml:"sender,omitempty"`
Status Ruletype `json:"status,omitempty" yaml:"status,omitempty"`
Tag Ruletype `json:"tag,omitempty" yaml:"tag,omitempty"`
Target Ruletype `json:"target,omitempty" yaml:"target,omitempty"`
Label Ruletype `json:"label,omitempty" yaml:"label,omitempty"`
Instance Ruletype `json:"instance,omitempty" yaml:"instance,omitempty"`
Parallel bool `json:"-" yaml:"-"`
}
// Ruletype is the pipeline representation of an element
// for a ruleset block for a step in a pipeline.
//
// swagger:model PipelineRuletype
Ruletype []string
// RuleData is the data to check our ruleset
// against for a step in a pipeline.
RuleData struct {
Branch string `json:"branch,omitempty" yaml:"branch,omitempty"`
Comment string `json:"comment,omitempty" yaml:"comment,omitempty"`
Event string `json:"event,omitempty" yaml:"event,omitempty"`
Path []string `json:"path,omitempty" yaml:"path,omitempty"`
Repo string `json:"repo,omitempty" yaml:"repo,omitempty"`
Sender string `json:"sender,omitempty" yaml:"sender,omitempty"`
Status string `json:"status,omitempty" yaml:"status,omitempty"`
Tag string `json:"tag,omitempty" yaml:"tag,omitempty"`
Target string `json:"target,omitempty" yaml:"target,omitempty"`
Label []string `json:"label,omitempty" yaml:"label,omitempty"`
Instance string `json:"instance,omitempty" yaml:"instance,omitempty"`
Parallel bool `json:"-" yaml:"-"`
}
)
// Match returns true when the provided ruledata matches
// the if rules and does not match any of the unless rules.
// When the provided if rules are empty, the function returns
// true. When both the provided if and unless rules are empty,
// the function also returns true.
func (r *Ruleset) Match(from *RuleData) (bool, error) {
// return true when the if and unless rules are empty
if r.If.Empty() && r.Unless.Empty() {
return true, nil
}
// return false when the unless rules are not empty and match
if !r.Unless.Empty() {
match, err := r.Unless.Match(from, r.Matcher, r.Operator)
if err != nil {
return false, err
}
if match {
return false, nil
}
}
// return true when the if rules are empty
if r.If.Empty() {
return true, nil
}
// return true when the if rules match
match, err := r.If.Match(from, r.Matcher, r.Operator)
return match, err
}
// NoStatus returns true if the status field is empty.
func (r *Rules) NoStatus() bool {
// return true if every ruletype is empty
return len(r.Status) == 0
}
// Empty returns true if the provided ruletypes are empty.
func (r *Rules) Empty() bool {
// return true if every ruletype is empty
if len(r.Branch) == 0 &&
len(r.Comment) == 0 &&
len(r.Event) == 0 &&
len(r.Path) == 0 &&
len(r.Repo) == 0 &&
len(r.Sender) == 0 &&
len(r.Status) == 0 &&
len(r.Tag) == 0 &&
len(r.Target) == 0 &&
len(r.Label) == 0 &&
len(r.Instance) == 0 {
return true
}
// return false if any of the ruletype is provided
return false
}
// Match returns true for the `or` operator when one of the
// ruletypes from the rules match the provided ruledata.
// Match returns true for the `and` operator when all of the
// ruletypes from the rules match the provided ruledata. For
// both operators, when none of the ruletypes from the rules
// match the provided ruledata, the function returns false.
func (r *Rules) Match(from *RuleData, matcher, op string) (bool, error) {
status := true
var err error
if len(from.Status) != 0 {
status, err = r.Status.MatchSingle(from.Status, matcher, op)
if err != nil {
return false, err
}
}
matchBranch, err := r.Branch.MatchSingle(from.Branch, matcher, op)
if err != nil {
return false, err
}
matchComment, err := r.Comment.MatchSingle(from.Comment, matcher, op)
if err != nil {
return false, err
}
matchEvent, err := r.Event.MatchSingle(from.Event, matcher, op)
if err != nil {
return false, err
}
matchPath, err := r.Path.MatchMultiple(from.Path, matcher, op)
if err != nil {
return false, err
}
matchRepo, err := r.Repo.MatchSingle(from.Repo, matcher, op)
if err != nil {
return false, err
}
matchSender, err := r.Sender.MatchSingle(from.Sender, matcher, op)
if err != nil {
return false, err
}
matchTag, err := r.Tag.MatchSingle(from.Tag, matcher, op)
if err != nil {
return false, err
}
matchTarget, err := r.Target.MatchSingle(from.Target, matcher, op)
if err != nil {
return false, err
}
matchLabel, err := r.Label.MatchMultiple(from.Label, matcher, op)
if err != nil {
return false, err
}
matchInstance, err := r.Instance.MatchSingle(from.Instance, matcher, op)
if err != nil {
return false, err
}
switch op {
case constants.OperatorOr:
return (matchBranch || matchComment || matchEvent || matchPath || matchRepo || matchSender || matchTag || matchTarget || matchLabel || matchInstance || status), nil
default:
return (matchBranch && matchComment && matchEvent && matchPath && matchRepo && matchSender && matchTag && matchTarget && matchLabel && matchInstance && status), nil
}
}
// MatchSingle returns true when the provided ruletype
// matches the provided ruledata. When the provided
// ruletype is empty, the function returns true for
// the `and` operator and false for the `or` operator.
func (r *Ruletype) MatchSingle(data, matcher, logic string) (bool, error) {
// return true for `and`, false for `or` if an empty ruletype is provided
if len(*r) == 0 {
return strings.EqualFold(logic, constants.OperatorAnd), nil
}
// iterate through each pattern in the ruletype
for _, pattern := range *r {
match, err := match(data, matcher, pattern)
if err != nil {
return false, err
}
if match {
return true, nil
}
}
// return false if no match is found
return false, nil
}
// MatchMultiple returns true when the provided ruletype
// matches the provided ruledata. When the provided
// ruletype is empty, the function returns true for
// the `and` operator and false for the `or` operator.
func (r *Ruletype) MatchMultiple(data []string, matcher, logic string) (bool, error) {
// return true for `and`, false for `or` if an empty ruletype is provided
if len(*r) == 0 {
return strings.EqualFold(logic, constants.OperatorAnd), nil
}
// iterate through each pattern in the ruletype
for _, pattern := range *r {
for _, value := range data {
match, err := match(value, matcher, pattern)
if err != nil {
return false, err
}
if match {
return true, nil
}
}
}
// return false if no match is found
return false, nil
}
// match is a helper function that compares data against a pattern
// and returns true if the data matches the pattern, depending on
// matcher specified.
func match(data, matcher, pattern string) (bool, error) {
// handle the pattern based off the matcher provided
switch matcher {
case constants.MatcherRegex, "regex":
regExpPattern, err := regexp.Compile(pattern)
if err != nil {
return false, fmt.Errorf("error in regex pattern %s: %w", pattern, err)
}
// return true if the regexp pattern matches the ruledata
if regExpPattern.MatchString(data) {
return true, nil
}
case constants.MatcherFilepath:
fallthrough
default:
// return true if the pattern matches the ruledata
ok, _ := filepath.Match(pattern, data)
if ok {
return true, nil
}
}
// return false if no match is found
return false, nil
}