Skip to content

Commit 9753c20

Browse files
committed
Add a constructor for RuleBase and change error and errorf from private to public, otherwise It's not possible to create new rules
1 parent 571fff4 commit 9753c20

18 files changed

+78
-71
lines changed

rule.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ type RuleBase struct {
1515
config *Config
1616
}
1717

18+
func NewRuleBase(name string, desc string) *RuleBase {
19+
return &RuleBase{
20+
name: name,
21+
desc: desc,
22+
}
23+
}
24+
1825
// VisitStep is callback when visiting Step node.
1926
func (r *RuleBase) VisitStep(node *Step) error { return nil }
2027

@@ -30,12 +37,12 @@ func (r *RuleBase) VisitWorkflowPre(node *Workflow) error { return nil }
3037
// VisitWorkflowPost is callback when visiting Workflow node after visiting its children.
3138
func (r *RuleBase) VisitWorkflowPost(node *Workflow) error { return nil }
3239

33-
func (r *RuleBase) error(pos *Pos, msg string) {
40+
func (r *RuleBase) Error(pos *Pos, msg string) {
3441
err := errorAt(pos, r.name, msg)
3542
r.errs = append(r.errs, err)
3643
}
3744

38-
func (r *RuleBase) errorf(pos *Pos, format string, args ...interface{}) {
45+
func (r *RuleBase) Errorf(pos *Pos, format string, args ...interface{}) {
3946
err := errorfAt(pos, r.name, format, args...)
4047
r.errs = append(r.errs, err)
4148
}

rule_action.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (rule *RuleAction) checkRepoAction(spec string, exec *ExecAction) {
9999
}
100100

101101
func (rule *RuleAction) invalidActionFormat(pos *Pos, spec string, why string) {
102-
rule.errorf(pos, "specifying action %q in invalid format because %s. available formats are \"{owner}/{repo}@{ref}\" or \"{owner}/{repo}/{path}@{ref}\"", spec, why)
102+
rule.Errorf(pos, "specifying action %q in invalid format because %s. available formats are \"{owner}/{repo}@{ref}\" or \"{owner}/{repo}/{path}@{ref}\"", spec, why)
103103
}
104104

105105
// https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#example-using-the-github-packages-container-registry
@@ -116,7 +116,7 @@ func (rule *RuleAction) checkDockerAction(uri string, exec *ExecAction) {
116116
}
117117

118118
if _, err := url.Parse(uri); err != nil {
119-
rule.errorf(
119+
rule.Errorf(
120120
exec.Uses.Pos,
121121
"URI for Docker container %q is invalid: %s (tag=%s)",
122122
uri,
@@ -126,15 +126,15 @@ func (rule *RuleAction) checkDockerAction(uri string, exec *ExecAction) {
126126
}
127127

128128
if tagExists && tag == "" {
129-
rule.errorf(exec.Uses.Pos, "tag of Docker action should not be empty: %q", uri)
129+
rule.Errorf(exec.Uses.Pos, "tag of Docker action should not be empty: %q", uri)
130130
}
131131
}
132132

133133
// https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#example-using-action-in-the-same-repository-as-the-workflow
134134
func (rule *RuleAction) checkLocalAction(path string, action *ExecAction) {
135135
meta, err := rule.cache.FindMetadata(path)
136136
if err != nil {
137-
rule.error(action.Uses.Pos, err.Error())
137+
rule.Error(action.Uses.Pos, err.Error())
138138
return
139139
}
140140
if meta == nil {
@@ -154,7 +154,7 @@ func (rule *RuleAction) checkAction(meta *ActionMetadata, exec *ExecAction, desc
154154
for _, i := range meta.Inputs {
155155
ns = append(ns, i.Name)
156156
}
157-
rule.errorf(
157+
rule.Errorf(
158158
i.Name.Pos,
159159
"input %q is not defined in action %s. available inputs are %s",
160160
i.Name.Value,
@@ -174,7 +174,7 @@ func (rule *RuleAction) checkAction(meta *ActionMetadata, exec *ExecAction, desc
174174
ns = append(ns, i.Name)
175175
}
176176
}
177-
rule.errorf(
177+
rule.Errorf(
178178
exec.Uses.Pos,
179179
"missing input %q which is required by action %s. all required inputs are %s",
180180
i.Name,

rule_credentials.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ func (rule *RuleCredentials) checkContainer(where string, n *Container) {
3737

3838
p := n.Credentials.Password
3939
if !p.IsExpressionAssigned() {
40-
rule.errorf(p.Pos, "\"password\" section in %s should be specified via secrets. do not put password value directly", where)
40+
rule.Errorf(p.Pos, "\"password\" section in %s should be specified via secrets. do not put password value directly", where)
4141
}
4242
}

rule_deprecated_commands.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (rule *RuleDeprecatedCommands) VisitStep(n *Step) error {
4646
panic("unreachable")
4747
}
4848

49-
rule.errorf(
49+
rule.Errorf(
5050
r.Run.Pos,
5151
"workflow command %q was deprecated. use `%s` instead: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions",
5252
c,

rule_env_var.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (rule *RuleEnvVar) checkEnv(env *Env) {
5050
continue // Key name can contain expressions (#312)
5151
}
5252
if strings.ContainsAny(v.Name.Value, "&= ") {
53-
rule.errorf(
53+
rule.Errorf(
5454
v.Name.Pos,
5555
"environment variable name %q is invalid. '&', '=' and spaces should not be contained",
5656
v.Name.Value,

rule_events.go

+19-19
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (rule *RuleEvents) checkCron(spec *String) {
5858
p := cron.NewParser(cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow)
5959
sched, err := p.Parse(spec.Value)
6060
if err != nil {
61-
rule.errorf(spec.Pos, "invalid CRON format %q in schedule event: %s", spec.Value, err.Error())
61+
rule.Errorf(spec.Pos, "invalid CRON format %q in schedule event: %s", spec.Value, err.Error())
6262
return
6363
}
6464

@@ -70,7 +70,7 @@ func (rule *RuleEvents) checkCron(spec *String) {
7070
//
7171
// > The shortest interval you can run scheduled workflows is once every 5 minutes.
7272
if diff < 60.0*5 {
73-
rule.errorf(spec.Pos, "scheduled job runs too frequently. it runs once per %g seconds. the shortest interval is once every 5 minutes", diff)
73+
rule.Errorf(spec.Pos, "scheduled job runs too frequently. it runs once per %g seconds. the shortest interval is once every 5 minutes", diff)
7474
}
7575
}
7676

@@ -79,7 +79,7 @@ func (rule *RuleEvents) filterNotAvailable(pos *Pos, filter, hook string, availa
7979
if len(available) < 2 {
8080
e = "event"
8181
}
82-
rule.errorf(pos, "%q filter is not available for %s event. it is only for %s %s", filter, hook, strings.Join(available, ", "), e)
82+
rule.Errorf(pos, "%q filter is not available for %s event. it is only for %s %s", filter, hook, strings.Join(available, ", "), e)
8383
}
8484

8585
func (rule *RuleEvents) checkExclusiveFilters(filter, ignore *WebhookEventFilter, hook string, available []string) {
@@ -97,7 +97,7 @@ func (rule *RuleEvents) checkExclusiveFilters(filter, ignore *WebhookEventFilter
9797
if p.IsBefore(ignore.Name.Pos) {
9898
p = ignore.Name.Pos
9999
}
100-
rule.errorf(p, "both %q and %q filters cannot be used for the same event %q. note: use '!' to negate patterns", filter.Name.Value, ignore.Name.Value, hook)
100+
rule.Errorf(p, "both %q and %q filters cannot be used for the same event %q. note: use '!' to negate patterns", filter.Name.Value, ignore.Name.Value, hook)
101101
}
102102
} else {
103103
if !filter.IsEmpty() {
@@ -115,19 +115,19 @@ func (rule *RuleEvents) checkWebhookEvent(event *WebhookEvent) {
115115

116116
types, ok := AllWebhookTypes[hook]
117117
if !ok {
118-
rule.errorf(event.Pos, "unknown Webhook event %q. see https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows#webhook-events for list of all Webhook event names", hook)
118+
rule.Errorf(event.Pos, "unknown Webhook event %q. see https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows#webhook-events for list of all Webhook event names", hook)
119119
return
120120
}
121121

122122
rule.checkTypes(event.Hook, event.Types, types)
123123

124124
if hook == "workflow_run" {
125125
if len(event.Workflows) == 0 {
126-
rule.error(event.Pos, "no workflow is configured for \"workflow_run\" event")
126+
rule.Error(event.Pos, "no workflow is configured for \"workflow_run\" event")
127127
}
128128
} else {
129129
if len(event.Workflows) != 0 {
130-
rule.errorf(event.Pos, "\"workflows\" cannot be configured for %q event. it is only for workflow_run event", hook)
130+
rule.Errorf(event.Pos, "\"workflows\" cannot be configured for %q event. it is only for workflow_run event", hook)
131131
}
132132
}
133133

@@ -158,7 +158,7 @@ func (rule *RuleEvents) checkWebhookEvent(event *WebhookEvent) {
158158

159159
func (rule *RuleEvents) checkTypes(hook *String, types []*String, expected []string) {
160160
if len(expected) == 0 && len(types) > 0 {
161-
rule.errorf(hook.Pos, "\"types\" cannot be specified for %q Webhook event", hook.Value)
161+
rule.Errorf(hook.Pos, "\"types\" cannot be specified for %q Webhook event", hook.Value)
162162
return
163163
}
164164

@@ -171,7 +171,7 @@ func (rule *RuleEvents) checkTypes(hook *String, types []*String, expected []str
171171
}
172172
}
173173
if !valid {
174-
rule.errorf(
174+
rule.Errorf(
175175
ty.Pos,
176176
"invalid activity type %q for %q Webhook event. available types are %s",
177177
ty.Value,
@@ -193,7 +193,7 @@ func (rule *RuleEvents) checkWorkflowCallEvent(event *WorkflowCallEvent) {
193193
switch i.Type {
194194
case WorkflowCallEventInputTypeNumber:
195195
if _, err := strconv.ParseFloat(i.Default.Value, 64); err != nil {
196-
rule.errorf(
196+
rule.Errorf(
197197
i.Default.Pos,
198198
"input of workflow_call event %q is typed as number but its default value %q cannot be parsed as a float number: %s",
199199
i.Name.Value,
@@ -203,7 +203,7 @@ func (rule *RuleEvents) checkWorkflowCallEvent(event *WorkflowCallEvent) {
203203
}
204204
case WorkflowCallEventInputTypeBoolean:
205205
if d := strings.ToLower(i.Default.Value); d != "true" && d != "false" {
206-
rule.errorf(
206+
rule.Errorf(
207207
i.Default.Pos,
208208
"input of workflow_call event %q is typed as boolean. its default value must be true or false but got %q",
209209
i.Name.Value,
@@ -213,7 +213,7 @@ func (rule *RuleEvents) checkWorkflowCallEvent(event *WorkflowCallEvent) {
213213
}
214214
}
215215
if i.IsRequired() {
216-
rule.errorf(
216+
rule.Errorf(
217217
i.Default.Pos,
218218
"input %q of workflow_call event has the default value %q, but it is also required. if an input is marked as required, its default value will never be used",
219219
i.Name.Value,
@@ -229,13 +229,13 @@ func (rule *RuleEvents) checkWorkflowDispatchEvent(event *WorkflowDispatchEvent)
229229
for n, i := range event.Inputs {
230230
if i.Type == WorkflowDispatchEventInputTypeChoice {
231231
if len(i.Options) == 0 {
232-
rule.errorf(i.Name.Pos, "input type of %q is \"choice\" but \"options\" is not set", n)
232+
rule.Errorf(i.Name.Pos, "input type of %q is \"choice\" but \"options\" is not set", n)
233233
continue
234234
}
235235
seen := make(map[string]struct{}, len(i.Options))
236236
for _, o := range i.Options {
237237
if _, ok := seen[o.Value]; ok {
238-
rule.errorf(o.Pos, "option %q is duplicated in options of %q input", o.Value, n)
238+
rule.Errorf(o.Pos, "option %q is duplicated in options of %q input", o.Value, n)
239239
continue
240240
}
241241
seen[o.Value] = struct{}{}
@@ -246,20 +246,20 @@ func (rule *RuleEvents) checkWorkflowDispatchEvent(event *WorkflowDispatchEvent)
246246
b.append(o.Value)
247247
}
248248
if _, ok := seen[i.Default.Value]; !ok {
249-
rule.errorf(i.Default.Pos, "default value %q of %q input is not included in its options %q", i.Default.Value, n, b.build())
249+
rule.Errorf(i.Default.Pos, "default value %q of %q input is not included in its options %q", i.Default.Value, n, b.build())
250250
}
251251
}
252252
} else {
253253
if len(i.Options) > 0 {
254-
rule.errorf(i.Name.Pos, "\"options\" can not be set to %q input because its input type is not \"choice\"", n)
254+
rule.Errorf(i.Name.Pos, "\"options\" can not be set to %q input because its input type is not \"choice\"", n)
255255
}
256256
if i.Default != nil {
257257
// TODO: Can some check be done for WorkflowDispatchEventInputTypeEnvironment?
258258
// What is suitable for default value of the type? (Or is a default value never suitable?)
259259
switch i.Type {
260260
case WorkflowDispatchEventInputTypeNumber:
261261
if _, err := strconv.ParseFloat(i.Default.Value, 64); err != nil {
262-
rule.errorf(
262+
rule.Errorf(
263263
i.Default.Pos,
264264
"type of %q input is \"number\" but its default value %q cannot be parsed as a float number: %s",
265265
i.Name.Value,
@@ -269,7 +269,7 @@ func (rule *RuleEvents) checkWorkflowDispatchEvent(event *WorkflowDispatchEvent)
269269
}
270270
case WorkflowDispatchEventInputTypeBoolean:
271271
if d := strings.ToLower(i.Default.Value); d != "true" && d != "false" {
272-
rule.errorf(i.Default.Pos, "type of %q input is \"boolean\". its default value %q must be \"true\" or \"false\"", n, i.Default.Value)
272+
rule.Errorf(i.Default.Pos, "type of %q input is \"boolean\". its default value %q must be \"true\" or \"false\"", n, i.Default.Value)
273273
}
274274
}
275275
}
@@ -278,7 +278,7 @@ func (rule *RuleEvents) checkWorkflowDispatchEvent(event *WorkflowDispatchEvent)
278278
// Maximum number of inputs is 10
279279
// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#providing-inputs
280280
if len(event.Inputs) > 10 {
281-
rule.errorf(
281+
rule.Errorf(
282282
event.Pos,
283283
"maximum number of inputs for \"workflow_dispatch\" event is 10 but %d inputs are provided. see https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#providing-inputs",
284284
len(event.Inputs),

0 commit comments

Comments
 (0)