forked from rhysd/actionlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule_events.go
209 lines (194 loc) · 5.07 KB
/
rule_events.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 (
"time"
"github.com/robfig/cron"
)
// RuleEvents is a rule to check 'on' field in workflow.
// https://docs.github.com/en/actions/reference/events-that-trigger-workflows
type RuleEvents struct {
RuleBase
}
// NewRuleEvents creates new RuleEvents instance.
func NewRuleEvents() *RuleEvents {
return &RuleEvents{
RuleBase: RuleBase{name: "events"},
}
}
// VisitWorkflowPre is callback when visiting Workflow node before visiting its children.
func (rule *RuleEvents) VisitWorkflowPre(n *Workflow) error {
for _, e := range n.On {
rule.checkEvent(e)
}
return nil
}
func (rule *RuleEvents) checkEvent(event Event) {
switch e := event.(type) {
case *ScheduledEvent:
for _, c := range e.Cron {
rule.checkCron(c)
}
case *WorkflowDispatchEvent:
// Nothing to do
case *RepositoryDispatchEvent:
// Nothing to do
case *WebhookEvent:
rule.checkWebhookEvent(e)
default:
panic("unreachable")
}
}
// https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#onschedule
func (rule *RuleEvents) checkCron(spec *String) {
p := cron.NewParser(cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow)
sched, err := p.Parse(spec.Value)
if err != nil {
rule.errorf(spec.Pos, "invalid CRON format %q in schedule event: %s", spec.Value, err.Error())
return
}
start := sched.Next(time.Unix(0, 0))
next := sched.Next(start)
diff := next.Sub(start).Seconds()
if diff <= 60.0 {
rule.errorf(spec.Pos, "scheduled job runs too frequently. it runs once per %g seconds", diff)
}
}
// https://docs.github.com/en/actions/reference/events-that-trigger-workflows#webhook-events
func (rule *RuleEvents) checkWebhookEvent(event *WebhookEvent) {
types := []string{}
switch event.Hook.Value {
case "check_run":
types = []string{"created", "rerequested", "completed"}
case "check_suite":
types = []string{"completed", "requested", "rerequested"}
case "create":
case "delete":
case "deployment":
case "deployment_status":
case "fork":
case "gollum":
case "issue_comment":
types = []string{"created", "edited", "deleted"}
case "issues":
types = []string{
"opened",
"edited",
"deleted",
"transferred",
"pinned",
"unpinned",
"closed",
"reopened",
"assigned",
"unassigned",
"labeled",
"unlabeled",
"locked",
"unlocked",
"milestoned",
"demilestoned",
}
case "label":
types = []string{"created", "edited", "deleted"}
case "milestone":
types = []string{"created", "closed", "opened", "edited", "deleted"}
case "page_build":
case "project":
types = []string{"created", "updated", "closed", "reopened", "edited", "deleted"}
case "project_card":
types = []string{"created", "moved", "converted", "edited", "deleted"}
case "project_column":
types = []string{"created", "updated", "moved", "deleted"}
case "public":
case "pull_request":
types = []string{
"assigned",
"unassigned",
"labeled",
"unlabeled",
"opened",
"edited",
"closed",
"reopened",
"synchronize",
"ready_for_review",
"locked",
"unlocked",
"review_requested",
"review_request_removed",
}
case "pull_request_review":
types = []string{"submitted", "edited", "dismissed"}
case "pull_request_review_comment":
types = []string{"created", "edited", "deleted"}
case "pull_request_target":
types = []string{
"assigned",
"unassigned",
"labeled",
"unlabeled",
"opened",
"edited",
"closed",
"reopened",
"synchronize",
"ready_for_review",
"locked",
"unlocked",
"review_requested",
"review_request_removed",
}
case "push":
case "registry_package":
types = []string{"published", "updated"}
case "release":
types = []string{
"published",
"unpublished",
"created",
"edited",
"deleted",
"prereleased",
"released",
}
case "status":
case "watch":
types = []string{"started"}
case "workflow_run":
types = []string{"completed", "requested"}
// TODO: Check "workflows" configuration looking at other workflow files
if len(event.Workflows) == 0 {
rule.error(event.Pos, "no workflow is configured for \"workflow_run\" event")
}
default:
rule.errorf(event.Pos, "unknown Webhook event %q. see https://docs.github.com/en/actions/reference/events-that-trigger-workflows#webhook-events for list of all Webhook event names", event.Hook.Value)
return
}
rule.checkTypes(event.Hook, event.Types, types)
if event.Hook.Value != "workflow_run" && len(event.Workflows) != 0 {
rule.errorf(event.Pos, "\"workflows\" cannot be configured for %q event. it is only for workflow_run event", event.Hook.Value)
}
}
func (rule *RuleEvents) checkTypes(hook *String, types []*String, expected []string) {
if len(expected) == 0 && len(types) > 0 {
rule.errorf(hook.Pos, "\"types\" cannot be specified for %q Webhook event", hook.Value)
return
}
for _, ty := range types {
valid := false
for _, e := range expected {
if ty.Value == e {
valid = true
break
}
}
if !valid {
rule.errorf(
ty.Pos,
"invalid activity type %q for %q Webhook event. available types are %s",
ty.Value,
hook.Value,
sortedQuotes(expected),
)
}
}
}