From 035293c52ea6408ab5d16a1e427dcae89fd2d2a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Brauer?= Date: Mon, 29 Nov 2021 16:37:36 +0000 Subject: [PATCH] wip: integrate exprparser into act Co-authored-by: Markus Wolf Co-authored-by: Philipp Hinrichsen --- go.mod | 2 - go.sum | 4 - pkg/runner/expression.go | 742 ++++++++-------------------------- pkg/runner/expression_test.go | 186 ++++++--- pkg/runner/run_context.go | 19 +- 5 files changed, 300 insertions(+), 653 deletions(-) diff --git a/go.mod b/go.mod index 02ceb4b863c..e97adb48228 100644 --- a/go.mod +++ b/go.mod @@ -31,7 +31,6 @@ require ( github.com/opencontainers/selinux v1.10.0 github.com/pkg/errors v0.9.1 github.com/rhysd/actionlint v1.6.8 - github.com/robertkrimen/otto v0.0.0-20210614181706-373ff5438452 github.com/sabhiram/go-gitignore v0.0.0-20201211210132-54b8a0bf510f github.com/sergi/go-diff v1.2.0 // indirect github.com/sirupsen/logrus v1.8.1 @@ -44,6 +43,5 @@ require ( golang.org/x/term v0.0.0-20210916214954-140adaaadfaf golang.org/x/text v0.3.7 // indirect google.golang.org/genproto v0.0.0-20210921142501-181ce0d877f6 // indirect - gopkg.in/sourcemap.v1 v1.0.5 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b ) diff --git a/go.sum b/go.sum index f791e1eacc7..a4018633b42 100644 --- a/go.sum +++ b/go.sum @@ -1037,8 +1037,6 @@ github.com/rhysd/actionlint v1.6.8 h1:li0691FNuuS3da2igfjMb9M58AgMXX7j9U5EgbCZFu github.com/rhysd/actionlint v1.6.8/go.mod h1:0AA4pvZ2nrZHT6D86eUhieH2NFmLqhxrNex0NEa2A2g= github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= -github.com/robertkrimen/otto v0.0.0-20210614181706-373ff5438452 h1:ewTtJ72GFy2e0e8uyiDwMG3pKCS5mBh+hdSTYsPKEP8= -github.com/robertkrimen/otto v0.0.0-20210614181706-373ff5438452/go.mod h1:xvqspoSXJTIpemEonrMDFq6XzwHYYgToXWj5eRX1OtY= github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ= github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= @@ -1782,8 +1780,6 @@ gopkg.in/ini.v1 v1.56.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k= gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= -gopkg.in/sourcemap.v1 v1.0.5 h1:inv58fC9f9J3TK2Y2R1NPntXEn3/wjWHkonhIUODNTI= -gopkg.in/sourcemap.v1 v1.0.5/go.mod h1:2RlvNNSMglmRrcvhfuzp4hQHwOtjxlbjX7UPY/GXb78= gopkg.in/square/go-jose.v2 v2.2.2/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI= gopkg.in/square/go-jose.v2 v2.3.1/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI= gopkg.in/square/go-jose.v2 v2.5.1/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI= diff --git a/pkg/runner/expression.go b/pkg/runner/expression.go index ac024552056..25c7a58bd47 100644 --- a/pkg/runner/expression.go +++ b/pkg/runner/expression.go @@ -1,55 +1,100 @@ package runner import ( - "crypto/sha256" - "encoding/hex" - "encoding/json" - "errors" "fmt" - "io" - "os" - "path/filepath" - "regexp" - "strconv" + "reflect" "strings" - "github.com/robertkrimen/otto" + "github.com/nektos/act/pkg/exprparser" log "github.com/sirupsen/logrus" ) -var expressionPattern, operatorPattern *regexp.Regexp - -func init() { - expressionPattern = regexp.MustCompile(`\${{\s*(.+?)\s*}}`) - operatorPattern = regexp.MustCompile("^[!=><|&]+$") -} - // NewExpressionEvaluator creates a new evaluator func (rc *RunContext) NewExpressionEvaluator() ExpressionEvaluator { - vm := rc.newVM() + // 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 + } + + jobs := rc.Run.Workflow.Jobs + jobNeeds := rc.Run.Job().Needs() + + using := make(map[string]map[string]map[string]string) + for _, needs := range jobNeeds { + using[needs] = map[string]map[string]string{ + "outputs": jobs[needs].Outputs, + } + } - return &expressionEvaluator{ - vm, + ee := &exprparser.EvaluationEnvironment{ + Github: rc.getGithubContext(), + Env: rc.Env, + Job: rc.getJobContext(), + // Steps: not steps on run context, + Runner: map[string]interface{}{ + "os": "Linux", + "temp": "/tmp", + "tool_cache": "/opt/hostedtoolcache", + }, + Secrets: rc.Config.Secrets, + Strategy: strategy, + Matrix: rc.Matrix, + Needs: using, + // Inputs: no inputs on run context, + } + return expressionEvaluator{ + interpreter: exprparser.NewInterpeter(ee, exprparser.Config{ + Run: rc.Run, + WorkingDir: rc.Config.Workdir, + }), } } // NewExpressionEvaluator creates a new evaluator func (sc *StepContext) NewExpressionEvaluator() ExpressionEvaluator { - vm := sc.RunContext.newVM() - configers := []func(*otto.Otto){ - sc.vmEnv(), - sc.vmInputs(), - - sc.vmNeeds(), - sc.vmSuccess(), - sc.vmFailure(), + rc := sc.RunContext + // 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 } - for _, configer := range configers { - configer(vm) + + jobs := rc.Run.Workflow.Jobs + jobNeeds := rc.Run.Job().Needs() + + using := make(map[string]map[string]map[string]string) + for _, needs := range jobNeeds { + using[needs] = map[string]map[string]string{ + "outputs": jobs[needs].Outputs, + } } - return &expressionEvaluator{ - vm, + ee := &exprparser.EvaluationEnvironment{ + Github: rc.getGithubContext(), + Env: rc.Env, + Job: rc.getJobContext(), + Steps: rc.getStepsContext(), + Runner: map[string]interface{}{ + "os": "Linux", + "temp": "/tmp", + "tool_cache": "/opt/hostedtoolcache", + }, + Secrets: rc.Config.Secrets, + Strategy: strategy, + Matrix: rc.Matrix, + Needs: using, + // Inputs: no inputs on run context, + } + return expressionEvaluator{ + interpreter: exprparser.NewInterpeter(ee, exprparser.Config{ + Run: rc.Run, + WorkingDir: rc.Config.Workdir, + }), } } @@ -58,598 +103,155 @@ type ExpressionEvaluator interface { Evaluate(string) (string, bool, error) Interpolate(string) string InterpolateWithStringCheck(string) (string, bool) - Rewrite(string) string } type expressionEvaluator struct { - vm *otto.Otto + interpreter exprparser.Interpreter } -func (ee *expressionEvaluator) Evaluate(in string) (string, bool, error) { - if strings.HasPrefix(in, `secrets.`) { - in = `secrets.` + strings.ToUpper(strings.SplitN(in, `.`, 2)[1]) - } - re := ee.Rewrite(in) - if re != in { - log.Debugf("Evaluating '%s' instead of '%s'", re, in) - } - - val, err := ee.vm.Run(re) - if err != nil { - return "", false, err - } - if val.IsNull() || val.IsUndefined() { - return "", false, nil - } - valAsString, err := val.ToString() +func (ee expressionEvaluator) Evaluate(in string) (string, bool, error) { + evaluated, _, err := ee.interpreter.Evaluate(in) if err != nil { return "", false, err } - return valAsString, val.IsString(), err + return fmt.Sprint(evaluated), false, nil } -func (ee *expressionEvaluator) Interpolate(in string) string { - interpolated, _ := ee.InterpolateWithStringCheck(in) - return interpolated +func (ee expressionEvaluator) Interpolate(in string) string { + str, _ := ee.InterpolateWithStringCheck(in) + return str } -func (ee *expressionEvaluator) InterpolateWithStringCheck(in string) (string, bool) { - errList := make([]error, 0) - - out := in - isString := false - for { - out = expressionPattern.ReplaceAllStringFunc(in, func(match string) string { - // Extract and trim the actual expression inside ${{...}} delimiters - expression := expressionPattern.ReplaceAllString(match, "$1") +type interpolateState int - // Evaluate the expression and retrieve errors if any - evaluated, evaluatedIsString, err := ee.Evaluate(expression) - if err != nil { - errList = append(errList, err) - } - isString = evaluatedIsString - return evaluated - }) - if len(errList) > 0 { - log.Errorf("Unable to interpolate string '%s' - %v", in, errList) - break - } - if out == in { - // No replacement occurred, we're done! - break - } - in = out - } - return out, isString -} - -// Rewrite tries to transform any javascript property accessor into its bracket notation. -// For instance, "object.property" would become "object['property']". -func (ee *expressionEvaluator) Rewrite(in string) string { - var buf strings.Builder - r := strings.NewReader(in) - for { - c, _, err := r.ReadRune() - if err == io.EOF { - break - } - //nolint - switch { - default: - buf.WriteRune(c) - case c == '\'': - buf.WriteRune(c) - ee.advString(&buf, r) - case c == '.': - buf.WriteString("['") - ee.advPropertyName(&buf, r) - buf.WriteString("']") - } - } - return buf.String() -} +const ( + passThrough interpolateState = iota + expressionStartDollar + expressionStartBracket1 + expressionStartBracket2 + expressionEndBracket1 + expressionEndBracket2 +) -func (*expressionEvaluator) advString(w *strings.Builder, r *strings.Reader) error { - for { - c, _, err := r.ReadRune() - if err != nil { - return err - } - if c != '\'' { - w.WriteRune(c) //nolint - continue - } +func (ee expressionEvaluator) InterpolateWithStringCheck(in string) (string, bool) { + output := "" + skip := 0 + // replacementIndex := "" - // Handles a escaped string: ex. 'It''s ok' - c, _, err = r.ReadRune() - if err != nil { - w.WriteString("'") //nolint - return err - } - if c != '\'' { - w.WriteString("'") //nolint - if err := r.UnreadRune(); err != nil { - return err - } - break - } - w.WriteString(`\'`) //nolint - } - return nil -} + state := passThrough + for i, character := range in { + switch state { + case passThrough: + switch character { + case '$': + state = expressionStartDollar -func (*expressionEvaluator) advPropertyName(w *strings.Builder, r *strings.Reader) error { - for { - c, _, err := r.ReadRune() - if err != nil { - return err - } - if !isLetter(c) { - if err := r.UnreadRune(); err != nil { - return err + default: + output += string(character) } - break - } - w.WriteRune(c) //nolint - } - return nil -} - -func isLetter(c rune) bool { - switch { - case c >= 'a' && c <= 'z': - return true - case c >= 'A' && c <= 'Z': - return true - case c >= '0' && c <= '9': - return true - case c == '_' || c == '-': - return true - default: - return false - } -} -func (rc *RunContext) newVM() *otto.Otto { - configers := []func(*otto.Otto){ - vmContains, - vmStartsWith, - vmEndsWith, - vmFormat, - vmJoin, - vmToJSON, - vmFromJSON, - vmAlways, - rc.vmCancelled(), - rc.vmSuccess(), - rc.vmFailure(), - rc.vmHashFiles(), - - rc.vmGithub(), - rc.vmJob(), - rc.vmSteps(), - rc.vmRunner(), - - rc.vmSecrets(), - rc.vmStrategy(), - rc.vmMatrix(), - rc.vmEnv(), - rc.vmNeeds(), - } - vm := otto.New() - for _, configer := range configers { - configer(vm) - } - return vm -} + case expressionStartDollar: + switch character { + case '{': + state = expressionStartBracket1 -func vmContains(vm *otto.Otto) { - _ = vm.Set("contains", func(searchString interface{}, searchValue string) bool { - if searchStringString, ok := searchString.(string); ok { - return strings.Contains(strings.ToLower(searchStringString), strings.ToLower(searchValue)) - } else if searchStringArray, ok := searchString.([]string); ok { - for _, s := range searchStringArray { - if strings.EqualFold(s, searchValue) { - return true - } + default: + output += "$" + output += string(character) + state = passThrough } - } - return false - }) -} - -func vmStartsWith(vm *otto.Otto) { - _ = vm.Set("startsWith", func(searchString string, searchValue string) bool { - return strings.HasPrefix(strings.ToLower(searchString), strings.ToLower(searchValue)) - }) -} -func vmEndsWith(vm *otto.Otto) { - _ = vm.Set("endsWith", func(searchString string, searchValue string) bool { - return strings.HasSuffix(strings.ToLower(searchString), strings.ToLower(searchValue)) - }) -} + case expressionStartBracket1: + switch character { + case '{': + state = expressionStartBracket2 -func vmFormat(vm *otto.Otto) { - _ = vm.Set("format", func(s string, vals ...otto.Value) string { - ex := regexp.MustCompile(`(\{[0-9]+\}|\{.?|\}.?)`) - return ex.ReplaceAllStringFunc(s, func(seg string) string { - switch seg { - case "{{": - return "{" - case "}}": - return "}" default: - if len(seg) < 3 || !strings.HasPrefix(seg, "{") { - log.Errorf("The following format string is invalid: '%v'", s) - return "" - } - _i := seg[1 : len(seg)-1] - i, err := strconv.ParseInt(_i, 10, 32) - if err != nil { - log.Errorf("The following format string is invalid: '%v'. Error: %v", s, err) - return "" - } - if i >= int64(len(vals)) { - log.Errorf("The following format string references more arguments than were supplied: '%v'", s) - return "" - } - if vals[i].IsNull() || vals[i].IsUndefined() { - return "" - } - return vals[i].String() + output += "${" + output += string(character) + state = passThrough } - }) - }) -} - -func vmJoin(vm *otto.Otto) { - _ = vm.Set("join", func(element interface{}, optionalElem string) string { - slist := make([]string, 0) - if elementString, ok := element.(string); ok { - slist = append(slist, elementString) - } else if elementArray, ok := element.([]string); ok { - slist = append(slist, elementArray...) - } - if optionalElem != "" { - slist = append(slist, optionalElem) - } - return strings.Join(slist, " ") - }) -} - -func vmToJSON(vm *otto.Otto) { - toJSON := func(o interface{}) string { - rtn, err := json.MarshalIndent(o, "", " ") - if err != nil { - log.Errorf("Unable to marshal: %v", err) - return "" - } - return string(rtn) - } - _ = vm.Set("toJSON", toJSON) - _ = vm.Set("toJson", toJSON) -} - -func vmFromJSON(vm *otto.Otto) { - fromJSON := func(str string) interface{} { - var dat interface{} - err := json.Unmarshal([]byte(str), &dat) - if err != nil { - log.Errorf("Unable to unmarshal: %v", err) - return dat - } - return dat - } - _ = vm.Set("fromJSON", fromJSON) - _ = vm.Set("fromJson", fromJSON) -} -func (rc *RunContext) vmHashFiles() func(*otto.Otto) { - return func(vm *otto.Otto) { - _ = vm.Set("hashFiles", func(paths ...string) string { - var files []string - for i := range paths { - newFiles, err := filepath.Glob(filepath.Join(rc.Config.Workdir, paths[i])) + case expressionStartBracket2: + if skip == 0 { + result, pos, err := ee.interpreter.Evaluate(in[i:]) if err != nil { - log.Errorf("Unable to glob.Glob: %v", err) - return "" + return "", false } - files = append(files, newFiles...) - } - hasher := sha256.New() - for _, file := range files { - f, err := os.Open(file) - if err != nil { - log.Errorf("Unable to os.Open: %v", err) - } - if _, err := io.Copy(hasher, f); err != nil { - log.Errorf("Unable to io.Copy: %v", err) - } - if err := f.Close(); err != nil { - log.Errorf("Unable to Close file: %v", err) - } - } - return hex.EncodeToString(hasher.Sum(nil)) - }) - } -} -func (rc *RunContext) vmSuccess() func(*otto.Otto) { - return func(vm *otto.Otto) { - _ = vm.Set("success", func() bool { - jobs := rc.Run.Workflow.Jobs - jobNeeds := rc.getNeedsTransitive(rc.Run.Job()) + output += ee.toString(result) - for _, needs := range jobNeeds { - if jobs[needs].Result != "success" { - return false + if (pos - 1) == 0 { + state = expressionEndBracket1 + } else { + skip = i + pos - 1 } + } else if i < skip { + // ignore + } else { + state = expressionEndBracket1 + skip = 0 } - return true - }) - } -} - -func (rc *RunContext) vmFailure() func(*otto.Otto) { - return func(vm *otto.Otto) { - _ = vm.Set("failure", func() bool { - jobs := rc.Run.Workflow.Jobs - jobNeeds := rc.getNeedsTransitive(rc.Run.Job()) - - for _, needs := range jobNeeds { - if jobs[needs].Result == "failure" { - return true - } + case expressionEndBracket1: + // todo: handle error + switch character { + case '}': + state = expressionEndBracket2 + } + case expressionEndBracket2: + // todo: handle error + switch character { + case '}': + state = passThrough } - - return false - }) - } -} - -func vmAlways(vm *otto.Otto) { - _ = vm.Set("always", func() bool { - return true - }) -} -func (rc *RunContext) vmCancelled() func(vm *otto.Otto) { - return func(vm *otto.Otto) { - _ = vm.Set("cancelled", func() bool { - return rc.getJobContext().Status == "cancelled" - }) - } -} - -func (rc *RunContext) vmGithub() func(*otto.Otto) { - github := rc.getGithubContext() - - return func(vm *otto.Otto) { - _ = vm.Set("github", github) - } -} - -func (rc *RunContext) vmEnv() func(*otto.Otto) { - return func(vm *otto.Otto) { - env := rc.GetEnv() - log.Debugf("context env => %v", env) - _ = vm.Set("env", env) - } -} - -func (sc *StepContext) vmEnv() func(*otto.Otto) { - return func(vm *otto.Otto) { - log.Debugf("context env => %v", sc.Env) - _ = vm.Set("env", sc.Env) - } -} - -func (sc *StepContext) vmInputs() func(*otto.Otto) { - inputs := make(map[string]string) - - // Set Defaults - if sc.Action != nil { - for k, input := range sc.Action.Inputs { - inputs[k] = sc.RunContext.NewExpressionEvaluator().Interpolate(input.Default) - } - } - - for k, v := range sc.Step.With { - inputs[k] = sc.RunContext.NewExpressionEvaluator().Interpolate(v) - } - - return func(vm *otto.Otto) { - _ = vm.Set("inputs", inputs) - } -} - -func (sc *StepContext) vmNeeds() func(*otto.Otto) { - jobs := sc.RunContext.Run.Workflow.Jobs - jobNeeds := sc.RunContext.Run.Job().Needs() - - using := make(map[string]map[string]map[string]string) - for _, needs := range jobNeeds { - using[needs] = map[string]map[string]string{ - "outputs": jobs[needs].Outputs, } } - return func(vm *otto.Otto) { - log.Debugf("context needs => %v", using) - _ = vm.Set("needs", using) - } -} - -func (sc *StepContext) vmSuccess() func(*otto.Otto) { - return func(vm *otto.Otto) { - _ = vm.Set("success", func() bool { - return sc.RunContext.getJobContext().Status == "success" - }) - } -} - -func (sc *StepContext) vmFailure() func(*otto.Otto) { - return func(vm *otto.Otto) { - _ = vm.Set("failure", func() bool { - return sc.RunContext.getJobContext().Status == "failure" - }) - } -} - -type vmNeedsStruct struct { - Outputs map[string]string `json:"outputs"` - Result string `json:"result"` -} - -func (rc *RunContext) vmNeeds() func(*otto.Otto) { - return func(vm *otto.Otto) { - needsFunc := func() otto.Value { - jobs := rc.Run.Workflow.Jobs - jobNeeds := rc.Run.Job().Needs() - - using := make(map[string]vmNeedsStruct) - for _, needs := range jobNeeds { - using[needs] = vmNeedsStruct{ - Outputs: jobs[needs].Outputs, - Result: jobs[needs].Result, - } - } - - log.Debugf("context needs => %+v", using) - - value, err := vm.ToValue(using) - if err != nil { - return vm.MakeTypeError(err.Error()) - } - - return value + if state != passThrough { + switch state { + case expressionStartDollar, expressionStartBracket1, expressionStartBracket2, expressionEndBracket1, expressionEndBracket2: + panic("unexpected state while parsing expression") } - - // Results might change after the Otto VM was created - // and initialized. To access the current state - // we can't just pass a copy to Otto - instead we - // created a 'live-binding'. - // Technical Note: We don't want to pollute the global - // js namespace (and add things github actions hasn't) - // we delete the helper function after installing it - // as a getter. - global, _ := vm.Run("this") - _ = global.Object().Set("__needs__", needsFunc) - _, _ = vm.Run(` - (function (global) { - Object.defineProperty(global, 'needs', { get: global.__needs__ }); - delete global.__needs__; - })(this) - `) } -} -func (rc *RunContext) vmJob() func(*otto.Otto) { - job := rc.getJobContext() - - return func(vm *otto.Otto) { - _ = vm.Set("job", job) - } + return output, true } -func (rc *RunContext) vmSteps() func(*otto.Otto) { - ctxSteps := rc.getStepsContext() +func (ee expressionEvaluator) toString(in interface{}) string { + value := reflect.ValueOf(in) - steps := make(map[string]interface{}) - for id, ctxStep := range ctxSteps { - steps[id] = map[string]interface{}{ - "conclusion": ctxStep.Conclusion.String(), - "outcome": ctxStep.Outcome.String(), - "outputs": ctxStep.Outputs, - } - } + switch value.Kind() { + case reflect.String: + return value.String() - return func(vm *otto.Otto) { - log.Debugf("context steps => %v", steps) - _ = vm.Set("steps", steps) - } -} + case reflect.Int: + return fmt.Sprint(value.Int()) -func (rc *RunContext) vmRunner() func(*otto.Otto) { - runner := map[string]interface{}{ - "os": "Linux", - "temp": "/tmp", - "tool_cache": "/opt/hostedtoolcache", - } + case reflect.Float64: + return fmt.Sprint(value.Float()) - return func(vm *otto.Otto) { - _ = vm.Set("runner", runner) - } -} + case reflect.Bool: + return fmt.Sprint(value.Bool()) -func (rc *RunContext) vmSecrets() func(*otto.Otto) { - return func(vm *otto.Otto) { - _ = vm.Set("secrets", rc.Config.Secrets) - } -} + case reflect.Invalid: + return "null" -func (rc *RunContext) vmStrategy() func(*otto.Otto) { - 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 - } - return func(vm *otto.Otto) { - _ = vm.Set("strategy", strategy) - } -} - -func (rc *RunContext) vmMatrix() func(*otto.Otto) { - return func(vm *otto.Otto) { - _ = vm.Set("matrix", rc.Matrix) + default: + panic(fmt.Sprintf("unimplemented %s %+v", value.Kind(), in)) } } // EvalBool evaluates an expression against given evaluator func EvalBool(evaluator ExpressionEvaluator, expr string) (bool, error) { - if splitPattern == nil { - splitPattern = regexp.MustCompile(fmt.Sprintf(`%s|%s|\S+`, expressionPattern.String(), operatorPattern.String())) - } - if strings.HasPrefix(strings.TrimSpace(expr), "!") { - return false, errors.New("expressions starting with ! must be wrapped in ${{ }}") + expr = strings.Replace(expr, "${{", "", -1) + evaluated, _, err := evaluator.Evaluate(expr) + if err != nil { + return false, err } - if expr != "" { - parts := splitPattern.FindAllString(expr, -1) - var evaluatedParts []string - for i, part := range parts { - if operatorPattern.MatchString(part) { - evaluatedParts = append(evaluatedParts, part) - continue - } - interpolatedPart, isString := evaluator.InterpolateWithStringCheck(part) - - // This peculiar transformation has to be done because the GitHub parser - // treats false returned from contexts as a string, not a boolean. - // Hence env.SOMETHING will be evaluated to true in an if: expression - // regardless if SOMETHING is set to false, true or any other string. - // It also handles some other weirdness that I found by trial and error. - if (expressionPattern.MatchString(part) && // it is an expression - !strings.Contains(part, "!")) && // but it's not negated - interpolatedPart == "false" && // and the interpolated string is false - (isString || previousOrNextPartIsAnOperator(i, parts)) { // and it's of type string or has an logical operator before or after - interpolatedPart = fmt.Sprintf("'%s'", interpolatedPart) // then we have to quote the false expression - } - - evaluatedParts = append(evaluatedParts, interpolatedPart) - } + log.Debugf("expression '%s' evaluated to '%s'", expr, evaluated) - joined := strings.Join(evaluatedParts, " ") - v, _, err := evaluator.Evaluate(fmt.Sprintf("Boolean(%s)", joined)) - if err != nil { - return false, err - } - log.Debugf("expression '%s' evaluated to '%s'", expr, v) - return v == "true", nil - } - return true, nil + return evaluated == "true", nil } diff --git a/pkg/runner/expression_test.go b/pkg/runner/expression_test.go index 2d77034078d..f791fa24081 100644 --- a/pkg/runner/expression_test.go +++ b/pkg/runner/expression_test.go @@ -12,7 +12,7 @@ import ( yaml "gopkg.in/yaml.v3" ) -func TestEvaluate(t *testing.T) { +func TestEvaluateRunContext(t *testing.T) { var yml yaml.Node err := yml.Encode(map[string][]interface{}{ "os": {"Linux", "Windows"}, @@ -79,28 +79,29 @@ func TestEvaluate(t *testing.T) { errMesg string }{ {" 1 ", "1", ""}, - {"1 + 3", "4", ""}, - {"(1 + 3) * -2", "-8", ""}, + // {"1 + 3", "4", ""}, + // {"(1 + 3) * -2", "-8", ""}, {"'my text'", "my text", ""}, {"contains('my text', 'te')", "true", ""}, {"contains('my TEXT', 'te')", "true", ""}, - {"contains(['my text'], 'te')", "false", ""}, - {"contains(['foo','bar'], 'bar')", "true", ""}, + {"contains(fromJSON('[\"my text\"]'), 'te')", "false", ""}, + {"contains(fromJSON('[\"foo\",\"bar\"]'), 'bar')", "true", ""}, {"startsWith('hello world', 'He')", "true", ""}, {"endsWith('hello world', 'ld')", "true", ""}, {"format('0:{0} 2:{2} 1:{1}', 'zero', 'one', 'two')", "0:zero 2:two 1:one", ""}, - {"join(['hello'],'octocat')", "hello octocat", ""}, - {"join(['hello','mona','the'],'octocat')", "hello mona the octocat", ""}, - {"join('hello','mona')", "hello mona", ""}, - {"toJSON({'foo':'bar'})", "{\n \"foo\": \"bar\"\n}", ""}, - {"toJson({'foo':'bar'})", "{\n \"foo\": \"bar\"\n}", ""}, + {"join(fromJSON('[\"hello\"]'),'octocat')", "hello", ""}, + {"join(fromJSON('[\"hello\",\"mona\",\"the\"]'),'octocat')", "hellooctocatmonaoctocatthe", ""}, + {"join('hello','mona')", "hello", ""}, + {"toJSON(env)", "{\n \"key\": \"value\"\n}", ""}, + {"toJson(env)", "{\n \"key\": \"value\"\n}", ""}, {"(fromJSON('{\"foo\":\"bar\"}')).foo", "bar", ""}, {"(fromJson('{\"foo\":\"bar\"}')).foo", "bar", ""}, {"(fromJson('[\"foo\",\"bar\"]'))[1]", "bar", ""}, - {"hashFiles('**/non-extant-files')", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", ""}, - {"hashFiles('**/non-extant-files', '**/more-non-extant-files')", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", ""}, - {"hashFiles('**/non.extant.files')", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", ""}, - {"hashFiles('**/non''extant''files')", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", ""}, + // github does return an empty string for non-existent files + {"hashFiles('**/non-extant-files')", "", ""}, + {"hashFiles('**/non-extant-files', '**/more-non-extant-files')", "", ""}, + {"hashFiles('**/non.extant.files')", "", ""}, + {"hashFiles('**/non''extant''files')", "", ""}, {"success()", "true", ""}, {"failure()", "false", ""}, {"always()", "true", ""}, @@ -110,15 +111,6 @@ func TestEvaluate(t *testing.T) { {"github.run_id", "1", ""}, {"github.run_number", "1", ""}, {"job.status", "success", ""}, - {"steps.idwithnothing.conclusion", "success", ""}, - {"steps.idwithnothing.outcome", "failure", ""}, - {"steps.idwithnothing.outputs.foowithnothing", "barwithnothing", ""}, - {"steps.id-with-hyphens.conclusion", "success", ""}, - {"steps.id-with-hyphens.outcome", "failure", ""}, - {"steps.id-with-hyphens.outputs.foo-with-hyphens", "bar-with-hyphens", ""}, - {"steps.id_with_underscores.conclusion", "success", ""}, - {"steps.id_with_underscores.outcome", "failure", ""}, - {"steps.id_with_underscores.outputs.foo_with_underscores", "bar_with_underscores", ""}, {"runner.os", "Linux", ""}, {"matrix.os", "Linux", ""}, {"matrix.foo", "bar", ""}, @@ -151,6 +143,103 @@ func TestEvaluate(t *testing.T) { } } +func TestEvaluateStepContext(t *testing.T) { + var yml yaml.Node + err := yml.Encode(map[string][]interface{}{ + "os": {"Linux", "Windows"}, + "foo": {"bar", "baz"}, + }) + assert.NoError(t, err) + + rc := &RunContext{ + Config: &Config{ + Workdir: ".", + Secrets: map[string]string{ + "CASE_INSENSITIVE_SECRET": "value", + }, + }, + Env: map[string]string{ + "key": "value", + }, + Run: &model.Run{ + JobID: "job1", + Workflow: &model.Workflow{ + Name: "test-workflow", + Jobs: map[string]*model.Job{ + "job1": { + Strategy: &model.Strategy{ + RawMatrix: yml, + }, + }, + }, + }, + }, + Matrix: map[string]interface{}{ + "os": "Linux", + "foo": "bar", + }, + StepResults: map[string]*model.StepResult{ + "idwithnothing": { + Conclusion: model.StepStatusSuccess, + Outcome: model.StepStatusFailure, + Outputs: map[string]string{ + "foowithnothing": "barwithnothing", + }, + }, + "id-with-hyphens": { + Conclusion: model.StepStatusSuccess, + Outcome: model.StepStatusFailure, + Outputs: map[string]string{ + "foo-with-hyphens": "bar-with-hyphens", + }, + }, + "id_with_underscores": { + Conclusion: model.StepStatusSuccess, + Outcome: model.StepStatusFailure, + Outputs: map[string]string{ + "foo_with_underscores": "bar_with_underscores", + }, + }, + }, + } + + sc := &StepContext{ + RunContext: rc, + } + ee := sc.NewExpressionEvaluator() + + tables := []struct { + in string + out string + errMesg string + }{ + {"steps.idwithnothing.conclusion", "success", ""}, + {"steps.idwithnothing.outcome", "failure", ""}, + {"steps.idwithnothing.outputs.foowithnothing", "barwithnothing", ""}, + {"steps.id-with-hyphens.conclusion", "success", ""}, + {"steps.id-with-hyphens.outcome", "failure", ""}, + {"steps.id-with-hyphens.outputs.foo-with-hyphens", "bar-with-hyphens", ""}, + {"steps.id_with_underscores.conclusion", "success", ""}, + {"steps.id_with_underscores.outcome", "failure", ""}, + {"steps.id_with_underscores.outputs.foo_with_underscores", "bar_with_underscores", ""}, + } + + for _, table := range tables { + table := table + t.Run(table.in, func(t *testing.T) { + assertObject := assert.New(t) + out, _, err := ee.Evaluate(table.in) + if table.errMesg == "" { + assertObject.NoError(err, table.in) + assertObject.Equal(table.out, out, table.in) + } else { + assertObject.Error(err, table.in) + assertObject.Equal(table.errMesg, err.Error(), table.in) + } + }) + } +} + func TestInterpolate(t *testing.T) { rc := &RunContext{ Config: &Config{ @@ -181,7 +270,12 @@ func TestInterpolate(t *testing.T) { in string out string }{ - {" ${{1}} to ${{2}} ", " 1 to 2 "}, + {" text ", " text "}, + {" $text ", " $text "}, + {" ${text} ", " ${text} "}, + {" ${{ 1 }} to ${{2}} ", " 1 to 2 "}, + {" ${{ (true || false) }} to ${{2}} ", " true to 2 "}, + {" ${{ (false || '}}' ) }} to ${{2}} ", " }} to 2 "}, {" ${{ env.KEYWITHNOTHING }} ", " valuewithnothing "}, {" ${{ env.KEY-WITH-HYPHENS }} ", " value-with-hyphens "}, {" ${{ env.KEY_WITH_UNDERSCORES }} ", " value_with_underscores "}, @@ -205,12 +299,13 @@ func TestInterpolate(t *testing.T) { {"${{ env.SOMETHING_TRUE || false }}", "true"}, {"${{ env.SOMETHING_FALSE || false }}", "false"}, {"${{ env.SOMETHING_FALSE }} && ${{ env.SOMETHING_TRUE }}", "false && true"}, + {"${{ fromJSON('{}') < 2 }}", "false"}, } updateTestExpressionWorkflow(t, tables, rc) for _, table := range tables { table := table - t.Run(table.in, func(t *testing.T) { + t.Run("interpolate", func(t *testing.T) { assertObject := assert.New(t) out := ee.Interpolate(table.in) assertObject.Equal(table.out, out, table.in) @@ -265,44 +360,3 @@ jobs: t.Fatal(err) } } - -func TestRewrite(t *testing.T) { - rc := &RunContext{ - Config: &Config{}, - Run: &model.Run{ - JobID: "job1", - Workflow: &model.Workflow{ - Jobs: map[string]*model.Job{ - "job1": {}, - }, - }, - }, - } - ee := rc.NewExpressionEvaluator() - - tables := []struct { - in string - re string - }{ - {"ecole", "ecole"}, - {"ecole.centrale", "ecole['centrale']"}, - {"ecole['centrale']", "ecole['centrale']"}, - {"ecole.centrale.paris", "ecole['centrale']['paris']"}, - {"ecole['centrale'].paris", "ecole['centrale']['paris']"}, - {"ecole.centrale['paris']", "ecole['centrale']['paris']"}, - {"ecole['centrale']['paris']", "ecole['centrale']['paris']"}, - {"ecole.centrale-paris", "ecole['centrale-paris']"}, - {"ecole['centrale-paris']", "ecole['centrale-paris']"}, - {"ecole.centrale_paris", "ecole['centrale_paris']"}, - {"ecole['centrale_paris']", "ecole['centrale_paris']"}, - } - - for _, table := range tables { - table := table - t.Run(table.in, func(t *testing.T) { - assertObject := assert.New(t) - re := ee.Rewrite(table.in) - assertObject.Equal(table.re, re, table.in) - }) - } -} diff --git a/pkg/runner/run_context.go b/pkg/runner/run_context.go index c191c2c0398..a5bc71916ac 100644 --- a/pkg/runner/run_context.go +++ b/pkg/runner/run_context.go @@ -25,6 +25,14 @@ import ( const ActPath string = "/var/run/act" +// TODO: remove these patterns and rewrite EvalBool +var expressionPattern, operatorPattern *regexp.Regexp + +func init() { + expressionPattern = regexp.MustCompile(`\${{\s*(.+?)\s*}}`) + operatorPattern = regexp.MustCompile("^[!=><|&]+$") +} + // RunContext contains info about current job type RunContext struct { Name string @@ -470,17 +478,6 @@ func (rc *RunContext) getStepsContext() map[string]*model.StepResult { return rc.StepResults } -func (rc *RunContext) getNeedsTransitive(job *model.Job) []string { - needs := job.Needs() - - for _, need := range needs { - parentNeeds := rc.getNeedsTransitive(rc.Run.Workflow.GetJob(need)) - needs = append(needs, parentNeeds...) - } - - return needs -} - type githubContext struct { Event map[string]interface{} `json:"event"` EventPath string `json:"event_path"`