Skip to content

Commit d8f9ab6

Browse files
fix: Clone of app with external-ci (#3624)
* CreateCdPipeline fucntion call * calling CreateCdPipeline for webhookMapping * ci_pipeline_id * Indentation * webhook fucntion calling * cdPipeline object refactor * handling of multiple wf creation * conditional handling * validation * PR comments * addion of gitConfigured code block * PR comments * logs * Error handling * error handling
1 parent 9cc90fb commit d8f9ab6

File tree

1 file changed

+74
-5
lines changed

1 file changed

+74
-5
lines changed

pkg/appClone/AppCloneService.go

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -586,12 +586,26 @@ func (impl *AppCloneServiceImpl) CreateWf(oldAppId, newAppId int, userId int32,
586586
AppWorkflowMappingDto: nil, //first create new mapping then add it
587587
UserId: userId,
588588
}
589-
thisWf, err := impl.appWorkflowService.CreateAppWorkflow(thisWf)
590-
if err != nil {
591-
return nil, err
589+
590+
isExternalCiPresent := false
591+
for _, awm := range refAppWF.AppWorkflowMappingDto {
592+
if awm.Type == appWorkflow2.WEBHOOK {
593+
isExternalCiPresent = true
594+
}
592595
}
596+
597+
if !isExternalCiPresent {
598+
thisWf, err = impl.appWorkflowService.CreateAppWorkflow(thisWf)
599+
impl.logger.Debugw("workflow found", thisWf)
600+
if err != nil {
601+
impl.logger.Errorw("errir in creating workflow without extenal-ci", "err", err)
602+
return nil, err
603+
}
604+
}
605+
593606
err = impl.createWfMappings(refAppWF.AppWorkflowMappingDto, oldAppId, newAppId, userId, thisWf.Id, gitMaterialMapping, ctx, isSameProject)
594607
if err != nil {
608+
impl.logger.Errorw("error in creating workflow mapping", "err", err)
595609
return nil, err
596610
}
597611
}
@@ -614,18 +628,40 @@ func (impl *AppCloneServiceImpl) createWfMappings(refWfMappings []appWorkflow.Ap
614628
return fmt.Errorf("unsupported wf type: %s", appWf.Type)
615629
}
616630
}
631+
refApp, err := impl.pipelineBuilder.GetApp(oldAppId)
617632
if len(webhookMappings) > 0 {
618-
impl.logger.Warn("external ci webhook found in workflow, not supported for clone")
633+
if isSameProject {
634+
for _, refwebhookMappings := range cdMappings {
635+
cdCloneReq := &cloneCdPipelineRequest{
636+
refCdPipelineId: refwebhookMappings.ComponentId,
637+
refAppId: oldAppId,
638+
appId: newAppId,
639+
userId: userId,
640+
ciPipelineId: 0,
641+
appWfId: thisWfId,
642+
refAppName: refApp.AppName,
643+
}
644+
pipeline, err := impl.CreateCdPipeline(cdCloneReq, ctx)
645+
impl.logger.Debugw("cd pipeline created", "pipeline", pipeline)
646+
if err != nil {
647+
impl.logger.Errorw("error in getting cd-pipeling", "err", err)
648+
return err
649+
}
650+
}
651+
} else {
652+
impl.logger.Debug("not the same project, skipping cd pipeline creation")
653+
}
619654
return nil
620655
}
656+
621657
if len(ciMapping) == 0 {
622658
impl.logger.Warn("no ci pipeline found")
623659
return nil
624660
} else if len(ciMapping) != 1 {
625661
impl.logger.Warn("more than one cd pipeline not supported")
626662
return nil
627663
}
628-
refApp, err := impl.pipelineBuilder.GetApp(oldAppId)
664+
629665
if err != nil {
630666
return err
631667
}
@@ -644,6 +680,7 @@ func (impl *AppCloneServiceImpl) createWfMappings(refWfMappings []appWorkflow.Ap
644680
}
645681
ci, err = impl.CreateCiPipeline(cloneCiPipelineRequest)
646682
if err != nil {
683+
impl.logger.Errorw("error in creating ci pipeline, app clone", "err", err)
647684
return err
648685
}
649686
impl.logger.Debugw("ci created", "ci", ci)
@@ -661,6 +698,7 @@ func (impl *AppCloneServiceImpl) createWfMappings(refWfMappings []appWorkflow.Ap
661698
}
662699
pipeline, err := impl.CreateCdPipeline(cdCloneReq, ctx)
663700
if err != nil {
701+
impl.logger.Errorw("error in creating cd pipeline, app clone", "err", err)
664702
return err
665703
}
666704
impl.logger.Debugw("cd pipeline created", "pipeline", pipeline)
@@ -867,6 +905,36 @@ func (impl *AppCloneServiceImpl) CreateCdPipeline(req *cloneCdPipelineRequest, c
867905
deploymentAppType = util.PIPELINE_DEPLOYMENT_TYPE_ACD
868906
} else if AllowedDeploymentAppTypes[util.PIPELINE_DEPLOYMENT_TYPE_HELM] {
869907
deploymentAppType = util.PIPELINE_DEPLOYMENT_TYPE_HELM
908+
909+
if refCdPipeline.ParentPipelineType == "WEBHOOK" {
910+
cdPipeline := &bean.CDPipelineConfigObject{
911+
Id: 0,
912+
EnvironmentId: refCdPipeline.EnvironmentId,
913+
CiPipelineId: 0,
914+
TriggerType: refCdPipeline.TriggerType,
915+
Name: pipelineName,
916+
Strategies: refCdPipeline.Strategies,
917+
Namespace: refCdPipeline.Namespace,
918+
AppWorkflowId: 0,
919+
DeploymentTemplate: refCdPipeline.DeploymentTemplate,
920+
PreStage: refCdPipeline.PreStage, //FIXME
921+
PostStage: refCdPipeline.PostStage,
922+
PreStageConfigMapSecretNames: refCdPipeline.PreStageConfigMapSecretNames,
923+
PostStageConfigMapSecretNames: refCdPipeline.PostStageConfigMapSecretNames,
924+
RunPostStageInEnv: refCdPipeline.RunPostStageInEnv,
925+
RunPreStageInEnv: refCdPipeline.RunPreStageInEnv,
926+
DeploymentAppType: refCdPipeline.DeploymentAppType,
927+
ParentPipelineId: 0,
928+
ParentPipelineType: refCdPipeline.ParentPipelineType,
929+
}
930+
cdPipelineReq := &bean.CdPipelines{
931+
Pipelines: []*bean.CDPipelineConfigObject{cdPipeline},
932+
AppId: req.appId,
933+
UserId: req.userId,
934+
}
935+
cdPipelineRes, err := impl.pipelineBuilder.CreateCdPipelines(cdPipelineReq, ctx)
936+
return cdPipelineRes, err
937+
}
870938
}
871939
cdPipeline := &bean.CDPipelineConfigObject{
872940
Id: 0,
@@ -893,4 +961,5 @@ func (impl *AppCloneServiceImpl) CreateCdPipeline(req *cloneCdPipelineRequest, c
893961
}
894962
cdPipelineRes, err := impl.pipelineBuilder.CreateCdPipelines(cdPipelineReq, ctx)
895963
return cdPipelineRes, err
964+
896965
}

0 commit comments

Comments
 (0)