diff --git a/api/restHandler/app/BuildPipelineRestHandler.go b/api/restHandler/app/BuildPipelineRestHandler.go index 056a6e52fb4..105412812d2 100644 --- a/api/restHandler/app/BuildPipelineRestHandler.go +++ b/api/restHandler/app/BuildPipelineRestHandler.go @@ -1834,22 +1834,13 @@ func (handler PipelineConfigRestHandlerImpl) CreateUpdateImageTagging(w http.Res return } - pipelineId, err := strconv.Atoi(vars["ciPipelineId"]) - if err != nil { - common.WriteJsonResp(w, err, nil, http.StatusBadRequest) - return - } - externalCi, ciPipelineId, appId, err := handler.extractCipipelineMetaForImageTags(artifactId) if err != nil { handler.Logger.Errorw("error occurred in fetching extractCipipelineMetaForImageTags by artifact Id ", "err", err, "artifactId", artifactId) common.WriteJsonResp(w, err, "Unauthorized User", http.StatusInternalServerError) return } - if !externalCi && (ciPipelineId != pipelineId) { - common.WriteJsonResp(w, errors.New("ciPipelineId and artifactId sent in the request are not related"), nil, http.StatusBadRequest) - return - } + decoder := json.NewDecoder(r.Body) req := &types.ImageTaggingRequestDTO{} err = decoder.Decode(&req) diff --git a/internal/sql/repository/CiArtifactRepository.go b/internal/sql/repository/CiArtifactRepository.go index 32dd10ea270..380dfe8e2e9 100644 --- a/internal/sql/repository/CiArtifactRepository.go +++ b/internal/sql/repository/CiArtifactRepository.go @@ -310,11 +310,11 @@ func (impl CiArtifactRepositoryImpl) GetArtifactsByCDPipelineV3(listingFilterOpt if len(artifacts) == 0 { return artifacts, totalCount, nil } - artifacts, err = impl.setDeployedDataInArtifacts(artifacts) + artifacts, err = impl.setDeployedDataInArtifacts(listingFilterOpts.PipelineId, artifacts) return artifacts, totalCount, err } -func (impl CiArtifactRepositoryImpl) setDeployedDataInArtifacts(artifacts []*CiArtifact) ([]*CiArtifact, error) { +func (impl CiArtifactRepositoryImpl) setDeployedDataInArtifacts(pipelineId int, artifacts []*CiArtifact) ([]*CiArtifact, error) { //processing artifactsMap := make(map[int]*CiArtifact) artifactsIds := make([]int, 0, len(artifacts)) @@ -332,7 +332,7 @@ func (impl CiArtifactRepositoryImpl) setDeployedDataInArtifacts(artifacts []*CiA " AND cia.id IN (?) " + " ORDER BY pco.id desc;" - _, err := impl.dbConnection.Query(&artifactsDeployed, query, pg.In(artifactsIds)) + _, err := impl.dbConnection.Query(&artifactsDeployed, query, pipelineId, pg.In(artifactsIds)) if err != nil { return artifacts, nil } diff --git a/internal/sql/repository/pipelineConfig/CiPipelineRepository.go b/internal/sql/repository/pipelineConfig/CiPipelineRepository.go index c0631f1217e..ff49491d7e0 100644 --- a/internal/sql/repository/pipelineConfig/CiPipelineRepository.go +++ b/internal/sql/repository/pipelineConfig/CiPipelineRepository.go @@ -101,6 +101,8 @@ type CiPipelineRepository interface { FindByAppId(appId int) (pipelines []*CiPipeline, err error) FindCiPipelineByAppIdAndEnvIds(appId int, envIds []int) ([]*CiPipeline, error) FindByAppIds(appIds []int) (pipelines []*CiPipeline, err error) + //find any pipeline by id, includes soft deleted as well + FindByIdIncludingInActive(id int) (pipeline *CiPipeline, err error) //find non deleted pipeline FindById(id int) (pipeline *CiPipeline, err error) FindCiEnvMappingByCiPipelineId(ciPipelineId int) (*CiEnvMapping, error) @@ -297,6 +299,19 @@ func (impl CiPipelineRepositoryImpl) SaveCiPipelineScript(ciPipelineScript *CiPi return tx.Insert(ciPipelineScript) } +func (impl CiPipelineRepositoryImpl) FindByIdIncludingInActive(id int) (pipeline *CiPipeline, err error) { + pipeline = &CiPipeline{Id: id} + err = impl.dbConnection.Model(pipeline). + Column("ci_pipeline.*", "App", "CiPipelineMaterials", "CiTemplate", "CiTemplate.DockerRegistry", "CiPipelineMaterials.GitMaterial"). + Relation("CiPipelineMaterials", func(q *orm.Query) (query *orm.Query, err error) { + return q.Where("(ci_pipeline_material.active=true)"), nil + }). + Where("ci_pipeline.id= ?", id). + Select() + + return pipeline, err +} + func (impl CiPipelineRepositoryImpl) FindById(id int) (pipeline *CiPipeline, err error) { pipeline = &CiPipeline{Id: id} err = impl.dbConnection.Model(pipeline). @@ -545,7 +560,7 @@ func (impl CiPipelineRepositoryImpl) GetCiPipelineByArtifactId(artifactId int) ( err := impl.dbConnection.Model(ciPipeline). Column("ci_pipeline.*"). Join("INNER JOIN ci_artifact cia on cia.pipeline_id = ci_pipeline.id"). - Where("ci_pipeline.deleted=?", false). + //Where("ci_pipeline.deleted=?", false). Where("cia.id = ?", artifactId). Select() return ciPipeline, err diff --git a/pkg/bulkAction/BulkUpdateService.go b/pkg/bulkAction/BulkUpdateService.go index 398643f82cf..69cdaf30563 100644 --- a/pkg/bulkAction/BulkUpdateService.go +++ b/pkg/bulkAction/BulkUpdateService.go @@ -1052,10 +1052,10 @@ func (impl BulkUpdateServiceImpl) BulkHibernate(request *BulkApplicationForEnvir impl.logger.Infow("application already hibernated", "app_id", pipeline.AppId) pipelineResponse := response[appKey] pipelineResponse[pipelineKey] = false - if deploymentHistory.Status == application.Progressing { - pipelineResponse[Skipped] = "Hibernation already in progress" - } else { + if deploymentHistory.Status == application.HIBERNATING { pipelineResponse[Skipped] = "Application is already hibernated" + } else { + pipelineResponse[Skipped] = "Hibernation already in progress" } response[appKey] = pipelineResponse continue @@ -1203,14 +1203,15 @@ func (impl BulkUpdateServiceImpl) BulkUnHibernate(request *BulkApplicationForEnv continue } deploymentHistory := deploymentTypeMap[pipeline.Id] - if deploymentHistory.DeploymentType == models.DEPLOYMENTTYPE_START { + if deploymentHistory.DeploymentType == models.DEPLOYMENTTYPE_START || + deploymentHistory.DeploymentType == models.DEPLOYMENTTYPE_DEPLOY { impl.logger.Infow("application already UnHibernated", "app_id", pipeline.AppId) pipelineResponse := response[appKey] pipelineResponse[pipelineKey] = false - if deploymentHistory.Status == application.Progressing { - pipelineResponse[Skipped] = "Un-hibernation already in progress" - } else { + if deploymentHistory.Status == application.Healthy { pipelineResponse[Skipped] = "Application is already un-hibernated" + } else { + pipelineResponse[Skipped] = "Un-hibernation already in progress" } response[appKey] = pipelineResponse continue diff --git a/pkg/pipeline/AppArtifactManager.go b/pkg/pipeline/AppArtifactManager.go index b709eb721f4..1ded1d4c131 100644 --- a/pkg/pipeline/AppArtifactManager.go +++ b/pkg/pipeline/AppArtifactManager.go @@ -321,8 +321,8 @@ func (impl *AppArtifactManagerImpl) FetchArtifactForRollbackV2(cdPipelineId, app if deployedCiArtifacts[i].CredentialsSourceType == repository.GLOBAL_CONTAINER_REGISTRY { dockerRegistryId = deployedCiArtifacts[i].CredentialsSourceValue } - } else { - ciPipeline, err := impl.CiPipelineRepository.FindById(deployedCiArtifacts[i].CiPipelineId) + } else if deployedCiArtifacts[i].DataSource == repository.CI_RUNNER { + ciPipeline, err := impl.CiPipelineRepository.FindByIdIncludingInActive(deployedCiArtifacts[i].CiPipelineId) if err != nil { impl.logger.Errorw("error in fetching ciPipeline", "ciPipelineId", ciPipeline.Id, "error", err) return deployedCiArtifactsResponse, err @@ -511,7 +511,7 @@ func (impl *AppArtifactManagerImpl) RetrieveArtifactsByCDPipeline(pipeline *pipe } var dockerRegistryId string if artifact.PipelineId != 0 { - ciPipeline, err := impl.CiPipelineRepository.FindById(artifact.PipelineId) + ciPipeline, err := impl.CiPipelineRepository.FindByIdIncludingInActive(artifact.PipelineId) if err != nil { impl.logger.Errorw("error in fetching ciPipeline", "ciPipelineId", ciPipeline.Id, "error", err) return nil, err @@ -677,9 +677,10 @@ func (impl *AppArtifactManagerImpl) setAdditionalDataInArtifacts(ciArtifacts []b dockerRegistryId = ciArtifacts[i].CredentialsSourceValue } } else if ciArtifacts[i].DataSource == repository.CI_RUNNER { - ciPipeline, err := impl.CiPipelineRepository.FindById(ciArtifacts[i].CiPipelineId) + //need this if the artifact's ciPipeline gets switched, then the previous ci-pipeline will be in deleted state + ciPipeline, err := impl.CiPipelineRepository.FindByIdIncludingInActive(ciArtifacts[i].CiPipelineId) if err != nil { - impl.logger.Errorw("error in fetching ciPipeline", "ciPipelineId", ciPipeline.Id, "error", err) + impl.logger.Errorw("error in fetching ciPipeline", "ciPipelineId", ciArtifacts[i].CiPipelineId, "error", err) return nil, err } if !ciPipeline.IsExternal && ciPipeline.IsDockerConfigOverridden { diff --git a/pkg/pipeline/BuildPipelineSwitchService.go b/pkg/pipeline/BuildPipelineSwitchService.go index d5535446fc1..3aae2a5fb67 100644 --- a/pkg/pipeline/BuildPipelineSwitchService.go +++ b/pkg/pipeline/BuildPipelineSwitchService.go @@ -271,23 +271,7 @@ func (impl *BuildPipelineSwitchServiceImpl) deleteBuildPipeline(tx *pg.Tx, ciPip impl.logger.Errorw("error in deleting ci pipeline and its env mappings", "pipelineId", ciPipeline.Id, "err", err) return err } - materials, err := impl.DeleteCiMaterial(tx, ciPipeline) - if err != nil { - return err - } - if !ciPipeline.IsDockerConfigOverridden { - err = impl.ciCdPipelineOrchestrator.SaveHistoryOfBaseTemplate(userId, ciPipeline, materials) - if err != nil { - impl.logger.Errorw("error in saving history of base template", "pipelineId", ciPipeline.Id, "err", err) - return err - } - } else if ciPipeline.ParentCiPipeline == 0 { - err = impl.saveHistoryOfOverriddenTemplate(ciPipeline, userId, materials) - if err != nil { - impl.logger.Errorw("error in saving history for overridden template", "pipelineId", ciPipeline.Id, "err", err) - return err - } - } + //not deleting ciPipeline material or template override as these can be useful for artifact built from the old ciPipeline return err } diff --git a/pkg/pipeline/CiHandler.go b/pkg/pipeline/CiHandler.go index 44c40548561..19fdde0b2ea 100644 --- a/pkg/pipeline/CiHandler.go +++ b/pkg/pipeline/CiHandler.go @@ -1453,7 +1453,7 @@ func (impl *CiHandlerImpl) FetchMaterialInfoByArtifactId(ciArtifactId int, envId return &types.GitTriggerInfoResponse{}, err } - ciPipeline, err := impl.ciPipelineRepository.FindById(ciArtifact.PipelineId) + ciPipeline, err := impl.ciPipelineRepository.FindByIdIncludingInActive(ciArtifact.PipelineId) if err != nil { impl.Logger.Errorw("err", "ciArtifactId", ciArtifactId, "err", err) return &types.GitTriggerInfoResponse{}, err diff --git a/pkg/security/ImageScanService.go b/pkg/security/ImageScanService.go index f8120e59211..68433fb6463 100644 --- a/pkg/security/ImageScanService.go +++ b/pkg/security/ImageScanService.go @@ -325,7 +325,7 @@ func (impl ImageScanServiceImpl) FetchExecutionDetailResult(request *ImageScanRe impl.Logger.Errorw("error while fetching scan execution result", "err", err) return nil, err } - ciPipeline, err := impl.ciPipelineRepository.FindById(ciArtifact.PipelineId) + ciPipeline, err := impl.ciPipelineRepository.FindByIdIncludingInActive(ciArtifact.PipelineId) if err != nil { impl.Logger.Errorw("error while fetching scan execution result", "err", err) return nil, err diff --git a/scripts/sql/194_parent_ci_remove_constraint.down.sql b/scripts/sql/194_parent_ci_remove_constraint.down.sql index 51b610460b3..ec59031881e 100644 --- a/scripts/sql/194_parent_ci_remove_constraint.down.sql +++ b/scripts/sql/194_parent_ci_remove_constraint.down.sql @@ -1,4 +1 @@ - - -ALTER TABLE public.ci_pipeline - ADD CONSTRAINT ci_pipeline_parent_ci_pipeline_fkey FOREIGN KEY (parent_ci_pipeline) REFERENCES public.ci_pipeline(id); +ALTER TABLE public.ci_pipeline ADD CONSTRAINT ci_pipeline_parent_ci_pipeline_fkey FOREIGN KEY (parent_ci_pipeline) REFERENCES public.ci_pipeline(id) ; \ No newline at end of file diff --git a/scripts/sql/194_parent_ci_remove_constraint.up.sql b/scripts/sql/194_parent_ci_remove_constraint.up.sql index 75913d5e800..a0caa1be88f 100644 --- a/scripts/sql/194_parent_ci_remove_constraint.up.sql +++ b/scripts/sql/194_parent_ci_remove_constraint.up.sql @@ -1 +1,3 @@ -ALTER TABLE public.ci_pipeline DROP CONSTRAINT ci_pipeline_parent_ci_pipeline_fkey; \ No newline at end of file +-- Deleting foreign key relation for parent_ci_pipeline to make it generic, ci_pipeline_type will explain the parent type +-- Handling exists for tables which doesn't have this relation +ALTER TABLE public.ci_pipeline DROP CONSTRAINT IF EXISTS ci_pipeline_parent_ci_pipeline_fkey; \ No newline at end of file