forked from rhysd/actionlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule_action.go
209 lines (180 loc) · 5.25 KB
/
rule_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
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
package actionlint
import (
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v3"
)
// RuleAction is a rule to check running action in steps of jobs.
// https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstepsuses
type RuleAction struct {
RuleBase
repoPath string
}
// ActionSpec represents structure of action.yaml.
// https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions
type ActionSpec struct {
// Name is "name" field of action.yaml
Name string `yaml:"name"`
// Inputs is "inputs" field of action.yaml
Inputs map[string]struct {
Required bool `yaml:"required"`
Default *string `yaml:"default"`
} `yaml:"inputs"`
// Outputs is "outputs" field of action.yaml
Outputs map[string]struct{} `yaml:"outputs"`
}
// NewRuleAction creates new RuleAction instance.
func NewRuleAction(repoDir string) *RuleAction {
return &RuleAction{
RuleBase: RuleBase{name: "action"},
repoPath: repoDir,
}
}
// VisitStep is callback when visiting Step node.
func (rule *RuleAction) VisitStep(n *Step) error {
e, ok := n.Exec.(*ExecAction)
if !ok || e.Uses == nil {
return nil
}
spec := e.Uses.Value
if strings.Contains(spec, "${{") && strings.Contains(spec, "}}") {
// Cannot parse specification made with interpolation. Give up
return nil
}
if strings.HasPrefix(spec, "./") {
// Relative to repository root
rule.checkActionInSameRepo(spec, e)
return nil
}
if strings.HasPrefix(spec, "docker://") {
rule.checkDockerAction(spec, e)
return nil
}
rule.checkRepoAction(spec, e)
return nil
}
// Parse {owner}/{repo}@{ref} or {owner}/{repo}/{path}@{ref}
func (rule *RuleAction) checkRepoAction(spec string, exec *ExecAction) {
s := spec
idx := strings.IndexRune(s, '@')
if idx == -1 {
rule.invalidActionFormat(exec.Uses.Pos, spec, "ref is missing")
return
}
ref := s[idx+1:]
s = s[:idx] // remove {ref}
idx = strings.IndexRune(s, '/')
if idx == -1 {
rule.invalidActionFormat(exec.Uses.Pos, spec, "owner is missing")
return
}
owner := s[:idx]
s = s[idx+1:] // eat {owner}
repo := s
if idx := strings.IndexRune(s, '/'); idx >= 0 {
repo = s[:idx]
// path = s[idx+1:]
}
if owner == "" || repo == "" || ref == "" {
rule.invalidActionFormat(exec.Uses.Pos, spec, "owner and repo and ref should not be empty")
}
// TODO?: Fetch action.yaml from GitHub and check the specification with checkAction()
}
func (rule *RuleAction) invalidActionFormat(pos *Pos, spec string, why string) {
rule.errorf(pos, "specifying action %q in invalid format because %s. available formats are \"{owner}/{repo}@{ref}\" or \"{owner}/{repo}/{path}@{ref}\"", spec, why)
}
// https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#example-using-the-github-packages-container-registry
func (rule *RuleAction) checkDockerAction(uri string, exec *ExecAction) {
tag := ""
tagExists := false
if idx := strings.IndexRune(uri[len("docker://"):], ':'); idx != -1 {
idx += len("docker://")
if idx < len(uri) {
tag = uri[idx+1:]
uri = uri[:idx]
tagExists = true
}
}
if _, err := url.Parse(uri); err != nil {
rule.errorf(
exec.Uses.Pos,
"URI for Docker container %q is invalid: %s (tag=%s)",
uri,
err.Error(),
tag,
)
}
if tagExists && tag == "" {
rule.errorf(exec.Uses.Pos, "tag of Docker action should not be empty: %q", uri)
}
}
// https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#example-using-action-in-the-same-repository-as-the-workflow
func (rule *RuleAction) checkActionInSameRepo(path string, action *ExecAction) {
if rule.repoPath == "" {
return // Give up
}
dir := filepath.Join(rule.repoPath, filepath.FromSlash(path))
b := rule.readActionSpecFile(dir, action.Uses.Pos)
if len(b) == 0 {
return
}
var spec ActionSpec
if err := yaml.Unmarshal(b, &spec); err != nil {
rule.errorf(action.Uses.Pos, "action.yaml in %q is invalid: %s", dir, err.Error())
return
}
rule.checkAction(path, &spec, action)
}
func (rule *RuleAction) checkAction(path string, spec *ActionSpec, exec *ExecAction) {
// Check specified inputs are defined in action's inputs spec
for name, val := range exec.Inputs {
if _, ok := spec.Inputs[name]; !ok {
ss := make([]string, 0, len(spec.Inputs))
for k := range spec.Inputs {
ss = append(ss, k)
}
rule.errorf(
val.Name.Pos,
"input %q is not defined in action %q defined at %q. available inputs are %s",
name,
path,
spec.Name,
sortedQuotes(ss),
)
}
}
// Check mandatory inputs are specified
for name, input := range spec.Inputs {
if input.Required && input.Default == nil {
if _, ok := exec.Inputs[name]; !ok {
rule.errorf(
exec.Uses.Pos,
"missing input %q which is required by action %q defined at %q",
name,
spec.Name,
path,
)
}
}
}
}
func (rule *RuleAction) readActionSpecFile(dir string, pos *Pos) []byte {
for _, p := range []string{
filepath.Join(dir, "action.yaml"),
filepath.Join(dir, "action.yml"),
} {
if b, err := ioutil.ReadFile(p); err == nil {
return b
}
}
if wd, err := os.Getwd(); err == nil {
if p, err := filepath.Rel(wd, dir); err == nil {
dir = p
}
}
rule.errorf(pos, "Neither action.yaml nor action.yml is found in directory \"%s\"", dir)
return nil
}