-
Notifications
You must be signed in to change notification settings - Fork 31
/
linter.go
440 lines (352 loc) · 11 KB
/
linter.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// Copyright 2022 Explore.dev Unipessoal Lda. All Rights Reserved.
// Use of this source code is governed by a license that can be
// found in the LICENSE file.
package engine
import (
"fmt"
"regexp"
"github.com/reviewpad/reviewpad/v4/utils"
"github.com/sirupsen/logrus"
)
func getAllMatches(pattern string, groups []PadGroup, rules []PadRule, workflows []PadWorkflow) []string {
rePatternFnCall := regexp.MustCompile(pattern)
allGroupFunctionCalls := make([]string, 0)
for _, group := range groups {
spec := group.Spec
groupFunctionCalls := rePatternFnCall.FindAllString(spec, -1)
if len(groupFunctionCalls) == 0 {
continue
}
allGroupFunctionCalls = append(allGroupFunctionCalls, groupFunctionCalls...)
}
for _, rule := range rules {
spec := rule.Spec
groupFunctionCalls := rePatternFnCall.FindAllString(spec, -1)
if len(groupFunctionCalls) == 0 {
continue
}
allGroupFunctionCalls = append(allGroupFunctionCalls, groupFunctionCalls...)
}
for _, workflow := range workflows {
actions := workflow.Actions
groupFunctionCalls := make([]string, 0)
for _, action := range actions {
groupFunctionCalls = append(groupFunctionCalls, rePatternFnCall.FindAllString(action, -1)...)
}
if len(groupFunctionCalls) == 0 {
continue
}
allGroupFunctionCalls = append(allGroupFunctionCalls, groupFunctionCalls...)
}
return allGroupFunctionCalls
}
// Validations:
// - Every rule has a (unique) name
// - Every rule has a kind
// - Every rules has a spec
func lintRules(padRules []PadRule) error {
rulesName := make([]string, 0)
for _, rule := range padRules {
if rule.Name == "" {
return fmt.Errorf("rule %v has invalid name", rule)
}
for _, ruleName := range rulesName {
if ruleName == rule.Name {
return fmt.Errorf("rule with the name %v already exists", rule.Name)
}
}
ruleKind := rule.Kind
if !utils.ElementOf(kinds, ruleKind) {
return fmt.Errorf("rule %v has invalid kind %v", rule.Name, ruleKind)
}
if rule.Spec == "" {
return fmt.Errorf("rule %v has empty spec", rule.Name)
}
rulesName = append(rulesName, rule.Name)
}
return nil
}
// Validations:
// - Group has unique name
func lintGroups(log *logrus.Entry, padGroups []PadGroup) error {
groupsName := make([]string, 0)
for _, group := range padGroups {
log.Infof("analyzing group %v", group.Name)
if group.Name == "" {
return fmt.Errorf("group %v has invalid name", group)
}
for _, groupName := range groupsName {
if groupName == group.Name {
return fmt.Errorf("group with the name %v already exists", group.Name)
}
}
groupsName = append(groupsName, group.Name)
}
return nil
}
// Validations:
// - Workflow has unique name
// - Workflow has non empty rules
// - Workflow has only known rules
// - Workflow run block has a then block or actions
func lintWorkflows(log *logrus.Entry, rules []PadRule, padWorkflows []PadWorkflow) error {
workflowsName := make([]string, 0)
workflowHasExtraActions := false
for _, workflow := range padWorkflows {
workflowHasActions := len(workflow.Actions) > 0
for _, workflowName := range workflowsName {
if workflowName == workflow.Name {
return fmt.Errorf("workflow with the name `%v` already exists", workflow.Name)
}
}
for _, rule := range workflow.Rules {
ruleName := rule.Rule
if ruleName == "" {
return fmt.Errorf("workflow has an empty rule")
}
_, exists := findRule(rules, ruleName)
if !exists {
return fmt.Errorf("rule `%v` is unknown", ruleName)
}
workflowHasExtraActions = len(rule.ExtraActions) > 0
if !workflowHasExtraActions && !workflowHasActions {
log.Warnf("rule `%v` will be ignored since it has no actions", ruleName)
}
}
if !workflowHasActions && !workflowHasExtraActions && len(workflow.Runs) == 0 {
log.Debug("workflow has no actions")
}
workflowsName = append(workflowsName, workflow.Name)
for _, run := range workflow.Runs {
if err := validateWorkflowRun(&run, &workflow); err != nil {
return err
}
}
}
return nil
}
func validateWorkflowRun(run *PadWorkflowRunBlock, workflow *PadWorkflow) error {
var hasForEachBlock = run.ForEach != nil
var hasActions = run.Actions != nil && len(run.Actions) > 0
var hasThenActions = run.Then != nil && len(run.Then) > 0
var hasExtraActions bool
for _, rule := range run.If {
hasExtraActions = len(rule.ExtraActions) > 0
if hasExtraActions {
break
}
}
// Old style workflow if/then/else are converted to new run block format.
// The old style workflow allows if blocks to have extra actions.
// Because of this, a run block can have extra actions.
if !hasThenActions && !hasActions && !hasExtraActions && !hasForEachBlock {
return fmt.Errorf("workflow `%v` has a run block without a 'then' block, no actions, no extra actions or a for each block", workflow.Name)
}
for _, thenRun := range run.Then {
err := validateWorkflowRun(&thenRun, workflow)
if err != nil {
return err
}
}
for _, elseRun := range run.Else {
err := validateWorkflowRun(&elseRun, workflow)
if err != nil {
return err
}
}
return nil
}
// Validations
// - Check that all rules are being used
// - Check that all referenced rules exist
func lintRulesMentions(log *logrus.Entry, rules []PadRule, groups []PadGroup, workflows []PadWorkflow) error {
totalUsesByRule := make(map[string]int, len(rules))
for _, rule := range rules {
totalUsesByRule[rule.Name] = 0
}
for _, workflow := range workflows {
for _, rule := range workflow.Rules {
ruleName := rule.Rule
_, exists := findRule(rules, ruleName)
if exists {
totalUsesByRule[ruleName]++
}
}
for _, run := range workflow.Runs {
err := lintRulesMentionsInRun(run, rules, totalUsesByRule)
if err != nil {
return err
}
}
}
for _, ruleName := range getCallsToRuleBuiltIn(groups, rules, workflows) {
_, ok := findRule(rules, ruleName)
if !ok {
return fmt.Errorf("the rule `%v` isn't defined", ruleName)
}
totalUsesByRule[ruleName]++
}
for ruleName, totalUses := range totalUsesByRule {
if totalUses == 0 {
log.Warnf("unused rule `%v`", ruleName)
}
}
return nil
}
func getCallsToRuleBuiltIn(groups []PadGroup, rules []PadRule, workflows []PadWorkflow) []string {
allRuleFunctionCalls := make([]string, 0)
gotFunctionCalls := getAllMatches(`\$rule\("[^)]*"\)`, groups, rules, workflows)
reRuleMention := regexp.MustCompile(`"(.*?)"`)
for _, ruleWithRuleCall := range gotFunctionCalls {
for _, ruleCall := range reRuleMention.FindAllString(ruleWithRuleCall, -1) {
ruleName := ruleCall[1 : len(ruleCall)-1]
allRuleFunctionCalls = append(allRuleFunctionCalls, ruleName)
}
}
return allRuleFunctionCalls
}
// Validations
// - Check that all groups are being used
// - Check that all referenced groups exist
func lintGroupsMentions(groups []PadGroup, rules []PadRule, workflows []PadWorkflow) error {
allGroupFunctionCalls := getAllMatches(`\$group\(".*"\)`, groups, rules, workflows)
reGroupMention := regexp.MustCompile(`"(.*?)"`)
for _, groupFunctionCall := range allGroupFunctionCalls {
groupMention := reGroupMention.FindString(groupFunctionCall)
// Remove quotation marks
groupMention = groupMention[1 : len(groupMention)-1]
_, ok := findGroup(groups, groupMention)
if !ok {
return fmt.Errorf("the group `%v` isn't defined", groupMention)
}
}
return nil
}
func lintShadowedVariablesInRuns(runs []PadWorkflowRunBlock, definedVariables map[string]bool) error {
for _, run := range runs {
if run.ForEach != nil {
// since the key may not be present in some for each blocks
// we wanna disregard empty keys so that we don't get a lint error
if run.ForEach.Key != "" {
if _, ok := definedVariables[run.ForEach.Key]; ok {
return fmt.Errorf("variable shadowing is not allowed: the variable %s is already defined", run.ForEach.Key)
}
}
if _, ok := definedVariables[run.ForEach.Value]; ok {
return fmt.Errorf("variable shadowing is not allowed: the variable %s is already defined", run.ForEach.Value)
}
definedVariables[run.ForEach.Value] = true
definedVariables[run.ForEach.Key] = true
err := lintShadowedVariablesInRuns(run.ForEach.Do, definedVariables)
if err != nil {
return err
}
}
err := lintShadowedVariablesInRuns(run.Then, definedVariables)
if err != nil {
return err
}
err = lintShadowedVariablesInRuns(run.Else, definedVariables)
if err != nil {
return err
}
}
return nil
}
func lintShadowedBuiltInsInRuns(runs []PadWorkflowRunBlock, definedBuiltIns map[string]bool) error {
for _, run := range runs {
if run.ForEach != nil {
if _, ok := definedBuiltIns[run.ForEach.Key]; ok {
return fmt.Errorf("built-in shadowing is not allowed: the variable %s is a reserved name", run.ForEach.Key)
}
if _, ok := definedBuiltIns[run.ForEach.Value]; ok {
return fmt.Errorf("built-in shadowing is not allowed: the variable %s is a reserved name", run.ForEach.Value)
}
err := lintShadowedBuiltInsInRuns(run.ForEach.Do, definedBuiltIns)
if err != nil {
return err
}
}
err := lintShadowedBuiltInsInRuns(run.Then, definedBuiltIns)
if err != nil {
return err
}
err = lintShadowedBuiltInsInRuns(run.Else, definedBuiltIns)
if err != nil {
return err
}
}
return nil
}
func lintShadowedVariables(workflows []PadWorkflow) error {
for _, workflow := range workflows {
definedVariables := map[string]bool{}
err := lintShadowedVariablesInRuns(workflow.Runs, definedVariables)
if err != nil {
return err
}
}
return nil
}
func lintReservedWords(workflows []PadWorkflow, reservedWords []string) error {
definedVariables := map[string]bool{}
for _, reservedWord := range reservedWords {
definedVariables[fmt.Sprintf("$%s", reservedWord)] = true
}
for _, workflow := range workflows {
err := lintShadowedBuiltInsInRuns(workflow.Runs, definedVariables)
if err != nil {
return err
}
}
return nil
}
func lintRulesMentionsInRun(run PadWorkflowRunBlock, rules []PadRule, totalUsesByRule map[string]int) error {
for _, rule := range run.If {
ruleName := rule.Rule
_, exists := findRule(rules, ruleName)
if exists {
totalUsesByRule[ruleName]++
}
}
for _, thenRun := range run.Then {
err := lintRulesMentionsInRun(thenRun, rules, totalUsesByRule)
if err != nil {
return err
}
}
for _, elseRun := range run.Else {
err := lintRulesMentionsInRun(elseRun, rules, totalUsesByRule)
if err != nil {
return err
}
}
return nil
}
func Lint(file *ReviewpadFile, reserved []string, logger *logrus.Entry) error {
err := lintGroups(logger, file.Groups)
if err != nil {
return err
}
err = lintRules(file.Rules)
if err != nil {
return err
}
err = lintWorkflows(logger, file.Rules, file.Workflows)
if err != nil {
return err
}
err = lintRulesMentions(logger, file.Rules, file.Groups, file.Workflows)
if err != nil {
return err
}
err = lintReservedWords(file.Workflows, reserved)
if err != nil {
return err
}
err = lintShadowedVariables(file.Workflows)
if err != nil {
return err
}
return lintGroupsMentions(file.Groups, file.Rules, file.Workflows)
}