Skip to content

Commit c30bc82

Browse files
ChristopherHXcpleemergify[bot]
authored
fix: processing of strategy.matrix.include (#1200)
* Update workflow.go * Update workflow.go * Update workflow.go * Update workflow.go * Update workflow.go * Update workflow.go * Add Tests * Update workflow.go * Modify Test * use tabs Co-authored-by: Casey Lee <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent c3fb686 commit c30bc82

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

pkg/model/workflow.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,26 +224,37 @@ func (j *Job) GetMatrixes() []map[string]interface{} {
224224

225225
if m := j.Matrix(); m != nil {
226226
includes := make([]map[string]interface{}, 0)
227+
extraIncludes := make([]map[string]interface{}, 0)
227228
for _, v := range m["include"] {
228229
switch t := v.(type) {
229230
case []interface{}:
230231
for _, i := range t {
231232
i := i.(map[string]interface{})
233+
extraInclude := true
232234
for k := range i {
233235
if _, ok := m[k]; ok {
234236
includes = append(includes, i)
237+
extraInclude = false
235238
break
236239
}
237240
}
241+
if extraInclude {
242+
extraIncludes = append(extraIncludes, i)
243+
}
238244
}
239245
case interface{}:
240246
v := v.(map[string]interface{})
247+
extraInclude := true
241248
for k := range v {
242249
if _, ok := m[k]; ok {
243250
includes = append(includes, v)
251+
extraInclude = false
244252
break
245253
}
246254
}
255+
if extraInclude {
256+
extraIncludes = append(extraIncludes, v)
257+
}
247258
}
248259
}
249260
delete(m, "include")
@@ -274,9 +285,27 @@ func (j *Job) GetMatrixes() []map[string]interface{} {
274285
matrixes = append(matrixes, matrix)
275286
}
276287
for _, include := range includes {
288+
matched := false
289+
for _, matrix := range matrixes {
290+
if commonKeysMatch2(matrix, include, m) {
291+
matched = true
292+
log.Debugf("Adding include values '%v' to existing entry", include)
293+
for k, v := range include {
294+
matrix[k] = v
295+
}
296+
}
297+
}
298+
if !matched {
299+
extraIncludes = append(extraIncludes, include)
300+
}
301+
}
302+
for _, include := range extraIncludes {
277303
log.Debugf("Adding include '%v'", include)
278304
matrixes = append(matrixes, include)
279305
}
306+
if len(matrixes) == 0 {
307+
matrixes = append(matrixes, make(map[string]interface{}))
308+
}
280309
} else {
281310
matrixes = append(matrixes, make(map[string]interface{}))
282311
}
@@ -295,6 +324,16 @@ func commonKeysMatch(a map[string]interface{}, b map[string]interface{}) bool {
295324
return true
296325
}
297326

327+
func commonKeysMatch2(a map[string]interface{}, b map[string]interface{}, m map[string][]interface{}) bool {
328+
for aKey, aVal := range a {
329+
_, useKey := m[aKey]
330+
if bVal, ok := b[aKey]; useKey && ok && !reflect.DeepEqual(aVal, bVal) {
331+
return false
332+
}
333+
}
334+
return true
335+
}
336+
298337
// ContainerSpec is the specification of the container to use for the job
299338
type ContainerSpec struct {
300339
Image string `yaml:"image"`

pkg/model/workflow_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ func TestReadWorkflow_Strategy(t *testing.T) {
247247
{"datacenter": "site-c", "node-version": "14.x", "site": "staging"},
248248
{"datacenter": "site-c", "node-version": "16.x", "site": "staging"},
249249
{"datacenter": "site-d", "node-version": "16.x", "site": "staging"},
250+
{"php-version": 5.4},
250251
{"datacenter": "site-a", "node-version": "10.x", "site": "prod"},
251252
{"datacenter": "site-b", "node-version": "12.x", "site": "dev"},
252253
},

pkg/runner/testdata/evalmatrix/push.yml

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,65 @@ jobs:
1515
echo $MATRIX
1616
exit ${{matrix.A && '0' || '1'}}
1717
env:
18-
MATRIX: ${{toJSON(matrix)}}
18+
MATRIX: ${{toJSON(matrix)}}
19+
_additionalInclude_0:
20+
strategy:
21+
matrix:
22+
include:
23+
- def: val
24+
runs-on: ubuntu-latest
25+
steps:
26+
- name: Check if the matrix key A exists
27+
run: |
28+
echo $MATRIX
29+
exit ${{matrix.def == 'val' && '0' || '1'}}
30+
env:
31+
MATRIX: ${{toJSON(matrix)}}
32+
- run: |
33+
echo "::set-output name=result::success"
34+
id: result
35+
outputs:
36+
result: ${{ steps.result.outputs.result }}
37+
_additionalInclude_1:
38+
needs: _additionalInclude_0
39+
if: always()
40+
runs-on: ubuntu-latest
41+
steps:
42+
- name: Check if the matrix key A exists
43+
run: |
44+
echo $MATRIX
45+
exit ${{needs._additionalInclude_0.outputs.result == 'success' && '0' || '1'}}
46+
_additionalProperties_0:
47+
strategy:
48+
matrix:
49+
x:
50+
- 0
51+
y:
52+
- 0
53+
z:
54+
- 0
55+
include:
56+
- def: val
57+
z: 0
58+
runs-on: ubuntu-latest
59+
steps:
60+
- name: Check if the matrix key A exists
61+
run: |
62+
echo $MATRIX
63+
exit ${{matrix.def == 'val' && matrix.x == 0 && matrix.y == 0 && matrix.z == 0 && '0' || '1'}}
64+
env:
65+
MATRIX: ${{toJSON(matrix)}}
66+
- run: |
67+
echo "::set-output name=result::success"
68+
id: result
69+
outputs:
70+
result: ${{ steps.result.outputs.result }}
71+
_additionalProperties_1:
72+
needs: _additionalProperties_0
73+
if: always()
74+
runs-on: ubuntu-latest
75+
steps:
76+
- name: Check if the matrix key A exists
77+
run: |
78+
echo $MATRIX
79+
exit ${{needs._additionalProperties_0.outputs.result == 'success' && '0' || '1'}}

0 commit comments

Comments
 (0)