From 60e6fc9c729b7bfdc406a2c0fade5e6e2fde74df Mon Sep 17 00:00:00 2001 From: Prakash Kumar Date: Thu, 20 Feb 2025 13:52:08 +0530 Subject: [PATCH] IsCdQualifiedForAutoTriggerForWebhookCiEvent --- .../pipelineConfig/PipelineRepository.go | 12 ++++++++++-- pkg/workflow/dag/WorkflowDagExecutor.go | 3 ++- pkg/workflow/dag/helper/helper.go | 16 ++++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/internal/sql/repository/pipelineConfig/PipelineRepository.go b/internal/sql/repository/pipelineConfig/PipelineRepository.go index cf3723b6de..14d465f4ed 100644 --- a/internal/sql/repository/pipelineConfig/PipelineRepository.go +++ b/internal/sql/repository/pipelineConfig/PipelineRepository.go @@ -44,6 +44,14 @@ func (t TriggerType) ToString() string { return string(t) } +func (t TriggerType) IsManual() bool { + return t == TRIGGER_TYPE_MANUAL +} + +func (t TriggerType) IsAuto() bool { + return t == TRIGGER_TYPE_AUTOMATIC +} + const TRIGGER_TYPE_AUTOMATIC TriggerType = "AUTOMATIC" const TRIGGER_TYPE_MANUAL TriggerType = "MANUAL" @@ -59,8 +67,8 @@ type Pipeline struct { Deleted bool `sql:"deleted,notnull"` PreStageConfig string `sql:"pre_stage_config_yaml"` PostStageConfig string `sql:"post_stage_config_yaml"` - PreTriggerType TriggerType `sql:"pre_trigger_type"` // automatic, manual - PostTriggerType TriggerType `sql:"post_trigger_type"` // automatic, manual + PreTriggerType TriggerType `sql:"pre_trigger_type"` // automatic, manual; when a pre-cd task doesn't exist/removed in a cd then this field is updated as null + PostTriggerType TriggerType `sql:"post_trigger_type"` // automatic, manual; when a post-cd task doesn't exist/removed in a cd then this field is updated as null PreStageConfigMapSecretNames string `sql:"pre_stage_config_map_secret_names"` // configmap names PostStageConfigMapSecretNames string `sql:"post_stage_config_map_secret_names"` // secret names RunPreStageInEnv bool `sql:"run_pre_stage_in_env"` // secret names diff --git a/pkg/workflow/dag/WorkflowDagExecutor.go b/pkg/workflow/dag/WorkflowDagExecutor.go index 2cddf7a65c..9426ff56f7 100644 --- a/pkg/workflow/dag/WorkflowDagExecutor.go +++ b/pkg/workflow/dag/WorkflowDagExecutor.go @@ -472,7 +472,8 @@ func (impl *WorkflowDagExecutorImpl) handleWebhookExternalCiEvent(artifact *repo err = &util.ApiError{Code: "401", HttpStatusCode: 401, UserMessage: "Unauthorized"} return hasAnyTriggered, err } - if pipeline.TriggerType == pipelineConfig.TRIGGER_TYPE_MANUAL { + isQualifiedForCdAutoTrigger := helper.IsCdQualifiedForAutoTriggerForWebhookCiEvent(pipeline) + if !isQualifiedForCdAutoTrigger { impl.logger.Warnw("skipping deployment for manual trigger for webhook", "pipeline", pipeline) continue } diff --git a/pkg/workflow/dag/helper/helper.go b/pkg/workflow/dag/helper/helper.go index 6c6c97f732..d016e03774 100644 --- a/pkg/workflow/dag/helper/helper.go +++ b/pkg/workflow/dag/helper/helper.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "github.com/devtron-labs/devtron/internal/sql/repository" + "github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig" ) func GetMaterialInfoJson(materialInfo json.RawMessage) ([]byte, error) { @@ -29,3 +30,18 @@ func UpdateScanStatusInCiArtifact(ciArtifact *repository.CiArtifact, isScanPlugi ciArtifact.Scanned = true } } + +// IsCdQualifiedForAutoTriggerForWebhookCiEvent returns bool, if a cd/pre-cd is qualified for auto trigger for a webhook ci event +func IsCdQualifiedForAutoTriggerForWebhookCiEvent(pipeline *pipelineConfig.Pipeline) bool { + /* + A cd is qualified for auto trigger for webhookCiEvent if it satisfies below two conditions:- + 1. If pre-cd exists and is set on auto. + 2. If only cd exists and is set on auto. + */ + if len(pipeline.PreTriggerType) > 0 && pipeline.PreTriggerType.IsAuto() { + return true + } else if len(pipeline.PreTriggerType) == 0 && pipeline.TriggerType.IsAuto() { + return true + } + return false +}