2022-09-21 14:26:19 +08:00
|
|
|
package jobparser
|
|
|
|
|
|
|
|
import (
|
2023-01-31 15:49:55 +08:00
|
|
|
"fmt"
|
|
|
|
|
2022-09-21 14:26:19 +08:00
|
|
|
"github.com/nektos/act/pkg/model"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SingleWorkflow is a workflow with single job and single matrix
|
|
|
|
type SingleWorkflow struct {
|
|
|
|
Name string `yaml:"name,omitempty"`
|
|
|
|
RawOn yaml.Node `yaml:"on,omitempty"`
|
|
|
|
Env map[string]string `yaml:"env,omitempty"`
|
2023-03-28 11:38:40 +08:00
|
|
|
RawJobs yaml.Node `yaml:"jobs,omitempty"`
|
2022-09-21 14:26:19 +08:00
|
|
|
Defaults Defaults `yaml:"defaults,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *SingleWorkflow) Job() (string, *Job) {
|
2023-03-28 11:38:40 +08:00
|
|
|
ids, jobs, _ := w.jobs()
|
|
|
|
if len(ids) >= 1 {
|
|
|
|
return ids[0], jobs[0]
|
2022-09-21 14:26:19 +08:00
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2023-03-28 11:38:40 +08:00
|
|
|
func (w *SingleWorkflow) jobs() ([]string, []*Job, error) {
|
2023-04-24 23:16:41 +08:00
|
|
|
ids, jobs, err := parseMappingNode[*Job](&w.RawJobs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, job := range jobs {
|
|
|
|
steps := make([]*Step, 0, len(job.Steps))
|
|
|
|
for _, s := range job.Steps {
|
|
|
|
if s != nil {
|
|
|
|
steps = append(steps, s)
|
2023-04-21 20:21:15 +08:00
|
|
|
}
|
2023-03-28 11:38:40 +08:00
|
|
|
}
|
2023-04-24 23:16:41 +08:00
|
|
|
job.Steps = steps
|
2023-03-28 11:38:40 +08:00
|
|
|
}
|
2023-04-24 23:16:41 +08:00
|
|
|
|
2023-03-28 11:38:40 +08:00
|
|
|
return ids, jobs, nil
|
|
|
|
}
|
|
|
|
|
2023-03-29 13:57:29 +08:00
|
|
|
func (w *SingleWorkflow) SetJob(id string, job *Job) error {
|
2023-03-28 11:38:40 +08:00
|
|
|
m := map[string]*Job{
|
|
|
|
id: job,
|
|
|
|
}
|
|
|
|
out, err := yaml.Marshal(m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
node := yaml.Node{}
|
|
|
|
if err := yaml.Unmarshal(out, &node); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(node.Content) != 1 || node.Content[0].Kind != yaml.MappingNode {
|
|
|
|
return fmt.Errorf("can not set job: %q", out)
|
|
|
|
}
|
|
|
|
w.RawJobs = *node.Content[0]
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-21 14:26:19 +08:00
|
|
|
func (w *SingleWorkflow) Marshal() ([]byte, error) {
|
|
|
|
return yaml.Marshal(w)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Job struct {
|
|
|
|
Name string `yaml:"name,omitempty"`
|
|
|
|
RawNeeds yaml.Node `yaml:"needs,omitempty"`
|
|
|
|
RawRunsOn yaml.Node `yaml:"runs-on,omitempty"`
|
|
|
|
Env yaml.Node `yaml:"env,omitempty"`
|
|
|
|
If yaml.Node `yaml:"if,omitempty"`
|
|
|
|
Steps []*Step `yaml:"steps,omitempty"`
|
|
|
|
TimeoutMinutes string `yaml:"timeout-minutes,omitempty"`
|
|
|
|
Services map[string]*ContainerSpec `yaml:"services,omitempty"`
|
|
|
|
Strategy Strategy `yaml:"strategy,omitempty"`
|
|
|
|
RawContainer yaml.Node `yaml:"container,omitempty"`
|
|
|
|
Defaults Defaults `yaml:"defaults,omitempty"`
|
|
|
|
Outputs map[string]string `yaml:"outputs,omitempty"`
|
|
|
|
Uses string `yaml:"uses,omitempty"`
|
2023-04-04 10:59:53 +08:00
|
|
|
With map[string]interface{} `yaml:"with,omitempty"`
|
2023-04-17 13:41:02 +08:00
|
|
|
RawSecrets yaml.Node `yaml:"secrets,omitempty"`
|
2022-09-21 14:26:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (j *Job) Clone() *Job {
|
|
|
|
if j == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &Job{
|
|
|
|
Name: j.Name,
|
|
|
|
RawNeeds: j.RawNeeds,
|
|
|
|
RawRunsOn: j.RawRunsOn,
|
|
|
|
Env: j.Env,
|
|
|
|
If: j.If,
|
|
|
|
Steps: j.Steps,
|
|
|
|
TimeoutMinutes: j.TimeoutMinutes,
|
|
|
|
Services: j.Services,
|
|
|
|
Strategy: j.Strategy,
|
|
|
|
RawContainer: j.RawContainer,
|
|
|
|
Defaults: j.Defaults,
|
|
|
|
Outputs: j.Outputs,
|
|
|
|
Uses: j.Uses,
|
2023-04-04 10:59:53 +08:00
|
|
|
With: j.With,
|
2023-04-17 13:41:02 +08:00
|
|
|
RawSecrets: j.RawSecrets,
|
2022-09-21 14:26:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *Job) Needs() []string {
|
|
|
|
return (&model.Job{RawNeeds: j.RawNeeds}).Needs()
|
|
|
|
}
|
|
|
|
|
2023-03-29 13:57:29 +08:00
|
|
|
func (j *Job) EraseNeeds() *Job {
|
2022-09-21 14:26:19 +08:00
|
|
|
j.RawNeeds = yaml.Node{}
|
2023-03-29 13:57:29 +08:00
|
|
|
return j
|
2022-09-21 14:26:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (j *Job) RunsOn() []string {
|
|
|
|
return (&model.Job{RawRunsOn: j.RawRunsOn}).RunsOn()
|
|
|
|
}
|
|
|
|
|
|
|
|
type Step struct {
|
|
|
|
ID string `yaml:"id,omitempty"`
|
|
|
|
If yaml.Node `yaml:"if,omitempty"`
|
|
|
|
Name string `yaml:"name,omitempty"`
|
|
|
|
Uses string `yaml:"uses,omitempty"`
|
|
|
|
Run string `yaml:"run,omitempty"`
|
|
|
|
WorkingDirectory string `yaml:"working-directory,omitempty"`
|
|
|
|
Shell string `yaml:"shell,omitempty"`
|
|
|
|
Env yaml.Node `yaml:"env,omitempty"`
|
|
|
|
With map[string]string `yaml:"with,omitempty"`
|
|
|
|
ContinueOnError bool `yaml:"continue-on-error,omitempty"`
|
|
|
|
TimeoutMinutes string `yaml:"timeout-minutes,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// String gets the name of step
|
|
|
|
func (s *Step) String() string {
|
2023-04-21 14:45:38 +08:00
|
|
|
if s == nil {
|
|
|
|
return ""
|
|
|
|
}
|
2022-09-21 14:26:19 +08:00
|
|
|
return (&model.Step{
|
|
|
|
ID: s.ID,
|
|
|
|
Name: s.Name,
|
|
|
|
Uses: s.Uses,
|
|
|
|
Run: s.Run,
|
|
|
|
}).String()
|
|
|
|
}
|
|
|
|
|
|
|
|
type ContainerSpec struct {
|
|
|
|
Image string `yaml:"image,omitempty"`
|
|
|
|
Env map[string]string `yaml:"env,omitempty"`
|
|
|
|
Ports []string `yaml:"ports,omitempty"`
|
|
|
|
Volumes []string `yaml:"volumes,omitempty"`
|
|
|
|
Options string `yaml:"options,omitempty"`
|
|
|
|
Credentials map[string]string `yaml:"credentials,omitempty"`
|
2023-04-23 14:55:17 +08:00
|
|
|
Cmd []string `yaml:"cmd,omitempty"`
|
2022-09-21 14:26:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type Strategy struct {
|
|
|
|
FailFastString string `yaml:"fail-fast,omitempty"`
|
|
|
|
MaxParallelString string `yaml:"max-parallel,omitempty"`
|
|
|
|
RawMatrix yaml.Node `yaml:"matrix,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Defaults struct {
|
|
|
|
Run RunDefaults `yaml:"run,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type RunDefaults struct {
|
|
|
|
Shell string `yaml:"shell,omitempty"`
|
|
|
|
WorkingDirectory string `yaml:"working-directory,omitempty"`
|
|
|
|
}
|
2023-01-31 15:49:55 +08:00
|
|
|
|
|
|
|
type Event struct {
|
2023-03-24 20:15:46 +08:00
|
|
|
Name string
|
|
|
|
acts map[string][]string
|
|
|
|
schedules []map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (evt *Event) IsSchedule() bool {
|
|
|
|
return evt.schedules != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (evt *Event) Acts() map[string][]string {
|
|
|
|
return evt.acts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (evt *Event) Schedules() []map[string]string {
|
|
|
|
return evt.schedules
|
2023-01-31 15:49:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func ParseRawOn(rawOn *yaml.Node) ([]*Event, error) {
|
|
|
|
switch rawOn.Kind {
|
|
|
|
case yaml.ScalarNode:
|
|
|
|
var val string
|
|
|
|
err := rawOn.Decode(&val)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return []*Event{
|
|
|
|
{Name: val},
|
|
|
|
}, nil
|
|
|
|
case yaml.SequenceNode:
|
|
|
|
var val []interface{}
|
|
|
|
err := rawOn.Decode(&val)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res := make([]*Event, 0, len(val))
|
|
|
|
for _, v := range val {
|
|
|
|
switch t := v.(type) {
|
|
|
|
case string:
|
|
|
|
res = append(res, &Event{Name: t})
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("invalid type %T", t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
case yaml.MappingNode:
|
2023-04-24 23:16:41 +08:00
|
|
|
events, triggers, err := parseMappingNode[interface{}](rawOn)
|
2023-01-31 15:49:55 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-24 23:16:41 +08:00
|
|
|
res := make([]*Event, 0, len(events))
|
|
|
|
for i, k := range events {
|
|
|
|
v := triggers[i]
|
2023-03-25 12:13:50 +08:00
|
|
|
if v == nil {
|
|
|
|
res = append(res, &Event{
|
|
|
|
Name: k,
|
2023-03-26 21:01:46 +08:00
|
|
|
acts: map[string][]string{},
|
2023-03-25 12:13:50 +08:00
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
2023-01-31 15:49:55 +08:00
|
|
|
switch t := v.(type) {
|
|
|
|
case string:
|
|
|
|
res = append(res, &Event{
|
|
|
|
Name: k,
|
2023-03-24 20:15:46 +08:00
|
|
|
acts: map[string][]string{},
|
2023-01-31 15:49:55 +08:00
|
|
|
})
|
|
|
|
case []string:
|
|
|
|
res = append(res, &Event{
|
|
|
|
Name: k,
|
2023-03-24 20:15:46 +08:00
|
|
|
acts: map[string][]string{},
|
2023-01-31 15:49:55 +08:00
|
|
|
})
|
|
|
|
case map[string]interface{}:
|
|
|
|
acts := make(map[string][]string, len(t))
|
|
|
|
for act, branches := range t {
|
|
|
|
switch b := branches.(type) {
|
|
|
|
case string:
|
|
|
|
acts[act] = []string{b}
|
|
|
|
case []string:
|
|
|
|
acts[act] = b
|
|
|
|
case []interface{}:
|
|
|
|
acts[act] = make([]string, len(b))
|
|
|
|
for i, v := range b {
|
2023-03-24 20:15:46 +08:00
|
|
|
var ok bool
|
|
|
|
if acts[act][i], ok = v.(string); !ok {
|
|
|
|
return nil, fmt.Errorf("unknown on type: %#v", branches)
|
|
|
|
}
|
2023-01-31 15:49:55 +08:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown on type: %#v", branches)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res = append(res, &Event{
|
|
|
|
Name: k,
|
2023-03-24 20:15:46 +08:00
|
|
|
acts: acts,
|
|
|
|
})
|
|
|
|
case []interface{}:
|
|
|
|
if k != "schedule" {
|
|
|
|
return nil, fmt.Errorf("unknown on type: %#v", v)
|
|
|
|
}
|
|
|
|
schedules := make([]map[string]string, len(t))
|
|
|
|
for i, tt := range t {
|
|
|
|
vv, ok := tt.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unknown on type: %#v", v)
|
|
|
|
}
|
|
|
|
schedules[i] = make(map[string]string, len(vv))
|
|
|
|
for k, vvv := range vv {
|
|
|
|
var ok bool
|
|
|
|
if schedules[i][k], ok = vvv.(string); !ok {
|
|
|
|
return nil, fmt.Errorf("unknown on type: %#v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res = append(res, &Event{
|
|
|
|
Name: k,
|
|
|
|
schedules: schedules,
|
2023-01-31 15:49:55 +08:00
|
|
|
})
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown on type: %#v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown on type: %v", rawOn.Kind)
|
|
|
|
}
|
|
|
|
}
|
2023-04-24 23:16:41 +08:00
|
|
|
|
|
|
|
// parseMappingNode parse a mapping node and preserve order.
|
|
|
|
func parseMappingNode[T any](node *yaml.Node) ([]string, []T, error) {
|
|
|
|
if node.Kind != yaml.MappingNode {
|
|
|
|
return nil, nil, fmt.Errorf("input node is not a mapping node")
|
|
|
|
}
|
|
|
|
|
|
|
|
var scalars []string
|
|
|
|
var datas []T
|
|
|
|
expectKey := true
|
|
|
|
for _, item := range node.Content {
|
|
|
|
if expectKey {
|
|
|
|
if item.Kind != yaml.ScalarNode {
|
|
|
|
return nil, nil, fmt.Errorf("not a valid scalar node: %v", item.Value)
|
|
|
|
}
|
|
|
|
scalars = append(scalars, item.Value)
|
|
|
|
expectKey = false
|
|
|
|
} else {
|
|
|
|
var val T
|
|
|
|
if err := item.Decode(&val); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
datas = append(datas, val)
|
|
|
|
expectKey = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(scalars) != len(datas) {
|
|
|
|
return nil, nil, fmt.Errorf("invalid definition of on: %v", node.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return scalars, datas, nil
|
|
|
|
}
|