Skip to content

Commit

Permalink
fix: hotfix v0.14.0 fixes (#5500)
Browse files Browse the repository at this point in the history
* fix: patch ci rbac fix (#5498)

* handle nil check (#5497)

* fix: patch ci rbac fix

---------

Co-authored-by: ayu-devtron <[email protected]>

* fix: external ci creation fix (#5499)

* handle nil check (#5497)

* fix: external ci creation fix

---------

Co-authored-by: ayu-devtron <[email protected]>

* fix for deployment config - auto post cd not working (#5501)

* fix: do not create deployment config for switch ci, if pipeline is already created. (#5506)

* chore: main sync (#5510)

* handle nil check (#5497)

* doc: Added FAQ no. 28 + GoLang-migrate Link + Code Block Fix (#5502)

* Added FAQ no. 28 + GoLang-migrate Link + Code Block Fix

* Removed extra spacing at EOF

* fix: bitbucket commit race condition for concurrent requests (#5505)

---------

Co-authored-by: ayu-devtron <[email protected]>
Co-authored-by: ashokdevtron <[email protected]>
Co-authored-by: Asutosh Das <[email protected]>

---------

Co-authored-by: ayu-devtron <[email protected]>
Co-authored-by: kartik-579 <[email protected]>
Co-authored-by: ashokdevtron <[email protected]>
Co-authored-by: Asutosh Das <[email protected]>
  • Loading branch information
5 people authored Jul 17, 2024
1 parent 71b46cd commit 9db1489
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/devtron-labs/devtron/internal/sql/repository/appWorkflow"
"golang.org/x/exp/maps"
"io"
"net/http"
Expand Down Expand Up @@ -418,7 +419,7 @@ func (handler *PipelineConfigRestHandlerImpl) PatchCiPipelines(w http.ResponseWr
resourceName := handler.enforcerUtil.GetAppRBACName(app.AppName)
workflowResourceName := handler.enforcerUtil.GetRbacObjectNameByAppAndWorkflow(app.AppName, appWorkflowName)

cdPipelines, err := handler.pipelineRepository.FindByCiPipelineId(patchRequest.CiPipeline.Id)
cdPipelines, err := handler.getCdPipelinesForCIPatchRbac(&patchRequest)
if err != nil && err != pg.ErrNoRows {
handler.Logger.Errorw("error in finding ccd cdPipelines by ciPipelineId", "ciPipelineId", patchRequest.CiPipeline.Id, "err", err)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
Expand Down Expand Up @@ -478,6 +479,57 @@ func (handler *PipelineConfigRestHandlerImpl) PatchCiPipelines(w http.ResponseWr
common.WriteJsonResp(w, err, createResp, http.StatusOK)
}

func (handler *PipelineConfigRestHandlerImpl) getCdPipelinesForCIPatchRbac(patchRequest *bean.CiPatchRequest) (cdPipelines []*pipelineConfig.Pipeline, err error) {
// if the request is for create, then there will be no cd pipelines created yet
if patchRequest.IsCreateRequest() {
return
}
// request is to either patch the existing pipeline or patch it by switching the source pipeline or delete or update-source

// for switch , this API handles following switches
// any -> any (except switching to external-ci)

// to find the cd pipelines of the current ci pipelines workflow , we should query from appWorkflow Mappings.
// cannot directly query cd-pipeline table as we don't store info about external pipeline in cdPipeline.

// approach:
// find the workflow in which we are patching and use the workflow id to fetch all the workflow mappings using the workflow.
// get cd pipeline ids from those workflows and fetch the cd pipelines.

// get the ciPipeline
switchFromPipelineId, switchFromType := patchRequest.SwitchSourceInfo()

// in app workflow mapping all the build source types are 'CI_PIPELINE' type, except external -> WEBHOOK.
componentType := appWorkflow.CIPIPELINE
if switchFromType == CiPipeline.EXTERNAL {
componentType = appWorkflow.WEBHOOK
}

// the appWorkflowId can be taken from patchRequest.AppWorkflowId but doing this can make 2 sources of truth to find the workflow
sourceAppWorkflowMapping, err := handler.appWorkflowService.FindWFMappingByComponent(componentType, switchFromPipelineId)
if err != nil {
handler.Logger.Errorw("error in finding the appWorkflowMapping using componentId and componentType", "componentType", componentType, "componentId", switchFromPipelineId, "err", err)
return nil, err
}

cdPipelineWFMappings, err := handler.appWorkflowService.FindWFCDMappingsByWorkflowId(sourceAppWorkflowMapping.AppWorkflowId)
if err != nil {
handler.Logger.Errorw("error in finding the appWorkflowMappings of cd pipeline for an appWorkflow", "appWorkflowId", sourceAppWorkflowMapping.AppWorkflowId, "err", err)
return cdPipelines, err
}

if len(cdPipelineWFMappings) == 0 {
return
}

cdPipelineIds := make([]int, 0, len(cdPipelineWFMappings))
for _, cdWfMapping := range cdPipelineWFMappings {
cdPipelineIds = append(cdPipelineIds, cdWfMapping.ComponentId)
}

return handler.pipelineRepository.FindByIdsIn(cdPipelineIds)
}

// checkCiPatchAccess assumes all the cdPipelines belong to same app
func (handler *PipelineConfigRestHandlerImpl) checkCiPatchAccess(token string, resourceName string, cdPipelines []*pipelineConfig.Pipeline) bool {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ func (impl *CdWorkflowRepositoryImpl) GetLatestTriggersOfHelmPipelinesStuckInNon
excludedStatusList = append(excludedStatusList, WorkflowInitiated, WorkflowInQueue, WorkflowStarting)
err := impl.dbConnection.
Model(&wfrList).
Column("cd_workflow_runner.*", "CdWorkflow.id", "CdWorkflow.pipeline_id", "CdWorkflow.Pipeline.id", "CdWorkflow.Pipeline.deployment_app_name", "CdWorkflow.Pipeline.deleted", "CdWorkflow.Pipeline.Environment").
Column("cd_workflow_runner.*", "CdWorkflow.id", "CdWorkflow.pipeline_id", "CdWorkflow.Pipeline.id", "CdWorkflow.Pipeline.app_id", "CdWorkflow.Pipeline.environment_id", "CdWorkflow.Pipeline.deployment_app_name", "CdWorkflow.Pipeline.deleted", "CdWorkflow.Pipeline.Environment").
Join("INNER JOIN cd_workflow wf on wf.id = cd_workflow_runner.cd_workflow_id").
Join("INNER JOIN pipeline p on p.id = wf.pipeline_id").
Join("INNER JOIN environment e on e.id = p.environment_id").
Expand Down
10 changes: 10 additions & 0 deletions pkg/appWorkflow/AppWorkflowService.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type AppWorkflowService interface {
FilterWorkflows(triggerViewConfig *TriggerViewWorkflowConfig, envIds []int) (*TriggerViewWorkflowConfig, error)
FindCdPipelinesByAppId(appId int) (*bean.CdPipelines, error)
FindAppWorkflowByCiPipelineId(ciPipelineId int) ([]*appWorkflow.AppWorkflowMapping, error)
FindWFMappingByComponent(componentType string, componentId int) (*appWorkflow.AppWorkflowMapping, error)
FindWFCDMappingsByWorkflowId(appWorkflowId int) ([]*appWorkflow.AppWorkflowMapping, error)
}

type AppWorkflowServiceImpl struct {
Expand Down Expand Up @@ -891,6 +893,14 @@ func (impl AppWorkflowServiceImpl) FindAppWorkflowByCiPipelineId(ciPipelineId in

}

func (impl AppWorkflowServiceImpl) FindWFCDMappingsByWorkflowId(appWorkflowId int) ([]*appWorkflow.AppWorkflowMapping, error) {
return impl.appWorkflowRepository.FindWFCDMappingsByWorkflowId(appWorkflowId)
}

func (impl AppWorkflowServiceImpl) FindWFMappingByComponent(componentType string, componentId int) (*appWorkflow.AppWorkflowMapping, error) {
return impl.appWorkflowRepository.FindWFMappingByComponent(componentType, componentId)
}

// LevelWiseSort performs level wise sort for workflow mappings starting from leaves
// This will break if ever the workflow mappings array break the assumption of being a DAG with one root node
func LevelWiseSort(appWorkflowMappings []AppWorkflowMappingDto) []AppWorkflowMappingDto {
Expand Down
19 changes: 19 additions & 0 deletions pkg/bean/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,29 @@ type CiPatchRequest struct {
SwitchToCiPipelineType CiPipeline2.PipelineType `json:"-"`
}

func (ciPatchRequest CiPatchRequest) SwitchSourceInfo() (int, CiPipeline2.PipelineType) {
// get the ciPipeline
var switchFromType CiPipeline2.PipelineType
var switchFromPipelineId int
if ciPatchRequest.SwitchFromExternalCiPipelineId != 0 {
switchFromType = CiPipeline2.EXTERNAL
switchFromPipelineId = ciPatchRequest.SwitchFromExternalCiPipelineId
} else {
switchFromPipelineId = ciPatchRequest.SwitchFromCiPipelineId
switchFromType = ciPatchRequest.SwitchFromCiPipelineType
}

return switchFromPipelineId, switchFromType
}

func (ciPatchRequest CiPatchRequest) IsSwitchCiPipelineRequest() bool {
return (ciPatchRequest.SwitchFromCiPipelineId != 0 || ciPatchRequest.SwitchFromExternalCiPipelineId != 0)
}

func (ciPatchRequest CiPatchRequest) IsCreateRequest() bool {
return ciPatchRequest.Action == CREATE && !ciPatchRequest.IsSwitchCiPipelineRequest()
}

type CiRegexPatchRequest struct {
CiPipelineMaterial []*CiPipelineMaterial `json:"ciPipelineMaterial,omitempty"`
Id int `json:"id,omitempty" `
Expand Down
29 changes: 16 additions & 13 deletions pkg/pipeline/DeploymentPipelineConfigService.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,19 +456,22 @@ func (impl *CdPipelineConfigServiceImpl) CreateCdPipelines(pipelineCreateRequest

for _, pipeline := range pipelineCreateRequest.Pipelines {

envDeploymentConfig := &bean4.DeploymentConfig{
AppId: app.Id,
EnvironmentId: pipeline.EnvironmentId,
ConfigType: AppDeploymentConfig.ConfigType,
DeploymentAppType: pipeline.DeploymentAppType,
RepoURL: AppDeploymentConfig.RepoURL,
RepoName: AppDeploymentConfig.RepoName,
Active: true,
}
envDeploymentConfig, err := impl.deploymentConfigService.CreateOrUpdateConfig(nil, envDeploymentConfig, pipelineCreateRequest.UserId)
if err != nil {
impl.logger.Errorw("error in fetching creating env config", "appId", app.Id, "envId", pipeline.EnvironmentId, "err", err)
return nil, err
// skip creation of DeploymentConfig if envId is not set
if pipeline.EnvironmentId > 0 {
envDeploymentConfig := &bean4.DeploymentConfig{
AppId: app.Id,
EnvironmentId: pipeline.EnvironmentId,
ConfigType: AppDeploymentConfig.ConfigType,
DeploymentAppType: pipeline.DeploymentAppType,
RepoURL: AppDeploymentConfig.RepoURL,
RepoName: AppDeploymentConfig.RepoName,
Active: true,
}
envDeploymentConfig, err := impl.deploymentConfigService.CreateOrUpdateConfig(nil, envDeploymentConfig, pipelineCreateRequest.UserId)
if err != nil {
impl.logger.Errorw("error in fetching creating env config", "appId", app.Id, "envId", pipeline.EnvironmentId, "err", err)
return nil, err
}
}

id, err := impl.createCdPipeline(ctx, app, pipeline, pipelineCreateRequest.UserId)
Expand Down

0 comments on commit 9db1489

Please sign in to comment.