ParseRawOn support schedules #29

Merged
appleboy merged 2 commits from lunny/act:lunny/parse_on_schedule into main 2023-03-24 12:15:47 +00:00
2 changed files with 66 additions and 15 deletions

View File

@ -125,8 +125,21 @@ type RunDefaults struct {
}
type Event struct {
Name string
Acts map[string][]string
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
}
func ParseRawOn(rawOn *yaml.Node) ([]*Event, error) {
@ -168,12 +181,12 @@ func ParseRawOn(rawOn *yaml.Node) ([]*Event, error) {
case string:
res = append(res, &Event{
Name: k,
Acts: map[string][]string{},
acts: map[string][]string{},
})
case []string:
res = append(res, &Event{
Name: k,
Acts: map[string][]string{},
acts: map[string][]string{},
})
case map[string]interface{}:
acts := make(map[string][]string, len(t))
@ -186,7 +199,10 @@ func ParseRawOn(rawOn *yaml.Node) ([]*Event, error) {
case []interface{}:
acts[act] = make([]string, len(b))
for i, v := range b {
acts[act][i] = v.(string)
var ok bool
if acts[act][i], ok = v.(string); !ok {
return nil, fmt.Errorf("unknown on type: %#v", branches)
}
}
default:
return nil, fmt.Errorf("unknown on type: %#v", branches)
@ -194,7 +210,29 @@ func ParseRawOn(rawOn *yaml.Node) ([]*Event, error) {
}
res = append(res, &Event{
Name: k,
Acts: acts,
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)
}
lunny marked this conversation as resolved
Review

use s, ok := vvv.(string) to avoid panic?

use `s, ok := vvv.(string)` to avoid panic?
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,
})
default:
return nil, fmt.Errorf("unknown on type: %#v", v)

View File

@ -47,7 +47,7 @@ func TestParseRawOn(t *testing.T) {
result: []*Event{
{
Name: "push",
Acts: map[string][]string{
acts: map[string][]string{
"branches": {
"master",
},
@ -60,7 +60,7 @@ func TestParseRawOn(t *testing.T) {
result: []*Event{
{
Name: "branch_protection_rule",
Acts: map[string][]string{
acts: map[string][]string{
"types": {
"created",
"deleted",
@ -74,7 +74,7 @@ func TestParseRawOn(t *testing.T) {
result: []*Event{
{
Name: "project",
Acts: map[string][]string{
acts: map[string][]string{
"types": {
"created",
"deleted",
@ -83,7 +83,7 @@ func TestParseRawOn(t *testing.T) {
},
{
Name: "milestone",
Acts: map[string][]string{
acts: map[string][]string{
"types": {
"opened",
"deleted",
@ -97,7 +97,7 @@ func TestParseRawOn(t *testing.T) {
result: []*Event{
{
Name: "pull_request",
Acts: map[string][]string{
acts: map[string][]string{
"types": {
"opened",
},
@ -113,7 +113,7 @@ func TestParseRawOn(t *testing.T) {
result: []*Event{
{
Name: "push",
Acts: map[string][]string{
acts: map[string][]string{
"branches": {
"main",
},
@ -121,7 +121,7 @@ func TestParseRawOn(t *testing.T) {
},
{
Name: "pull_request",
Acts: map[string][]string{
acts: map[string][]string{
"types": {
"opened",
},
@ -137,7 +137,7 @@ func TestParseRawOn(t *testing.T) {
result: []*Event{
{
Name: "push",
Acts: map[string][]string{
acts: map[string][]string{
"branches": {
"main",
"releases/**",
@ -151,7 +151,7 @@ func TestParseRawOn(t *testing.T) {
result: []*Event{
{
Name: "push",
Acts: map[string][]string{
acts: map[string][]string{
"tags": {
"v1.**",
},
@ -170,6 +170,19 @@ func TestParseRawOn(t *testing.T) {
},
},
},
{
input: "on:\n schedule:\n - cron: '20 6 * * *'",
result: []*Event{
{
Name: "schedule",
schedules: []map[string]string{
{
"cron": "20 6 * * *",
},
},
},
},
},
}
for _, kase := range kases {
t.Run(kase.input, func(t *testing.T) {