summaryrefslogtreecommitdiffstats
path: root/pkg
diff options
context:
space:
mode:
author胖梁 <418094911@qq.com>2024-01-20 15:07:36 +0100
committerGitHub <noreply@github.com>2024-01-20 15:07:36 +0100
commit8072a00a77548df46d8b4a8ec9024d36d52b0c67 (patch)
tree94a6efb2ef07960701ef6ab224c79c1054f80974 /pkg
parentfix: match cache `restore-keys` in creation reverse order (#2153) (diff)
downloadforgejo-act-8072a00a77548df46d8b4a8ec9024d36d52b0c67.tar.xz
forgejo-act-8072a00a77548df46d8b4a8ec9024d36d52b0c67.zip
WorkflowDispatchConfig supports multiple yaml node kinds (#2123)
* WorkflowDispatchConfig supports ScalarNode and SequenceNode yaml node kinds * Avoid using log.Fatal * package slices is not in golang 1.20 --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Diffstat (limited to 'pkg')
-rw-r--r--pkg/model/workflow.go44
-rw-r--r--pkg/model/workflow_test.go104
2 files changed, 135 insertions, 13 deletions
diff --git a/pkg/model/workflow.go b/pkg/model/workflow.go
index 9860aad..1186454 100644
--- a/pkg/model/workflow.go
+++ b/pkg/model/workflow.go
@@ -79,22 +79,40 @@ type WorkflowDispatch struct {
}
func (w *Workflow) WorkflowDispatchConfig() *WorkflowDispatch {
- if w.RawOn.Kind != yaml.MappingNode {
- return nil
- }
-
- var val map[string]yaml.Node
- if !decodeNode(w.RawOn, &val) {
- return nil
- }
+ switch w.RawOn.Kind {
+ case yaml.ScalarNode:
+ var val string
+ if !decodeNode(w.RawOn, &val) {
+ return nil
+ }
+ if val == "workflow_dispatch" {
+ return &WorkflowDispatch{}
+ }
+ case yaml.SequenceNode:
+ var val []string
+ if !decodeNode(w.RawOn, &val) {
+ return nil
+ }
+ for _, v := range val {
+ if v == "workflow_dispatch" {
+ return &WorkflowDispatch{}
+ }
+ }
+ case yaml.MappingNode:
+ var val map[string]yaml.Node
+ if !decodeNode(w.RawOn, &val) {
+ return nil
+ }
- var config WorkflowDispatch
- node := val["workflow_dispatch"]
- if !decodeNode(node, &config) {
+ n, found := val["workflow_dispatch"]
+ var workflowDispatch WorkflowDispatch
+ if found && decodeNode(n, &workflowDispatch) {
+ return &workflowDispatch
+ }
+ default:
return nil
}
-
- return &config
+ return nil
}
type WorkflowCallInput struct {
diff --git a/pkg/model/workflow_test.go b/pkg/model/workflow_test.go
index bc12f8e..88c2096 100644
--- a/pkg/model/workflow_test.go
+++ b/pkg/model/workflow_test.go
@@ -417,3 +417,107 @@ func TestStep_ShellCommand(t *testing.T) {
})
}
}
+
+func TestReadWorkflow_WorkflowDispatchConfig(t *testing.T) {
+ yaml := `
+ name: local-action-docker-url
+ `
+ workflow, err := ReadWorkflow(strings.NewReader(yaml))
+ assert.NoError(t, err, "read workflow should succeed")
+ workflowDispatch := workflow.WorkflowDispatchConfig()
+ assert.Nil(t, workflowDispatch)
+
+ yaml = `
+ name: local-action-docker-url
+ on: push
+ `
+ workflow, err = ReadWorkflow(strings.NewReader(yaml))
+ assert.NoError(t, err, "read workflow should succeed")
+ workflowDispatch = workflow.WorkflowDispatchConfig()
+ assert.Nil(t, workflowDispatch)
+
+ yaml = `
+ name: local-action-docker-url
+ on: workflow_dispatch
+ `
+ workflow, err = ReadWorkflow(strings.NewReader(yaml))
+ assert.NoError(t, err, "read workflow should succeed")
+ workflowDispatch = workflow.WorkflowDispatchConfig()
+ assert.NotNil(t, workflowDispatch)
+ assert.Nil(t, workflowDispatch.Inputs)
+
+ yaml = `
+ name: local-action-docker-url
+ on: [push, pull_request]
+ `
+ workflow, err = ReadWorkflow(strings.NewReader(yaml))
+ assert.NoError(t, err, "read workflow should succeed")
+ workflowDispatch = workflow.WorkflowDispatchConfig()
+ assert.Nil(t, workflowDispatch)
+
+ yaml = `
+ name: local-action-docker-url
+ on: [push, workflow_dispatch]
+ `
+ workflow, err = ReadWorkflow(strings.NewReader(yaml))
+ assert.NoError(t, err, "read workflow should succeed")
+ workflowDispatch = workflow.WorkflowDispatchConfig()
+ assert.NotNil(t, workflowDispatch)
+ assert.Nil(t, workflowDispatch.Inputs)
+
+ yaml = `
+ name: local-action-docker-url
+ on:
+ - push
+ - workflow_dispatch
+ `
+ workflow, err = ReadWorkflow(strings.NewReader(yaml))
+ assert.NoError(t, err, "read workflow should succeed")
+ workflowDispatch = workflow.WorkflowDispatchConfig()
+ assert.NotNil(t, workflowDispatch)
+ assert.Nil(t, workflowDispatch.Inputs)
+
+ yaml = `
+ name: local-action-docker-url
+ on:
+ push:
+ pull_request:
+ `
+ workflow, err = ReadWorkflow(strings.NewReader(yaml))
+ assert.NoError(t, err, "read workflow should succeed")
+ workflowDispatch = workflow.WorkflowDispatchConfig()
+ assert.Nil(t, workflowDispatch)
+
+ yaml = `
+ name: local-action-docker-url
+ on:
+ push:
+ pull_request:
+ workflow_dispatch:
+ inputs:
+ logLevel:
+ description: 'Log level'
+ required: true
+ default: 'warning'
+ type: choice
+ options:
+ - info
+ - warning
+ - debug
+ `
+ workflow, err = ReadWorkflow(strings.NewReader(yaml))
+ assert.NoError(t, err, "read workflow should succeed")
+ workflowDispatch = workflow.WorkflowDispatchConfig()
+ assert.NotNil(t, workflowDispatch)
+ assert.Equal(t, WorkflowDispatchInput{
+ Default: "warning",
+ Description: "Log level",
+ Options: []string{
+ "info",
+ "warning",
+ "debug",
+ },
+ Required: true,
+ Type: "choice",
+ }, workflowDispatch.Inputs["logLevel"])
+}