From 88cce47022d34d5cbc49771b3753541fe905f5b6 Mon Sep 17 00:00:00 2001 From: appleboy Date: Sat, 10 Dec 2022 09:14:14 +0800 Subject: [PATCH] feat(workflow): support schedule event (#4) fix https://gitea.com/gitea/act/issues/3 Signed-off-by: Bo-Yi.Wu Co-authored-by: Bo-Yi.Wu Reviewed-on: https://gitea.com/gitea/act/pulls/4 --- pkg/model/workflow.go | 26 +++++++++++- pkg/model/workflow_test.go | 82 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 1 deletion(-) diff --git a/pkg/model/workflow.go b/pkg/model/workflow.go index f1a2cb3..b898d15 100644 --- a/pkg/model/workflow.go +++ b/pkg/model/workflow.go @@ -67,6 +67,30 @@ func (w *Workflow) OnEvent(event string) interface{} { return nil } +func (w *Workflow) OnSchedule() []string { + schedules := w.OnEvent("schedule") + if schedules == nil { + return []string{} + } + + switch val := schedules.(type) { + case []interface{}: + allSchedules := []string{} + for _, v := range val { + for k, cron := range v.(map[string]interface{}) { + if k != "cron" { + continue + } + allSchedules = append(allSchedules, cron.(string)) + } + } + return allSchedules + default: + } + + return []string{} +} + type WorkflowDispatchInput struct { Description string `yaml:"description"` Required bool `yaml:"required"` @@ -487,7 +511,7 @@ func (s *Step) GetEnv() map[string]string { func (s *Step) ShellCommand() string { shellCommand := "" - //Reference: https://github.com/actions/runner/blob/8109c962f09d9acc473d92c595ff43afceddb347/src/Runner.Worker/Handlers/ScriptHandlerHelpers.cs#L9-L17 + // Reference: https://github.com/actions/runner/blob/8109c962f09d9acc473d92c595ff43afceddb347/src/Runner.Worker/Handlers/ScriptHandlerHelpers.cs#L9-L17 switch s.Shell { case "", "bash": shellCommand = "bash --noprofile --norc -e -o pipefail {0}" diff --git a/pkg/model/workflow_test.go b/pkg/model/workflow_test.go index d978f16..80c6795 100644 --- a/pkg/model/workflow_test.go +++ b/pkg/model/workflow_test.go @@ -7,6 +7,88 @@ import ( "github.com/stretchr/testify/assert" ) +func TestReadWorkflow_ScheduleEvent(t *testing.T) { + yaml := ` +name: local-action-docker-url +on: + schedule: + - cron: '30 5 * * 1,3' + - cron: '30 5 * * 2,4' + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: ./actions/docker-url +` + + workflow, err := ReadWorkflow(strings.NewReader(yaml)) + assert.NoError(t, err, "read workflow should succeed") + + schedules := workflow.OnEvent("schedule") + assert.Len(t, schedules, 2) + + newSchedules := workflow.OnSchedule() + assert.Len(t, newSchedules, 2) + + assert.Equal(t, "30 5 * * 1,3", newSchedules[0]) + assert.Equal(t, "30 5 * * 2,4", newSchedules[1]) + + yaml = ` +name: local-action-docker-url +on: + schedule: + test: '30 5 * * 1,3' + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: ./actions/docker-url +` + + workflow, err = ReadWorkflow(strings.NewReader(yaml)) + assert.NoError(t, err, "read workflow should succeed") + + newSchedules = workflow.OnSchedule() + assert.Len(t, newSchedules, 0) + + yaml = ` +name: local-action-docker-url +on: + schedule: + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: ./actions/docker-url +` + + workflow, err = ReadWorkflow(strings.NewReader(yaml)) + assert.NoError(t, err, "read workflow should succeed") + + newSchedules = workflow.OnSchedule() + assert.Len(t, newSchedules, 0) + + yaml = ` +name: local-action-docker-url +on: [push, tag] + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: ./actions/docker-url +` + + workflow, err = ReadWorkflow(strings.NewReader(yaml)) + assert.NoError(t, err, "read workflow should succeed") + + newSchedules = workflow.OnSchedule() + assert.Len(t, newSchedules, 0) +} + func TestReadWorkflow_StringEvent(t *testing.T) { yaml := ` name: local-action-docker-url