2022-09-21 01:26:19 -05:00
|
|
|
package jobparser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
|
|
|
"github.com/nektos/act/pkg/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Parse(content []byte, options ...ParseOption) ([]*SingleWorkflow, error) {
|
|
|
|
origin, err := model.ReadWorkflow(bytes.NewReader(content))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("model.ReadWorkflow: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
workflow := &SingleWorkflow{}
|
|
|
|
if err := yaml.Unmarshal(content, workflow); err != nil {
|
|
|
|
return nil, fmt.Errorf("yaml.Unmarshal: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pc := &parseContext{}
|
|
|
|
for _, o := range options {
|
|
|
|
o(pc)
|
|
|
|
}
|
|
|
|
results := map[string]*JobResult{}
|
|
|
|
for id, job := range origin.Jobs {
|
|
|
|
results[id] = &JobResult{
|
|
|
|
Needs: job.Needs(),
|
|
|
|
Result: pc.jobResults[id],
|
|
|
|
Outputs: nil, // not supported yet
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var ret []*SingleWorkflow
|
2023-03-27 22:38:40 -05:00
|
|
|
ids, jobs, err := workflow.jobs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid jobs: %w", err)
|
|
|
|
}
|
|
|
|
for i, id := range ids {
|
|
|
|
job := jobs[i]
|
2023-04-07 03:31:03 -05:00
|
|
|
matricxes, err := getMatrixes(origin.GetJob(id))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("getMatrixes: %w", err)
|
|
|
|
}
|
|
|
|
for _, matrix := range matricxes {
|
2022-09-21 01:26:19 -05:00
|
|
|
job := job.Clone()
|
2024-07-01 13:23:17 -05:00
|
|
|
evaluator := NewExpressionEvaluator(NewInterpeter(id, origin.GetJob(id), matrix, pc.gitContext, results, pc.vars))
|
2022-09-21 01:26:19 -05:00
|
|
|
if job.Name == "" {
|
2024-07-01 13:23:17 -05:00
|
|
|
job.Name = nameWithMatrix(id, matrix)
|
|
|
|
} else {
|
|
|
|
job.Name = evaluator.Interpolate(job.Name)
|
2022-09-21 01:26:19 -05:00
|
|
|
}
|
2024-07-01 13:23:17 -05:00
|
|
|
|
2022-09-21 01:26:19 -05:00
|
|
|
job.Strategy.RawMatrix = encodeMatrix(matrix)
|
2024-07-01 13:23:17 -05:00
|
|
|
|
2022-09-21 01:26:19 -05:00
|
|
|
runsOn := origin.GetJob(id).RunsOn()
|
|
|
|
for i, v := range runsOn {
|
|
|
|
runsOn[i] = evaluator.Interpolate(v)
|
|
|
|
}
|
|
|
|
job.RawRunsOn = encodeRunsOn(runsOn)
|
2023-03-27 22:38:40 -05:00
|
|
|
swf := &SingleWorkflow{
|
2022-09-21 01:26:19 -05:00
|
|
|
Name: workflow.Name,
|
|
|
|
RawOn: workflow.RawOn,
|
|
|
|
Env: workflow.Env,
|
|
|
|
Defaults: workflow.Defaults,
|
2023-03-27 22:38:40 -05:00
|
|
|
}
|
2023-03-29 00:57:29 -05:00
|
|
|
if err := swf.SetJob(id, job); err != nil {
|
|
|
|
return nil, fmt.Errorf("SetJob: %w", err)
|
2023-03-27 22:38:40 -05:00
|
|
|
}
|
|
|
|
ret = append(ret, swf)
|
2022-09-21 01:26:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithJobResults(results map[string]string) ParseOption {
|
|
|
|
return func(c *parseContext) {
|
|
|
|
c.jobResults = results
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithGitContext(context *model.GithubContext) ParseOption {
|
|
|
|
return func(c *parseContext) {
|
|
|
|
c.gitContext = context
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-29 22:29:58 -06:00
|
|
|
func WithVars(vars map[string]string) ParseOption {
|
|
|
|
return func(c *parseContext) {
|
|
|
|
c.vars = vars
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 01:26:19 -05:00
|
|
|
type parseContext struct {
|
|
|
|
jobResults map[string]string
|
|
|
|
gitContext *model.GithubContext
|
2024-02-29 22:29:58 -06:00
|
|
|
vars map[string]string
|
2022-09-21 01:26:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type ParseOption func(c *parseContext)
|
|
|
|
|
2023-04-07 03:31:03 -05:00
|
|
|
func getMatrixes(job *model.Job) ([]map[string]interface{}, error) {
|
|
|
|
ret, err := job.GetMatrixes()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("GetMatrixes: %w", err)
|
|
|
|
}
|
2022-09-21 01:26:19 -05:00
|
|
|
sort.Slice(ret, func(i, j int) bool {
|
|
|
|
return matrixName(ret[i]) < matrixName(ret[j])
|
|
|
|
})
|
2023-04-07 03:31:03 -05:00
|
|
|
return ret, nil
|
2022-09-21 01:26:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func encodeMatrix(matrix map[string]interface{}) yaml.Node {
|
|
|
|
if len(matrix) == 0 {
|
|
|
|
return yaml.Node{}
|
|
|
|
}
|
|
|
|
value := map[string][]interface{}{}
|
|
|
|
for k, v := range matrix {
|
|
|
|
value[k] = []interface{}{v}
|
|
|
|
}
|
|
|
|
node := yaml.Node{}
|
|
|
|
_ = node.Encode(value)
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeRunsOn(runsOn []string) yaml.Node {
|
|
|
|
node := yaml.Node{}
|
|
|
|
if len(runsOn) == 1 {
|
|
|
|
_ = node.Encode(runsOn[0])
|
|
|
|
} else {
|
|
|
|
_ = node.Encode(runsOn)
|
|
|
|
}
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
|
|
|
func nameWithMatrix(name string, m map[string]interface{}) string {
|
|
|
|
if len(m) == 0 {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
return name + " " + matrixName(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func matrixName(m map[string]interface{}) string {
|
|
|
|
ks := make([]string, 0, len(m))
|
|
|
|
for k := range m {
|
|
|
|
ks = append(ks, k)
|
|
|
|
}
|
|
|
|
sort.Strings(ks)
|
|
|
|
vs := make([]string, 0, len(m))
|
|
|
|
for _, v := range ks {
|
|
|
|
vs = append(vs, fmt.Sprint(m[v]))
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("(%s)", strings.Join(vs, ", "))
|
|
|
|
}
|