From f29ddf49d892893d95883ac1cd8fa75f467eef7a Mon Sep 17 00:00:00 2001 From: Itay Paz Date: Mon, 12 Aug 2024 12:24:07 +0300 Subject: [PATCH 1/4] Remove spaces from brnach name to prevent duplication --- internal/commands/scan.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/commands/scan.go b/internal/commands/scan.go index e39a16b13..e82a0d12b 100644 --- a/internal/commands/scan.go +++ b/internal/commands/scan.go @@ -1747,7 +1747,7 @@ func setupScanHandler(cmd *cobra.Command, uploadsWrapper wrappers.UploadsWrapper ) { zipFilePath := "" scanHandler := wrappers.ScanHandler{} - scanHandler.Branch = viper.GetString(commonParams.BranchKey) + scanHandler.Branch = strings.TrimSpace(viper.GetString(commonParams.BranchKey)) uploadType := getUploadType(cmd) @@ -2634,7 +2634,7 @@ func deprecatedFlagValue(cmd *cobra.Command, deprecatedFlagKey, inUseFlagKey str } func validateCreateScanFlags(cmd *cobra.Command) error { - branch := viper.GetString(commonParams.BranchKey) + branch := strings.TrimSpace(viper.GetString(commonParams.BranchKey)) if branch == "" { return errors.Errorf("%s: Please provide a branch", failedCreating) } From 4567c8651b3915f8a5987a5ee5e25856ef261280 Mon Sep 17 00:00:00 2001 From: Itay Paz Date: Mon, 12 Aug 2024 14:50:18 +0300 Subject: [PATCH 2/4] add test to the bug fix --- test/integration/scan_test.go | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/test/integration/scan_test.go b/test/integration/scan_test.go index bddd0e976..7ac6b950e 100644 --- a/test/integration/scan_test.go +++ b/test/integration/scan_test.go @@ -8,6 +8,7 @@ import ( "context" "encoding/json" "fmt" + "github.com/google/uuid" "io" "log" "os" @@ -1838,3 +1839,43 @@ func validateCheckmarxDomains(t *testing.T, usedDomainsInTests []string) { assert.Assert(t, slices.Contains(usedDomainsInTests, domain), "Domain "+domain+" not found in used domains") } } + +func TestCreateScan_TwoScansWithSameBranchNameWithWhiteSpace_Success(t *testing.T) { + projectName := "uniqueName" + uuid.New().String() + args := []string{ + scanCommand, "create", + flag(params.ProjectName), projectName, + flag(params.SourcesFlag), Zip, + flag(params.ScanTypes), "iac-security", + flag(params.BranchFlag), "dummy_branch", + flag(params.ScanInfoFormatFlag), printer.FormatJSON, + } + _, projectID := executeCreateScan(t, args) + args2 := []string{ + scanCommand, "create", + flag(params.ProjectName), projectName, + flag(params.SourcesFlag), Zip, + flag(params.ScanTypes), "iac-security", + flag(params.BranchFlag), " dummy_branch ", + } + err, _ := executeCommand(t, args2...) + assert.NilError(t, err) + + response := listScanByProjectID(t, projectID) + assert.Assert(t, len(response) == 2) + for _, scan := range response { + assert.Equal(t, scan.Branch, "dummy_branch", "Branch name should be dummy_branch") + } +} +func listScanByProjectID(t *testing.T, projectID string) []wrappers.ScanResponseModel { + scanFilter := fmt.Sprintf("project-id=%s", projectID) + outputBuffer := executeCmdNilAssertion( + t, + "Getting the scan should pass", + "scan", scanList, flag(params.FormatFlag), printer.FormatJSON, flag(params.FilterFlag), scanFilter, + ) + // Read response from buffer + var scanList []wrappers.ScanResponseModel + _ = unmarshall(t, outputBuffer, &scanList, "Reading scan response JSON should pass") + return scanList +} From 69c10daafea66cbd320ac7f2cc8b5e069684cc8f Mon Sep 17 00:00:00 2001 From: Itay Paz Date: Tue, 13 Aug 2024 13:27:32 +0300 Subject: [PATCH 3/4] remove duplicate uuid import --- test/integration/scan_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/test/integration/scan_test.go b/test/integration/scan_test.go index 01b92884b..cd787b5d8 100644 --- a/test/integration/scan_test.go +++ b/test/integration/scan_test.go @@ -30,7 +30,6 @@ import ( "github.com/checkmarx/ast-cli/internal/services" "github.com/checkmarx/ast-cli/internal/wrappers" "github.com/checkmarx/ast-cli/internal/wrappers/configuration" - "github.com/google/uuid" "github.com/pkg/errors" "github.com/spf13/viper" asserts "github.com/stretchr/testify/assert" From f5271756074369cf68128b4b4aae7a25a1c30472 Mon Sep 17 00:00:00 2001 From: Itay Paz Date: Tue, 13 Aug 2024 13:37:03 +0300 Subject: [PATCH 4/4] create random project name for test --- test/integration/scan_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/scan_test.go b/test/integration/scan_test.go index cd787b5d8..b6d4ba755 100644 --- a/test/integration/scan_test.go +++ b/test/integration/scan_test.go @@ -1844,7 +1844,7 @@ func validateCheckmarxDomains(t *testing.T, usedDomainsInTests []string) { } func TestCreateScan_TwoScansWithSameBranchNameWithWhiteSpace_Success(t *testing.T) { - projectName := "uniqueName" + uuid.New().String() + projectName := generateRandomProjectNameForScan() args := []string{ scanCommand, "create", flag(params.ProjectName), projectName,