From 8072a00a77548df46d8b4a8ec9024d36d52b0c67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=96=E6=A2=81?= <418094911@qq.com> Date: Sat, 20 Jan 2024 22:07:36 +0800 Subject: [PATCH] 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> --- pkg/model/workflow.go | 46 +++++++++++----- pkg/model/workflow_test.go | 104 +++++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+), 14 deletions(-) diff --git a/pkg/model/workflow.go b/pkg/model/workflow.go index 9860aad4..11864540 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 { + 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 + } + + n, found := val["workflow_dispatch"] + var workflowDispatch WorkflowDispatch + if found && decodeNode(n, &workflowDispatch) { + return &workflowDispatch + } + default: return nil } - - var val map[string]yaml.Node - if !decodeNode(w.RawOn, &val) { - return nil - } - - var config WorkflowDispatch - node := val["workflow_dispatch"] - if !decodeNode(node, &config) { - return nil - } - - return &config + return nil } type WorkflowCallInput struct { diff --git a/pkg/model/workflow_test.go b/pkg/model/workflow_test.go index bc12f8ee..88c2096a 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"]) +}