Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: change-ci and app-group fixes #4332

Merged
merged 7 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions api/restHandler/app/BuildPipelineRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions internal/sql/repository/CiArtifactRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
}
Expand Down
17 changes: 16 additions & 1 deletion internal/sql/repository/pipelineConfig/CiPipelineRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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
Expand Down
15 changes: 8 additions & 7 deletions pkg/bulkAction/BulkUpdateService.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions pkg/pipeline/AppArtifactManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 1 addition & 17 deletions pkg/pipeline/BuildPipelineSwitchService.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/pipeline/CiHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/security/ImageScanService.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions scripts/sql/194_parent_ci_remove_constraint.down.sql
Original file line number Diff line number Diff line change
@@ -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) ;
4 changes: 3 additions & 1 deletion scripts/sql/194_parent_ci_remove_constraint.up.sql
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
ALTER TABLE public.ci_pipeline DROP CONSTRAINT ci_pipeline_parent_ci_pipeline_fkey;
-- 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;
kripanshdevtron marked this conversation as resolved.
Show resolved Hide resolved
Loading