From cedef63c401102688a97d382a7a6f889b52e5b10 Mon Sep 17 00:00:00 2001 From: Itay Paz Date: Sun, 2 Feb 2025 14:40:09 +0200 Subject: [PATCH 1/9] Suppot custom states sub command with -all flag --- cmd/main.go | 2 + internal/commands/predicates.go | 44 +++++++++++++++++++- internal/commands/predicates_test.go | 28 +++++++++++++ internal/commands/root.go | 3 +- internal/commands/root_test.go | 3 +- internal/params/binds.go | 1 + internal/params/envs.go | 1 + internal/params/flags.go | 1 + internal/params/keys.go | 1 + internal/wrappers/mock/custom-states-mock.go | 19 +++++++++ internal/wrappers/predicates-http.go | 44 ++++++++++++++++++++ internal/wrappers/predicates.go | 13 ++++++ 12 files changed, 156 insertions(+), 4 deletions(-) create mode 100644 internal/wrappers/mock/custom-states-mock.go diff --git a/cmd/main.go b/cmd/main.go index b809e61fc..5df3c62ff 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -55,6 +55,7 @@ func main() { accessManagementPath := viper.GetString(params.AccessManagementPathKey) byorPath := viper.GetString(params.ByorPathKey) + customStatesWrapper := wrappers.NewCustomStatesHTTPWrapper() scansWrapper := wrappers.NewHTTPScansWrapper(scans) resultsPdfReportsWrapper := wrappers.NewResultsPdfReportsHTTPWrapper(resultsPdfPath) exportWrapper := wrappers.NewExportHTTPWrapper(exportPath) @@ -94,6 +95,7 @@ func main() { exportWrapper, resultsPdfReportsWrapper, resultsPredicatesWrapper, + customStatesWrapper, codeBashingWrapper, uploadsWrapper, projectsWrapper, diff --git a/internal/commands/predicates.go b/internal/commands/predicates.go index c1a565543..7c59a9660 100644 --- a/internal/commands/predicates.go +++ b/internal/commands/predicates.go @@ -1,6 +1,7 @@ package commands import ( + "fmt" "strings" "time" @@ -12,7 +13,7 @@ import ( "github.com/spf13/cobra" ) -func NewResultsPredicatesCommand(resultsPredicatesWrapper wrappers.ResultsPredicatesWrapper, featureFlagsWrapper wrappers.FeatureFlagsWrapper) *cobra.Command { +func NewResultsPredicatesCommand(resultsPredicatesWrapper wrappers.ResultsPredicatesWrapper, featureFlagsWrapper wrappers.FeatureFlagsWrapper, customStatesWrapper wrappers.CustomStatesWrapper ) *cobra.Command { triageCmd := &cobra.Command{ Use: "triage", Short: "Manage results", @@ -20,16 +21,55 @@ func NewResultsPredicatesCommand(resultsPredicatesWrapper wrappers.ResultsPredic } triageShowCmd := triageShowSubCommand(resultsPredicatesWrapper) triageUpdateCmd := triageUpdateSubCommand(resultsPredicatesWrapper, featureFlagsWrapper) + triageGetStatesCmd := triageGetStatesSubCommand(customStatesWrapper) + addFormatFlagToMultipleCommands( []*cobra.Command{triageShowCmd}, printer.FormatList, printer.FormatTable, printer.FormatJSON, ) - triageCmd.AddCommand(triageShowCmd, triageUpdateCmd) + triageCmd.AddCommand(triageShowCmd, triageUpdateCmd, triageGetStatesCmd) return triageCmd } +func triageGetStatesSubCommand(customStatesWrapper wrappers.CustomStatesWrapper) *cobra.Command { + triageGetStatesCmd := &cobra.Command{ + Use: "get-states", + Short: "Fetch and display custom states.", + Long: "Retrieves a list of custom states and prints their names.", + Example: heredoc.Doc( + ` + $ cx triage get-states + $ cx triage get-states --all + `, + ), + RunE: runTriageGetStates(customStatesWrapper), + } + + triageGetStatesCmd.PersistentFlags().Bool(params.AllStatesFlag, false, "Include deleted states") + + return triageGetStatesCmd +} + +func runTriageGetStates(customStatesWrapper wrappers.CustomStatesWrapper) func(*cobra.Command, []string) error { + return func(cmd *cobra.Command, _ []string) error { + includeDeleted, _ := cmd.Flags().GetBool(params.AllStatesFlag) + + states, err := customStatesWrapper.GetAllCustomStates(includeDeleted) + if err != nil { + return errors.Wrap(err, "Failed to fetch custom states") + } + + for _, state := range states { + fmt.Println(strings.TrimSpace(state.Name)) + } + + return nil + } +} + + func triageShowSubCommand(resultsPredicatesWrapper wrappers.ResultsPredicatesWrapper) *cobra.Command { triageShowCmd := &cobra.Command{ Use: "show", diff --git a/internal/commands/predicates_test.go b/internal/commands/predicates_test.go index 75418acbe..5805e4e18 100644 --- a/internal/commands/predicates_test.go +++ b/internal/commands/predicates_test.go @@ -4,6 +4,7 @@ package commands import ( "fmt" + "github.com/checkmarx/ast-cli/internal/wrappers" "testing" "gotest.tools/assert" @@ -48,3 +49,30 @@ func TestRunUpdateTriageCommandWithNoInput(t *testing.T) { t, err.Error() == "required flag(s) \"project-id\", \"scan-type\", \"severity\", \"similarity-id\", \"state\" not set") } + +type MockCustomStatesWrapper struct { + ReceivedIncludeDeleted bool +} + +func (m *MockCustomStatesWrapper) GetAllCustomStates(includeDeleted bool) ([]wrappers.CustomState, error) { + m.ReceivedIncludeDeleted = includeDeleted + return []wrappers.CustomState{ + {ID: 1, Name: "State1", Type: "Custom"}, + {ID: 2, Name: "State2", Type: "System"}, + }, nil +} + +func TestTriageGetStatesFlag(t *testing.T) { + mockWrapper := &MockCustomStatesWrapper{} + cmd := triageGetStatesSubCommand(mockWrapper) + + cmd.SetArgs([]string{}) + err := cmd.Execute() + assert.NilError(t, err) + assert.Equal(t, mockWrapper.ReceivedIncludeDeleted, false) + + cmd.SetArgs([]string{"--all"}) + err = cmd.Execute() + assert.NilError(t, err) + assert.Equal(t, mockWrapper.ReceivedIncludeDeleted, true) +} \ No newline at end of file diff --git a/internal/commands/root.go b/internal/commands/root.go index a440f165c..e1c747009 100644 --- a/internal/commands/root.go +++ b/internal/commands/root.go @@ -26,6 +26,7 @@ func NewAstCLI( exportWrapper wrappers.ExportWrapper, resultsPdfReportsWrapper wrappers.ResultsPdfWrapper, resultsPredicatesWrapper wrappers.ResultsPredicatesWrapper, + customStatesWrapper wrappers.CustomStatesWrapper, codeBashingWrapper wrappers.CodeBashingWrapper, uploadsWrapper wrappers.UploadsWrapper, projectsWrapper wrappers.ProjectsWrapper, @@ -195,7 +196,7 @@ func NewAstCLI( ) configCmd := util.NewConfigCommand() - triageCmd := NewResultsPredicatesCommand(resultsPredicatesWrapper, featureFlagsWrapper) + triageCmd := NewResultsPredicatesCommand(resultsPredicatesWrapper, featureFlagsWrapper, customStatesWrapper) chatCmd := NewChatCommand(chatWrapper, tenantWrapper) diff --git a/internal/commands/root_test.go b/internal/commands/root_test.go index 9fee22ce1..e2370cbff 100644 --- a/internal/commands/root_test.go +++ b/internal/commands/root_test.go @@ -66,13 +66,14 @@ func createASTTestCommand() *cobra.Command { accessManagementWrapper := &mock.AccessManagementMockWrapper{} byorWrapper := &mock.ByorMockWrapper{} containerResolverMockWrapper := &mock.ContainerResolverMockWrapper{} - + customStatesMockWrapper := &mock.CustomStatesMockWrapper{} return NewAstCLI( applicationWrapper, scansMockWrapper, exportWrapper, resultsPdfWrapper, resultsPredicatesMockWrapper, + customStatesMockWrapper, codeBashingWrapper, uploadsMockWrapper, projectsMockWrapper, diff --git a/internal/params/binds.go b/internal/params/binds.go index 75315fadd..59e2d303a 100644 --- a/internal/params/binds.go +++ b/internal/params/binds.go @@ -5,6 +5,7 @@ var EnvVarsBinds = []struct { Env string Default string }{ + {CustomStatesAPIPathKey, CustomStatesAPIPathEnv, "api/custom-states"}, {BaseURIKey, BaseURIEnv, ""}, {ProxyTypeKey, ProxyTypeEnv, "basic"}, {ProxyDomainKey, ProxyDomainEnv, ""}, diff --git a/internal/params/envs.go b/internal/params/envs.go index a4ed087ee..ffe49c23b 100644 --- a/internal/params/envs.go +++ b/internal/params/envs.go @@ -1,6 +1,7 @@ package params const ( + CustomStatesAPIPathEnv = "CX_CUSTOM_STATES_PATH" TenantEnv = "CX_TENANT" BranchEnv = "CX_BRANCH" BaseURIEnv = "CX_BASE_URI" diff --git a/internal/params/flags.go b/internal/params/flags.go index ae40c6527..ee293a97b 100644 --- a/internal/params/flags.go +++ b/internal/params/flags.go @@ -2,6 +2,7 @@ package params // Flags const ( + AllStatesFlag = "all" AgentFlag = "agent" AgentFlagUsage = "Scan origin name" ApplicationName = "application-name" diff --git a/internal/params/keys.go b/internal/params/keys.go index 37c0de34d..af31d4ea1 100644 --- a/internal/params/keys.go +++ b/internal/params/keys.go @@ -3,6 +3,7 @@ package params import "strings" var ( + CustomStatesAPIPathKey = strings.ToLower(CustomStatesAPIPathEnv) TenantKey = strings.ToLower(TenantEnv) BranchKey = strings.ToLower(BranchEnv) BaseURIKey = strings.ToLower(BaseURIEnv) diff --git a/internal/wrappers/mock/custom-states-mock.go b/internal/wrappers/mock/custom-states-mock.go new file mode 100644 index 000000000..6d9260b3d --- /dev/null +++ b/internal/wrappers/mock/custom-states-mock.go @@ -0,0 +1,19 @@ +package mock + +import "github.com/checkmarx/ast-cli/internal/wrappers" + +type CustomStatesMockWrapper struct{} + +func (m *CustomStatesMockWrapper) GetAllCustomStates(includeDeleted bool) ([]wrappers.CustomState, error) { + if includeDeleted { + return []wrappers.CustomState{ + {ID: 1, Name: "demo1", Type: "Custom"}, + {ID: 2, Name: "demo2", Type: "System"}, + {ID: 3, Name: "demo3", Type: "Custom"}, + }, nil + } + return []wrappers.CustomState{ + {ID: 2, Name: "demo2", Type: "System"}, + {ID: 3, Name: "demo3", Type: "Custom"}, + }, nil +} \ No newline at end of file diff --git a/internal/wrappers/predicates-http.go b/internal/wrappers/predicates-http.go index db1130d68..c96e1c90f 100644 --- a/internal/wrappers/predicates-http.go +++ b/internal/wrappers/predicates-http.go @@ -155,3 +155,47 @@ func handleResponseWithBody(resp *http.Response, err error) (*PredicatesCollecti func responsePredicateParsingFailed(err error) (*PredicatesCollectionResponseModel, *WebError, error) { return nil, nil, errors.Wrapf(err, failedToParsePredicates) } + + +type CustomStatesHTTPWrapper struct { +path string +} + +func NewCustomStatesHTTPWrapper() CustomStatesWrapper { + return &CustomStatesHTTPWrapper{ + path: viper.GetString(params.CustomStatesAPIPathKey), + } +} + +func (c *CustomStatesHTTPWrapper) GetAllCustomStates(includeDeleted bool) ([]CustomState, error) { + clientTimeout := viper.GetUint(params.ClientTimeoutKey) + + if c.path == "" { + return nil, errors.New("CustomStatesAPIPathKey is not set") + } + + requestURL := c.path + if includeDeleted { + requestURL += "?include-deleted=true" + } + + logger.PrintIfVerbose(fmt.Sprintf("Fetching custom states from: %s", requestURL)) + + resp, err := SendHTTPRequest(http.MethodGet, requestURL, http.NoBody, true, clientTimeout) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return nil, errors.Errorf("Failed to fetch custom states. HTTP status: %d", resp.StatusCode) + } + + var states []CustomState + err = json.NewDecoder(resp.Body).Decode(&states) + if err != nil { + return nil, errors.Wrap(err, "Failed to parse custom states response") + } + + return states, nil +} \ No newline at end of file diff --git a/internal/wrappers/predicates.go b/internal/wrappers/predicates.go index 7f01737e5..f530a0006 100644 --- a/internal/wrappers/predicates.go +++ b/internal/wrappers/predicates.go @@ -39,6 +39,19 @@ type PredicatesCollectionResponseModel struct { TotalCount int `json:"totalCount"` } +type CustomState struct { + ID int `json:"id"` + Name string `json:"name"` + Type string `json:"type"` +} + + + +type CustomStatesWrapper interface { + GetAllCustomStates(includeDeleted bool) ([]CustomState, error) +} + + type ResultsPredicatesWrapper interface { PredicateSeverityAndState(predicate *PredicateRequest, scanType string) (*WebError, error) GetAllPredicatesForSimilarityID( From d6aac2d4f7b500b02105b18a777e157f75f1df3f Mon Sep 17 00:00:00 2001 From: Itay Paz Date: Sun, 2 Feb 2025 16:09:54 +0200 Subject: [PATCH 2/9] util_command.go file --- test/integration/util_command.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/integration/util_command.go b/test/integration/util_command.go index b51abd0ca..878370fc2 100644 --- a/test/integration/util_command.go +++ b/test/integration/util_command.go @@ -90,6 +90,7 @@ func createASTIntegrationTestCommand(t *testing.T) *cobra.Command { applicationsWrapper := wrappers.NewApplicationsHTTPWrapper(applications) resultsPdfReportsWrapper := wrappers.NewResultsPdfReportsHTTPWrapper(resultsPdfPath) exportWrapper := wrappers.NewExportHTTPWrapper(exportPath) + customStatesWrapper := wrappers.NewCustomStatesHTTPWrapper() resultsPredicatesWrapper := wrappers.NewResultsPredicatesHTTPWrapper() groupsWrapper := wrappers.NewHTTPGroupsWrapper(groups) @@ -125,6 +126,7 @@ func createASTIntegrationTestCommand(t *testing.T) *cobra.Command { exportWrapper, resultsPdfReportsWrapper, resultsPredicatesWrapper, + customStatesWrapper, codeBashingWrapper, uploadsWrapper, projectsWrapper, @@ -212,7 +214,7 @@ func executeCmdWithTimeOutNilAssertion( func executeWithTimeout(cmd *cobra.Command, timeout time.Duration, args ...string) error { args = append(args, flag(params.RetryFlag), "3", flag(params.RetryDelayFlag), "5") - args = appendProxyArgs(args) + //args = appendProxyArgs(args) cmd.SetArgs(args) ctx, cancel := context.WithTimeout(context.Background(), timeout) From f7d0baa7ee592147fc89e1d78dd2728d800928f9 Mon Sep 17 00:00:00 2001 From: Itay Paz Date: Sun, 2 Feb 2025 16:12:52 +0200 Subject: [PATCH 3/9] fix pr --- test/integration/util_command.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/util_command.go b/test/integration/util_command.go index 878370fc2..243186860 100644 --- a/test/integration/util_command.go +++ b/test/integration/util_command.go @@ -214,7 +214,7 @@ func executeCmdWithTimeOutNilAssertion( func executeWithTimeout(cmd *cobra.Command, timeout time.Duration, args ...string) error { args = append(args, flag(params.RetryFlag), "3", flag(params.RetryDelayFlag), "5") - //args = appendProxyArgs(args) + args = appendProxyArgs(args) cmd.SetArgs(args) ctx, cancel := context.WithTimeout(context.Background(), timeout) From 88188e367c20158f7cabbf1ca747a9f3ecb6933f Mon Sep 17 00:00:00 2001 From: Itay Paz Date: Mon, 3 Feb 2025 10:29:40 +0200 Subject: [PATCH 4/9] fix runTriageGetStates function --- internal/commands/predicates.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/internal/commands/predicates.go b/internal/commands/predicates.go index 7c59a9660..b698b570e 100644 --- a/internal/commands/predicates.go +++ b/internal/commands/predicates.go @@ -1,7 +1,6 @@ package commands import ( - "fmt" "strings" "time" @@ -55,21 +54,20 @@ func triageGetStatesSubCommand(customStatesWrapper wrappers.CustomStatesWrapper) func runTriageGetStates(customStatesWrapper wrappers.CustomStatesWrapper) func(*cobra.Command, []string) error { return func(cmd *cobra.Command, _ []string) error { includeDeleted, _ := cmd.Flags().GetBool(params.AllStatesFlag) - states, err := customStatesWrapper.GetAllCustomStates(includeDeleted) if err != nil { return errors.Wrap(err, "Failed to fetch custom states") } - - for _, state := range states { - fmt.Println(strings.TrimSpace(state.Name)) + err = printer.Print(cmd.OutOrStdout(), states, printer.FormatJSON) + if err != nil { + return err } - return nil } } + func triageShowSubCommand(resultsPredicatesWrapper wrappers.ResultsPredicatesWrapper) *cobra.Command { triageShowCmd := &cobra.Command{ Use: "show", From 53bbc4b240afbd98f57e27008844f410257dcb5d Mon Sep 17 00:00:00 2001 From: Itay Paz Date: Mon, 3 Feb 2025 10:53:22 +0200 Subject: [PATCH 5/9] fix test file --- internal/commands/predicates_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/internal/commands/predicates_test.go b/internal/commands/predicates_test.go index 5805e4e18..97545e582 100644 --- a/internal/commands/predicates_test.go +++ b/internal/commands/predicates_test.go @@ -65,12 +65,10 @@ func (m *MockCustomStatesWrapper) GetAllCustomStates(includeDeleted bool) ([]wra func TestTriageGetStatesFlag(t *testing.T) { mockWrapper := &MockCustomStatesWrapper{} cmd := triageGetStatesSubCommand(mockWrapper) - cmd.SetArgs([]string{}) err := cmd.Execute() assert.NilError(t, err) assert.Equal(t, mockWrapper.ReceivedIncludeDeleted, false) - cmd.SetArgs([]string{"--all"}) err = cmd.Execute() assert.NilError(t, err) From a48ac9b166732dee4d1adfc3f01c628e4ef3a486 Mon Sep 17 00:00:00 2001 From: Itay Paz Date: Mon, 3 Feb 2025 11:13:53 +0200 Subject: [PATCH 6/9] add const to include deleted flag --- internal/params/flags.go | 3 +++ internal/wrappers/predicates-http.go | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/params/flags.go b/internal/params/flags.go index 570060346..2461be4f7 100644 --- a/internal/params/flags.go +++ b/internal/params/flags.go @@ -282,3 +282,6 @@ const ScaAgent = "SCA_AGENT" var ( Version = "dev" ) + +// Custom states +const IncludeDeleted = "?include-deleted=true" diff --git a/internal/wrappers/predicates-http.go b/internal/wrappers/predicates-http.go index c96e1c90f..ad8aa4fcf 100644 --- a/internal/wrappers/predicates-http.go +++ b/internal/wrappers/predicates-http.go @@ -176,7 +176,7 @@ func (c *CustomStatesHTTPWrapper) GetAllCustomStates(includeDeleted bool) ([]Cus requestURL := c.path if includeDeleted { - requestURL += "?include-deleted=true" + requestURL += params.IncludeDeleted } logger.PrintIfVerbose(fmt.Sprintf("Fetching custom states from: %s", requestURL)) From 7734cc5794d6dccd98c523c1f74a10c4e558d898 Mon Sep 17 00:00:00 2001 From: Itay Paz Date: Mon, 3 Feb 2025 11:28:42 +0200 Subject: [PATCH 7/9] change const name to querypharms --- internal/params/flags.go | 2 +- internal/wrappers/predicates-http.go | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/internal/params/flags.go b/internal/params/flags.go index 2461be4f7..abb278e94 100644 --- a/internal/params/flags.go +++ b/internal/params/flags.go @@ -284,4 +284,4 @@ var ( ) // Custom states -const IncludeDeleted = "?include-deleted=true" +const IncludeDeletedQueryParam = "?include-deleted=true" diff --git a/internal/wrappers/predicates-http.go b/internal/wrappers/predicates-http.go index ad8aa4fcf..51acbdd93 100644 --- a/internal/wrappers/predicates-http.go +++ b/internal/wrappers/predicates-http.go @@ -176,7 +176,7 @@ func (c *CustomStatesHTTPWrapper) GetAllCustomStates(includeDeleted bool) ([]Cus requestURL := c.path if includeDeleted { - requestURL += params.IncludeDeleted + requestURL += params.IncludeDeletedQueryParam } logger.PrintIfVerbose(fmt.Sprintf("Fetching custom states from: %s", requestURL)) @@ -196,6 +196,5 @@ func (c *CustomStatesHTTPWrapper) GetAllCustomStates(includeDeleted bool) ([]Cus if err != nil { return nil, errors.Wrap(err, "Failed to parse custom states response") } - return states, nil } \ No newline at end of file From e68ca28f58ed21fd14a638578ea16bbbdba5488a Mon Sep 17 00:00:00 2001 From: Itay Paz Date: Mon, 3 Feb 2025 14:43:15 +0200 Subject: [PATCH 8/9] fix pr --- internal/commands/predicates_test.go | 24 +++++---------- internal/params/binds.go | 2 +- internal/params/flags.go | 3 +- internal/wrappers/predicates-http.go | 46 ++++++++++++++++++++++------ 4 files changed, 48 insertions(+), 27 deletions(-) diff --git a/internal/commands/predicates_test.go b/internal/commands/predicates_test.go index 97545e582..e21952d3d 100644 --- a/internal/commands/predicates_test.go +++ b/internal/commands/predicates_test.go @@ -4,7 +4,7 @@ package commands import ( "fmt" - "github.com/checkmarx/ast-cli/internal/wrappers" + "github.com/checkmarx/ast-cli/internal/wrappers/mock" "testing" "gotest.tools/assert" @@ -50,27 +50,19 @@ func TestRunUpdateTriageCommandWithNoInput(t *testing.T) { err.Error() == "required flag(s) \"project-id\", \"scan-type\", \"severity\", \"similarity-id\", \"state\" not set") } -type MockCustomStatesWrapper struct { - ReceivedIncludeDeleted bool -} - -func (m *MockCustomStatesWrapper) GetAllCustomStates(includeDeleted bool) ([]wrappers.CustomState, error) { - m.ReceivedIncludeDeleted = includeDeleted - return []wrappers.CustomState{ - {ID: 1, Name: "State1", Type: "Custom"}, - {ID: 2, Name: "State2", Type: "System"}, - }, nil -} - func TestTriageGetStatesFlag(t *testing.T) { - mockWrapper := &MockCustomStatesWrapper{} + mockWrapper := &mock.CustomStatesMockWrapper{} cmd := triageGetStatesSubCommand(mockWrapper) cmd.SetArgs([]string{}) err := cmd.Execute() assert.NilError(t, err) - assert.Equal(t, mockWrapper.ReceivedIncludeDeleted, false) + states, err := mockWrapper.GetAllCustomStates(false) + assert.NilError(t, err) + assert.Equal(t, len(states), 2) cmd.SetArgs([]string{"--all"}) err = cmd.Execute() assert.NilError(t, err) - assert.Equal(t, mockWrapper.ReceivedIncludeDeleted, true) + states, err = mockWrapper.GetAllCustomStates(true) + assert.NilError(t, err) + assert.Equal(t, len(states), 3) } \ No newline at end of file diff --git a/internal/params/binds.go b/internal/params/binds.go index 59e2d303a..772b6bf29 100644 --- a/internal/params/binds.go +++ b/internal/params/binds.go @@ -5,7 +5,6 @@ var EnvVarsBinds = []struct { Env string Default string }{ - {CustomStatesAPIPathKey, CustomStatesAPIPathEnv, "api/custom-states"}, {BaseURIKey, BaseURIEnv, ""}, {ProxyTypeKey, ProxyTypeEnv, "basic"}, {ProxyDomainKey, ProxyDomainEnv, ""}, @@ -14,6 +13,7 @@ var EnvVarsBinds = []struct { {IgnoreProxyKey, IgnoreProxyEnv, ""}, {AgentNameKey, AgentNameEnv, "ASTCLI"}, {CodeBashingPathKey, ScansPathEnv, "api/codebashing/lessons"}, + {CustomStatesAPIPathKey, CustomStatesAPIPathEnv, "api/custom-states"}, {ScansPathKey, ScansPathEnv, "api/scans"}, {ProjectsPathKey, ProjectsPathEnv, "api/projects"}, {ApplicationsPathKey, ApplicationsPathEnv, "api/applications"}, diff --git a/internal/params/flags.go b/internal/params/flags.go index abb278e94..02cacddb1 100644 --- a/internal/params/flags.go +++ b/internal/params/flags.go @@ -284,4 +284,5 @@ var ( ) // Custom states -const IncludeDeletedQueryParam = "?include-deleted=true" +const IncludeDeletedQueryParam = "include-deleted" +const True = "true" diff --git a/internal/wrappers/predicates-http.go b/internal/wrappers/predicates-http.go index 51acbdd93..829746dc4 100644 --- a/internal/wrappers/predicates-http.go +++ b/internal/wrappers/predicates-http.go @@ -167,34 +167,62 @@ func NewCustomStatesHTTPWrapper() CustomStatesWrapper { } } +//func (c *CustomStatesHTTPWrapper) GetAllCustomStates(includeDeleted bool) ([]CustomState, error) { +// clientTimeout := viper.GetUint(params.ClientTimeoutKey) +// +// if c.path == "" { +// return nil, errors.New("CustomStatesAPIPathKey is not set") +// } +// +// requestURL := c.path +// if includeDeleted { +// requestURL += params.IncludeDeletedQueryParam +// } +// +// logger.PrintIfVerbose(fmt.Sprintf("Fetching custom states from: %s", requestURL)) +// +// resp, err := SendHTTPRequest(http.MethodGet, requestURL, http.NoBody, true, clientTimeout) +// if err != nil { +// return nil, err +// } +// defer resp.Body.Close() +// +// if resp.StatusCode != http.StatusOK { +// return nil, errors.Errorf("Failed to fetch custom states. HTTP status: %d", resp.StatusCode) +// } +// +// var states []CustomState +// err = json.NewDecoder(resp.Body).Decode(&states) +// if err != nil { +// return nil, errors.Wrap(err, "Failed to parse custom states response") +// } +// return states, nil +//} + func (c *CustomStatesHTTPWrapper) GetAllCustomStates(includeDeleted bool) ([]CustomState, error) { clientTimeout := viper.GetUint(params.ClientTimeoutKey) if c.path == "" { return nil, errors.New("CustomStatesAPIPathKey is not set") } - - requestURL := c.path + queryParams := make(map[string]string) if includeDeleted { - requestURL += params.IncludeDeletedQueryParam + queryParams[params.IncludeDeletedQueryParam] = params.True } - logger.PrintIfVerbose(fmt.Sprintf("Fetching custom states from: %s", requestURL)) - - resp, err := SendHTTPRequest(http.MethodGet, requestURL, http.NoBody, true, clientTimeout) + logger.PrintIfVerbose(fmt.Sprintf("Fetching custom states from: %s with params: %v", c.path, queryParams)) + resp, err := SendHTTPRequestWithQueryParams(http.MethodGet, c.path, queryParams, http.NoBody, clientTimeout) if err != nil { return nil, err } defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { return nil, errors.Errorf("Failed to fetch custom states. HTTP status: %d", resp.StatusCode) } - var states []CustomState err = json.NewDecoder(resp.Body).Decode(&states) if err != nil { return nil, errors.Wrap(err, "Failed to parse custom states response") } return states, nil -} \ No newline at end of file +} From 6f1942ad3da74b03a5878ead86e95018de582288 Mon Sep 17 00:00:00 2001 From: Itay Paz Date: Mon, 3 Feb 2025 15:18:03 +0200 Subject: [PATCH 9/9] fix pr --- internal/commands/predicates.go | 5 +---- internal/wrappers/predicates-http.go | 32 ---------------------------- 2 files changed, 1 insertion(+), 36 deletions(-) diff --git a/internal/commands/predicates.go b/internal/commands/predicates.go index b698b570e..6781ae2b3 100644 --- a/internal/commands/predicates.go +++ b/internal/commands/predicates.go @@ -59,10 +59,7 @@ func runTriageGetStates(customStatesWrapper wrappers.CustomStatesWrapper) func(* return errors.Wrap(err, "Failed to fetch custom states") } err = printer.Print(cmd.OutOrStdout(), states, printer.FormatJSON) - if err != nil { - return err - } - return nil + return err } } diff --git a/internal/wrappers/predicates-http.go b/internal/wrappers/predicates-http.go index 829746dc4..d58d7de4b 100644 --- a/internal/wrappers/predicates-http.go +++ b/internal/wrappers/predicates-http.go @@ -167,38 +167,6 @@ func NewCustomStatesHTTPWrapper() CustomStatesWrapper { } } -//func (c *CustomStatesHTTPWrapper) GetAllCustomStates(includeDeleted bool) ([]CustomState, error) { -// clientTimeout := viper.GetUint(params.ClientTimeoutKey) -// -// if c.path == "" { -// return nil, errors.New("CustomStatesAPIPathKey is not set") -// } -// -// requestURL := c.path -// if includeDeleted { -// requestURL += params.IncludeDeletedQueryParam -// } -// -// logger.PrintIfVerbose(fmt.Sprintf("Fetching custom states from: %s", requestURL)) -// -// resp, err := SendHTTPRequest(http.MethodGet, requestURL, http.NoBody, true, clientTimeout) -// if err != nil { -// return nil, err -// } -// defer resp.Body.Close() -// -// if resp.StatusCode != http.StatusOK { -// return nil, errors.Errorf("Failed to fetch custom states. HTTP status: %d", resp.StatusCode) -// } -// -// var states []CustomState -// err = json.NewDecoder(resp.Body).Decode(&states) -// if err != nil { -// return nil, errors.Wrap(err, "Failed to parse custom states response") -// } -// return states, nil -//} - func (c *CustomStatesHTTPWrapper) GetAllCustomStates(includeDeleted bool) ([]CustomState, error) { clientTimeout := viper.GetUint(params.ClientTimeoutKey)