Skip to content

Commit

Permalink
delete NONE value from attributes in case of adding empty-tags param
Browse files Browse the repository at this point in the history
  • Loading branch information
miryamfoiferCX committed Dec 22, 2024
1 parent 06bb281 commit 159d3b6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
20 changes: 20 additions & 0 deletions internal/commands/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,29 @@ func hasNoneValueInAttribute(params map[string]string, attribute string) bool {
}

func addEmptyTagsParam(params map[string]string) {
removeNoneKeyAndValue(params)
params[commonParams.TagsEmptyQueryParam] = "true"
}

func removeNoneKeyAndValue(params map[string]string) {
removeNoneAttribute(params, commonParams.TagsKeyQueryParam)
removeNoneAttribute(params, commonParams.TagsValueQueryParam)
}

func removeNoneAttribute(params map[string]string, attribute string) {
values, exists := params[attribute]
if exists {
values = strings.ReplaceAll(values, ","+emptyTag, "")
values = strings.ReplaceAll(values, emptyTag+",", "")
values = strings.ReplaceAll(values, emptyTag, "")
if values == "" {
delete(params, attribute)
} else {
params[attribute] = values
}
}
}

func runGetProjectByIDCommand(projectsWrapper wrappers.ProjectsWrapper) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
var projectResponseModel *wrappers.ProjectResponseModel
Expand Down
19 changes: 12 additions & 7 deletions internal/commands/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package commands

import (
asserts "github.com/stretchr/testify/assert"

Check failure on line 6 in internal/commands/project_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed with -local github.com/golangci/golangci-lint (goimports)
"testing"

errorConstants "github.com/checkmarx/ast-cli/internal/constants/errors"
Expand Down Expand Up @@ -211,7 +212,7 @@ func TestSupportEmptyTags_whenTagsFlagsNotExists_shouldNotChangeParams(t *testin
assert.Equal(t, len(params), 2)
}

func TestSupportEmptyTags_whenTagsFlagsHasOnlyEmptyValues_shouldAddEmptyTagParam(t *testing.T) {
func TestSupportEmptyTags_whenTagsFlagsHasOnlyEmptyValues_shouldAddEmptyTagParamAndRemoveNoneTags(t *testing.T) {
params := map[string]string{
"limit": "10",
"ids": "1,2,3",
Expand All @@ -223,13 +224,15 @@ func TestSupportEmptyTags_whenTagsFlagsHasOnlyEmptyValues_shouldAddEmptyTagParam

assert.Equal(t, params["limit"], "10")
assert.Equal(t, params["ids"], "1,2,3")
assert.Equal(t, params["tags-keys"], emptyTag)
assert.Equal(t, params["tags-values"], emptyTag)
assert.Equal(t, params["empty-tags"], "true")
assert.Equal(t, len(params), 5)
_, existsKey := params["tags-keys"]
_, existsValue := params["tags-values"]
asserts.False(t, existsKey, "tags-keys should not exist")
asserts.False(t, existsValue, "tags-values should not exist")
assert.Equal(t, len(params), 3)
}

func TestSupportEmptyTags_whenTagsFlagsHaveAlsoEmptyValues_shouldAddEmptyTagParam(t *testing.T) {
func TestSupportEmptyTags_whenTagsFlagsHaveAlsoEmptyValues_shouldAddEmptyTagParamAndRemoveNoneTags(t *testing.T) {
params := map[string]string{
"limit": "10",
"ids": "1,2,3",
Expand All @@ -241,8 +244,10 @@ func TestSupportEmptyTags_whenTagsFlagsHaveAlsoEmptyValues_shouldAddEmptyTagPara

assert.Equal(t, params["limit"], "10")
assert.Equal(t, params["ids"], "1,2,3")
assert.Equal(t, params["tags-keys"], "key1,key2,"+emptyTag)
assert.Equal(t, params["tags-values"], emptyTag+",value1")
keys, _ := params["tags-keys"]

Check failure on line 247 in internal/commands/project_test.go

View workflow job for this annotation

GitHub Actions / lint

S1005: unnecessary assignment to the blank identifier (gosimple)
values, _ := params["tags-values"]

Check failure on line 248 in internal/commands/project_test.go

View workflow job for this annotation

GitHub Actions / lint

S1005: unnecessary assignment to the blank identifier (gosimple)
assert.Equal(t, keys, "key1,key2")
assert.Equal(t, values, "value1")
assert.Equal(t, params["empty-tags"], "true")
assert.Equal(t, len(params), 5)
}
Expand Down

0 comments on commit 159d3b6

Please sign in to comment.