forked from rhysd/actionlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule_deprecated_commands.go
58 lines (51 loc) · 1.8 KB
/
rule_deprecated_commands.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
package actionlint
import "regexp"
var deprecatedCommandsPattern = regexp.MustCompile(`(?:::(save-state|set-output|set-env)\s+name=[a-zA-Z][a-zA-Z_-]*::\S+|::(add-path)::\S+)`)
// RuleDeprecatedCommands is a rule checker to detect deprecated workflow commands. Currently
// 'set-state', 'set-output', `set-env' and 'add-path' are detected as deprecated.
//
// - https://github.blog/changelog/2020-10-01-github-actions-deprecating-set-env-and-add-path-commands/
// - https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/
type RuleDeprecatedCommands struct {
RuleBase
}
// NewRuleDeprecatedCommands creates a new RuleDeprecatedCommands instance.
func NewRuleDeprecatedCommands() *RuleDeprecatedCommands {
return &RuleDeprecatedCommands{
RuleBase: RuleBase{
name: "deprecated-commands",
desc: "Checks for deprecated \"set-output\", \"save-state\", \"set-env\", and \"add-path\" commands at \"run:\"",
},
}
}
// VisitStep is callback when visiting Step node.
func (rule *RuleDeprecatedCommands) VisitStep(n *Step) error {
if r, ok := n.Exec.(*ExecRun); ok && r.Run != nil {
for _, m := range deprecatedCommandsPattern.FindAllStringSubmatch(r.Run.Value, -1) {
c := m[1]
if len(c) == 0 {
c = m[2]
}
var a string
switch c {
case "set-output":
a = `echo "{name}={value}" >> $GITHUB_OUTPUT`
case "save-state":
a = `echo "{name}={value}" >> $GITHUB_STATE`
case "set-env":
a = `echo "{name}={value}" >> $GITHUB_ENV`
case "add-path":
a = `echo "{path}" >> $GITHUB_PATH`
default:
panic("unreachable")
}
rule.Errorf(
r.Run.Pos,
"workflow command %q was deprecated. use `%s` instead: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions",
c,
a,
)
}
}
return nil
}