Repository: datreeio/datree
Pull request #588 (2022-04-26 13:37:29)

feat: send isCi query param on prerun request


Patch:
diff --git a/bl/evaluation/evaluator.go b/bl/evaluation/evaluator.go
index f5fdd7d..a054137 100644
--- a/bl/evaluation/evaluator.go
+++ b/bl/evaluation/evaluator.go
@@ -27,10 +27,10 @@ type Evaluator struct {
 	jsonSchemaValidator *jsonSchemaValidator.JSONSchemaValidator
 }
 
-func New(c CLIClient) *Evaluator {
+func New(c CLIClient, ciContext *ciContext.CIContext) *Evaluator {
 	return &Evaluator{
 		cliClient:           c,
-		ciContext:           ciContext.Extract(),
+		ciContext:           ciContext,
 		jsonSchemaValidator: jsonSchemaValidator.New(),
 	}
 }
diff --git a/cmd/kustomize/main.go b/cmd/kustomize/main.go
index b2bbfca..b4be98d 100644
--- a/cmd/kustomize/main.go
+++ b/cmd/kustomize/main.go
@@ -4,6 +4,7 @@ import (
 	"os"
 	"strings"
 
+	"github.com/datreeio/datree/pkg/ciContext"
 	"github.com/datreeio/datree/pkg/cliClient"
 
 	"github.com/datreeio/datree/cmd/test"
@@ -13,7 +14,7 @@ import (
 )
 
 type CliClient interface {
-	RequestEvaluationPrerunData(token string) (*cliClient.EvaluationPrerunDataResponse, error)
+	RequestEvaluationPrerunData(token string, isCi bool) (*cliClient.EvaluationPrerunDataResponse, error)
 }
 
 type KustomizeCommandRunner interface {
@@ -68,7 +69,8 @@ func New(testCtx *test.TestCommandContext, kustomizeCtx *KustomizeContext) *cobr
 				return err
 			}
 
-			evaluationPrerunData, err := testCtx.CliClient.RequestEvaluationPrerunData(localConfigContent.Token)
+			ciContext := ciContext.Extract()
+			evaluationPrerunData, err := testCtx.CliClient.RequestEvaluationPrerunData(localConfigContent.Token, ciContext.IsCI)
 			if err != nil {
 				return err
 			}
diff --git a/cmd/root.go b/cmd/root.go
index 7f03c1a..beddf1e 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -12,6 +12,7 @@ import (
 	schema_validator "github.com/datreeio/datree/cmd/schema-validator"
 	"github.com/datreeio/datree/cmd/test"
 	"github.com/datreeio/datree/cmd/version"
+	"github.com/datreeio/datree/pkg/ciContext"
 	"github.com/datreeio/datree/pkg/cliClient"
 	"github.com/datreeio/datree/pkg/executor"
 	"github.com/datreeio/datree/pkg/fileReader"
@@ -41,6 +42,7 @@ func NewRootCommand(app *App) *cobra.Command {
 		K8sValidator:   app.Context.K8sValidator,
 		CliClient:      app.Context.CliClient,
 		FilesExtractor: app.Context.FilesExtractor,
+		CiContext:      app.Context.CiContext,
 	}))
 
 	rootCmd.AddCommand(kustomize.New(&test.TestCommandContext{
@@ -90,6 +92,7 @@ func NewRootCommand(app *App) *cobra.Command {
 type Context struct {
 	LocalConfig         *localConfig.LocalConfigClient
 	Evaluator           *evaluation.Evaluator
+	CiContext           *ciContext.CIContext
 	CliClient           *cliClient.CliClient
 	Messager            *messager.Messager
 	Printer             *printer.Printer
diff --git a/go.mod b/go.mod
index 9fa6910..193835b 100644
--- a/go.mod
+++ b/go.mod
@@ -26,6 +26,8 @@ require (
 require (
 	github.com/gogo/protobuf v1.3.2 // indirect
 	gopkg.in/inf.v0 v0.9.1 // indirect
+	github.com/kr/pretty v0.2.1 // indirect
+	github.com/rivo/uniseg v0.2.0 // indirect
 	sigs.k8s.io/yaml v1.2.0 // indirect
 )
 
@@ -39,7 +41,7 @@ require (
 	github.com/magiconair/properties v1.8.1 // indirect
 	github.com/mattn/go-colorable v0.1.8 // indirect
 	github.com/mattn/go-isatty v0.0.12 // indirect
-	github.com/mattn/go-runewidth v0.0.9 // indirect
+	github.com/mattn/go-runewidth v0.0.13 // indirect
 	github.com/mitchellh/mapstructure v1.1.2 // indirect
 	github.com/pelletier/go-toml v1.2.0 // indirect
 	github.com/pkg/errors v0.9.1
diff --git a/go.sum b/go.sum
index 6a36881..f8d183b 100644
--- a/go.sum
+++ b/go.sum
@@ -238,6 +238,8 @@ github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
 github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
 github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
 github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
+github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
+github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
 github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
 github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
 github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
@@ -260,8 +262,9 @@ github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNx
 github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
 github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
 github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
-github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
 github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
+github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
+github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
 github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
 github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
 github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
@@ -319,6 +322,8 @@ github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y8
 github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
 github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
 github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
+github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
+github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
 github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
 github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
 github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
diff --git a/main.go b/main.go
index 8eb7c44..f75bdc6 100644
--- a/main.go
+++ b/main.go
@@ -7,6 +7,7 @@ import (
 
 	"github.com/datreeio/datree/bl/files"
 
+	"github.com/datreeio/datree/pkg/ciContext"
 	"github.com/datreeio/datree/pkg/deploymentConfig"
 
 	"github.com/datreeio/datree/pkg/executor"
@@ -34,6 +35,7 @@ func main() {
 	validator := networkValidator.NewNetworkValidator()
 	cliClient := cliClient.NewCliClient(deploymentConfig.URL, validator)
 	localConfig := localConfig.NewLocalConfigClient(cliClient, validator)
+	ciContext := ciContext.Extract()
 
 	reporter := errorReporter.NewErrorReporter(cliClient, localConfig)
 	globalPrinter := printer.CreateNewPrinter()
@@ -41,7 +43,8 @@ func main() {
 	app := &cmd.App{
 		Context: &cmd.Context{
 			LocalConfig:         localConfig,
-			Evaluator:           evaluation.New(cliClient),
+			CiContext:           ciContext,
+			Evaluator:           evaluation.New(cliClient, ciContext),
 			CliClient:           cliClient,
 			Messager:            messager.New(cliClient),
 			Printer:             globalPrinter,
diff --git a/pkg/cliClient/evaluation.go b/pkg/cliClient/evaluation.go
index ea0c339..4c2bff4 100644
--- a/pkg/cliClient/evaluation.go
+++ b/pkg/cliClient/evaluation.go
@@ -3,6 +3,7 @@ package cliClient
 import (
 	"encoding/json"
 	"net/http"
+	"strconv"
 
 	"github.com/datreeio/datree/pkg/ciContext"
 	"github.com/datreeio/datree/pkg/extractor"
@@ -101,12 +102,13 @@ type EvaluationPrerunDataResponse struct {
 	DefaultRulesYaml      string                    `json:"defaultRulesYaml"`
 }
 
-func (c *CliClient) RequestEvaluationPrerunData(tokenId string) (*EvaluationPrerunDataResponse, error) {
+func (c *CliClient) RequestEvaluationPrerunData(tokenId string, isCi bool) (*EvaluationPrerunDataResponse, error) {
 	if c.networkValidator.IsLocalMode() {
 		return &EvaluationPrerunDataResponse{IsPolicyAsCodeMode: true}, nil
 	}
 
-	res, err := c.httpClient.Request(http.MethodGet, "/cli/evaluation/tokens/"+tokenId+"/prerun", nil, nil)
+	isCiQueryParam := "isCi=" + strconv.FormatBool(isCi)
+	res, err := c.httpClient.Request(http.MethodGet, "/cli/evaluation/tokens/"+tokenId+"/prerun?"+isCiQueryParam, nil, nil)
 
 	if err != nil {
 		networkErr := c.networkValidator.IdentifyNetworkError(err.Error())


Test patch:
diff --git a/cmd/test/main.go b/cmd/test/main.go
index 9a94ea4..dd0babe 100644
--- a/cmd/test/main.go
+++ b/cmd/test/main.go
@@ -108,7 +108,7 @@ type LocalConfig interface {
 var ViolationsFoundError = errors.New("")
 
 type CliClient interface {
-	RequestEvaluationPrerunData(token string) (*cliClient.EvaluationPrerunDataResponse, error)
+	RequestEvaluationPrerunData(token string, isCi bool) (*cliClient.EvaluationPrerunDataResponse, error)
 }
 
 type TestCommandData struct {
@@ -128,6 +128,7 @@ type TestCommandData struct {
 
 type TestCommandContext struct {
 	CliVersion     string
+	CiContext      *ciContext.CIContext
 	LocalConfig    LocalConfig
 	Evaluator      Evaluator
 	Messager       Messager
@@ -195,7 +196,7 @@ func New(ctx *TestCommandContext) *cobra.Command {
 				return err
 			}
 
-			evaluationPrerunData, err := ctx.CliClient.RequestEvaluationPrerunData(localConfigContent.Token)
+			evaluationPrerunData, err := ctx.CliClient.RequestEvaluationPrerunData(localConfigContent.Token, ctx.CiContext.IsCI)
 			saveDefaultRulesAsFile(ctx, evaluationPrerunData.DefaultRulesYaml)
 
 			if err != nil {
diff --git a/cmd/test/main_test.go b/cmd/test/main_test.go
index e55e450..8d0b30c 100644
--- a/cmd/test/main_test.go
+++ b/cmd/test/main_test.go
@@ -10,6 +10,7 @@ import (
 	"github.com/datreeio/datree/bl/messager"
 	policy_factory "github.com/datreeio/datree/bl/policy"
 	"github.com/datreeio/datree/bl/validation"
+	"github.com/datreeio/datree/pkg/ciContext"
 	"github.com/datreeio/datree/pkg/fileReader"
 
 	"github.com/datreeio/datree/pkg/cliClient"
@@ -168,7 +169,7 @@ type CliClientMock struct {
 	mock.Mock
 }
 
-func (c *CliClientMock) RequestEvaluationPrerunData(token string) (*cliClient.EvaluationPrerunDataResponse, error) {
+func (c *CliClientMock) RequestEvaluationPrerunData(token string, isCi bool) (*cliClient.EvaluationPrerunDataResponse, error) {
 	args := c.Called(token)
 	return args.Get(0).(*cliClient.EvaluationPrerunDataResponse), nil
 }
@@ -242,6 +243,11 @@ func TestTestFlow(t *testing.T) {
 			printerMock.On("PrintMessage", mock.Anything, mock.Anything)
 			printerMock.On("PrintPromptMessage", mock.Anything)
 			printerMock.On("SetTheme", mock.Anything)
+
+			ciContext := &ciContext.CIContext{
+				IsCI: false,
+			}
+
 			ctx := &TestCommandContext{
 				K8sValidator:   k8sValidatorMock,
 				Evaluator:      evaluatorMock,
@@ -250,6 +256,7 @@ func TestTestFlow(t *testing.T) {
 				Printer:        printerMock,
 				Reader:         readerMock,
 				FilesExtractor: filesExtractorMock,
+				CiContext:      ciContext,
 			}
 
 			err := Test(ctx, tt.args.path, &TestCommandData{K8sVersion: "1.18.0", Output: "", Policy: tt.expected.Evaluate.evaluationData.Policy, Token: "134kh"})
@@ -684,6 +691,10 @@ func setup() {
 	mockedCliClient = &CliClientMock{}
 	mockedCliClient.On("RequestEvaluationPrerunData", mock.Anything).Return(prerunData, nil)
 
+	ciContext := &ciContext.CIContext{
+		IsCI: false,
+	}
+
 	ctx = &TestCommandContext{
 		K8sValidator:   k8sValidatorMock,
 		Evaluator:      mockedEvaluator,
@@ -693,6 +704,7 @@ func setup() {
 		Reader:         readerMock,
 		FilesExtractor: filesExtractorMock,
 		CliClient:      mockedCliClient,
+		CiContext:      ciContext,
 	}
 
 	testingPolicy, _ = policy_factory.CreatePolicy(prerunData.PoliciesJson, "", prerunData.RegistrationURL)
diff --git a/pkg/cliClient/cliClient_test.go b/pkg/cliClient/cliClient_test.go
index 3df590b..5ffae2b 100644
--- a/pkg/cliClient/cliClient_test.go
+++ b/pkg/cliClient/cliClient_test.go
@@ -155,7 +155,7 @@ func TestRequestEvaluationPrerunDataSuccess(t *testing.T) {
 				networkValidator: networkValidator,
 			}
 
-			policyCheckData, _ := client.RequestEvaluationPrerunData(tt.args.token)
+			policyCheckData, _ := client.RequestEvaluationPrerunData(tt.args.token, false)
 
 			httpClientMock.AssertCalled(t, "Request", tt.expected.request.method, tt.expected.request.uri, tt.expected.request.body, tt.expected.request.headers)
 			assert.Equal(t, tt.expected.response, policyCheckData)
@@ -186,7 +186,7 @@ func TestRequestEvaluationPrerunDataFail(t *testing.T) {
 				networkValidator: validator,
 			}
 
-			preRunDataResp, err := client.RequestEvaluationPrerunData(tt.args.token)
+			preRunDataResp, err := client.RequestEvaluationPrerunData(tt.args.token, false)
 
 			httpClientMock.AssertCalled(t, "Request", tt.expected.request.method, tt.expected.request.uri, tt.expected.request.body, tt.expected.request.headers)
 			assert.Equal(t, tt.expected.response, preRunDataResp)
@@ -455,7 +455,7 @@ func test_requestEvaluationPrerunData_success() *RequestEvaluationPrerunDataTest
 				headers map[string]string
 			}{
 				method:  http.MethodGet,
-				uri:     "/cli/evaluation/tokens/internal_test_token/prerun",
+				uri:     "/cli/evaluation/tokens/internal_test_token/prerun?isCi=false",
 				body:    nil,
 				headers: nil,
 			},
@@ -507,7 +507,7 @@ func test_requestEvaluationPrerunData_error() *RequestEvaluationPrerunDataTestCa
 				headers map[string]string
 			}{
 				method:  http.MethodGet,
-				uri:     "/cli/evaluation/tokens/internal_test_token/prerun",
+				uri:     "/cli/evaluation/tokens/internal_test_token/prerun?isCi=false",
 				body:    nil,
 				headers: nil,
 			},
@@ -566,7 +566,7 @@ func test_requestEvaluationPrerunData_network_error(offlineMode string, expected
 				headers map[string]string
 			}{
 				method:  http.MethodGet,
-				uri:     "/cli/evaluation/tokens/internal_test_token/prerun",
+				uri:     "/cli/evaluation/tokens/internal_test_token/prerun?isCi=false",
 				body:    nil,
 				headers: nil,
 			},