Mapping workflow_dispatch inputs into the Expression inputs context (#1363)
				
					
				
			* test: check workflow_dispatch inputs This implements a test to check for `workflow_dispatch` inputs. This will be a prerequisite for implementing the inputs. * feat: map workflow_dispatch input to expression evaluator This changes adds the workflow_dispatch event inputs to the `inputs` context and maintaining the boolean type * fix: coerce boolean input types * fix: use step env if available, rc env otherwise
This commit is contained in:
		| @@ -9,6 +9,7 @@ import ( | ||||
| 	"github.com/nektos/act/pkg/common" | ||||
| 	"github.com/nektos/act/pkg/container" | ||||
| 	"github.com/nektos/act/pkg/exprparser" | ||||
| 	"github.com/nektos/act/pkg/model" | ||||
| 	"gopkg.in/yaml.v3" | ||||
| ) | ||||
|  | ||||
| @@ -39,15 +40,11 @@ func (rc *RunContext) NewExpressionEvaluator(ctx context.Context) ExpressionEval | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	inputs := make(map[string]interface{}) | ||||
| 	for k, v := range rc.GetEnv() { | ||||
| 		if strings.HasPrefix(k, "INPUT_") { | ||||
| 			inputs[strings.ToLower(strings.TrimPrefix(k, "INPUT_"))] = v | ||||
| 		} | ||||
| 	} | ||||
| 	ghc := rc.getGithubContext(ctx) | ||||
| 	inputs := getEvaluatorInputs(ctx, rc, nil, ghc) | ||||
|  | ||||
| 	ee := &exprparser.EvaluationEnvironment{ | ||||
| 		Github: rc.getGithubContext(ctx), | ||||
| 		Github: ghc, | ||||
| 		Env:    rc.GetEnv(), | ||||
| 		Job:    rc.getJobContext(), | ||||
| 		// todo: should be unavailable | ||||
| @@ -94,12 +91,8 @@ func (rc *RunContext) NewStepExpressionEvaluator(ctx context.Context, step step) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	inputs := make(map[string]interface{}) | ||||
| 	for k, v := range *step.getEnv() { | ||||
| 		if strings.HasPrefix(k, "INPUT_") { | ||||
| 			inputs[strings.ToLower(strings.TrimPrefix(k, "INPUT_"))] = v | ||||
| 		} | ||||
| 	} | ||||
| 	ghc := rc.getGithubContext(ctx) | ||||
| 	inputs := getEvaluatorInputs(ctx, rc, step, ghc) | ||||
|  | ||||
| 	ee := &exprparser.EvaluationEnvironment{ | ||||
| 		Github: step.getGithubContext(ctx), | ||||
| @@ -319,3 +312,37 @@ func rewriteSubExpression(ctx context.Context, in string, forceFormat bool) (str | ||||
| 	} | ||||
| 	return out, nil | ||||
| } | ||||
|  | ||||
| func getEvaluatorInputs(ctx context.Context, rc *RunContext, step step, ghc *model.GithubContext) map[string]interface{} { | ||||
| 	inputs := map[string]interface{}{} | ||||
|  | ||||
| 	var env map[string]string | ||||
| 	if step != nil { | ||||
| 		env = *step.getEnv() | ||||
| 	} else { | ||||
| 		env = rc.GetEnv() | ||||
| 	} | ||||
|  | ||||
| 	for k, v := range env { | ||||
| 		if strings.HasPrefix(k, "INPUT_") { | ||||
| 			inputs[strings.ToLower(strings.TrimPrefix(k, "INPUT_"))] = v | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if ghc.EventName == "workflow_dispatch" { | ||||
| 		config := rc.Run.Workflow.WorkflowDispatchConfig() | ||||
| 		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 | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user