Skip to content

Commit b917ecc

Browse files
fix: update reusable workflow input handling (#2349)
* update reusable workflow input handling * make test stricter --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 55a8f9a commit b917ecc

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

pkg/model/workflow.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ func (w *Workflow) WorkflowDispatchConfig() *WorkflowDispatch {
116116
}
117117

118118
type WorkflowCallInput struct {
119-
Description string `yaml:"description"`
120-
Required bool `yaml:"required"`
121-
Default string `yaml:"default"`
122-
Type string `yaml:"type"`
119+
Description string `yaml:"description"`
120+
Required bool `yaml:"required"`
121+
Default yaml.Node `yaml:"default"`
122+
Type string `yaml:"type"`
123123
}
124124

125125
type WorkflowCallOutput struct {

pkg/runner/expression.go

+11-8
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ func getEvaluatorInputs(ctx context.Context, rc *RunContext, step step, ghc *mod
512512
for k, v := range config.Inputs {
513513
value := nestedMapLookup(ghc.Event, "inputs", k)
514514
if value == nil {
515-
value = v.Default
515+
v.Default.Decode(&value)
516516
}
517517
if v.Type == "boolean" {
518518
inputs[k] = value == "true"
@@ -531,21 +531,24 @@ func setupWorkflowInputs(ctx context.Context, inputs *map[string]interface{}, rc
531531

532532
for name, input := range config.Inputs {
533533
value := rc.caller.runContext.Run.Job().With[name]
534+
534535
if value != nil {
535-
if str, ok := value.(string); ok {
536+
node := yaml.Node{}
537+
_ = node.Encode(value)
538+
if rc.caller.runContext.ExprEval != nil {
536539
// evaluate using the calling RunContext (outside)
537-
value = rc.caller.runContext.ExprEval.Interpolate(ctx, str)
540+
_ = rc.caller.runContext.ExprEval.EvaluateYamlNode(ctx, &node)
538541
}
542+
_ = node.Decode(&value)
539543
}
540544

541545
if value == nil && config != nil && config.Inputs != nil {
542-
value = input.Default
546+
def := input.Default
543547
if rc.ExprEval != nil {
544-
if str, ok := value.(string); ok {
545-
// evaluate using the called RunContext (inside)
546-
value = rc.ExprEval.Interpolate(ctx, str)
547-
}
548+
// evaluate using the called RunContext (inside)
549+
_ = rc.ExprEval.EvaluateYamlNode(ctx, &def)
548550
}
551+
_ = def.Decode(&value)
549552
}
550553

551554
(*inputs)[name] = value

pkg/runner/testdata/.github/workflows/local-reusable-workflow.yml

+8-8
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,23 @@ jobs:
4545
4646
- name: test required bool
4747
run: |
48-
echo inputs.bool_required=${{ inputs.bool_required }}
49-
[[ "${{ inputs.bool_required }}" = "true" ]] || exit 1
48+
echo inputs.bool_required=${{ tojson(inputs.bool_required) }}
49+
[[ "${{ tojson(inputs.bool_required) }}" = "true" ]] || exit 1
5050
5151
- name: test optional bool
5252
run: |
53-
echo inputs.bool_optional=${{ inputs.bool_optional }}
54-
[[ "${{ inputs.bool_optional }}" = "true" ]] || exit 1
53+
echo inputs.bool_optional=${{ tojson(inputs.bool_optional) }}
54+
[[ "${{ tojson(inputs.bool_optional) }}" = "true" ]] || exit 1
5555
5656
- name: test required number
5757
run: |
58-
echo inputs.number_required=${{ inputs.number_required }}
59-
[[ "${{ inputs.number_required == 1 }}" = "true" ]] || exit 1
58+
echo inputs.number_required=${{ tojson(inputs.number_required) }}
59+
[[ "${{ tojson(inputs.number_required) == '1' }}" = "true" ]] || exit 1
6060
6161
- name: test optional number
6262
run: |
63-
echo inputs.number_optional=${{ inputs.number_optional }}
64-
[[ "${{ inputs.number_optional == 1 }}" = "true" ]] || exit 1
63+
echo inputs.number_optional=${{ tojson(inputs.number_optional) }}
64+
[[ "${{ tojson(inputs.number_optional) == '1' }}" = "true" ]] || exit 1
6565
6666
- name: test secret
6767
run: |

0 commit comments

Comments
 (0)