Skip to content

Commit 5f0af1a

Browse files
committed
add polymorphic parsing and convert the objects to a json string if present
1 parent 6906909 commit 5f0af1a

File tree

2 files changed

+69
-5
lines changed

2 files changed

+69
-5
lines changed

models/github_actions.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package models
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"gopkg.in/yaml.v3"
67
"strings"
@@ -559,13 +560,42 @@ func (o *GithubActionsStrategy) UnmarshalYAML(node *yaml.Node) error {
559560
for i := 0; i < len(node.Content); i += 2 {
560561
key := node.Content[i].Value
561562
value := node.Content[i+1]
562-
if key == "matrix" {
563-
var m map[string]StringList
564-
if err := value.Decode(&m); err != nil {
565-
return err
563+
if key != "matrix" {
564+
continue
565+
}
566+
if value.Kind != yaml.MappingNode {
567+
return fmt.Errorf("matrix must be a mapping")
568+
}
569+
m := make(map[string]StringList, len(value.Content)/2)
570+
// walk each matrix dimension
571+
for j := 0; j < len(value.Content); j += 2 {
572+
dim := value.Content[j].Value
573+
listNode := value.Content[j+1]
574+
if listNode.Kind != yaml.SequenceNode {
575+
return fmt.Errorf("matrix.%s must be a sequence", dim)
576+
}
577+
var items StringList
578+
for _, item := range listNode.Content {
579+
switch item.Kind {
580+
case yaml.ScalarNode:
581+
items = append(items, item.Value)
582+
case yaml.MappingNode:
583+
var obj map[string]interface{}
584+
if err := item.Decode(&obj); err != nil {
585+
return err
586+
}
587+
b, err := json.Marshal(obj)
588+
if err != nil {
589+
return fmt.Errorf("failed to marshal matrix item: %w", err)
590+
}
591+
items = append(items, string(b))
592+
default:
593+
return fmt.Errorf("unsupported node kind %v in matrix.%s", item.Kind, dim)
594+
}
566595
}
567-
o.Matrix = m
596+
m[dim] = items
568597
}
598+
o.Matrix = m
569599
}
570600
return nil
571601
}

models/github_actions_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,40 @@ func TestGithubActionsWorkflowJobs(t *testing.T) {
200200
},
201201
},
202202
},
203+
{
204+
Name: "multi dimension matrix",
205+
Input: `example_matrix: { strategy: { matrix: { os: [ubuntu-22.04, ubuntu-20.04], version: [10, 12, 14] } }, runs-on: '${{ matrix.os }}' }`,
206+
Expected: GithubActionsJob{
207+
ID: "example_matrix",
208+
RunsOn: []string{"${{ matrix.os }}"},
209+
Strategy: GithubActionsStrategy{
210+
Matrix: map[string]StringList{
211+
"os": {"ubuntu-22.04", "ubuntu-20.04"},
212+
"version": {"10", "12", "14"},
213+
},
214+
},
215+
Lines: map[string]int{"runs_on": 1, "start": 1},
216+
},
217+
},
218+
{
219+
Name: "multi dimension matrix list of objects",
220+
Input: `example_matrix: { strategy: { matrix: { os: [ubuntu-latest, macos-latest], node: [ { version: 14 }, { version: 20, env: NODE_OPTIONS=--openssl-legacy-provider } ] } } }`,
221+
Expected: GithubActionsJob{
222+
ID: "example_matrix",
223+
Strategy: GithubActionsStrategy{
224+
Matrix: map[string]StringList{
225+
"os": {
226+
"ubuntu-latest",
227+
"macos-latest",
228+
},
229+
"node": {
230+
`{"version":14}`,
231+
`{"env":"NODE_OPTIONS=--openssl-legacy-provider","version":20}`,
232+
},
233+
},
234+
},
235+
},
236+
},
203237
}
204238

205239
for _, tt := range tests {

0 commit comments

Comments
 (0)