feat(workflow): support schedule event (#4)
fix https://gitea.com/gitea/act/issues/3 Signed-off-by: Bo-Yi.Wu <appleboy.tw@gmail.com> Co-authored-by: Bo-Yi.Wu <appleboy.tw@gmail.com> Reviewed-on: https://gitea.com/gitea/act/pulls/4
This commit is contained in:
parent
7920109e89
commit
88cce47022
2 changed files with 107 additions and 1 deletions
|
@ -67,6 +67,30 @@ func (w *Workflow) OnEvent(event string) interface{} {
|
||||||
return nil
|
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 {
|
type WorkflowDispatchInput struct {
|
||||||
Description string `yaml:"description"`
|
Description string `yaml:"description"`
|
||||||
Required bool `yaml:"required"`
|
Required bool `yaml:"required"`
|
||||||
|
|
|
@ -7,6 +7,88 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"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) {
|
func TestReadWorkflow_StringEvent(t *testing.T) {
|
||||||
yaml := `
|
yaml := `
|
||||||
name: local-action-docker-url
|
name: local-action-docker-url
|
||||||
|
|
Loading…
Reference in a new issue