From 865c22b71f41669d17b11cfff6227638b922bf4c Mon Sep 17 00:00:00 2001 From: Nikolay Edigaryev Date: Thu, 1 Apr 2021 21:20:12 +0300 Subject: [PATCH] Evaluator: apply environment from EvaluateFunctionRequest (#353) --- go.mod | 5 ++--- go.sum | 6 ++---- internal/evaluator/evaluator.go | 4 +++- internal/evaluator/evaluator_test.go | 9 ++++++--- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index a36c8ebc..71564015 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/antihax/optional v1.0.0 github.com/avast/retry-go v3.0.0+incompatible github.com/certifi/gocertifi v0.0.0-20200922220541-2c3bb06c6054 - github.com/cirruslabs/cirrus-ci-agent v1.33.0 + github.com/cirruslabs/cirrus-ci-agent v1.37.0 github.com/cirruslabs/echelon v1.4.1 github.com/cirruslabs/go-java-glob v0.1.0 github.com/cirruslabs/podmanapi v0.1.0 @@ -22,7 +22,6 @@ require ( github.com/dustin/go-humanize v1.0.0 github.com/go-git/go-billy/v5 v5.0.0 github.com/go-git/go-git/v5 v5.2.0 - github.com/go-test/deep v1.0.7 github.com/golang/protobuf v1.4.3 github.com/google/go-cmp v0.5.4 // indirect github.com/google/go-github/v32 v32.1.0 @@ -48,7 +47,7 @@ require ( github.com/pelletier/go-toml v1.8.1 // indirect github.com/pkg/sftp v1.12.0 github.com/qri-io/starlib v0.4.2 - github.com/sergi/go-diff v1.1.0 // indirect + github.com/sergi/go-diff v1.1.0 github.com/sirupsen/logrus v1.7.0 github.com/spf13/afero v1.5.1 // indirect github.com/spf13/cast v1.3.1 // indirect diff --git a/go.sum b/go.sum index b61c087f..aff25f45 100644 --- a/go.sum +++ b/go.sum @@ -86,8 +86,8 @@ github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5P github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= github.com/cilium/ebpf v0.0.0-20200110133405-4032b1d8aae3/go.mod h1:MA5e5Lr8slmEg9bt0VpxxWqJlO4iwu3FBdHUzV7wQVg= github.com/cilium/ebpf v0.0.0-20200507155900-a9f01edf17e3/go.mod h1:XT+cAw5wfvsodedcijoh1l9cf7v1x9FlFB/3VmF/O8s= -github.com/cirruslabs/cirrus-ci-agent v1.33.0 h1:6qbhUCO2QaUXjzLkz0l97HGqUCZr4P5cshPwnVLPZX0= -github.com/cirruslabs/cirrus-ci-agent v1.33.0/go.mod h1:T3amh8kB54wkmpWrQ/VXVl9osHXdt3ILS0cH5ZaYGF4= +github.com/cirruslabs/cirrus-ci-agent v1.37.0 h1:C05EnW7eeMWH115JXHc6WswXhqorO3Tb7CNwe+wwGVY= +github.com/cirruslabs/cirrus-ci-agent v1.37.0/go.mod h1:T3amh8kB54wkmpWrQ/VXVl9osHXdt3ILS0cH5ZaYGF4= github.com/cirruslabs/cirrus-ci-annotations v0.1.0/go.mod h1:xrmxzL58Pf4cSSQCmQEOPGQ3poeARxJdHneurUrqjZk= github.com/cirruslabs/echelon v1.4.1 h1:7ij7cANbL1feOyds5sRtYLPBJwycFi3nvLKJI4vCOPs= github.com/cirruslabs/echelon v1.4.1/go.mod h1:1jFBACMy3tzodXyTtNNLN9bw6UUU7Xpq9tYMRydehtY= @@ -186,8 +186,6 @@ github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9 github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-test/deep v1.0.1/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= -github.com/go-test/deep v1.0.7 h1:/VSMRlnY/JSyqxQUzQLKVMAskpY/NZKFA5j2P+0pP2M= -github.com/go-test/deep v1.0.7/go.mod h1:QV8Hv/iy04NyLBxAdO9njL0iVPN1S4d/A3NVv1V36o8= github.com/godbus/dbus v0.0.0-20190422162347-ade71ed3457e/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4= github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= diff --git a/internal/evaluator/evaluator.go b/internal/evaluator/evaluator.go index 485ba019..1f06a9c2 100644 --- a/internal/evaluator/evaluator.go +++ b/internal/evaluator/evaluator.go @@ -205,8 +205,10 @@ func (r *ConfigurationEvaluatorServiceServer) EvaluateFunction( ctx context.Context, request *api.EvaluateFunctionRequest, ) (*api.EvaluateFunctionResponse, error) { + lrk := larker.New(larker.WithEnvironment(request.Environment)) + // Run Starlark hook - result, err := larker.New().Hook(ctx, request.StarlarkConfig, request.FunctionName, request.Arguments.AsSlice()) + result, err := lrk.Hook(ctx, request.StarlarkConfig, request.FunctionName, request.Arguments.AsSlice()) if err != nil { return nil, status.Errorf(codes.Internal, err.Error()) } diff --git a/internal/evaluator/evaluator_test.go b/internal/evaluator/evaluator_test.go index 5e9353bc..c000e6af 100644 --- a/internal/evaluator/evaluator_test.go +++ b/internal/evaluator/evaluator_test.go @@ -316,8 +316,10 @@ task: } func TestHook(t *testing.T) { - config := `def on_build_failure(ctx): - print("I'm alive!") + config := `load("cirrus", "env") + +def on_build_failure(ctx): + print(env.get("SOME_VARIABLE")) return [ctx.build.id, ctx.task.id] ` @@ -339,6 +341,7 @@ func TestHook(t *testing.T) { StarlarkConfig: config, FunctionName: "on_build_failure", Arguments: arguments, + Environment: map[string]string{"SOME_VARIABLE": "some value"}, }) if err != nil { t.Fatal(err) @@ -355,7 +358,7 @@ func TestHook(t *testing.T) { assert.Empty(t, res.ErrorMessage, "hook should evaluate successfully") - assert.Equal(t, string(res.OutputLogs), "I'm alive!\n", "hook should generate some debugging output") + assert.Equal(t, string(res.OutputLogs), "some value\n", "hook should generate some debugging output") assert.Greater(t, res.DurationNanos, int64(0), "execution time doesn't seem to be counted properly")