Skip to content

Commit

Permalink
fix: update reusable workflow input handling (#2349)
Browse files Browse the repository at this point in the history
* update reusable workflow input handling

* make test stricter

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
ChristopherHX and mergify[bot] committed Jun 5, 2024
1 parent 55a8f9a commit b917ecc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
8 changes: 4 additions & 4 deletions pkg/model/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ func (w *Workflow) WorkflowDispatchConfig() *WorkflowDispatch {
}

type WorkflowCallInput struct {
Description string `yaml:"description"`
Required bool `yaml:"required"`
Default string `yaml:"default"`
Type string `yaml:"type"`
Description string `yaml:"description"`
Required bool `yaml:"required"`
Default yaml.Node `yaml:"default"`
Type string `yaml:"type"`
}

type WorkflowCallOutput struct {
Expand Down
19 changes: 11 additions & 8 deletions pkg/runner/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ func getEvaluatorInputs(ctx context.Context, rc *RunContext, step step, ghc *mod
for k, v := range config.Inputs {
value := nestedMapLookup(ghc.Event, "inputs", k)
if value == nil {
value = v.Default
v.Default.Decode(&value)
}
if v.Type == "boolean" {
inputs[k] = value == "true"
Expand All @@ -531,21 +531,24 @@ func setupWorkflowInputs(ctx context.Context, inputs *map[string]interface{}, rc

for name, input := range config.Inputs {
value := rc.caller.runContext.Run.Job().With[name]

if value != nil {
if str, ok := value.(string); ok {
node := yaml.Node{}
_ = node.Encode(value)
if rc.caller.runContext.ExprEval != nil {
// evaluate using the calling RunContext (outside)
value = rc.caller.runContext.ExprEval.Interpolate(ctx, str)
_ = rc.caller.runContext.ExprEval.EvaluateYamlNode(ctx, &node)
}
_ = node.Decode(&value)
}

if value == nil && config != nil && config.Inputs != nil {
value = input.Default
def := input.Default
if rc.ExprEval != nil {
if str, ok := value.(string); ok {
// evaluate using the called RunContext (inside)
value = rc.ExprEval.Interpolate(ctx, str)
}
// evaluate using the called RunContext (inside)
_ = rc.ExprEval.EvaluateYamlNode(ctx, &def)
}
_ = def.Decode(&value)
}

(*inputs)[name] = value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,23 @@ jobs:
- name: test required bool
run: |
echo inputs.bool_required=${{ inputs.bool_required }}
[[ "${{ inputs.bool_required }}" = "true" ]] || exit 1
echo inputs.bool_required=${{ tojson(inputs.bool_required) }}
[[ "${{ tojson(inputs.bool_required) }}" = "true" ]] || exit 1
- name: test optional bool
run: |
echo inputs.bool_optional=${{ inputs.bool_optional }}
[[ "${{ inputs.bool_optional }}" = "true" ]] || exit 1
echo inputs.bool_optional=${{ tojson(inputs.bool_optional) }}
[[ "${{ tojson(inputs.bool_optional) }}" = "true" ]] || exit 1
- name: test required number
run: |
echo inputs.number_required=${{ inputs.number_required }}
[[ "${{ inputs.number_required == 1 }}" = "true" ]] || exit 1
echo inputs.number_required=${{ tojson(inputs.number_required) }}
[[ "${{ tojson(inputs.number_required) == '1' }}" = "true" ]] || exit 1
- name: test optional number
run: |
echo inputs.number_optional=${{ inputs.number_optional }}
[[ "${{ inputs.number_optional == 1 }}" = "true" ]] || exit 1
echo inputs.number_optional=${{ tojson(inputs.number_optional) }}
[[ "${{ tojson(inputs.number_optional) == '1' }}" = "true" ]] || exit 1
- name: test secret
run: |
Expand Down

0 comments on commit b917ecc

Please sign in to comment.