diff --git a/pkg/cluster/EnvironmentService.go b/pkg/cluster/EnvironmentService.go index 40db732bce..0bbba767f7 100644 --- a/pkg/cluster/EnvironmentService.go +++ b/pkg/cluster/EnvironmentService.go @@ -392,8 +392,11 @@ func (impl EnvironmentServiceImpl) GetEnvironmentListForAutocomplete(isDeploymen allowedDeploymentConfigString []string ) deploymentConfigValues, _ := impl.attributesRepository.FindByKey(fmt.Sprintf("%d", model.Id)) - if err = json.Unmarshal([]byte(deploymentConfigValues.Value), &deploymentConfig); err != nil { - return nil, err + //if empty config received(doesn't exist in table) which can't be parsed + if deploymentConfigValues.Value != "" { + if err = json.Unmarshal([]byte(deploymentConfigValues.Value), &deploymentConfig); err != nil { + return nil, err + } } // if real config along with absurd values exist in table {"argo_cd": true, "helm": false, "absurd": false}", diff --git a/pkg/pipeline/PipelineBuilder.go b/pkg/pipeline/PipelineBuilder.go index 7ee0e7c119..39800b3e2b 100644 --- a/pkg/pipeline/PipelineBuilder.go +++ b/pkg/pipeline/PipelineBuilder.go @@ -1856,13 +1856,16 @@ func (impl PipelineBuilderImpl) CreateCdPipelines(pipelineCreateRequest *bean.Cd func (impl PipelineBuilderImpl) validateDeploymentAppType(pipeline *bean.CDPipelineConfigObject) error { var deploymentConfig map[string]bool deploymentConfigValues, _ := impl.attributesRepository.FindByKey(fmt.Sprintf("%d", pipeline.EnvironmentId)) - if err := json.Unmarshal([]byte(deploymentConfigValues.Value), &deploymentConfig); err != nil { - rerr := &util.ApiError{ - HttpStatusCode: http.StatusInternalServerError, - InternalMessage: err.Error(), - UserMessage: "Failed to fetch deployment config values from the attributes table", + //if empty config received(doesn't exist in table) which can't be parsed + if deploymentConfigValues.Value != "" { + if err := json.Unmarshal([]byte(deploymentConfigValues.Value), &deploymentConfig); err != nil { + rerr := &util.ApiError{ + HttpStatusCode: http.StatusInternalServerError, + InternalMessage: err.Error(), + UserMessage: "Failed to fetch deployment config values from the attributes table", + } + return rerr } - return rerr } // Config value doesn't exist in attribute table diff --git a/pkg/pipeline/PipelineBuilder_test.go b/pkg/pipeline/PipelineBuilder_test.go index 1b6ff87632..34f8dc32d7 100644 --- a/pkg/pipeline/PipelineBuilder_test.go +++ b/pkg/pipeline/PipelineBuilder_test.go @@ -28,7 +28,32 @@ func TestPipelineBuilderImpl_validateDeploymentAppType(t *testing.T) { mockDeploymentConfigConfig := &repository.Attributes{ Id: 1, Key: "2", - Value: "", + Value: "{\"argo_cd\": true, \"helm\": true}", + Active: false, + AuditLog: sql.AuditLog{}, + } + mockError := error(nil) + attributesRepoMock.On("FindByKey", mock.Anything).Return(mockDeploymentConfigConfig, mockError) + + err := impl.validateDeploymentAppType(pipeline) + assert.Nil(t, err) + }) + + t.Run("JsonUnmarshalThrowsErrorParsingDeploymentConfigValue", func(t *testing.T) { + attributesRepoMock := mocks.NewAttributesRepository(t) + + impl := PipelineBuilderImpl{ + attributesRepository: attributesRepoMock, // Provide a mock implementation of attributesRepository + } + pipeline := &bean.CDPipelineConfigObject{ + EnvironmentId: 123, + DeploymentAppType: "SomeAppType", + } + + mockDeploymentConfigConfig := &repository.Attributes{ + Id: 1, + Key: "2", + Value: "absurd_value", Active: false, AuditLog: sql.AuditLog{}, }