-
Notifications
You must be signed in to change notification settings - Fork 163
/
example_your_own_rule_test.go
65 lines (55 loc) · 1.74 KB
/
example_your_own_rule_test.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
package actionlint_test
import (
"fmt"
"io"
"path/filepath"
"github.com/rhysd/actionlint"
)
// A rule type to check every steps have their names.
type RuleStepName struct {
// Embedding RuleBase struct implements the minimal Rule interface.
actionlint.RuleBase
}
// Reimplement methods in RuleBase. Visit* methods are called on checking workflows.
func (r *RuleStepName) VisitStep(n *actionlint.Step) error {
// Implement your own check
if n.Name == nil {
// RuleBase provides methods to report errors. See RuleBase.Error and RuleBase.Errorf.
r.Error(n.Pos, "every step must have its name")
}
return nil
}
func NewRuleStepName() *RuleStepName {
return &RuleStepName{
RuleBase: actionlint.NewRuleBase("step-name", "Checks every step has their own name"),
}
}
func ExampleLinter_yourOwnRule() {
// The function set at OnRulesCreated is called after rule instances are created. You can
// add/remove some rules and return the modified slice. This function is called on linting
// each workflow files.
o := &actionlint.LinterOptions{
OnRulesCreated: func(rules []actionlint.Rule) []actionlint.Rule {
rules = append(rules, NewRuleStepName())
return rules
},
}
l, err := actionlint.NewLinter(io.Discard, o)
if err != nil {
panic(err)
}
f := filepath.Join("testdata", "ok", "minimal.yaml")
// First return value is an array of lint errors found in the workflow file.
errs, err := l.LintFile(f, nil)
if err != nil {
panic(err)
}
// `errs` includes errors like below:
//
// testdata/examples/main.yaml:14:9: every step must have its name [step-name]
// |
// 14 | - uses: actions/checkout@v4
// | ^~~~~
fmt.Println(len(errs), "lint errors found by actionlint")
// Output: 1 lint errors found by actionlint
}