2020-02-13 01:27:37 -06:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/robertkrimen/otto"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"gopkg.in/godo.v2/glob"
|
|
|
|
)
|
|
|
|
|
|
|
|
const prefix = "${{"
|
|
|
|
const suffix = "}}"
|
|
|
|
|
|
|
|
var pattern *regexp.Regexp
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
pattern = regexp.MustCompile(fmt.Sprintf("\\%s.+?%s", prefix, suffix))
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewExpressionEvaluator creates a new evaluator
|
|
|
|
func (rc *RunContext) NewExpressionEvaluator() ExpressionEvaluator {
|
|
|
|
vm := rc.newVM()
|
|
|
|
return &expressionEvaluator{
|
|
|
|
vm,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-23 17:01:25 -06:00
|
|
|
// NewExpressionEvaluator creates a new evaluator
|
|
|
|
func (sc *StepContext) NewExpressionEvaluator() ExpressionEvaluator {
|
|
|
|
vm := sc.RunContext.newVM()
|
2020-02-14 02:41:20 -06:00
|
|
|
configers := []func(*otto.Otto){
|
2020-02-23 17:01:25 -06:00
|
|
|
sc.vmEnv(),
|
2020-02-24 00:34:48 -06:00
|
|
|
sc.vmInputs(),
|
2020-02-14 02:41:20 -06:00
|
|
|
}
|
|
|
|
for _, configer := range configers {
|
|
|
|
configer(vm)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &expressionEvaluator{
|
|
|
|
vm,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-13 01:27:37 -06:00
|
|
|
// ExpressionEvaluator is the interface for evaluating expressions
|
|
|
|
type ExpressionEvaluator interface {
|
|
|
|
Evaluate(string) (string, error)
|
2020-02-14 02:41:20 -06:00
|
|
|
Interpolate(string) string
|
2020-02-13 01:27:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type expressionEvaluator struct {
|
|
|
|
vm *otto.Otto
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ee *expressionEvaluator) Evaluate(in string) (string, error) {
|
|
|
|
val, err := ee.vm.Run(in)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return val.ToString()
|
|
|
|
}
|
|
|
|
|
2020-02-14 02:41:20 -06:00
|
|
|
func (ee *expressionEvaluator) Interpolate(in string) string {
|
2020-02-13 01:27:37 -06:00
|
|
|
errList := make([]error, 0)
|
|
|
|
out := pattern.ReplaceAllStringFunc(in, func(match string) string {
|
|
|
|
expression := strings.TrimPrefix(strings.TrimSuffix(match, suffix), prefix)
|
|
|
|
evaluated, err := ee.Evaluate(expression)
|
|
|
|
if err != nil {
|
|
|
|
errList = append(errList, err)
|
|
|
|
}
|
|
|
|
return evaluated
|
|
|
|
})
|
|
|
|
if len(errList) > 0 {
|
2020-02-14 02:41:20 -06:00
|
|
|
logrus.Errorf("Unable to interpolate string '%s' - %v", in, errList)
|
|
|
|
return in
|
2020-02-13 01:27:37 -06:00
|
|
|
}
|
2020-02-14 02:41:20 -06:00
|
|
|
return out
|
2020-02-13 01:27:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *RunContext) newVM() *otto.Otto {
|
|
|
|
configers := []func(*otto.Otto){
|
|
|
|
vmContains,
|
|
|
|
vmStartsWith,
|
|
|
|
vmEndsWith,
|
|
|
|
vmFormat,
|
|
|
|
vmJoin,
|
|
|
|
vmToJSON,
|
2020-02-13 13:47:38 -06:00
|
|
|
vmAlways,
|
2020-02-14 02:41:20 -06:00
|
|
|
rc.vmCancelled(),
|
2020-02-13 13:47:38 -06:00
|
|
|
rc.vmSuccess(),
|
|
|
|
rc.vmFailure(),
|
2020-02-14 02:41:20 -06:00
|
|
|
rc.vmHashFiles(),
|
2020-02-13 13:47:38 -06:00
|
|
|
|
|
|
|
rc.vmGithub(),
|
|
|
|
rc.vmJob(),
|
|
|
|
rc.vmSteps(),
|
|
|
|
rc.vmRunner(),
|
2020-02-14 02:41:20 -06:00
|
|
|
|
|
|
|
rc.vmSecrets(),
|
|
|
|
rc.vmStrategy(),
|
|
|
|
rc.vmMatrix(),
|
2020-02-13 01:27:37 -06:00
|
|
|
}
|
|
|
|
vm := otto.New()
|
|
|
|
for _, configer := range configers {
|
|
|
|
configer(vm)
|
|
|
|
}
|
|
|
|
return vm
|
|
|
|
}
|
|
|
|
|
|
|
|
func vmContains(vm *otto.Otto) {
|
2020-02-13 13:47:38 -06:00
|
|
|
_ = vm.Set("contains", func(searchString interface{}, searchValue string) bool {
|
2020-02-13 01:27:37 -06:00
|
|
|
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 {
|
2020-02-13 13:47:38 -06:00
|
|
|
if strings.EqualFold(s, searchValue) {
|
2020-02-13 01:27:37 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func vmStartsWith(vm *otto.Otto) {
|
2020-02-13 13:47:38 -06:00
|
|
|
_ = vm.Set("startsWith", func(searchString string, searchValue string) bool {
|
2020-02-13 01:27:37 -06:00
|
|
|
return strings.HasPrefix(strings.ToLower(searchString), strings.ToLower(searchValue))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func vmEndsWith(vm *otto.Otto) {
|
2020-02-13 13:47:38 -06:00
|
|
|
_ = vm.Set("endsWith", func(searchString string, searchValue string) bool {
|
2020-02-13 01:27:37 -06:00
|
|
|
return strings.HasSuffix(strings.ToLower(searchString), strings.ToLower(searchValue))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func vmFormat(vm *otto.Otto) {
|
2020-02-13 13:47:38 -06:00
|
|
|
_ = vm.Set("format", func(s string, vals ...string) string {
|
2020-02-13 01:27:37 -06:00
|
|
|
for i, v := range vals {
|
|
|
|
s = strings.ReplaceAll(s, fmt.Sprintf("{%d}", i), v)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func vmJoin(vm *otto.Otto) {
|
2020-02-13 13:47:38 -06:00
|
|
|
_ = vm.Set("join", func(element interface{}, optionalElem string) string {
|
2020-02-13 01:27:37 -06:00
|
|
|
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) {
|
2020-02-28 17:20:31 -06:00
|
|
|
toJSON := func(o interface{}) string {
|
2020-02-13 01:27:37 -06:00
|
|
|
rtn, err := json.MarshalIndent(o, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Unable to marsal: %v", err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return string(rtn)
|
2020-02-28 17:20:31 -06:00
|
|
|
}
|
|
|
|
_ = vm.Set("toJSON", toJSON)
|
|
|
|
_ = vm.Set("toJson", toJSON)
|
2020-02-13 01:27:37 -06:00
|
|
|
}
|
|
|
|
|
2020-02-13 13:47:38 -06:00
|
|
|
func (rc *RunContext) vmHashFiles() func(*otto.Otto) {
|
2020-02-13 01:27:37 -06:00
|
|
|
return func(vm *otto.Otto) {
|
2020-02-13 13:47:38 -06:00
|
|
|
_ = vm.Set("hashFiles", func(path string) string {
|
|
|
|
files, _, err := glob.Glob([]string{filepath.Join(rc.Config.Workdir, path)})
|
2020-02-13 01:27:37 -06:00
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
hasher := sha256.New()
|
|
|
|
for _, file := range files {
|
|
|
|
f, err := os.Open(file.Path)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
if _, err := io.Copy(hasher, f); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hex.EncodeToString(hasher.Sum(nil))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-02-13 13:47:38 -06:00
|
|
|
|
|
|
|
func (rc *RunContext) vmSuccess() func(*otto.Otto) {
|
|
|
|
return func(vm *otto.Otto) {
|
|
|
|
_ = vm.Set("success", func() bool {
|
2020-02-14 02:41:20 -06:00
|
|
|
return rc.getJobContext().Status == "success"
|
2020-02-13 13:47:38 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (rc *RunContext) vmFailure() func(*otto.Otto) {
|
|
|
|
return func(vm *otto.Otto) {
|
|
|
|
_ = vm.Set("failure", func() bool {
|
2020-02-14 02:41:20 -06:00
|
|
|
return rc.getJobContext().Status == "failure"
|
2020-02-13 13:47:38 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func vmAlways(vm *otto.Otto) {
|
|
|
|
_ = vm.Set("always", func() bool {
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
2020-02-14 02:41:20 -06:00
|
|
|
func (rc *RunContext) vmCancelled() func(vm *otto.Otto) {
|
|
|
|
return func(vm *otto.Otto) {
|
|
|
|
_ = vm.Set("cancelled", func() bool {
|
|
|
|
return rc.getJobContext().Status == "cancelled"
|
|
|
|
})
|
|
|
|
}
|
2020-02-13 13:47:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *RunContext) vmGithub() func(*otto.Otto) {
|
2020-02-14 02:41:20 -06:00
|
|
|
github := rc.getGithubContext()
|
2020-02-13 13:47:38 -06:00
|
|
|
|
|
|
|
return func(vm *otto.Otto) {
|
|
|
|
_ = vm.Set("github", github)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-23 17:01:25 -06:00
|
|
|
func (sc *StepContext) vmEnv() func(*otto.Otto) {
|
2020-02-13 13:47:38 -06:00
|
|
|
return func(vm *otto.Otto) {
|
2020-02-23 17:01:25 -06:00
|
|
|
_ = vm.Set("env", sc.Env)
|
2020-02-13 13:47:38 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-24 00:34:48 -06:00
|
|
|
func (sc *StepContext) vmInputs() func(*otto.Otto) {
|
|
|
|
inputs := make(map[string]string)
|
|
|
|
for k, v := range sc.Step.With {
|
|
|
|
inputs[k] = v
|
|
|
|
}
|
|
|
|
return func(vm *otto.Otto) {
|
|
|
|
_ = vm.Set("inputs", inputs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-13 13:47:38 -06:00
|
|
|
func (rc *RunContext) vmJob() func(*otto.Otto) {
|
2020-02-14 02:41:20 -06:00
|
|
|
job := rc.getJobContext()
|
2020-02-13 13:47:38 -06:00
|
|
|
|
|
|
|
return func(vm *otto.Otto) {
|
|
|
|
_ = vm.Set("job", job)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *RunContext) vmSteps() func(*otto.Otto) {
|
2020-02-14 02:41:20 -06:00
|
|
|
steps := rc.getStepsContext()
|
2020-02-13 13:47:38 -06:00
|
|
|
|
|
|
|
return func(vm *otto.Otto) {
|
|
|
|
_ = vm.Set("steps", steps)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *RunContext) vmRunner() func(*otto.Otto) {
|
|
|
|
runner := map[string]interface{}{
|
|
|
|
"os": "Linux",
|
|
|
|
"temp": "/tmp",
|
2020-02-27 01:29:43 -06:00
|
|
|
"tool_cache": "/opt/hostedtoolcache",
|
2020-02-13 13:47:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return func(vm *otto.Otto) {
|
|
|
|
_ = vm.Set("runner", runner)
|
|
|
|
}
|
|
|
|
}
|
2020-02-14 02:41:20 -06:00
|
|
|
|
|
|
|
func (rc *RunContext) vmSecrets() func(*otto.Otto) {
|
|
|
|
return func(vm *otto.Otto) {
|
2020-02-17 23:51:49 -06:00
|
|
|
_ = vm.Set("secrets", rc.Config.Secrets)
|
2020-02-14 02:41:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|