Skip to content

Commit 8540372

Browse files
authored
feat: allow to set a expression_timeout in variables (#399)
Signed-off-by: Thomas Bétrancourt <[email protected]>
1 parent 4b2ed72 commit 8540372

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

engine/values/values.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ type Values struct {
4444
// Variable holds a named variable, with either a JS expression to be evalued
4545
// or a concrete value
4646
type Variable struct {
47-
Name string `json:"name"`
48-
Expression string `json:"expression"`
49-
Value interface{} `json:"value"`
50-
evalCachedResult interface{}
47+
Name string `json:"name"`
48+
Expression string `json:"expression"`
49+
ExpressionTimeout string `json:"expression_timeout"`
50+
Value interface{} `json:"value"`
51+
evalCachedResult interface{}
5152
}
5253

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

433-
res, err := evalUnsafe(exp, time.Second*10)
434+
var timeout = time.Second * 10
435+
if i.ExpressionTimeout != "" {
436+
timeout, err = time.ParseDuration(i.ExpressionTimeout)
437+
if err != nil {
438+
return nil, err
439+
}
440+
}
441+
442+
res, err := evalUnsafe(exp, timeout)
434443
if err != nil {
435444
return nil, err
436445
}

hack/template-schema.json

+4
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,10 @@
976976
"expression": {
977977
"type": "string",
978978
"description": "Javascript expression that will be evaluated (can be templated)"
979+
},
980+
"expression_timeout": {
981+
"type": "string",
982+
"description": "A valid golang duration which can be parsed by time.ParseDuration()"
979983
}
980984
}
981985
},

models/tasktemplate/fromdir_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,22 @@ func TestInvalidVariablesTemplates(t *testing.T) {
138138
tt.Normalize()
139139
err = tt.Valid()
140140
assert.Contains(t, fmt.Sprint(err), "expression and value can't be empty at the same time")
141+
142+
tt.Variables[0].Name = "toto"
143+
tt.Variables[0].Expression = ""
144+
tt.Variables[0].Value = "foo"
145+
tt.Variables[0].ExpressionTimeout = "30s"
146+
tt.Normalize()
147+
err = tt.Valid()
148+
assert.Contains(t, fmt.Sprint(err), "expression timeout cannot be defined when value is defined")
149+
150+
tt.Variables[0].Name = "toto"
151+
tt.Variables[0].Expression = "1+1"
152+
tt.Variables[0].Value = nil
153+
tt.Variables[0].ExpressionTimeout = "foo"
154+
tt.Normalize()
155+
err = tt.Valid()
156+
assert.Contains(t, fmt.Sprint(err), "invalid duration \"foo\"")
141157
}
142158

143159
func TestDependenciesValidation(t *testing.T) {

models/tasktemplate/template.go

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package tasktemplate
33
import (
44
"encoding/json"
55
"fmt"
6+
"time"
67

78
"github.com/Masterminds/squirrel"
89
"github.com/juju/errors"
@@ -451,6 +452,15 @@ func validateVariables(variables []values.Variable) error {
451452
if variable.Value == nil && variable.Expression == "" {
452453
return errors.BadRequestf("variable %q expression and value can't be empty at the same time", variable.Name)
453454
}
455+
if variable.Value != nil && variable.ExpressionTimeout != "" {
456+
return errors.BadRequestf("variable %q expression timeout cannot be defined when value is defined", variable.Name)
457+
}
458+
if variable.ExpressionTimeout != "" {
459+
_, err := time.ParseDuration(variable.ExpressionTimeout)
460+
if err != nil {
461+
return errors.BadRequestf("variable %q %s", variable.Name, err)
462+
}
463+
}
454464
}
455465

456466
return nil

0 commit comments

Comments
 (0)