forked from rhysd/actionlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule_matrix.go
198 lines (180 loc) · 4.52 KB
/
rule_matrix.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
package actionlint
import "strings"
// RuleMatrix is a rule checker to check 'matrix' field of job.
type RuleMatrix struct {
RuleBase
}
// NewRuleMatrix creates new RuleMatrix instance.
func NewRuleMatrix() *RuleMatrix {
return &RuleMatrix{
RuleBase: RuleBase{
name: "matrix",
desc: "Checks for matrix combinations in \"matrix:\"",
},
}
}
// VisitJobPre is callback when visiting Job node before visiting its children.
func (rule *RuleMatrix) VisitJobPre(n *Job) error {
if n.Strategy == nil || n.Strategy.Matrix == nil || n.Strategy.Matrix.Expression != nil {
return nil
}
m := n.Strategy.Matrix
for _, row := range m.Rows {
rule.checkDuplicateInRow(row)
}
// Note:
// Any new value can be set in the section as new combination. It can add new value to existing
// column also.
//
// matrix:
// os: [ubuntu-latest, macos-latest]
// include:
// - os: windows-latest
// sh: pwsh
rule.checkExclude(m)
return nil
}
func (rule *RuleMatrix) checkDuplicateInRow(row *MatrixRow) {
if row.Values == nil {
return // Give up when ${{ }} is specified
}
seen := make([]RawYAMLValue, 0, len(row.Values))
for _, v := range row.Values {
ok := true
for _, p := range seen {
if p.Equals(v) {
rule.Errorf(
v.Pos(),
"duplicate value %s is found in matrix %q. the same value is at %s",
v.String(),
row.Name.Value,
p.Pos().String(),
)
ok = false
break
}
}
if ok {
seen = append(seen, v)
}
}
}
func filterMatchesYAMLValue(v, filter RawYAMLValue) bool {
switch v := v.(type) {
case *RawYAMLObject:
// `exclude` and `include` filter can match to objects in matrix as subset of them (#249).
// For example,
//
// matrix:
// os:
// - { name: Ubuntu, matrix: ubuntu }
// - { name: Windows, matrix: windows }
// arch:
// - { name: ARM, matrix: arm }
// - { name: Intel, matrix: intel }
// exclude:
// - os: { matrix: windows }
// arch: { matrix: arm }
//
// The `exclude` filters out `{ os: { name: Windows, matrix: windows }, arch: {name: ARM, matrix: arm } }`
switch filter := filter.(type) {
case *RawYAMLObject:
for n, f := range filter.Props {
if p, ok := v.Props[n]; !ok || !filterMatchesYAMLValue(p, f) {
return false
}
}
return true
default:
return false
}
default:
return v.Equals(filter)
}
}
func filterMatchesMatrixRow(row []RawYAMLValue, filter RawYAMLValue) bool {
for _, v := range row {
if filterMatchesYAMLValue(v, filter) {
return true
}
}
return false
}
func (rule *RuleMatrix) checkExclude(m *Matrix) {
if m.Exclude == nil || len(m.Exclude.Combinations) == 0 || (m.Include != nil && m.Include.ContainsExpression()) {
return
}
if len(m.Rows) == 0 && (m.Include == nil || len(m.Include.Combinations) == 0) {
rule.Error(m.Pos, "\"exclude\" section exists but no matrix variation exists")
return
}
vals := make(map[string][]RawYAMLValue, len(m.Rows))
ignored := map[string]struct{}{}
Outer:
for name, row := range m.Rows {
if row.Expression != nil {
ignored[name] = struct{}{}
continue
}
// When some item is constructed with ${{ }} dynamically, give up checking combination values (#261)
for _, y := range row.Values {
if s, ok := y.(*RawYAMLString); ok && isExprAssigned(s.Value) {
ignored[name] = struct{}{}
continue Outer
}
}
vals[name] = row.Values
}
if m.Include != nil {
for _, combi := range m.Include.Combinations {
for n, a := range combi.Assigns {
if _, ok := ignored[n]; ok {
continue
}
vs, ok := vals[n]
if !ok {
vals[n] = []RawYAMLValue{a.Value}
continue
}
if !filterMatchesMatrixRow(vs, a.Value) {
vals[n] = append(vs, a.Value)
}
}
}
}
for _, combi := range m.Exclude.Combinations {
for k, a := range combi.Assigns {
if _, ok := ignored[k]; ok {
continue
}
vs, ok := vals[k]
if !ok {
ss := make([]string, 0, len(vals))
for k := range vals {
ss = append(ss, k)
}
rule.Errorf(
a.Key.Pos,
"%q in \"exclude\" section does not exist in matrix. available matrix configurations are %s",
k,
sortedQuotes(ss),
)
continue
}
if filterMatchesMatrixRow(vs, a.Value) {
continue
}
ss := make([]string, 0, len(vs))
for _, v := range vs {
ss = append(ss, v.String())
}
rule.Errorf(
a.Value.Pos(),
"value %s in \"exclude\" does not match in matrix %q combinations. possible values are %s",
a.Value.String(),
k,
strings.Join(ss, ", "), // Note: do not use quotesBuilder
)
}
}
}