2020-02-13 01:27:37 -06:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
2022-06-17 10:55:21 -05:00
|
|
|
"context"
|
2021-12-08 14:57:42 -06:00
|
|
|
"fmt"
|
2020-02-13 01:27:37 -06:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
"github.com/nektos/act/pkg/common"
|
2022-01-21 10:07:20 -06:00
|
|
|
"github.com/nektos/act/pkg/exprparser"
|
2022-10-17 11:25:26 -05:00
|
|
|
"github.com/nektos/act/pkg/model"
|
2022-02-15 10:35:02 -06:00
|
|
|
"gopkg.in/yaml.v3"
|
2020-02-13 01:27:37 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// ExpressionEvaluator is the interface for evaluating expressions
|
|
|
|
type ExpressionEvaluator interface {
|
2022-06-17 10:55:21 -05:00
|
|
|
evaluate(context.Context, string, exprparser.DefaultStatusCheck) (interface{}, error)
|
|
|
|
EvaluateYamlNode(context.Context, *yaml.Node) error
|
|
|
|
Interpolate(context.Context, string) string
|
2020-02-13 01:27:37 -06:00
|
|
|
}
|
|
|
|
|
2022-01-21 10:07:20 -06:00
|
|
|
// NewExpressionEvaluator creates a new evaluator
|
2022-06-17 10:55:21 -05:00
|
|
|
func (rc *RunContext) NewExpressionEvaluator(ctx context.Context) ExpressionEvaluator {
|
2022-12-06 10:46:20 -06:00
|
|
|
return rc.NewExpressionEvaluatorWithEnv(ctx, rc.GetEnv())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *RunContext) NewExpressionEvaluatorWithEnv(ctx context.Context, env map[string]string) ExpressionEvaluator {
|
2023-02-23 16:34:47 -06:00
|
|
|
var workflowCallResult map[string]*model.WorkflowCallResult
|
|
|
|
|
2022-01-21 10:07:20 -06:00
|
|
|
// todo: cleanup EvaluationEnvironment creation
|
2022-12-19 02:37:53 -06:00
|
|
|
using := make(map[string]exprparser.Needs)
|
2022-01-21 10:07:20 -06:00
|
|
|
strategy := make(map[string]interface{})
|
2022-11-16 15:29:45 -06:00
|
|
|
if rc.Run != nil {
|
|
|
|
job := rc.Run.Job()
|
|
|
|
if job != nil && job.Strategy != nil {
|
|
|
|
strategy["fail-fast"] = job.Strategy.FailFast
|
|
|
|
strategy["max-parallel"] = job.Strategy.MaxParallel
|
|
|
|
}
|
2020-02-13 01:27:37 -06:00
|
|
|
|
2022-11-16 15:29:45 -06:00
|
|
|
jobs := rc.Run.Workflow.Jobs
|
|
|
|
jobNeeds := rc.Run.Job().Needs()
|
2020-02-13 01:27:37 -06:00
|
|
|
|
2022-11-16 15:29:45 -06:00
|
|
|
for _, needs := range jobNeeds {
|
2022-12-19 02:37:53 -06:00
|
|
|
using[needs] = exprparser.Needs{
|
|
|
|
Outputs: jobs[needs].Outputs,
|
|
|
|
Result: jobs[needs].Result,
|
2022-11-16 15:29:45 -06:00
|
|
|
}
|
2020-09-01 15:55:29 -05:00
|
|
|
}
|
2023-02-23 16:34:47 -06:00
|
|
|
|
|
|
|
// only setup jobs context in case of workflow_call
|
|
|
|
// and existing expression evaluator (this means, jobs are at
|
|
|
|
// least ready to run)
|
|
|
|
if rc.caller != nil && rc.ExprEval != nil {
|
|
|
|
workflowCallResult = map[string]*model.WorkflowCallResult{}
|
|
|
|
|
|
|
|
for jobName, job := range jobs {
|
|
|
|
result := model.WorkflowCallResult{
|
|
|
|
Outputs: map[string]string{},
|
|
|
|
}
|
|
|
|
for k, v := range job.Outputs {
|
|
|
|
result.Outputs[k] = v
|
|
|
|
}
|
|
|
|
workflowCallResult[jobName] = &result
|
|
|
|
}
|
|
|
|
}
|
2020-09-01 15:55:29 -05:00
|
|
|
}
|
|
|
|
|
2022-10-17 11:25:26 -05:00
|
|
|
ghc := rc.getGithubContext(ctx)
|
|
|
|
inputs := getEvaluatorInputs(ctx, rc, nil, ghc)
|
2022-10-06 16:58:16 -05:00
|
|
|
|
2022-01-21 10:07:20 -06:00
|
|
|
ee := &exprparser.EvaluationEnvironment{
|
2022-10-17 11:25:26 -05:00
|
|
|
Github: ghc,
|
2022-12-06 10:46:20 -06:00
|
|
|
Env: env,
|
2022-01-21 10:07:20 -06:00
|
|
|
Job: rc.getJobContext(),
|
2023-02-23 16:34:47 -06:00
|
|
|
Jobs: &workflowCallResult,
|
2022-01-21 10:07:20 -06:00
|
|
|
// todo: should be unavailable
|
|
|
|
// but required to interpolate/evaluate the step outputs on the job
|
2022-11-16 15:29:45 -06:00
|
|
|
Steps: rc.getStepsContext(),
|
2022-12-15 10:45:22 -06:00
|
|
|
Secrets: getWorkflowSecrets(ctx, rc),
|
2023-06-12 01:54:17 -05:00
|
|
|
Vars: getWorkflowVars(ctx, rc),
|
2022-01-21 10:07:20 -06:00
|
|
|
Strategy: strategy,
|
|
|
|
Matrix: rc.Matrix,
|
|
|
|
Needs: using,
|
2022-10-06 16:58:16 -05:00
|
|
|
Inputs: inputs,
|
2020-02-13 13:47:38 -06:00
|
|
|
}
|
2022-11-16 15:29:45 -06:00
|
|
|
if rc.JobContainer != nil {
|
|
|
|
ee.Runner = rc.JobContainer.GetRunnerContext(ctx)
|
|
|
|
}
|
2022-01-21 10:07:20 -06:00
|
|
|
return expressionEvaluator{
|
|
|
|
interpreter: exprparser.NewInterpeter(ee, exprparser.Config{
|
|
|
|
Run: rc.Run,
|
|
|
|
WorkingDir: rc.Config.Workdir,
|
|
|
|
Context: "job",
|
|
|
|
}),
|
2020-02-14 02:41:20 -06:00
|
|
|
}
|
2020-02-13 13:47:38 -06:00
|
|
|
}
|
|
|
|
|
2022-01-21 10:07:20 -06:00
|
|
|
// NewExpressionEvaluator creates a new evaluator
|
2022-06-17 10:55:21 -05:00
|
|
|
func (rc *RunContext) NewStepExpressionEvaluator(ctx context.Context, step step) ExpressionEvaluator {
|
2022-01-21 10:07:20 -06:00
|
|
|
// todo: cleanup EvaluationEnvironment creation
|
|
|
|
job := rc.Run.Job()
|
|
|
|
strategy := make(map[string]interface{})
|
|
|
|
if job.Strategy != nil {
|
|
|
|
strategy["fail-fast"] = job.Strategy.FailFast
|
|
|
|
strategy["max-parallel"] = job.Strategy.MaxParallel
|
2020-02-13 13:47:38 -06:00
|
|
|
}
|
|
|
|
|
2022-01-21 10:07:20 -06:00
|
|
|
jobs := rc.Run.Workflow.Jobs
|
|
|
|
jobNeeds := rc.Run.Job().Needs()
|
2021-07-01 10:20:20 -05:00
|
|
|
|
2022-12-19 02:37:53 -06:00
|
|
|
using := make(map[string]exprparser.Needs)
|
2021-07-01 10:20:20 -05:00
|
|
|
for _, needs := range jobNeeds {
|
2022-12-19 02:37:53 -06:00
|
|
|
using[needs] = exprparser.Needs{
|
|
|
|
Outputs: jobs[needs].Outputs,
|
|
|
|
Result: jobs[needs].Result,
|
2021-07-01 10:20:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-17 11:25:26 -05:00
|
|
|
ghc := rc.getGithubContext(ctx)
|
|
|
|
inputs := getEvaluatorInputs(ctx, rc, step, ghc)
|
2022-10-06 16:58:16 -05:00
|
|
|
|
2022-01-21 10:07:20 -06:00
|
|
|
ee := &exprparser.EvaluationEnvironment{
|
2022-11-16 15:29:45 -06:00
|
|
|
Github: step.getGithubContext(ctx),
|
|
|
|
Env: *step.getEnv(),
|
|
|
|
Job: rc.getJobContext(),
|
|
|
|
Steps: rc.getStepsContext(),
|
2022-12-15 10:45:22 -06:00
|
|
|
Secrets: getWorkflowSecrets(ctx, rc),
|
2023-06-12 01:54:17 -05:00
|
|
|
Vars: getWorkflowVars(ctx, rc),
|
2022-01-21 10:07:20 -06:00
|
|
|
Strategy: strategy,
|
|
|
|
Matrix: rc.Matrix,
|
|
|
|
Needs: using,
|
|
|
|
// todo: should be unavailable
|
|
|
|
// but required to interpolate/evaluate the inputs in actions/composite
|
2022-10-06 16:58:16 -05:00
|
|
|
Inputs: inputs,
|
2021-12-08 14:57:42 -06:00
|
|
|
}
|
2022-11-16 15:29:45 -06:00
|
|
|
if rc.JobContainer != nil {
|
|
|
|
ee.Runner = rc.JobContainer.GetRunnerContext(ctx)
|
|
|
|
}
|
2022-01-21 10:07:20 -06:00
|
|
|
return expressionEvaluator{
|
|
|
|
interpreter: exprparser.NewInterpeter(ee, exprparser.Config{
|
|
|
|
Run: rc.Run,
|
|
|
|
WorkingDir: rc.Config.Workdir,
|
|
|
|
Context: "step",
|
|
|
|
}),
|
2021-12-08 14:57:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-21 10:07:20 -06:00
|
|
|
type expressionEvaluator struct {
|
|
|
|
interpreter exprparser.Interpreter
|
2021-12-08 14:57:42 -06:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
func (ee expressionEvaluator) evaluate(ctx context.Context, in string, defaultStatusCheck exprparser.DefaultStatusCheck) (interface{}, error) {
|
|
|
|
logger := common.Logger(ctx)
|
|
|
|
logger.Debugf("evaluating expression '%s'", in)
|
2022-05-24 08:36:06 -05:00
|
|
|
evaluated, err := ee.interpreter.Evaluate(in, defaultStatusCheck)
|
2022-06-17 10:55:21 -05:00
|
|
|
|
|
|
|
printable := regexp.MustCompile(`::add-mask::.*`).ReplaceAllString(fmt.Sprintf("%t", evaluated), "::add-mask::***)")
|
|
|
|
logger.Debugf("expression '%s' evaluated to '%s'", in, printable)
|
|
|
|
|
2022-01-21 10:07:20 -06:00
|
|
|
return evaluated, err
|
|
|
|
}
|
2021-12-08 14:57:42 -06:00
|
|
|
|
2023-05-03 12:26:28 -05:00
|
|
|
func (ee expressionEvaluator) evaluateScalarYamlNode(ctx context.Context, node *yaml.Node) (*yaml.Node, error) {
|
2022-02-15 10:35:02 -06:00
|
|
|
var in string
|
|
|
|
if err := node.Decode(&in); err != nil {
|
2023-05-03 12:26:28 -05:00
|
|
|
return nil, err
|
2022-02-15 10:35:02 -06:00
|
|
|
}
|
|
|
|
if !strings.Contains(in, "${{") || !strings.Contains(in, "}}") {
|
2023-05-03 12:26:28 -05:00
|
|
|
return nil, nil
|
2022-02-15 10:35:02 -06:00
|
|
|
}
|
2022-06-17 10:55:21 -05:00
|
|
|
expr, _ := rewriteSubExpression(ctx, in, false)
|
|
|
|
res, err := ee.evaluate(ctx, expr, exprparser.DefaultStatusCheckNone)
|
2022-02-15 10:35:02 -06:00
|
|
|
if err != nil {
|
2023-05-03 12:26:28 -05:00
|
|
|
return nil, err
|
2022-02-15 10:35:02 -06:00
|
|
|
}
|
2023-05-03 12:26:28 -05:00
|
|
|
ret := &yaml.Node{}
|
|
|
|
if err := ret.Encode(res); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ret, err
|
2022-02-15 10:35:02 -06:00
|
|
|
}
|
|
|
|
|
2023-05-03 12:26:28 -05:00
|
|
|
func (ee expressionEvaluator) evaluateMappingYamlNode(ctx context.Context, node *yaml.Node) (*yaml.Node, error) {
|
|
|
|
var ret *yaml.Node = nil
|
2022-02-15 10:35:02 -06:00
|
|
|
// GitHub has this undocumented feature to merge maps, called insert directive
|
|
|
|
insertDirective := regexp.MustCompile(`\${{\s*insert\s*}}`)
|
2023-05-03 12:26:28 -05:00
|
|
|
for i := 0; i < len(node.Content)/2; i++ {
|
|
|
|
changed := func() error {
|
|
|
|
if ret == nil {
|
|
|
|
ret = &yaml.Node{}
|
|
|
|
if err := ret.Encode(node); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ret.Content = ret.Content[:i*2]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-02-15 10:35:02 -06:00
|
|
|
k := node.Content[i*2]
|
|
|
|
v := node.Content[i*2+1]
|
2023-05-03 12:26:28 -05:00
|
|
|
ev, err := ee.evaluateYamlNodeInternal(ctx, v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if ev != nil {
|
|
|
|
if err := changed(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ev = v
|
2022-02-15 10:35:02 -06:00
|
|
|
}
|
|
|
|
var sk string
|
|
|
|
// Merge the nested map of the insert directive
|
|
|
|
if k.Decode(&sk) == nil && insertDirective.MatchString(sk) {
|
2023-05-03 12:26:28 -05:00
|
|
|
if ev.Kind != yaml.MappingNode {
|
|
|
|
return nil, fmt.Errorf("failed to insert node %v into mapping %v unexpected type %v expected MappingNode", ev, node, ev.Kind)
|
|
|
|
}
|
|
|
|
if err := changed(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret.Content = append(ret.Content, ev.Content...)
|
2022-02-15 10:35:02 -06:00
|
|
|
} else {
|
2023-05-03 12:26:28 -05:00
|
|
|
ek, err := ee.evaluateYamlNodeInternal(ctx, k)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if ek != nil {
|
|
|
|
if err := changed(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ek = k
|
|
|
|
}
|
|
|
|
if ret != nil {
|
|
|
|
ret.Content = append(ret.Content, ek, ev)
|
2022-02-15 10:35:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-03 12:26:28 -05:00
|
|
|
return ret, nil
|
2022-02-15 10:35:02 -06:00
|
|
|
}
|
|
|
|
|
2023-05-03 12:26:28 -05:00
|
|
|
func (ee expressionEvaluator) evaluateSequenceYamlNode(ctx context.Context, node *yaml.Node) (*yaml.Node, error) {
|
|
|
|
var ret *yaml.Node = nil
|
|
|
|
for i := 0; i < len(node.Content); i++ {
|
2022-02-15 10:35:02 -06:00
|
|
|
v := node.Content[i]
|
|
|
|
// Preserve nested sequences
|
|
|
|
wasseq := v.Kind == yaml.SequenceNode
|
2023-05-03 12:26:28 -05:00
|
|
|
ev, err := ee.evaluateYamlNodeInternal(ctx, v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-02-15 10:35:02 -06:00
|
|
|
}
|
2023-05-03 12:26:28 -05:00
|
|
|
if ev != nil {
|
|
|
|
if ret == nil {
|
|
|
|
ret = &yaml.Node{}
|
|
|
|
if err := ret.Encode(node); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret.Content = ret.Content[:i]
|
|
|
|
}
|
|
|
|
// GitHub has this undocumented feature to merge sequences / arrays
|
|
|
|
// We have a nested sequence via evaluation, merge the arrays
|
|
|
|
if ev.Kind == yaml.SequenceNode && !wasseq {
|
|
|
|
ret.Content = append(ret.Content, ev.Content...)
|
|
|
|
} else {
|
|
|
|
ret.Content = append(ret.Content, ev)
|
|
|
|
}
|
|
|
|
} else if ret != nil {
|
|
|
|
ret.Content = append(ret.Content, v)
|
2022-02-15 10:35:02 -06:00
|
|
|
}
|
|
|
|
}
|
2023-05-03 12:26:28 -05:00
|
|
|
return ret, nil
|
2022-02-15 10:35:02 -06:00
|
|
|
}
|
|
|
|
|
2023-05-03 12:26:28 -05:00
|
|
|
func (ee expressionEvaluator) evaluateYamlNodeInternal(ctx context.Context, node *yaml.Node) (*yaml.Node, error) {
|
2022-02-15 10:35:02 -06:00
|
|
|
switch node.Kind {
|
|
|
|
case yaml.ScalarNode:
|
2022-06-17 10:55:21 -05:00
|
|
|
return ee.evaluateScalarYamlNode(ctx, node)
|
2022-02-15 10:35:02 -06:00
|
|
|
case yaml.MappingNode:
|
2022-06-17 10:55:21 -05:00
|
|
|
return ee.evaluateMappingYamlNode(ctx, node)
|
2022-02-15 10:35:02 -06:00
|
|
|
case yaml.SequenceNode:
|
2022-06-17 10:55:21 -05:00
|
|
|
return ee.evaluateSequenceYamlNode(ctx, node)
|
2022-02-15 10:35:02 -06:00
|
|
|
default:
|
2023-05-03 12:26:28 -05:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ee expressionEvaluator) EvaluateYamlNode(ctx context.Context, node *yaml.Node) error {
|
|
|
|
ret, err := ee.evaluateYamlNodeInternal(ctx, node)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ret != nil {
|
|
|
|
return ret.Decode(node)
|
2022-02-15 10:35:02 -06:00
|
|
|
}
|
2023-05-03 12:26:28 -05:00
|
|
|
return nil
|
2022-02-15 10:35:02 -06:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
func (ee expressionEvaluator) Interpolate(ctx context.Context, in string) string {
|
2022-01-21 10:07:20 -06:00
|
|
|
if !strings.Contains(in, "${{") || !strings.Contains(in, "}}") {
|
|
|
|
return in
|
2021-12-08 14:57:42 -06:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
expr, _ := rewriteSubExpression(ctx, in, true)
|
|
|
|
evaluated, err := ee.evaluate(ctx, expr, exprparser.DefaultStatusCheckNone)
|
2022-01-21 10:07:20 -06:00
|
|
|
if err != nil {
|
2022-06-17 10:55:21 -05:00
|
|
|
common.Logger(ctx).Errorf("Unable to interpolate expression '%s': %s", expr, err)
|
2022-01-21 10:07:20 -06:00
|
|
|
return ""
|
2020-02-13 13:47:38 -06:00
|
|
|
}
|
|
|
|
|
2022-01-21 10:07:20 -06:00
|
|
|
value, ok := evaluated.(string)
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("Expression %s did not evaluate to a string", expr))
|
2021-11-27 11:55:26 -06:00
|
|
|
}
|
2020-02-13 13:47:38 -06:00
|
|
|
|
2022-01-21 10:07:20 -06:00
|
|
|
return value
|
2020-02-13 13:47:38 -06:00
|
|
|
}
|
|
|
|
|
2022-01-21 10:07:20 -06:00
|
|
|
// EvalBool evaluates an expression against given evaluator
|
2022-06-17 10:55:21 -05:00
|
|
|
func EvalBool(ctx context.Context, evaluator ExpressionEvaluator, expr string, defaultStatusCheck exprparser.DefaultStatusCheck) (bool, error) {
|
|
|
|
nextExpr, _ := rewriteSubExpression(ctx, expr, false)
|
2020-02-14 02:41:20 -06:00
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
evaluated, err := evaluator.evaluate(ctx, nextExpr, defaultStatusCheck)
|
2022-01-21 10:07:20 -06:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2022-02-25 12:39:50 -06:00
|
|
|
return exprparser.IsTruthy(evaluated), nil
|
2020-02-14 02:41:20 -06:00
|
|
|
}
|
|
|
|
|
2022-01-21 10:07:20 -06:00
|
|
|
func escapeFormatString(in string) string {
|
|
|
|
return strings.ReplaceAll(strings.ReplaceAll(in, "{", "{{"), "}", "}}")
|
2020-02-14 02:41:20 -06:00
|
|
|
}
|
2021-12-08 14:57:42 -06:00
|
|
|
|
2022-01-21 10:07:20 -06:00
|
|
|
//nolint:gocyclo
|
2022-06-17 10:55:21 -05:00
|
|
|
func rewriteSubExpression(ctx context.Context, in string, forceFormat bool) (string, error) {
|
2022-01-21 10:07:20 -06:00
|
|
|
if !strings.Contains(in, "${{") || !strings.Contains(in, "}}") {
|
|
|
|
return in, nil
|
2021-12-08 14:57:42 -06:00
|
|
|
}
|
2022-01-21 10:07:20 -06:00
|
|
|
|
|
|
|
strPattern := regexp.MustCompile("(?:''|[^'])*'")
|
|
|
|
pos := 0
|
|
|
|
exprStart := -1
|
|
|
|
strStart := -1
|
|
|
|
var results []string
|
|
|
|
formatOut := ""
|
|
|
|
for pos < len(in) {
|
|
|
|
if strStart > -1 {
|
|
|
|
matches := strPattern.FindStringIndex(in[pos:])
|
|
|
|
if matches == nil {
|
|
|
|
panic("unclosed string.")
|
2021-12-08 14:57:42 -06:00
|
|
|
}
|
|
|
|
|
2022-01-21 10:07:20 -06:00
|
|
|
strStart = -1
|
|
|
|
pos += matches[1]
|
|
|
|
} else if exprStart > -1 {
|
|
|
|
exprEnd := strings.Index(in[pos:], "}}")
|
|
|
|
strStart = strings.Index(in[pos:], "'")
|
|
|
|
|
|
|
|
if exprEnd > -1 && strStart > -1 {
|
|
|
|
if exprEnd < strStart {
|
|
|
|
strStart = -1
|
|
|
|
} else {
|
|
|
|
exprEnd = -1
|
|
|
|
}
|
2021-12-08 14:57:42 -06:00
|
|
|
}
|
|
|
|
|
2022-01-21 10:07:20 -06:00
|
|
|
if exprEnd > -1 {
|
|
|
|
formatOut += fmt.Sprintf("{%d}", len(results))
|
|
|
|
results = append(results, strings.TrimSpace(in[exprStart:pos+exprEnd]))
|
|
|
|
pos += exprEnd + 2
|
|
|
|
exprStart = -1
|
|
|
|
} else if strStart > -1 {
|
|
|
|
pos += strStart + 1
|
|
|
|
} else {
|
|
|
|
panic("unclosed expression.")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
exprStart = strings.Index(in[pos:], "${{")
|
|
|
|
if exprStart != -1 {
|
|
|
|
formatOut += escapeFormatString(in[pos : pos+exprStart])
|
|
|
|
exprStart = pos + exprStart + 3
|
|
|
|
pos = exprStart
|
|
|
|
} else {
|
|
|
|
formatOut += escapeFormatString(in[pos:])
|
|
|
|
pos = len(in)
|
|
|
|
}
|
2021-12-08 14:57:42 -06:00
|
|
|
}
|
2022-01-21 10:07:20 -06:00
|
|
|
}
|
2021-12-08 14:57:42 -06:00
|
|
|
|
2022-01-21 10:07:20 -06:00
|
|
|
if len(results) == 1 && formatOut == "{0}" && !forceFormat {
|
|
|
|
return in, nil
|
2021-12-08 14:57:42 -06:00
|
|
|
}
|
2022-01-21 10:07:20 -06:00
|
|
|
|
2022-02-25 12:39:50 -06:00
|
|
|
out := fmt.Sprintf("format('%s', %s)", strings.ReplaceAll(formatOut, "'", "''"), strings.Join(results, ", "))
|
|
|
|
if in != out {
|
2022-06-17 10:55:21 -05:00
|
|
|
common.Logger(ctx).Debugf("expression '%s' rewritten to '%s'", in, out)
|
2022-02-25 12:39:50 -06:00
|
|
|
}
|
|
|
|
return out, nil
|
2021-12-08 14:57:42 -06:00
|
|
|
}
|
2022-10-17 11:25:26 -05:00
|
|
|
|
|
|
|
func getEvaluatorInputs(ctx context.Context, rc *RunContext, step step, ghc *model.GithubContext) map[string]interface{} {
|
|
|
|
inputs := map[string]interface{}{}
|
|
|
|
|
2022-12-15 10:45:22 -06:00
|
|
|
setupWorkflowInputs(ctx, &inputs, rc)
|
|
|
|
|
2022-10-17 11:25:26 -05:00
|
|
|
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()
|
2022-11-10 14:16:00 -06:00
|
|
|
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
|
|
|
|
}
|
2022-10-17 11:25:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return inputs
|
|
|
|
}
|
2022-12-15 10:45:22 -06:00
|
|
|
|
|
|
|
func setupWorkflowInputs(ctx context.Context, inputs *map[string]interface{}, rc *RunContext) {
|
|
|
|
if rc.caller != nil {
|
|
|
|
config := rc.Run.Workflow.WorkflowCallConfig()
|
|
|
|
|
|
|
|
for name, input := range config.Inputs {
|
|
|
|
value := rc.caller.runContext.Run.Job().With[name]
|
|
|
|
if value != nil {
|
|
|
|
if str, ok := value.(string); ok {
|
|
|
|
// evaluate using the calling RunContext (outside)
|
|
|
|
value = rc.caller.runContext.ExprEval.Interpolate(ctx, str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if value == nil && config != nil && config.Inputs != nil {
|
|
|
|
value = input.Default
|
|
|
|
if rc.ExprEval != nil {
|
|
|
|
if str, ok := value.(string); ok {
|
|
|
|
// evaluate using the called RunContext (inside)
|
|
|
|
value = rc.ExprEval.Interpolate(ctx, str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(*inputs)[name] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getWorkflowSecrets(ctx context.Context, rc *RunContext) map[string]string {
|
|
|
|
if rc.caller != nil {
|
|
|
|
job := rc.caller.runContext.Run.Job()
|
|
|
|
secrets := job.Secrets()
|
|
|
|
|
|
|
|
if secrets == nil && job.InheritSecrets() {
|
|
|
|
secrets = rc.caller.runContext.Config.Secrets
|
|
|
|
}
|
|
|
|
|
|
|
|
if secrets == nil {
|
|
|
|
secrets = map[string]string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range secrets {
|
|
|
|
secrets[k] = rc.caller.runContext.ExprEval.Interpolate(ctx, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return secrets
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc.Config.Secrets
|
|
|
|
}
|
2023-06-12 01:54:17 -05:00
|
|
|
|
|
|
|
func getWorkflowVars(ctx context.Context, rc *RunContext) map[string]string {
|
|
|
|
return rc.Config.Vars
|
|
|
|
}
|