Skip to content

Commit

Permalink
feat: allow to set a expression_timeout in variables (#399)
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Bétrancourt <[email protected]>
  • Loading branch information
rclsilver authored Dec 23, 2022
1 parent 4b2ed72 commit 8540372
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
19 changes: 14 additions & 5 deletions engine/values/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ type Values struct {
// Variable holds a named variable, with either a JS expression to be evalued
// or a concrete value
type Variable struct {
Name string `json:"name"`
Expression string `json:"expression"`
Value interface{} `json:"value"`
evalCachedResult interface{}
Name string `json:"name"`
Expression string `json:"expression"`
ExpressionTimeout string `json:"expression_timeout"`
Value interface{} `json:"value"`
evalCachedResult interface{}
}

// NewValues instantiates a new Values holder,
Expand Down Expand Up @@ -430,7 +431,15 @@ func (v *Values) varEval(varName string) (interface{}, error) {
return nil, err
}

res, err := evalUnsafe(exp, time.Second*10)
var timeout = time.Second * 10
if i.ExpressionTimeout != "" {
timeout, err = time.ParseDuration(i.ExpressionTimeout)
if err != nil {
return nil, err
}
}

res, err := evalUnsafe(exp, timeout)
if err != nil {
return nil, err
}
Expand Down
4 changes: 4 additions & 0 deletions hack/template-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,10 @@
"expression": {
"type": "string",
"description": "Javascript expression that will be evaluated (can be templated)"
},
"expression_timeout": {
"type": "string",
"description": "A valid golang duration which can be parsed by time.ParseDuration()"
}
}
},
Expand Down
16 changes: 16 additions & 0 deletions models/tasktemplate/fromdir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,22 @@ func TestInvalidVariablesTemplates(t *testing.T) {
tt.Normalize()
err = tt.Valid()
assert.Contains(t, fmt.Sprint(err), "expression and value can't be empty at the same time")

tt.Variables[0].Name = "toto"
tt.Variables[0].Expression = ""
tt.Variables[0].Value = "foo"
tt.Variables[0].ExpressionTimeout = "30s"
tt.Normalize()
err = tt.Valid()
assert.Contains(t, fmt.Sprint(err), "expression timeout cannot be defined when value is defined")

tt.Variables[0].Name = "toto"
tt.Variables[0].Expression = "1+1"
tt.Variables[0].Value = nil
tt.Variables[0].ExpressionTimeout = "foo"
tt.Normalize()
err = tt.Valid()
assert.Contains(t, fmt.Sprint(err), "invalid duration \"foo\"")
}

func TestDependenciesValidation(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions models/tasktemplate/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tasktemplate
import (
"encoding/json"
"fmt"
"time"

"github.com/Masterminds/squirrel"
"github.com/juju/errors"
Expand Down Expand Up @@ -451,6 +452,15 @@ func validateVariables(variables []values.Variable) error {
if variable.Value == nil && variable.Expression == "" {
return errors.BadRequestf("variable %q expression and value can't be empty at the same time", variable.Name)
}
if variable.Value != nil && variable.ExpressionTimeout != "" {
return errors.BadRequestf("variable %q expression timeout cannot be defined when value is defined", variable.Name)
}
if variable.ExpressionTimeout != "" {
_, err := time.ParseDuration(variable.ExpressionTimeout)
if err != nil {
return errors.BadRequestf("variable %q %s", variable.Name, err)
}
}
}

return nil
Expand Down

0 comments on commit 8540372

Please sign in to comment.