-
Notifications
You must be signed in to change notification settings - Fork 163
/
parse.go
1433 lines (1282 loc) · 40.8 KB
/
parse.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
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package actionlint
import (
"fmt"
"math"
"regexp"
"strconv"
"strings"
"gopkg.in/yaml.v3"
)
// https://pkg.go.dev/gopkg.in/yaml.v3#Kind
func nodeKindName(k yaml.Kind) string {
switch k {
case yaml.DocumentNode:
return "document"
case yaml.SequenceNode:
return "sequence"
case yaml.MappingNode:
return "mapping"
case yaml.ScalarNode:
return "scalar"
case yaml.AliasNode:
return "alias"
default:
panic(fmt.Sprintf("unreachable: unknown YAML kind: %v", k))
}
}
func posAt(n *yaml.Node) *Pos {
return &Pos{n.Line, n.Column}
}
func isNull(n *yaml.Node) bool {
return n.Kind == yaml.ScalarNode && n.Tag == "!!null"
}
func newString(n *yaml.Node) *String {
quoted := n.Style&(yaml.DoubleQuotedStyle|yaml.SingleQuotedStyle) != 0
return &String{n.Value, quoted, posAt(n)}
}
type workflowKeyVal struct {
// id is used for comparing keys. When the key is case insensitive, this field is in lower case.
id string
key *String
val *yaml.Node
}
type parser struct {
errors []*Error
}
func (p *parser) error(n *yaml.Node, m string) {
p.errors = append(p.errors, &Error{m, "", n.Line, n.Column, "syntax-check"})
}
func (p *parser) errorAt(pos *Pos, m string) {
p.errors = append(p.errors, &Error{m, "", pos.Line, pos.Col, "syntax-check"})
}
func (p *parser) errorfAt(pos *Pos, format string, args ...interface{}) {
m := fmt.Sprintf(format, args...)
p.errorAt(pos, m)
}
func (p *parser) errorf(n *yaml.Node, format string, args ...interface{}) {
m := fmt.Sprintf(format, args...)
p.error(n, m)
}
func (p *parser) unexpectedKey(s *String, sec string, expected []string) {
l := len(expected)
var m string
if l == 1 {
m = fmt.Sprintf("expected %q key for %q section but got %q", expected[0], sec, s.Value)
} else if l > 1 {
m = fmt.Sprintf("unexpected key %q for %q section. expected one of %v", s.Value, sec, sortedQuotes(expected))
} else {
m = fmt.Sprintf("unexpected key %q for %q section", s.Value, sec)
}
p.errorAt(s.Pos, m)
}
func (p *parser) checkNotEmpty(sec string, len int, n *yaml.Node) bool {
if len == 0 {
p.errorf(n, "%q section should not be empty", sec)
return false
}
return true
}
func (p *parser) checkSequence(sec string, n *yaml.Node, allowEmpty bool) bool {
if n.Kind != yaml.SequenceNode {
p.errorf(n, "%q section must be sequence node but got %s node with %q tag", sec, nodeKindName(n.Kind), n.Tag)
return false
}
return allowEmpty || p.checkNotEmpty(sec, len(n.Content), n)
}
func (p *parser) checkString(n *yaml.Node, allowEmpty bool) bool {
// Do not check n.Tag is !!str because we don't need to check the node is string strictly.
// In almost all cases, other nodes (like 42) are handled as string with its string representation.
if n.Kind != yaml.ScalarNode {
p.errorf(n, "expected scalar node for string value but found %s node with %q tag", nodeKindName(n.Kind), n.Tag)
return false
}
if !allowEmpty && n.Value == "" {
p.error(n, "string should not be empty")
return false
}
return true
}
func (p *parser) missingExpression(n *yaml.Node, expecting string) {
p.errorf(n, "expecting a single ${{...}} expression or %s, but found plain text node", expecting)
}
func (p *parser) parseExpression(n *yaml.Node, expecting string) *String {
if !isExprAssigned(n.Value) {
p.missingExpression(n, expecting)
return nil
}
return newString(n)
}
func (p *parser) mayParseExpression(n *yaml.Node) *String {
if n.Tag != "!!str" {
return nil
}
if !isExprAssigned(n.Value) {
return nil
}
return newString(n)
}
func (p *parser) parseString(n *yaml.Node, allowEmpty bool) *String {
if !p.checkString(n, allowEmpty) {
return &String{"", false, posAt(n)}
}
return newString(n)
}
func (p *parser) parseStringSequence(sec string, n *yaml.Node, allowEmpty bool, allowElemEmpty bool) []*String {
if ok := p.checkSequence(sec, n, allowEmpty); !ok {
return nil
}
ss := make([]*String, 0, len(n.Content))
for _, c := range n.Content {
s := p.parseString(c, allowElemEmpty)
if s != nil {
ss = append(ss, s)
}
}
return ss
}
func (p *parser) parseStringOrStringSequence(sec string, n *yaml.Node, allowEmpty bool, allowElemEmpty bool) []*String {
switch n.Kind {
case yaml.ScalarNode:
if allowEmpty && n.Tag == "!!null" {
return []*String{} // In the case of 'foo:'
}
return []*String{p.parseString(n, allowElemEmpty)}
default:
return p.parseStringSequence(sec, n, allowEmpty, allowElemEmpty)
}
}
func (p *parser) parseBool(n *yaml.Node) *Bool {
if n.Kind != yaml.ScalarNode || (n.Tag != "!!bool" && n.Tag != "!!str") {
p.errorf(n, "expected bool value but found %s node with %q tag", nodeKindName(n.Kind), n.Tag)
return nil
}
if n.Tag == "!!str" {
e := p.parseExpression(n, "boolean literal \"true\" or \"false\"")
return &Bool{
Expression: e,
Pos: posAt(n),
}
}
return &Bool{
Value: n.Value == "true",
Pos: posAt(n),
}
}
func (p *parser) parseInt(n *yaml.Node) *Int {
if n.Kind != yaml.ScalarNode || (n.Tag != "!!int" && n.Tag != "!!str") {
p.errorf(n, "expected scalar node for integer value but found %s node with %q tag", nodeKindName(n.Kind), n.Tag)
return nil
}
if n.Tag == "!!str" {
e := p.parseExpression(n, "integer literal")
if e == nil {
return nil
}
return &Int{
Expression: e,
Pos: posAt(n),
}
}
i, err := strconv.Atoi(n.Value)
if err != nil {
p.errorf(n, "invalid integer value: %q: %s", n.Value, err.Error())
return nil
}
return &Int{
Value: i,
Pos: posAt(n),
}
}
func (p *parser) parseFloat(n *yaml.Node) *Float {
if n.Kind != yaml.ScalarNode || (n.Tag != "!!float" && n.Tag != "!!int" && n.Tag != "!!str") {
p.errorf(n, "expected scalar node for float value but found %s node with %q tag", nodeKindName(n.Kind), n.Tag)
return nil
}
if n.Tag == "!!str" {
e := p.parseExpression(n, "float number literal")
if e == nil {
return nil
}
return &Float{
Expression: e,
Pos: posAt(n),
}
}
f, err := strconv.ParseFloat(n.Value, 64)
if err != nil || math.IsNaN(f) {
p.errorf(n, "invalid float value: %q: %s", n.Value, err.Error())
return nil
}
return &Float{
Value: f,
Pos: posAt(n),
}
}
func (p *parser) parseMapping(what string, n *yaml.Node, allowEmpty, caseSensitive bool) []workflowKeyVal {
isNull := isNull(n)
if !isNull && n.Kind != yaml.MappingNode {
p.errorf(n, "%s is %s node but mapping node is expected", what, nodeKindName(n.Kind))
return nil
}
if !allowEmpty && isNull {
p.errorf(n, "%s should not be empty. please remove this section if it's unnecessary", what)
return nil
}
l := len(n.Content) / 2
keys := make(map[string]*Pos, l)
m := make([]workflowKeyVal, 0, l)
for i := 0; i < len(n.Content); i += 2 {
k := p.parseString(n.Content[i], false)
if k == nil {
continue
}
id := k.Value
if !caseSensitive {
// Keys of mappings are sometimes case insensitive. For example, following matrix is invalid.
// matrix:
// foo: [1, 2, 3]
// FOO: [1, 2, 3]
// To detect case insensitive duplicate keys, we use lowercase keys
id = strings.ToLower(id)
}
if pos, ok := keys[id]; ok {
var note string
if !caseSensitive {
note = ". note that this key is case insensitive"
}
p.errorfAt(k.Pos, "key %q is duplicated in %s. previously defined at %s%s", k.Value, what, pos.String(), note)
continue
}
m = append(m, workflowKeyVal{id, k, n.Content[i+1]})
keys[id] = k.Pos
}
if !allowEmpty && len(m) == 0 {
p.errorf(n, "%s should not be empty. please remove this section if it's unnecessary", what)
}
return m
}
func (p *parser) parseSectionMapping(sec string, n *yaml.Node, allowEmpty, caseSensitive bool) []workflowKeyVal {
return p.parseMapping(fmt.Sprintf("%q section", sec), n, allowEmpty, caseSensitive)
}
func (p *parser) parseScheduleEvent(pos *Pos, n *yaml.Node) *ScheduledEvent {
if ok := p.checkSequence("schedule", n, false); !ok {
return nil
}
cron := make([]*String, 0, len(n.Content))
for _, c := range n.Content {
m := p.parseMapping("element of \"schedule\" section", c, false, true)
if len(m) != 1 || m[0].id != "cron" {
p.error(c, "element of \"schedule\" section must be mapping and must contain one key \"cron\"")
continue
}
s := p.parseString(m[0].val, false)
if s != nil {
cron = append(cron, s)
}
}
return &ScheduledEvent{cron, pos}
}
// https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows#workflow_dispatch
func (p *parser) parseWorkflowDispatchEvent(pos *Pos, n *yaml.Node) *WorkflowDispatchEvent {
ret := &WorkflowDispatchEvent{Pos: pos}
for _, kv := range p.parseSectionMapping("workflow_dispatch", n, true, true) {
if kv.id != "inputs" {
p.unexpectedKey(kv.key, "workflow_dispatch", []string{"inputs"})
continue
}
inputs := p.parseSectionMapping("inputs", kv.val, true, false)
ret.Inputs = make(map[string]*DispatchInput, len(inputs))
for _, input := range inputs {
name, spec := input.key, input.val
var desc *String
var req *Bool
var def *String
var ty WorkflowDispatchEventInputType = WorkflowDispatchEventInputTypeNone
var opts []*String
for _, attr := range p.parseMapping("input settings of workflow_dispatch event", spec, true, true) {
switch attr.id {
case "description":
desc = p.parseString(attr.val, true)
case "required":
req = p.parseBool(attr.val)
case "default":
def = p.parseString(attr.val, true)
case "type":
if !p.checkString(attr.val, false) {
continue
}
switch attr.val.Value {
case "string":
ty = WorkflowDispatchEventInputTypeString
case "number":
ty = WorkflowDispatchEventInputTypeNumber
case "boolean":
ty = WorkflowDispatchEventInputTypeBoolean
case "choice":
ty = WorkflowDispatchEventInputTypeChoice
case "environment":
ty = WorkflowDispatchEventInputTypeEnvironment
default:
p.errorf(attr.val, `input type of workflow_dispatch event must be one of "string", "number", "boolean", "choice", "environment" but got %q`, attr.val.Value)
}
case "options":
opts = p.parseStringSequence("options", attr.val, false, false)
default:
p.unexpectedKey(attr.key, "inputs", []string{"description", "required", "default"})
}
}
ret.Inputs[input.id] = &DispatchInput{
Name: name,
Description: desc,
Required: req,
Default: def,
Type: ty,
Options: opts,
}
}
}
return ret
}
// https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows#repository_dispatch
func (p *parser) parseRepositoryDispatchEvent(pos *Pos, n *yaml.Node) *RepositoryDispatchEvent {
ret := &RepositoryDispatchEvent{Pos: pos}
// Note: Omitting 'types' is ok. In the case, all types trigger the workflow
for _, kv := range p.parseSectionMapping("repository_dispatch", n, true, true) {
if kv.id == "types" {
ret.Types = p.parseStringOrStringSequence("types", kv.val, false, false)
} else {
p.unexpectedKey(kv.key, "repository_dispatch", []string{"types"})
}
}
return ret
}
// https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#using-filters
func (p *parser) parseWebhookEventFilter(name *String, n *yaml.Node) *WebhookEventFilter {
v := p.parseStringOrStringSequence(name.Value, n, false, false)
return &WebhookEventFilter{name, v}
}
func (p *parser) parseWebhookEvent(name *String, n *yaml.Node) *WebhookEvent {
ret := &WebhookEvent{Hook: name, Pos: name.Pos}
// Note: 'tags', 'tags-ignore', 'branches', 'branches-ignore' can be empty. Since there are
// some cases where setting empty values to them is necessary.
//
// > If only define only tags filter (tags/tags-ignore) or only branches filter
// > (branches/branches-ignore) for on.push, the workflow won’t run for events affecting the
// > undefined Git ref.
//
// https://github.community/t/using-on-push-tags-ignore-and-paths-ignore-together/16931
for _, kv := range p.parseSectionMapping(name.Value, n, true, true) {
// Note: Glob pattern cannot be empty, but it is checked by 'glob' rule with better error
// message. So parser allows empty patterns here.
switch kv.id {
case "types":
ret.Types = p.parseStringOrStringSequence(kv.key.Value, kv.val, false, false)
case "branches":
ret.Branches = p.parseWebhookEventFilter(kv.key, kv.val)
case "branches-ignore":
ret.BranchesIgnore = p.parseWebhookEventFilter(kv.key, kv.val)
case "tags":
ret.Tags = p.parseWebhookEventFilter(kv.key, kv.val)
case "tags-ignore":
ret.TagsIgnore = p.parseWebhookEventFilter(kv.key, kv.val)
case "paths":
ret.Paths = p.parseWebhookEventFilter(kv.key, kv.val)
case "paths-ignore":
ret.PathsIgnore = p.parseWebhookEventFilter(kv.key, kv.val)
case "workflows":
ret.Workflows = p.parseStringOrStringSequence(kv.key.Value, kv.val, false, false)
default:
p.unexpectedKey(kv.key, name.Value, []string{
"types",
"branches",
"branches-ignore",
"tags",
"tags-ignore",
"paths",
"paths-ignore",
"workflows",
})
}
}
return ret
}
// - https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows#workflow-reuse-events
// - https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#onworkflow_callinputs
// - https://docs.github.com/en/actions/learn-github-actions/reusing-workflows
func (p *parser) parseWorkflowCallEvent(pos *Pos, n *yaml.Node) *WorkflowCallEvent {
ret := &WorkflowCallEvent{Pos: pos}
for _, kv := range p.parseSectionMapping("workflow_call", n, true, true) {
switch kv.id {
case "inputs":
inputs := p.parseSectionMapping("inputs", kv.val, true, false)
ret.Inputs = make([]*WorkflowCallEventInput, 0, len(inputs))
for _, kv := range inputs {
name, spec := kv.key, kv.val
input := &WorkflowCallEventInput{Name: name, ID: kv.id}
sawType := false
for _, attr := range p.parseMapping("input of workflow_call event", spec, true, true) {
switch attr.id {
case "description":
input.Description = p.parseString(attr.val, true)
case "required":
input.Required = p.parseBool(attr.val)
case "default":
input.Default = p.parseString(attr.val, true)
case "type":
switch attr.val.Value {
case "boolean":
input.Type = WorkflowCallEventInputTypeBoolean
case "number":
input.Type = WorkflowCallEventInputTypeNumber
case "string":
input.Type = WorkflowCallEventInputTypeString
default:
p.errorf(attr.val, "invalid value %q for input type of workflow_call event. it must be one of \"boolean\", \"number\", or \"string\"", attr.val.Value)
}
sawType = true
default:
p.unexpectedKey(attr.key, "inputs at workflow_call event", []string{"description", "required", "default", "type"})
}
}
if !sawType {
p.errorfAt(name.Pos, "\"type\" is missing at %q input of workflow_call event", name.Value)
}
ret.Inputs = append(ret.Inputs, input)
}
case "secrets":
secrets := p.parseSectionMapping("secrets", kv.val, true, false)
ret.Secrets = make(map[string]*WorkflowCallEventSecret, len(secrets))
for _, kv := range secrets {
name, spec := kv.key, kv.val
secret := &WorkflowCallEventSecret{Name: name}
for _, attr := range p.parseMapping("secret of workflow_call event", spec, true, true) {
switch attr.id {
case "description":
secret.Description = p.parseString(attr.val, true)
case "required":
secret.Required = p.parseBool(attr.val)
default:
p.unexpectedKey(attr.key, "secrets", []string{"description", "required"})
}
}
ret.Secrets[kv.id] = secret
}
case "outputs":
outputs := p.parseSectionMapping("outputs", kv.val, true, false)
ret.Outputs = make(map[string]*WorkflowCallEventOutput, len(outputs))
for _, kv := range outputs {
name, spec := kv.key, kv.val
output := &WorkflowCallEventOutput{Name: name}
for _, attr := range p.parseMapping("output of workflow_call event", spec, true, true) {
switch attr.id {
case "description":
output.Description = p.parseString(attr.val, true)
case "value":
output.Value = p.parseString(attr.val, false)
default:
p.unexpectedKey(attr.key, "outputs at workflow_call event", []string{"description", "value"})
}
}
if output.Value == nil {
p.errorfAt(name.Pos, "\"value\" is missing at %q output of workflow_call event", name.Value)
}
ret.Outputs[kv.id] = output
}
default:
p.unexpectedKey(kv.key, "workflow_call", []string{"inputs", "secrets", "outputs"})
}
}
return ret
}
func (p *parser) parseEvents(pos *Pos, n *yaml.Node) []Event {
switch n.Kind {
case yaml.ScalarNode:
switch n.Value {
case "workflow_dispatch":
return []Event{
&WorkflowDispatchEvent{Pos: posAt(n)},
}
case "repository_dispatch":
return []Event{
&RepositoryDispatchEvent{Pos: posAt(n)},
}
case "schedule":
p.errorAt(pos, "schedule event must be configured with mapping")
return []Event{}
case "workflow_call":
return []Event{
&WorkflowCallEvent{Pos: posAt(n)},
}
default:
h := p.parseString(n, false)
if h.Value == "" {
return []Event{}
}
return []Event{
&WebhookEvent{
Hook: h,
Pos: posAt(n),
},
}
}
case yaml.MappingNode:
kvs := p.parseSectionMapping("on", n, false, true)
ret := make([]Event, 0, len(kvs))
for _, kv := range kvs {
pos := kv.key.Pos
switch kv.id {
case "schedule":
if e := p.parseScheduleEvent(pos, kv.val); e != nil {
ret = append(ret, e)
}
case "workflow_dispatch":
ret = append(ret, p.parseWorkflowDispatchEvent(pos, kv.val))
case "repository_dispatch":
ret = append(ret, p.parseRepositoryDispatchEvent(pos, kv.val))
case "workflow_call":
ret = append(ret, p.parseWorkflowCallEvent(pos, kv.val))
default:
ret = append(ret, p.parseWebhookEvent(kv.key, kv.val))
}
}
return ret
case yaml.SequenceNode:
l := len(n.Content)
p.checkNotEmpty("on", l, n)
ret := make([]Event, 0, l)
for _, c := range n.Content {
if s := p.parseString(c, false); s != nil {
switch s.Value {
case "schedule", "repository_dispatch":
p.errorf(c, "%q event should not be listed in sequence. Use mapping for \"on\" section and configure the event as values of the mapping", s.Value)
case "workflow_dispatch":
ret = append(ret, &WorkflowDispatchEvent{Pos: posAt(c)})
case "workflow_call":
ret = append(ret, &WorkflowCallEvent{Pos: posAt(c)})
default:
ret = append(ret, &WebhookEvent{Hook: s, Pos: posAt(c)})
}
}
}
return ret
default:
p.errorf(n, "\"on\" section value is expected to be mapping or sequence but found %s node", nodeKindName(n.Kind))
return nil
}
}
// https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#permissions
func (p *parser) parsePermissions(pos *Pos, n *yaml.Node) *Permissions {
ret := &Permissions{Pos: pos}
if n.Kind == yaml.ScalarNode {
ret.All = p.parseString(n, false)
} else {
m := p.parseSectionMapping("permissions", n, true, false) // XXX: Is the permission scope case insensitive?
scopes := make(map[string]*PermissionScope, len(m))
for _, kv := range m {
scopes[kv.id] = &PermissionScope{
Name: kv.key,
Value: p.parseString(kv.val, false),
}
}
ret.Scopes = scopes
}
return ret
}
// https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#env
func (p *parser) parseEnv(n *yaml.Node) *Env {
if n.Kind == yaml.ScalarNode {
return &Env{
Expression: p.parseExpression(n, "mapping value for \"env\" section"),
}
}
m := p.parseMapping("env", n, false, false)
vars := make(map[string]*EnvVar, len(m))
for _, kv := range m {
vars[kv.id] = &EnvVar{
Name: kv.key,
Value: p.parseString(kv.val, true),
}
}
return &Env{Vars: vars}
}
// https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#defaults
func (p *parser) parseDefaults(pos *Pos, n *yaml.Node) *Defaults {
ret := &Defaults{Pos: pos}
for _, kv := range p.parseSectionMapping("defaults", n, false, true) {
if kv.id != "run" {
p.unexpectedKey(kv.key, "defaults", []string{"run"})
continue
}
ret.Run = &DefaultsRun{Pos: kv.key.Pos}
for _, attr := range p.parseSectionMapping("run", kv.val, false, true) {
switch attr.id {
case "shell":
ret.Run.Shell = p.parseString(attr.val, false)
case "working-directory":
ret.Run.WorkingDirectory = p.parseString(attr.val, false)
default:
p.unexpectedKey(attr.key, "run", []string{"shell", "working-directory"})
}
}
}
if ret.Run == nil {
p.error(n, "\"defaults\" section should have \"run\" section")
}
return ret
}
// https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#jobsjob_idconcurrency
func (p *parser) parseConcurrency(pos *Pos, n *yaml.Node) *Concurrency {
ret := &Concurrency{Pos: pos}
if n.Kind == yaml.ScalarNode {
ret.Group = p.parseString(n, false)
} else {
groupFound := false
for _, kv := range p.parseSectionMapping("concurrency", n, false, true) {
switch kv.id {
case "group":
ret.Group = p.parseString(kv.val, false)
groupFound = true
case "cancel-in-progress":
ret.CancelInProgress = p.parseBool(kv.val)
default:
p.unexpectedKey(kv.key, "concurrency", []string{"group", "cancel-in-progress"})
}
}
if !groupFound {
p.errorAt(pos, "group name is missing in \"concurrency\" section")
}
}
return ret
}
// https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#jobsjob_idenvironment
func (p *parser) parseEnvironment(pos *Pos, n *yaml.Node) *Environment {
ret := &Environment{Pos: pos}
if n.Kind == yaml.ScalarNode {
ret.Name = p.parseString(n, false)
} else {
nameFound := false
for _, kv := range p.parseSectionMapping("environment", n, false, true) {
switch kv.id {
case "name":
ret.Name = p.parseString(kv.val, false)
nameFound = true
case "url":
ret.URL = p.parseString(kv.val, false)
default:
p.unexpectedKey(kv.key, "environment", []string{"name", "url"})
}
}
if !nameFound {
p.errorAt(pos, "name is missing in \"environment\" section")
}
}
return ret
}
// https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#jobsjob_idoutputs
func (p *parser) parseOutputs(n *yaml.Node) map[string]*Output {
outputs := p.parseSectionMapping("outputs", n, false, false)
ret := make(map[string]*Output, len(outputs))
for _, output := range outputs {
ret[output.id] = &Output{
Name: output.key,
Value: p.parseString(output.val, true),
}
}
p.checkNotEmpty("outputs", len(ret), n)
return ret
}
func (p *parser) parseRawYAMLValue(n *yaml.Node) RawYAMLValue {
switch n.Kind {
case yaml.ScalarNode:
return &RawYAMLString{n.Value, posAt(n)}
case yaml.SequenceNode:
vs := make([]RawYAMLValue, 0, len(n.Content))
for _, c := range n.Content {
if v := p.parseRawYAMLValue(c); v != nil {
vs = append(vs, v)
}
}
return &RawYAMLArray{vs, posAt(n)}
case yaml.MappingNode:
parsed := p.parseMapping("matrix row value", n, true, false)
m := make(map[string]RawYAMLValue, len(parsed))
for _, kv := range parsed {
if v := p.parseRawYAMLValue(kv.val); v != nil {
m[kv.id] = v
}
}
return &RawYAMLObject{m, posAt(n)}
default:
p.errorf(n, "unexpected %s node on parsing value in matrix row", nodeKindName(n.Kind))
return nil
}
}
// https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#example-including-additional-values-into-combinations
// https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#example-excluding-configurations-from-a-matrix
func (p *parser) parseMatrixCombinations(sec string, n *yaml.Node) *MatrixCombinations {
if n.Kind == yaml.ScalarNode {
return &MatrixCombinations{
Expression: p.parseExpression(n, "array of matrix combination"),
}
}
if ok := p.checkSequence(sec, n, false); !ok {
return nil
}
ret := make([]*MatrixCombination, 0, len(n.Content))
for _, c := range n.Content {
if c.Kind == yaml.ScalarNode {
if e := p.parseExpression(c, "mapping of matrix combination"); e != nil {
ret = append(ret, &MatrixCombination{Expression: e})
}
continue
}
kvs := p.parseMapping(fmt.Sprintf("element in %q section", sec), c, false, false)
assigns := make(map[string]*MatrixAssign, len(kvs))
for _, kv := range kvs {
if v := p.parseRawYAMLValue(kv.val); v != nil {
assigns[kv.id] = &MatrixAssign{kv.key, v}
}
}
ret = append(ret, &MatrixCombination{Assigns: assigns})
}
return &MatrixCombinations{Combinations: ret}
}
// https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#jobsjob_idstrategymatrix
func (p *parser) parseMatrix(pos *Pos, n *yaml.Node) *Matrix {
if n.Kind == yaml.ScalarNode {
return &Matrix{
Expression: p.parseExpression(n, "matrix"),
Pos: posAt(n),
}
}
ret := &Matrix{Pos: pos, Rows: make(map[string]*MatrixRow)}
for _, kv := range p.parseSectionMapping("matrix", n, false, false) {
switch kv.id {
case "include":
ret.Include = p.parseMatrixCombinations("include", kv.val)
case "exclude":
ret.Exclude = p.parseMatrixCombinations("exclude", kv.val)
default:
if kv.val.Kind == yaml.ScalarNode {
ret.Rows[kv.id] = &MatrixRow{
Expression: p.parseExpression(kv.val, "array value for matrix variations"),
}
continue
}
if ok := p.checkSequence("matrix values", kv.val, false); !ok {
continue
}
values := make([]RawYAMLValue, 0, len(kv.val.Content))
for _, c := range kv.val.Content {
if v := p.parseRawYAMLValue(c); v != nil {
values = append(values, v)
}
}
ret.Rows[kv.id] = &MatrixRow{
Name: kv.key,
Values: values,
}
}
}
return ret
}
// https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstrategymax-parallel
func (p *parser) parseMaxParallel(n *yaml.Node) *Int {
i := p.parseInt(n)
if i != nil && i.Expression == nil && i.Value <= 0 {
p.errorf(n, "value at \"max-parallel\" must be greater than zero: %v", i.Value)
}
return i
}
// https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#jobsjob_idstrategy
func (p *parser) parseStrategy(pos *Pos, n *yaml.Node) *Strategy {
ret := &Strategy{Pos: pos}
for _, kv := range p.parseSectionMapping("strategy", n, false, true) {
switch kv.id {
case "matrix":
ret.Matrix = p.parseMatrix(kv.key.Pos, kv.val)
case "fail-fast":
ret.FailFast = p.parseBool(kv.val)
case "max-parallel":
ret.MaxParallel = p.parseMaxParallel(kv.val)
default:
p.unexpectedKey(kv.key, "strategy", []string{"matrix", "fail-fast", "max-parallel"})
}
}
return ret
}
// https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#jobsjob_idcontainer
func (p *parser) parseContainer(sec string, pos *Pos, n *yaml.Node) *Container {
ret := &Container{Pos: pos}
if n.Kind == yaml.ScalarNode {
// When you only specify a container image, you can omit the image keyword.
ret.Image = p.parseString(n, false)
} else {
for _, kv := range p.parseSectionMapping(sec, n, false, true) {
switch kv.id {
case "image":
ret.Image = p.parseString(kv.val, false)
case "credentials":
cred := &Credentials{Pos: kv.key.Pos}
for _, c := range p.parseSectionMapping("credentials", kv.val, false, true) {
switch c.id {
case "username":
cred.Username = p.parseString(c.val, false)
case "password":
cred.Password = p.parseString(c.val, false)
default:
p.unexpectedKey(c.key, "credentials", []string{"username", "password"})
}
}
if cred.Username == nil || cred.Password == nil {
p.errorAt(kv.key.Pos, "both \"username\" and \"password\" must be specified in \"credentials\" section")
continue
}
ret.Credentials = cred
case "env":
ret.Env = p.parseEnv(kv.val)
case "ports":
ret.Ports = p.parseStringSequence("ports", kv.val, true, false)
case "volumes":
ret.Ports = p.parseStringSequence("volumes", kv.val, true, false)
case "options":
ret.Options = p.parseString(kv.val, true)
default:
p.unexpectedKey(kv.key, sec, []string{
"image",
"credentials",
"env",
"ports",
"volumes",
"options",
})
}
}
}
return ret
}
func (p *parser) parseServices(n *yaml.Node) *Services {
ret := &Services{Pos: posAt(n)}
if e := p.mayParseExpression(n); e != nil {
ret.Expression = e
} else {
services := p.parseSectionMapping("services", n, false, false) // XXX: Is the key case-insensitive?
ss := make(map[string]*Service, len(services))
for _, s := range services {
ss[s.id] = &Service{
Name: s.key,
Container: p.parseContainer("services", s.key.Pos, s.val),
}
}
ret.Value = ss
}
return ret
}
// https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idtimeout-minutes
func (p *parser) parseTimeoutMinutes(n *yaml.Node) *Float {
f := p.parseFloat(n)
if f != nil && f.Expression == nil && f.Value <= 0.0 {
p.errorf(n, "value at \"timeout-minutes\" must be greater than zero: %v", f.Value)
}
return f
}
// https://docs.github.com/en/actions/learn-github-actions/workflow-syntax-for-github-actions#jobsjob_idsteps