forked from rhysd/actionlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule_expression.go
738 lines (638 loc) · 17.6 KB
/
rule_expression.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
package actionlint
import (
"fmt"
"strconv"
"strings"
)
// RuleExpression is a rule checker to check expression syntax in string values of workflow syntax.
// It checks syntax and semantics of the expressions including type checks and functions/contexts
// definitions. For more details see
// https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions
type RuleExpression struct {
RuleBase
matrixTy *ObjectType
stepsTy *ObjectType
needsTy *ObjectType
workflow *Workflow
}
// NewRuleExpression creates new RuleExpression instance.
func NewRuleExpression() *RuleExpression {
return &RuleExpression{
RuleBase: RuleBase{name: "expression"},
matrixTy: nil,
stepsTy: nil,
needsTy: nil,
workflow: nil,
}
}
// VisitWorkflowPre is callback when visiting Workflow node before visiting its children.
func (rule *RuleExpression) VisitWorkflowPre(n *Workflow) error {
rule.checkString(n.Name)
for _, e := range n.On {
switch e := e.(type) {
case *WebhookEvent:
rule.checkStrings(e.Types)
rule.checkStrings(e.Branches)
rule.checkStrings(e.BranchesIgnore)
rule.checkStrings(e.Tags)
rule.checkStrings(e.TagsIgnore)
rule.checkStrings(e.Paths)
rule.checkStrings(e.PathsIgnore)
rule.checkStrings(e.Workflows)
case *ScheduledEvent:
rule.checkStrings(e.Cron)
case *WorkflowDispatchEvent:
for _, i := range e.Inputs {
rule.checkString(i.Description)
rule.checkString(i.Default)
rule.checkBool(i.Required)
}
case *RepositoryDispatchEvent:
rule.checkStrings(e.Types)
}
}
rule.checkEnv(n.Env)
rule.checkDefaults(n.Defaults)
rule.checkConcurrency(n.Concurrency)
rule.workflow = n
return nil
}
// VisitWorkflowPost is callback when visiting Workflow node after visiting its children
func (rule *RuleExpression) VisitWorkflowPost(n *Workflow) error {
rule.workflow = nil
return nil
}
// VisitJobPre is callback when visiting Job node before visiting its children.
func (rule *RuleExpression) VisitJobPre(n *Job) error {
// Type of needs must be resolved before resolving type of matrix because `needs` context can
// be used in matrix configuration.
rule.needsTy = rule.calcNeedsType(n)
// Set matrix type at start of VisitJobPre() because matrix values are available in
// jobs.<job_id> section. For example:
// jobs:
// foo:
// strategy:
// matrix:
// os: [ubuntu-latest, macos-latest, windows-latest]
// runs-on: ${{ matrix.os }}
if n.Strategy != nil && n.Strategy.Matrix != nil {
rule.matrixTy = rule.guessTypeOfMatrix(n.Strategy.Matrix)
}
rule.checkString(n.Name)
rule.checkStrings(n.Needs)
if n.RunsOn != nil {
for _, l := range n.RunsOn.Labels {
rule.checkString(l)
}
}
if n.Environment != nil {
rule.checkString(n.Environment.Name)
rule.checkString(n.Environment.URL)
}
rule.checkConcurrency(n.Concurrency)
rule.checkEnv(n.Env)
rule.checkDefaults(n.Defaults)
rule.checkIfCondition(n.If)
if n.Strategy != nil {
if n.Strategy.Matrix != nil {
for _, r := range n.Strategy.Matrix.Rows {
for _, v := range r.Values {
rule.checkRawYAMLValue(v)
}
}
rule.checkMatrixCombinations(n.Strategy.Matrix.Include, "include")
rule.checkMatrixCombinations(n.Strategy.Matrix.Exclude, "exclude")
}
rule.checkBool(n.Strategy.FailFast)
rule.checkInt(n.Strategy.MaxParallel)
}
rule.checkBool(n.ContinueOnError)
rule.checkFloat(n.TimeoutMinutes)
rule.checkContainer(n.Container)
for _, s := range n.Services {
rule.checkContainer(s.Container)
}
rule.stepsTy = NewStrictObjectType()
return nil
}
// VisitJobPost is callback when visiting Job node after visiting its children
func (rule *RuleExpression) VisitJobPost(n *Job) error {
// outputs section is evaluated after all steps are run
for _, output := range n.Outputs {
rule.checkString(output.Value)
}
rule.matrixTy = nil
rule.stepsTy = nil
rule.needsTy = nil
return nil
}
// VisitStep is callback when visiting Step node.
func (rule *RuleExpression) VisitStep(n *Step) error {
rule.checkIfCondition(n.If)
rule.checkString(n.Name)
switch e := n.Exec.(type) {
case *ExecRun:
rule.checkString(e.Run)
rule.checkString(e.Shell)
rule.checkString(e.WorkingDirectory)
case *ExecAction:
rule.checkString(e.Uses)
for _, i := range e.Inputs {
rule.checkString(i.Value)
}
rule.checkString(e.Entrypoint)
rule.checkString(e.Args)
}
rule.checkEnv(n.Env)
rule.checkBool(n.ContinueOnError)
rule.checkFloat(n.TimeoutMinutes)
if n.ID != nil {
// Step ID is case insensitive
id := strings.ToLower(n.ID.Value)
rule.stepsTy.Props[id] = &ObjectType{
Props: map[string]ExprType{
"outputs": NewObjectType(),
"conclusion": StringType{},
"outcome": StringType{},
},
StrictProps: true,
}
}
return nil
}
func (rule *RuleExpression) checkOneExpression(s *String, what string) ExprType {
if s == nil {
return nil
}
tys := rule.checkString(s)
if len(tys) != 1 {
// This case should be unreachable since only one ${{ }} is included is checked by parser
rule.errorf(s.Pos, "one ${{ }} expression should be included in %q value but got %d expressions", what, len(tys))
return nil
}
return tys[0]
}
func (rule *RuleExpression) checkObjectTy(ty ExprType, pos *Pos, what string) ExprType {
if ty == nil {
return nil
}
switch ty.(type) {
case *ObjectType, AnyType:
return ty
default:
rule.errorf(pos, "type of expression at %q must be object but found type %s", what, ty.String())
return nil
}
}
func (rule *RuleExpression) checkArrayTy(ty ExprType, pos *Pos, what string) ExprType {
if ty == nil {
return nil
}
switch ty.(type) {
case *ArrayType, AnyType:
return ty
default:
rule.errorf(pos, "type of expression at %q must be array but found type %s", what, ty.String())
return nil
}
}
func (rule *RuleExpression) checkNumberTy(ty ExprType, pos *Pos, what string) ExprType {
if ty == nil {
return nil
}
switch ty.(type) {
case NumberType, AnyType:
return ty
default:
rule.errorf(pos, "type of expression at %q must be number but found type %s", what, ty.String())
return nil
}
}
func (rule *RuleExpression) checkObjectExpression(s *String, what string) ExprType {
ty := rule.checkOneExpression(s, what)
if ty == nil {
return nil
}
return rule.checkObjectTy(ty, s.Pos, what)
}
func (rule *RuleExpression) checkArrayExpression(s *String, what string) ExprType {
ty := rule.checkOneExpression(s, what)
if ty == nil {
return nil
}
return rule.checkArrayTy(ty, s.Pos, what)
}
func (rule *RuleExpression) checkNumberExpression(s *String, what string) ExprType {
ty := rule.checkOneExpression(s, what)
if ty == nil {
return nil
}
return rule.checkNumberTy(ty, s.Pos, what)
}
func (rule *RuleExpression) checkMatrixCombinations(cs *MatrixCombinations, what string) {
if cs == nil {
return
}
if cs.Expression != nil {
ty := rule.checkArrayExpression(cs.Expression, what)
if elem, ok := ElemTypeOf(ty); ok {
rule.checkObjectTy(elem, cs.Expression.Pos, what)
}
return
}
what = fmt.Sprintf("matrix combination at element of %s section", what)
for _, combi := range cs.Combinations {
if combi.Expression != nil {
rule.checkObjectExpression(combi.Expression, what)
continue
}
for _, a := range combi.Assigns {
rule.checkRawYAMLValue(a.Value)
}
}
}
func (rule *RuleExpression) checkEnv(env *Env) {
if env == nil {
return
}
if env.Vars != nil {
for _, e := range env.Vars {
rule.checkString(e.Value)
}
return
}
// When form of "env: ${{...}}"
rule.checkObjectExpression(env.Expression, "env")
}
func (rule *RuleExpression) checkContainer(c *Container) {
if c == nil {
return
}
rule.checkString(c.Image)
if c.Credentials != nil {
rule.checkString(c.Credentials.Username)
rule.checkString(c.Credentials.Password)
}
rule.checkEnv(c.Env)
rule.checkStrings(c.Ports)
rule.checkStrings(c.Volumes)
rule.checkString(c.Options)
}
func (rule *RuleExpression) checkConcurrency(c *Concurrency) {
if c == nil {
return
}
rule.checkString(c.Group)
rule.checkBool(c.CancelInProgress)
}
func (rule *RuleExpression) checkDefaults(d *Defaults) {
if d == nil || d.Run == nil {
return
}
rule.checkString(d.Run.Shell)
rule.checkString(d.Run.WorkingDirectory)
}
func (rule *RuleExpression) checkStrings(ss []*String) {
for _, s := range ss {
rule.checkString(s)
}
}
func (rule *RuleExpression) checkIfCondition(str *String) {
if str == nil {
return
}
// Note:
// https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idif
//
// > When you use expressions in an if conditional, you may omit the expression syntax (${{ }})
// > because GitHub automatically evaluates the if conditional as an expression, unless the
// > expression contains any operators. If the expression contains any operators, the expression
// > must be contained within ${{ }} to explicitly mark it for evaluation.
//
// This document is actually wrong. I confirmed that any strings without surrounding in ${{ }}
// are evaluated.
//
// - run: echo 'run'
// if: '!false'
// - run: echo 'not run'
// if: '!true'
// - run: echo 'run'
// if: false || true
// - run: echo 'run'
// if: true && true
// - run: echo 'not run'
// if: true && false
var condTy ExprType
if strings.Contains(str.Value, "${{") && strings.Contains(str.Value, "}}") {
if tys := rule.checkString(str); len(tys) == 1 {
s := strings.TrimSpace(str.Value)
if strings.HasPrefix(s, "${{") && strings.HasSuffix(s, "}}") {
condTy = tys[0]
}
}
} else {
src := str.Value + "}}" // }} is necessary since lexer lexes it as end of tokens
line, col := str.Pos.Line, str.Pos.Col
l := NewExprLexer()
tok, _, err := l.Lex(src)
if err != nil {
rule.exprError(err, line, col)
return
}
p := NewExprParser()
expr, err := p.Parse(tok)
if err != nil {
rule.exprError(err, line, col)
return
}
if p.sawOperator != nil {
op := p.sawOperator
pos := convertExprLineColToPos(op.Line, op.Column, str.Pos.Line, str.Pos.Col)
rule.errorf(
pos,
"this expression must be contained within ${{ }} like `if: ${{ ... }}` since it contains operator %q. see https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idif for more details",
p.sawOperator.Kind.String(),
)
}
condTy = rule.checkSemanticsOfExprNode(expr, line, col)
}
if condTy != nil && !(BoolType{}).Assignable(condTy) {
rule.errorf(str.Pos, "\"if\" condition should be type \"bool\" but got type %q", condTy.String())
}
}
func (rule *RuleExpression) checkString(str *String) []ExprType {
if str == nil {
return nil
}
return rule.checkExprsIn(str.Value, str.Pos, str.Quoted)
}
func (rule *RuleExpression) checkBool(b *Bool) {
if b == nil || b.Expression == nil {
return
}
ty := rule.checkOneExpression(b.Expression, "bool value")
switch ty.(type) {
case BoolType, AnyType:
// ok
default:
rule.errorf(b.Expression.Pos, "type of expression must be bool but found type %s", ty.String())
}
}
func (rule *RuleExpression) checkInt(i *Int) {
if i == nil {
return
}
rule.checkNumberExpression(i.Expression, "integer value")
}
func (rule *RuleExpression) checkFloat(f *Float) {
if f == nil {
return
}
rule.checkNumberExpression(f.Expression, "float number value")
}
func (rule *RuleExpression) checkExprsIn(s string, pos *Pos, quoted bool) []ExprType {
// TODO: Line number is not correct when the string contains newlines.
line, col := pos.Line, pos.Col
if quoted {
col++ // when the string is quoted like 'foo' or "foo", column should be incremented
}
offset := 0
tys := []ExprType{}
for {
idx := strings.Index(s, "${{")
if idx == -1 {
break
}
start := idx + 3 // 3 means removing "${{"
s = s[start:]
offset += start
ty, offsetAfter := rule.checkSemantics(s, line, col+offset)
if ty == nil || offsetAfter == 0 {
return nil
}
s = s[offsetAfter:]
offset += offsetAfter
tys = append(tys, ty)
}
return tys
}
func (rule *RuleExpression) checkRawYAMLValue(v RawYAMLValue) {
switch v := v.(type) {
case *RawYAMLObject:
for _, p := range v.Props {
rule.checkRawYAMLValue(p)
}
case *RawYAMLArray:
for _, v := range v.Elems {
rule.checkRawYAMLValue(v)
}
case *RawYAMLString:
rule.checkExprsIn(v.Value, v.Pos(), false)
default:
panic("unreachable")
}
}
func (rule *RuleExpression) exprError(err *ExprError, lineBase, colBase int) {
pos := convertExprLineColToPos(err.Line, err.Column, lineBase, colBase)
rule.error(pos, err.Message)
}
func (rule *RuleExpression) checkSemanticsOfExprNode(expr ExprNode, line, col int) ExprType {
c := NewExprSemanticsChecker()
if rule.matrixTy != nil {
c.UpdateMatrix(rule.matrixTy)
}
if rule.stepsTy != nil {
c.UpdateSteps(rule.stepsTy)
}
if rule.needsTy != nil {
c.UpdateNeeds(rule.needsTy)
}
ty, errs := c.Check(expr)
for _, err := range errs {
rule.exprError(err, line, col)
}
return ty
}
func (rule *RuleExpression) checkSemantics(src string, line, col int) (ExprType, int) {
l := NewExprLexer()
tok, offset, err := l.Lex(src)
if err != nil {
rule.exprError(err, line, col)
return nil, offset
}
p := NewExprParser()
expr, err := p.Parse(tok)
if err != nil {
rule.exprError(err, line, col)
return nil, offset
}
return rule.checkSemanticsOfExprNode(expr, line, col), offset
}
func (rule *RuleExpression) calcNeedsType(job *Job) *ObjectType {
// https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#needs-context
o := NewStrictObjectType()
rule.populateDependantNeedsTypes(o, job, job)
return o
}
func (rule *RuleExpression) populateDependantNeedsTypes(out *ObjectType, job *Job, root *Job) {
for _, id := range job.Needs {
i := strings.ToLower(id.Value) // ID is case insensitive
if i == root.ID.Value {
continue // When cyclic dependency exists. This does not happen normally.
}
if _, ok := out.Props[i]; ok {
continue // Already added
}
j, ok := rule.workflow.Jobs[i]
if !ok {
continue
}
outputs := NewStrictObjectType()
for name := range j.Outputs {
outputs.Props[name] = StringType{}
}
out.Props[i] = &ObjectType{
Props: map[string]ExprType{
"outputs": outputs,
"result": StringType{},
},
StrictProps: true,
}
rule.populateDependantNeedsTypes(out, j, root) // Add necessary needs props recursively
}
}
func (rule *RuleExpression) guessTypeOfMatrixExpression(expr *String) *ObjectType {
matTy, ok := rule.checkObjectExpression(expr, "matrix").(*ObjectType)
if !ok {
return NewObjectType()
}
// Consider properties in include section elements since 'include' section adds matrix values
incTy, ok := matTy.Props["include"]
if ok {
delete(matTy.Props, "include")
if elem, ok := ElemTypeOf(incTy); ok {
if o, ok := elem.(*ObjectType); ok {
for n, p := range o.Props {
t, ok := matTy.Props[n]
if !ok {
matTy.Props[n] = p
continue
}
matTy.Props[n] = t.Fuse(p)
}
}
}
}
delete(matTy.Props, "exclude")
return matTy
}
func (rule *RuleExpression) guessTypeOfMatrix(m *Matrix) *ObjectType {
if m.Expression != nil {
return rule.guessTypeOfMatrixExpression(m.Expression)
}
o := NewStrictObjectType()
for n, r := range m.Rows {
o.Props[n] = rule.guessTypeOfMatrixRow(r)
}
// Note: Type check in 'include' section duplicates with checkMatrixCombinations() method
if m.Include == nil {
return o
}
if m.Include.Expression != nil {
elem, ok := ElemTypeOf(rule.checkOneExpression(m.Include.Expression, "include"))
if !ok {
return NewObjectType()
}
ret, ok := o.Fuse(elem).(*ObjectType)
if !ok {
return NewObjectType()
}
return ret
}
for _, combi := range m.Include.Combinations {
if combi.Expression != nil {
ty := rule.checkOneExpression(m.Include.Expression, "matrix combination at element of include section")
if fused, ok := o.Fuse(ty).(*ObjectType); ok {
o = fused
} else {
o.StrictProps = false
}
continue
}
for n, assign := range combi.Assigns {
ty := guessTypeOfRawYAMLValue(assign.Value)
if t, ok := o.Props[n]; ok {
// When the combination exists in 'matrix' section, fuse type into existing one
ty = t.Fuse(ty)
}
o.Props[n] = ty
}
}
// Note: m.Exclude is not considered when guessing type of matrix
return o
}
func (rule *RuleExpression) guessTypeOfMatrixRow(r *MatrixRow) ExprType {
if r.Expression != nil {
ty, ok := ElemTypeOf(rule.checkArrayExpression(r.Expression, "matrix row"))
if !ok {
return AnyType{}
}
return ty
}
var ty ExprType
for _, v := range r.Values {
t := guessTypeOfRawYAMLValue(v)
if ty == nil {
ty = t
} else {
ty = ty.Fuse(t)
}
}
if ty == nil {
return AnyType{} // No element
}
return ty
}
func guessTypeOfRawYAMLValue(v RawYAMLValue) ExprType {
switch v := v.(type) {
case *RawYAMLObject:
m := make(map[string]ExprType, len(v.Props))
for k, p := range v.Props {
m[k] = guessTypeOfRawYAMLValue(p)
}
return &ObjectType{Props: m, StrictProps: true}
case *RawYAMLArray:
if len(v.Elems) == 0 {
return &ArrayType{AnyType{}, false}
}
elem := guessTypeOfRawYAMLValue(v.Elems[0])
for _, v := range v.Elems[1:] {
elem = elem.Fuse(guessTypeOfRawYAMLValue(v))
}
return &ArrayType{elem, false}
case *RawYAMLString:
return guessTypeFromString(v.Value)
default:
panic("unreachable")
}
}
func guessTypeFromString(s string) ExprType {
// Note that keywords are case sensitive. TRUE, FALSE, NULL are invalid named value.
if s == "true" || s == "false" {
return BoolType{}
}
if s == "null" {
return NullType{}
}
if _, err := strconv.ParseFloat(s, 64); err == nil {
return NumberType{}
}
return StringType{}
}
func convertExprLineColToPos(line, col, lineBase, colBase int) *Pos {
// Line and column in ExprError are 1-based
return &Pos{
Line: line - 1 + lineBase,
Col: col - 1 + colBase,
}
}