From e60018a6d9403b27bfd1a1ed6111a6e9de0032fd Mon Sep 17 00:00:00 2001 From: Sam Foo Date: Tue, 27 Jun 2023 10:32:04 -0700 Subject: [PATCH] Allow inputs for workflow_calls (#1845) --- pkg/runner/expression.go | 17 +++++++++ pkg/runner/runner_test.go | 1 + .../testdata/workflow_call_inputs/event.json | 6 ++++ .../workflow_call_inputs.yml | 36 +++++++++++++++++++ 4 files changed, 60 insertions(+) create mode 100644 pkg/runner/testdata/workflow_call_inputs/event.json create mode 100644 pkg/runner/testdata/workflow_call_inputs/workflow_call_inputs.yml diff --git a/pkg/runner/expression.go b/pkg/runner/expression.go index cc144af..24b16f8 100644 --- a/pkg/runner/expression.go +++ b/pkg/runner/expression.go @@ -397,6 +397,7 @@ func rewriteSubExpression(ctx context.Context, in string, forceFormat bool) (str return out, nil } +//nolint:gocyclo func getEvaluatorInputs(ctx context.Context, rc *RunContext, step step, ghc *model.GithubContext) map[string]interface{} { inputs := map[string]interface{}{} @@ -432,6 +433,22 @@ func getEvaluatorInputs(ctx context.Context, rc *RunContext, step step, ghc *mod } } + if ghc.EventName == "workflow_call" { + config := rc.Run.Workflow.WorkflowCallConfig() + if config != nil && config.Inputs != nil { + for k, v := range config.Inputs { + value := nestedMapLookup(ghc.Event, "inputs", k) + if value == nil { + value = v.Default + } + if v.Type == "boolean" { + inputs[k] = value == "true" + } else { + inputs[k] = value + } + } + } + } return inputs } diff --git a/pkg/runner/runner_test.go b/pkg/runner/runner_test.go index db63fb9..5b8257c 100644 --- a/pkg/runner/runner_test.go +++ b/pkg/runner/runner_test.go @@ -288,6 +288,7 @@ func TestRunEvent(t *testing.T) { {workdir, "docker-action-custom-path", "push", "", platforms, secrets}, {workdir, "GITHUB_ENV-use-in-env-ctx", "push", "", platforms, secrets}, {workdir, "ensure-post-steps", "push", "Job 'second-post-step-should-fail' failed", platforms, secrets}, + {workdir, "workflow_call_inputs", "workflow_call", "", platforms, secrets}, {workdir, "workflow_dispatch", "workflow_dispatch", "", platforms, secrets}, {workdir, "workflow_dispatch_no_inputs_mapping", "workflow_dispatch", "", platforms, secrets}, {workdir, "workflow_dispatch-scalar", "workflow_dispatch", "", platforms, secrets}, diff --git a/pkg/runner/testdata/workflow_call_inputs/event.json b/pkg/runner/testdata/workflow_call_inputs/event.json new file mode 100644 index 0000000..d3ecab1 --- /dev/null +++ b/pkg/runner/testdata/workflow_call_inputs/event.json @@ -0,0 +1,6 @@ +{ + "inputs": { + "required": "required input", + "boolean": "true" + } +} diff --git a/pkg/runner/testdata/workflow_call_inputs/workflow_call_inputs.yml b/pkg/runner/testdata/workflow_call_inputs/workflow_call_inputs.yml new file mode 100644 index 0000000..1a5cca3 --- /dev/null +++ b/pkg/runner/testdata/workflow_call_inputs/workflow_call_inputs.yml @@ -0,0 +1,36 @@ +name: workflow_call + +on: + workflow_call: + inputs: + required: + description: a required input + required: true + with_default: + description: an input with default + required: false + default: default + boolean: + description: an input of type boolean + required: false + type: boolean + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: test required input + run: | + echo input.required=${{ inputs.required }} + [[ "${{ inputs.required }}" = "required input" ]] || exit 1 + - name: test input with default + run: | + echo input.with_default=${{ inputs.with_default }} + [[ "${{ inputs.with_default }}" = "default" ]] || exit 1 + - id: boolean-test + name: run on boolean input + if: ${{ inputs.boolean == true }} + run: echo "::set-output name=value::executed" + - name: has boolean test? + run: | + [[ "${{ steps.boolean-test.outputs.value }}" = "executed" ]] || exit 1