Repository: datreeio/datree
Pull request #418 (2022-02-16 08:48:19)

fix:import output for specific CI environment


Patch:
diff --git a/bl/evaluation/evaluator.go b/bl/evaluation/evaluator.go
index b02912e..6d30e1f 100644
--- a/bl/evaluation/evaluator.go
+++ b/bl/evaluation/evaluator.go
@@ -17,12 +17,14 @@ type CLIClient interface {
 type Evaluator struct {
 	cliClient CLIClient
 	osInfo    *OSInfo
+	ciContext *ciContext.CIContext
 }
 
 func New(c CLIClient) *Evaluator {
 	return &Evaluator{
 		cliClient: c,
 		osInfo:    NewOSInfo(),
+		ciContext: ciContext.Extract(),
 	}
 }
 
diff --git a/pkg/ciContext/ciContext.go b/pkg/ciContext/ciContext.go
index f6f8d59..54c789e 100644
--- a/pkg/ciContext/ciContext.go
+++ b/pkg/ciContext/ciContext.go
@@ -7,70 +7,85 @@ import (
 )
 
 type CIContext struct {
-	IsCI  bool   `json:"isCI"`
-	CIEnv string `json:"ciEnv"`
+	IsCI       bool        `json:"isCI"`
+	CIMetadata *CIMetadata `json:"ciMetadata"`
+}
+type CIMetadata struct {
+	CIEnvValue       string `json:"ciEnvValue"`
+	ShouldHideEmojis bool   `json:"shouldHideEmojis"`
 }
 
-var ciMap = map[string]string{
-	"GITHUB_ACTIONS":                     "github_actions",
-	"GITLAB_CI":                          "gitlab_ci",
-	"CIRCLECI":                           "circle-ci",
-	"JENKINS_HOME":                       "jenkins",
-	"JENKINS_URL":                        "jenkins",
-	"BUILDKITE":                          "buildkite",
-	"SYSTEM_COLLECTIONURI":               fmt.Sprintf("azure_devops_%s", os.Getenv("BUILD_REPOSITORY_PROVIDER")),
-	"SYSTEM_TEAMFOUNDATIONCOLLECTIONURI": "azure-pipelines",
-	"TFC_RUN_ID":                         "tfc",
-	"ENV0_ENVIRONMENT_ID":                "env0",
-	"CF_BUILD_ID":                        "codefresh",
-	"TRAVIS":                             "travis",
-	"CODEBUILD_CI":                       "codebuild",
-	"TEAMCITY_VERSION":                   "teamcity",
-	"BUDDYBUILD_BRANCH":                  "buddybuild",
-	"BUDDY_WORKSPACE_ID":                 "buddy",
-	"APPVEYOR":                           "appveyor",
-	"WERCKER_GIT_BRANCH":                 "wercker",
-	"WERCKER":                            "wercker",
-	"SHIPPABLE":                          "shippable",
-	"BITBUCKET_BUILD_NUMBER":             "bitbucket-pipelines",
-	"CIRRUS_CI":                          "cirrusci",
-	"DRONE":                              "drone",
-	"GO_PIPELINE_NAME":                   "gocd",
-	"SAIL_CI":                            "sail",
+var ciMap = map[string]*CIMetadata{
+	"GITHUB_ACTIONS":                     {CIEnvValue: "github_actions", ShouldHideEmojis: false},
+	"GITLAB_CI":                          {CIEnvValue: "gitlab_ci", ShouldHideEmojis: false},
+	"CIRCLECI":                           {CIEnvValue: "circle-ci", ShouldHideEmojis: false},
+	"JENKINS_HOME":                       {CIEnvValue: "jenkins", ShouldHideEmojis: true},
+	"JENKINS_URL":                        {CIEnvValue: "jenkins", ShouldHideEmojis: true},
+	"BUILDKITE":                          {CIEnvValue: "buildkite", ShouldHideEmojis: false},
+	"SYSTEM_COLLECTIONURI":               {CIEnvValue: fmt.Sprintf("azure_devops_%s", os.Getenv("BUILD_REPOSITORY_PROVIDER")), ShouldHideEmojis: false},
+	"SYSTEM_TEAMFOUNDATIONCOLLECTIONURI": {CIEnvValue: "azure-pipelines", ShouldHideEmojis: false},
+	"TFC_RUN_ID":                         {CIEnvValue: "tfc", ShouldHideEmojis: false},
+	"ENV0_ENVIRONMENT_ID":                {CIEnvValue: "env0", ShouldHideEmojis: false},
+	"CF_BUILD_ID":                        {CIEnvValue: "codefresh", ShouldHideEmojis: false},
+	"TRAVIS":                             {CIEnvValue: "travis", ShouldHideEmojis: true},
+	"CODEBUILD_CI":                       {CIEnvValue: "codebuild", ShouldHideEmojis: false},
+	"TEAMCITY_VERSION":                   {CIEnvValue: "teamcity", ShouldHideEmojis: false},
+	"BUDDYBUILD_BRANCH":                  {CIEnvValue: "buddybuild", ShouldHideEmojis: false},
+	"BUDDY_WORKSPACE_ID":                 {CIEnvValue: "buddy", ShouldHideEmojis: false},
+	"APPVEYOR":                           {CIEnvValue: "appveyor", ShouldHideEmojis: false},
+	"WERCKER_GIT_BRANCH":                 {CIEnvValue: "wercker", ShouldHideEmojis: false},
+	"WERCKER":                            {CIEnvValue: "wercker", ShouldHideEmojis: false},
+	"SHIPPABLE":                          {CIEnvValue: "shippable", ShouldHideEmojis: false},
+	"BITBUCKET_BUILD_NUMBER":             {CIEnvValue: "bitbucket-pipelines", ShouldHideEmojis: false},
+	"CIRRUS_CI":                          {CIEnvValue: "cirrusci", ShouldHideEmojis: false},
+	"DRONE":                              {CIEnvValue: "drone", ShouldHideEmojis: false},
+	"GO_PIPELINE_NAME":                   {CIEnvValue: "gocd", ShouldHideEmojis: false},
+	"SAIL_CI":                            {CIEnvValue: "sail", ShouldHideEmojis: false},
 }
 
-var ciMapPrefix = map[string]string{
-	"ATLANTIS_":  "atlantis",
-	"BITBUCKET_": "bitbucket",
-	"CONCOURSE_": "concourse",
-	"SPACELIFT_": "spacelift",
-	"HARNESS_":   "harness",
+var ciMapPrefix = map[string]CIMetadata{
+	"ATLANTIS_":  {CIEnvValue: "atlantis", ShouldHideEmojis: false},
+	"BITBUCKET_": {CIEnvValue: "bitbucket", ShouldHideEmojis: false},
+	"CONCOURSE_": {CIEnvValue: "concourse", ShouldHideEmojis: false},
+	"SPACELIFT_": {CIEnvValue: "spacelift", ShouldHideEmojis: false},
+	"HARNESS_":   {CIEnvValue: "harness", ShouldHideEmojis: false},
 }
 
 func Extract() *CIContext {
-	var checkCIEnv = isCI()
-	return &CIContext{
-		IsCI:  checkCIEnv != "none",
-		CIEnv: checkCIEnv,
+	ciName := getCIName()
+	isCI := len(ciName) > 0
+	if isCI {
+		return &CIContext{
+			IsCI:       isCI,
+			CIMetadata: ciMap[ciName],
+		}
+	} else {
+		return &CIContext{
+			IsCI: false,
+			CIMetadata: &CIMetadata{
+				CIEnvValue:       "",
+				ShouldHideEmojis: false,
+			},
+		}
 	}
 }
 
-func isCI() string {
-	for env, name := range ciMap {
+func getCIName() string {
+	for env := range ciMap {
 		if isKeyInEnv(env) {
-			return name
+			return env
 		}
 	}
 
 	for _, key := range os.Environ() {
-		for prefix, name := range ciMapPrefix {
+		for prefix := range ciMapPrefix {
 			if strings.HasPrefix(key, prefix) {
-				return name
+				return prefix
 			}
 		}
 	}
 
-	return "none"
+	return ""
 }
 
 func isKeyInEnv(key string) bool {


Test patch:
diff --git a/bl/evaluation/evaluator_test.go b/bl/evaluation/evaluator_test.go
index a725e7b..21785c7 100644
--- a/bl/evaluation/evaluator_test.go
+++ b/bl/evaluation/evaluator_test.go
@@ -80,8 +80,11 @@ func TestCreateEvaluation(t *testing.T) {
 		k8sVersion := "1.18.1"
 		policyName := "Default"
 		ciContext := &ciContext.CIContext{
-			IsCI:  true,
-			CIEnv: "travis",
+			IsCI: true,
+			CIMetadata: &ciContext.CIMetadata{
+				CIEnvValue:       "travis",
+				ShouldHideEmojis: false,
+			},
 		}
 
 		mockedCliClient.On("CreateEvaluation", mock.Anything).Return(&cliClient.CreateEvaluationResponse{EvaluationId: 1, K8sVersion: k8sVersion}, nil)
diff --git a/cmd/test/main.go b/cmd/test/main.go
index 41d01c0..67a8287 100644
--- a/cmd/test/main.go
+++ b/cmd/test/main.go
@@ -196,7 +196,7 @@ func New(ctx *TestCommandContext) *cobra.Command {
 		},
 	}
 
-	if ciContext.Extract().IsCI {
+	if ciContext.Extract() != nil && ciContext.Extract().CIMetadata.ShouldHideEmojis {
 		testCommand.Flags().StringP("output", "o", "simple", "Define output format")
 	} else {
 		testCommand.Flags().StringP("output", "o", "", "Define output format")