@@ -30,6 +30,7 @@ import (
3030 "github.com/devtron-labs/devtron/client/argocdServer/application"
3131 "github.com/devtron-labs/devtron/client/cron"
3232 "github.com/devtron-labs/devtron/internal/constants"
33+ "github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig"
3334 "github.com/devtron-labs/devtron/internal/util"
3435 "github.com/devtron-labs/devtron/pkg/app"
3536 service1 "github.com/devtron-labs/devtron/pkg/appStore/deployment/service"
@@ -61,6 +62,8 @@ type AppListingRestHandler interface {
6162 FetchOtherEnvironment (w http.ResponseWriter , r * http.Request )
6263 RedirectToLinkouts (w http.ResponseWriter , r * http.Request )
6364 GetHostUrlsByBatch (w http.ResponseWriter , r * http.Request )
65+
66+ ManualSyncAcdPipelineDeploymentStatus (w http.ResponseWriter , r * http.Request )
6467}
6568
6669type AppListingRestHandlerImpl struct {
@@ -80,6 +83,7 @@ type AppListingRestHandlerImpl struct {
8083 k8sApplicationService k8s.K8sApplicationService
8184 installedAppService service1.InstalledAppService
8285 cdApplicationStatusUpdateHandler cron.CdApplicationStatusUpdateHandler
86+ pipelineRepository pipelineConfig.PipelineRepository
8387}
8488
8589type AppStatus struct {
@@ -99,7 +103,8 @@ func NewAppListingRestHandlerImpl(application application.ServiceClient,
99103 deploymentGroupService deploymentGroup.DeploymentGroupService , userService user.UserService ,
100104 helmAppClient client.HelmAppClient , clusterService cluster.ClusterService , helmAppService client.HelmAppService ,
101105 argoUserService argo.ArgoUserService , k8sApplicationService k8s.K8sApplicationService , installedAppService service1.InstalledAppService ,
102- cdApplicationStatusUpdateHandler cron.CdApplicationStatusUpdateHandler ) * AppListingRestHandlerImpl {
106+ cdApplicationStatusUpdateHandler cron.CdApplicationStatusUpdateHandler ,
107+ pipelineRepository pipelineConfig.PipelineRepository ) * AppListingRestHandlerImpl {
103108 appListingHandler := & AppListingRestHandlerImpl {
104109 application : application ,
105110 appListingService : appListingService ,
@@ -117,6 +122,7 @@ func NewAppListingRestHandlerImpl(application application.ServiceClient,
117122 k8sApplicationService : k8sApplicationService ,
118123 installedAppService : installedAppService ,
119124 cdApplicationStatusUpdateHandler : cdApplicationStatusUpdateHandler ,
125+ pipelineRepository : pipelineRepository ,
120126 }
121127 return appListingHandler
122128}
@@ -703,12 +709,23 @@ func (handler AppListingRestHandlerImpl) getAppDetails(appIdParam, installedAppI
703709 return appDetail , err , appId
704710}
705711
712+ // TODO: move this to service
706713func (handler AppListingRestHandlerImpl ) fetchResourceTree (w http.ResponseWriter , r * http.Request , appId int , envId int , appDetail bean.AppDetailContainer ) bean.AppDetailContainer {
707714 if len (appDetail .AppName ) > 0 && len (appDetail .EnvironmentName ) > 0 && util .IsAcdApp (appDetail .DeploymentAppType ) {
708715 //RBAC enforcer Ends
709- acdAppName := appDetail .AppName + "-" + appDetail .EnvironmentName
716+ cdPipelines , err := handler .pipelineRepository .FindActiveByAppIdAndEnvironmentId (appId , envId )
717+ if err != nil {
718+ handler .logger .Errorw ("error in getting cdPipeline by appId and envId" , "err" , err , "appid" , appId , "envId" , envId )
719+ common .WriteJsonResp (w , err , "" , http .StatusInternalServerError )
720+ return appDetail
721+ }
722+ if len (cdPipelines ) != 1 {
723+ common .WriteJsonResp (w , err , "" , http .StatusInternalServerError )
724+ return appDetail
725+ }
726+ cdPipeline := cdPipelines [0 ]
710727 query := & application2.ResourcesQuery {
711- ApplicationName : & acdAppName ,
728+ ApplicationName : & cdPipeline . DeploymentAppName ,
712729 }
713730 ctx , cancel := context .WithCancel (r .Context ())
714731 if cn , ok := w .(http.CloseNotifier ); ok {
@@ -761,7 +778,7 @@ func (handler AppListingRestHandlerImpl) fetchResourceTree(w http.ResponseWriter
761778 }
762779 appDetail .ResourceTree = util2 .InterfaceToMapAdapter (resp )
763780 if resp .Status == string (health .HealthStatusHealthy ) {
764- err = handler .cdApplicationStatusUpdateHandler .SyncPipelineStatusForResourceTreeCall (acdAppName , appId , envId )
781+ err = handler .cdApplicationStatusUpdateHandler .SyncPipelineStatusForResourceTreeCall (cdPipeline )
765782 if err != nil {
766783 handler .logger .Errorw ("error in syncing pipeline status" , "err" , err )
767784 }
@@ -794,3 +811,45 @@ func (handler AppListingRestHandlerImpl) fetchResourceTree(w http.ResponseWriter
794811 }
795812 return appDetail
796813}
814+
815+ func (handler AppListingRestHandlerImpl ) ManualSyncAcdPipelineDeploymentStatus (w http.ResponseWriter , r * http.Request ) {
816+ token := r .Header .Get ("token" )
817+ vars := mux .Vars (r )
818+ userId , err := handler .userService .GetLoggedInUser (r )
819+ if userId == 0 || err != nil {
820+ common .WriteJsonResp (w , err , "Unauthorized User" , http .StatusUnauthorized )
821+ return
822+ }
823+ appId , err := strconv .Atoi (vars ["appId" ])
824+ if err != nil {
825+ handler .logger .Errorw ("request err, ManualSyncAcdPipelineDeploymentStatus" , "err" , err , "appId" , appId )
826+ common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
827+ return
828+ }
829+ envId , err := strconv .Atoi (vars ["envId" ])
830+ if err != nil {
831+ handler .logger .Errorw ("request err, ManualSyncAcdPipelineDeploymentStatus" , "err" , err , "envId" , envId )
832+ common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
833+ return
834+ }
835+ app , err := handler .pipeline .GetApp (appId )
836+ if err != nil {
837+ handler .logger .Errorw ("bad request" , "err" , err )
838+ common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
839+ return
840+ }
841+ // RBAC enforcer applying
842+ object := handler .enforcerUtil .GetAppRBACName (app .AppName )
843+ if ok := handler .enforcer .Enforce (token , casbin .ResourceApplications , casbin .ActionGet , object ); ! ok {
844+ common .WriteJsonResp (w , err , "unauthorized user" , http .StatusForbidden )
845+ return
846+ }
847+ //RBAC enforcer Ends
848+ err = handler .cdApplicationStatusUpdateHandler .ManualSyncPipelineStatus (appId , envId , userId )
849+ if err != nil {
850+ handler .logger .Errorw ("service err, ManualSyncAcdPipelineDeploymentStatus" , "err" , err , "appId" , appId , "envId" , envId )
851+ common .WriteJsonResp (w , err , nil , http .StatusInternalServerError )
852+ return
853+ }
854+ common .WriteJsonResp (w , nil , "App synced successfully." , http .StatusOK )
855+ }
0 commit comments