ParseRawOn support schedules ()

Fix 

Reviewed-on: https://gitea.com/gitea/act/pulls/29
Reviewed-by: Jason Song <i@wolfogre.com>
Reviewed-by: Zettat123 <zettat123@noreply.gitea.io>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-committed-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
Lunny Xiao 2023-03-24 20:15:46 +08:00 committed by appleboy
parent 929ea6df75
commit 7c5400d75b
2 changed files with 66 additions and 15 deletions

View file

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

View file

@ -47,7 +47,7 @@ func TestParseRawOn(t *testing.T) {
result: []*Event{ result: []*Event{
{ {
Name: "push", Name: "push",
Acts: map[string][]string{ acts: map[string][]string{
"branches": { "branches": {
"master", "master",
}, },
@ -60,7 +60,7 @@ func TestParseRawOn(t *testing.T) {
result: []*Event{ result: []*Event{
{ {
Name: "branch_protection_rule", Name: "branch_protection_rule",
Acts: map[string][]string{ acts: map[string][]string{
"types": { "types": {
"created", "created",
"deleted", "deleted",
@ -74,7 +74,7 @@ func TestParseRawOn(t *testing.T) {
result: []*Event{ result: []*Event{
{ {
Name: "project", Name: "project",
Acts: map[string][]string{ acts: map[string][]string{
"types": { "types": {
"created", "created",
"deleted", "deleted",
@ -83,7 +83,7 @@ func TestParseRawOn(t *testing.T) {
}, },
{ {
Name: "milestone", Name: "milestone",
Acts: map[string][]string{ acts: map[string][]string{
"types": { "types": {
"opened", "opened",
"deleted", "deleted",
@ -97,7 +97,7 @@ func TestParseRawOn(t *testing.T) {
result: []*Event{ result: []*Event{
{ {
Name: "pull_request", Name: "pull_request",
Acts: map[string][]string{ acts: map[string][]string{
"types": { "types": {
"opened", "opened",
}, },
@ -113,7 +113,7 @@ func TestParseRawOn(t *testing.T) {
result: []*Event{ result: []*Event{
{ {
Name: "push", Name: "push",
Acts: map[string][]string{ acts: map[string][]string{
"branches": { "branches": {
"main", "main",
}, },
@ -121,7 +121,7 @@ func TestParseRawOn(t *testing.T) {
}, },
{ {
Name: "pull_request", Name: "pull_request",
Acts: map[string][]string{ acts: map[string][]string{
"types": { "types": {
"opened", "opened",
}, },
@ -137,7 +137,7 @@ func TestParseRawOn(t *testing.T) {
result: []*Event{ result: []*Event{
{ {
Name: "push", Name: "push",
Acts: map[string][]string{ acts: map[string][]string{
"branches": { "branches": {
"main", "main",
"releases/**", "releases/**",
@ -151,7 +151,7 @@ func TestParseRawOn(t *testing.T) {
result: []*Event{ result: []*Event{
{ {
Name: "push", Name: "push",
Acts: map[string][]string{ acts: map[string][]string{
"tags": { "tags": {
"v1.**", "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 { for _, kase := range kases {
t.Run(kase.input, func(t *testing.T) { t.Run(kase.input, func(t *testing.T) {