Skip to content
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
6 changes: 6 additions & 0 deletions Wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,9 @@ func InitializeApp() (*App, error) {
pipeline.NewCiCdPipelineOrchestrator,
wire.Bind(new(pipeline.CiCdPipelineOrchestrator), new(*pipeline.CiCdPipelineOrchestratorImpl)),

pipeline.NewGitMaterialVariableSyncServiceImpl,
wire.Bind(new(pipeline.GitMaterialVariableSyncService), new(*pipeline.GitMaterialVariableSyncServiceImpl)),

// scoped variables start
variables.NewScopedVariableServiceImpl,
wire.Bind(new(variables.ScopedVariableService), new(*variables.ScopedVariableServiceImpl)),
Expand All @@ -367,6 +370,9 @@ func InitializeApp() (*App, error) {
variables.NewScopedVariableCMCSManagerImpl,
wire.Bind(new(variables.ScopedVariableCMCSManager), new(*variables.ScopedVariableCMCSManagerImpl)),

variables.NewRepoFieldVariableResolverImpl,
wire.Bind(new(variables.RepoFieldVariableResolver), new(*variables.RepoFieldVariableResolverImpl)),

// end

gitOpsConfig.NewDevtronAppGitOpConfigServiceImpl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
bean2 "github.com/devtron-labs/devtron/pkg/build/pipeline/bean"
eventProcessorBean "github.com/devtron-labs/devtron/pkg/eventProcessor/bean"
constants2 "github.com/devtron-labs/devtron/pkg/pipeline/constants"
"github.com/devtron-labs/devtron/pkg/variables"
"github.com/devtron-labs/devtron/util/stringsUtil"
"golang.org/x/exp/maps"

Expand Down Expand Up @@ -1363,6 +1364,11 @@ func (handler *PipelineConfigRestHandlerImpl) CreateMaterial(w http.ResponseWrit
return
}
for _, gitMaterial := range createMaterialDto.Material {
// a @{{VAR_NAME}} reference isn't a real URL yet - skip the format check here,
// RepoFieldVariableResolver validates and resolves it before use
if variables.IsFullVariableReference(gitMaterial.Url) {
continue
}
validationResult, err := handler.ValidateGitMaterialUrl(gitMaterial.GitProviderId, gitMaterial.Url)
if err != nil {
handler.Logger.Errorw("service err, CreateMaterial", "err", err, "CreateMaterial", createMaterialDto)
Expand Down Expand Up @@ -1409,16 +1415,20 @@ func (handler *PipelineConfigRestHandlerImpl) UpdateMaterial(w http.ResponseWrit
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
validationResult, err := handler.ValidateGitMaterialUrl(updateMaterialDto.Material.GitProviderId, updateMaterialDto.Material.Url)
if err != nil {
handler.Logger.Errorw("service err, UpdateMaterial", "err", err, "UpdateMaterial", updateMaterialDto)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
} else {
if !validationResult {
handler.Logger.Errorw("validation err, UpdateMaterial : invalid git material url", "err", err, "gitMaterialUrl", updateMaterialDto.Material.Url, "UpdateMaterial", updateMaterialDto)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
// a @{{VAR_NAME}} reference isn't a real URL yet - skip the format check here,
// RepoFieldVariableResolver validates and resolves it before use
if !variables.IsFullVariableReference(updateMaterialDto.Material.Url) {
validationResult, err := handler.ValidateGitMaterialUrl(updateMaterialDto.Material.GitProviderId, updateMaterialDto.Material.Url)
if err != nil {
handler.Logger.Errorw("service err, UpdateMaterial", "err", err, "UpdateMaterial", updateMaterialDto)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
} else {
if !validationResult {
handler.Logger.Errorw("validation err, UpdateMaterial : invalid git material url", "err", err, "gitMaterialUrl", updateMaterialDto.Material.Url, "UpdateMaterial", updateMaterialDto)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
}
}
resourceObject := handler.enforcerUtil.GetAppRBACNameByAppId(updateMaterialDto.AppId)
Expand Down
44 changes: 29 additions & 15 deletions api/restHandler/scopedVariable/ScopedVariableRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"net/http"
"strconv"

"github.com/devtron-labs/common-lib/async"
"github.com/devtron-labs/devtron/api/restHandler/common"
util2 "github.com/devtron-labs/devtron/internal/util"
"github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin"
Expand All @@ -47,28 +48,32 @@
}

type ScopedVariableRestHandlerImpl struct {
logger *zap.SugaredLogger
userAuthService user.UserService
validator *validator.Validate
pipelineBuilder pipeline.PipelineBuilder
enforcerUtil rbac.EnforcerUtil
enforcer casbin.Enforcer
scopedVariableService variables.ScopedVariableService
logger *zap.SugaredLogger
userAuthService user.UserService
validator *validator.Validate
pipelineBuilder pipeline.PipelineBuilder
enforcerUtil rbac.EnforcerUtil
enforcer casbin.Enforcer
scopedVariableService variables.ScopedVariableService
gitMaterialVariableSyncService pipeline.GitMaterialVariableSyncService
asyncRunnable *async.Runnable
}
type JsonResponse struct {
Manifest *models.ScopedVariableManifest `json:"manifest"`
JsonSchema string `json:"jsonSchema"`
}

func NewScopedVariableRestHandlerImpl(logger *zap.SugaredLogger, userAuthService user.UserService, validator *validator.Validate, pipelineBuilder pipeline.PipelineBuilder, enforcerUtil rbac.EnforcerUtil, enforcer casbin.Enforcer, scopedVariableService variables.ScopedVariableService) *ScopedVariableRestHandlerImpl {
func NewScopedVariableRestHandlerImpl(logger *zap.SugaredLogger, userAuthService user.UserService, validator *validator.Validate, pipelineBuilder pipeline.PipelineBuilder, enforcerUtil rbac.EnforcerUtil, enforcer casbin.Enforcer, scopedVariableService variables.ScopedVariableService, gitMaterialVariableSyncService pipeline.GitMaterialVariableSyncService, asyncRunnable *async.Runnable) *ScopedVariableRestHandlerImpl {

Check warning on line 66 in api/restHandler/scopedVariable/ScopedVariableRestHandler.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

This function has 9 parameters, which is greater than the 7 authorized.

See more on https://sonarcloud.io/project/issues?id=devtron-labs_devtron&issues=AaCkYUSbDNL5flFF2M0X&open=AaCkYUSbDNL5flFF2M0X&pullRequest=7036
return &ScopedVariableRestHandlerImpl{
logger: logger,
userAuthService: userAuthService,
validator: validator,
pipelineBuilder: pipelineBuilder,
enforcerUtil: enforcerUtil,
enforcer: enforcer,
scopedVariableService: scopedVariableService,
logger: logger,
userAuthService: userAuthService,
validator: validator,
pipelineBuilder: pipelineBuilder,
enforcerUtil: enforcerUtil,
enforcer: enforcer,
scopedVariableService: scopedVariableService,
gitMaterialVariableSyncService: gitMaterialVariableSyncService,
asyncRunnable: asyncRunnable,
}
}
func (handler *ScopedVariableRestHandlerImpl) CreateVariables(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -132,6 +137,15 @@
}
return
}
// CreateVariables replaces the entire variable set, so any Git Material referencing a
// scoped variable may need its git-sensor copy refreshed - run off-thread since each
// push to git-sensor can take up to its own timeout ceiling, and this must not block
// the response for a save that already succeeded above.
handler.asyncRunnable.Execute(func() {
if syncErr := handler.gitMaterialVariableSyncService.ResyncGitMaterialsForVariableChange(); syncErr != nil {
handler.logger.Errorw("error re-syncing git materials after variable change", "err", syncErr)
}
})
common.WriteJsonResp(w, nil, nil, http.StatusOK)
}

Expand Down
24 changes: 20 additions & 4 deletions pkg/build/trigger/HandlerService.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
customTagService pipeline2.CustomTagService
config *types.CiConfig
scopedVariableManager variables.ScopedVariableManager
repoFieldVariableResolver variables.RepoFieldVariableResolver
ciCdPipelineOrchestrator pipeline2.CiCdPipelineOrchestrator
buildxGlobalFlags *BuildxGlobalFlags
attributeService attributes.AttributesService
Expand Down Expand Up @@ -138,6 +139,7 @@
envRepository repository6.EnvironmentRepository,
appRepository appRepository.AppRepository,
scopedVariableManager variables.ScopedVariableManager,
repoFieldVariableResolver variables.RepoFieldVariableResolver,
customTagService pipeline2.CustomTagService,
ciCdPipelineOrchestrator pipeline2.CiCdPipelineOrchestrator, attributeService attributes.AttributesService,
pluginInputVariableParser pipeline2.PluginInputVariableParser,
Expand Down Expand Up @@ -171,6 +173,7 @@
envRepository: envRepository,
appRepository: appRepository,
scopedVariableManager: scopedVariableManager,
repoFieldVariableResolver: repoFieldVariableResolver,
customTagService: customTagService,
ciCdPipelineOrchestrator: ciCdPipelineOrchestrator,
buildxGlobalFlags: buildxCacheFlags,
Expand Down Expand Up @@ -847,7 +850,7 @@
trigger.RuntimeParameters = trigger.RuntimeParameters.AddSystemVariable(k, v)
}

workflowRequest, err := impl.buildWfRequestForCiPipeline(pipeline, trigger, ciMaterials, savedCiWf, ciWorkflowConfigNamespace, ciPipelineScripts, preCiSteps, postCiSteps, refPluginsData, isJob)
workflowRequest, err := impl.buildWfRequestForCiPipeline(pipeline, trigger, ciMaterials, savedCiWf, ciWorkflowConfigNamespace, ciPipelineScripts, preCiSteps, postCiSteps, refPluginsData, isJob, scope)
if err != nil {
impl.Logger.Errorw("make workflow req", "err", err)
return nil, nil, nil, err
Expand Down Expand Up @@ -1006,7 +1009,7 @@
return ArtifactLocation
}

func (impl *HandlerServiceImpl) buildWfRequestForCiPipeline(pipeline *pipelineConfig.CiPipeline, trigger *types.CiTriggerRequest, ciMaterials []*pipelineConfig.CiPipelineMaterial, savedWf *pipelineConfig.CiWorkflow, ciWorkflowConfigNamespace string, ciPipelineScripts []*pipelineConfig.CiPipelineScript, preCiSteps []*pipelineConfigBean.StepObject, postCiSteps []*pipelineConfigBean.StepObject, refPluginsData []*pipelineConfigBean.RefPluginObject, isJob bool) (*types.WorkflowRequest, error) {
func (impl *HandlerServiceImpl) buildWfRequestForCiPipeline(pipeline *pipelineConfig.CiPipeline, trigger *types.CiTriggerRequest, ciMaterials []*pipelineConfig.CiPipelineMaterial, savedWf *pipelineConfig.CiWorkflow, ciWorkflowConfigNamespace string, ciPipelineScripts []*pipelineConfig.CiPipelineScript, preCiSteps []*pipelineConfigBean.StepObject, postCiSteps []*pipelineConfigBean.StepObject, refPluginsData []*pipelineConfigBean.RefPluginObject, isJob bool, scope resourceQualifiers.Scope) (*types.WorkflowRequest, error) {

Check failure on line 1012 in pkg/build/trigger/HandlerService.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 54 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=devtron-labs_devtron&issues=AaCkYUQYDNL5flFF2M0V&open=AaCkYUQYDNL5flFF2M0V&pullRequest=7036

Check warning on line 1012 in pkg/build/trigger/HandlerService.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

This function has 11 parameters, which is greater than the 7 authorized.

See more on https://sonarcloud.io/project/issues?id=devtron-labs_devtron&issues=AaCkYUQYDNL5flFF2M0W&open=AaCkYUQYDNL5flFF2M0W&pullRequest=7036
var ciProjectDetails []pipelineConfigBean.CiProjectDetails
commitHashes := trigger.CommitHashes
for _, ciMaterial := range ciMaterials {
Expand All @@ -1015,8 +1018,15 @@
continue
}
commitHashForPipelineId := commitHashes[ciMaterial.Id]
// git_material.url may hold a @{{VAR_NAME}} reference (never resolved into the DB, see
// RepoFieldVariableResolver) - resolve it fresh here rather than at Git Material save time
gitRepository, _, err := impl.repoFieldVariableResolver.ResolveRepoFieldValue(scope, ciMaterial.GitMaterial.Url)
if err != nil {
impl.Logger.Errorw("error resolving git material url", "gitMaterialId", ciMaterial.GitMaterial.Id, "err", err)
return nil, err
}
ciProjectDetail := pipelineConfigBean.CiProjectDetails{
GitRepository: ciMaterial.GitMaterial.Url,
GitRepository: gitRepository,
MaterialName: ciMaterial.GitMaterial.Name,
CheckoutPath: ciMaterial.GitMaterial.CheckoutPath,
FetchSubmodules: ciMaterial.GitMaterial.FetchSubmodules,
Expand All @@ -1040,7 +1050,6 @@
CaCert: ciMaterial.GitMaterial.GitProvider.CaCert,
},
}
var err error
ciProjectDetail, err = impl.updateCIProjectDetailWithCloningMode(pipeline.AppId, ciMaterial, ciProjectDetail)
if err != nil {
impl.Logger.Errorw("error, updateCIProjectDetailWithCloningMode", "pipelineId", pipeline.Id, "err", err)
Expand Down Expand Up @@ -1147,6 +1156,13 @@
}

}
// ci_template(_override).docker_repository may hold a @{{VAR_NAME}} reference (never
// resolved at save time, so a variable value update takes effect on the next build)
dockerRepository, _, err = impl.repoFieldVariableResolver.ResolveRepoFieldValue(scope, dockerRepository)
if err != nil {
impl.Logger.Errorw("error resolving container repository", "pipelineId", pipeline.Id, "err", err)
return nil, err
}
if checkoutPath == "" {
checkoutPath = "./"
}
Expand Down
Loading
Loading