Skip to content

Commit c22a646

Browse files
authored
Merge pull request #6489 from devtron-labs/triggeredBy-changes
fix: made triggeredBy for CI & CD uniform
2 parents ac74eb4 + 7bafe73 commit c22a646

File tree

9 files changed

+31
-32
lines changed

9 files changed

+31
-32
lines changed

client/cron/CiTriggerCron.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"github.com/caarlos0/env"
2222
repository2 "github.com/devtron-labs/devtron/internal/sql/repository"
23+
bean2 "github.com/devtron-labs/devtron/pkg/auth/user/bean"
2324
"github.com/devtron-labs/devtron/pkg/bean"
2425
pipelineConfigBean "github.com/devtron-labs/devtron/pkg/build/pipeline/bean"
2526
"github.com/devtron-labs/devtron/pkg/pipeline"
@@ -98,7 +99,7 @@ func (impl *CiTriggerCronImpl) TriggerCiCron() {
9899
ciTriggerRequest := bean.CiTriggerRequest{
99100
PipelineId: ciPipelineId,
100101
CiPipelineMaterial: ciPipelineMaterials,
101-
TriggeredBy: 1,
102+
TriggeredBy: bean2.SYSTEM_USER_ID,
102103
InvalidateCache: false,
103104
PipelineType: string(pipelineConfigBean.CI_JOB),
104105
}

internal/sql/repository/app/AppRepository.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ func (app *App) IsEmpty() bool {
4848
return app.Id == 0
4949
}
5050

51-
const (
52-
SYSTEM_USER_ID = 1
53-
)
54-
5551
func (app *App) IsAppJobOrExternalType() bool {
5652
return len(app.DisplayName) > 0
5753
}

pkg/build/git/gitWebhook/GitWebhookService.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/devtron-labs/devtron/client/gitSensor"
2121
"github.com/devtron-labs/devtron/internal/sql/constants"
2222
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig"
23+
bean2 "github.com/devtron-labs/devtron/pkg/auth/user/bean"
2324
"github.com/devtron-labs/devtron/pkg/bean"
2425
"github.com/devtron-labs/devtron/pkg/build/git/gitWebhook/repository"
2526
"github.com/devtron-labs/devtron/pkg/pipeline"
@@ -71,7 +72,7 @@ func (impl *GitWebhookServiceImpl) HandleGitWebhook(gitWebhookRequest gitSensor.
7172

7273
resp, err := impl.ciHandler.HandleCIWebhook(bean.GitCiTriggerRequest{
7374
CiPipelineMaterial: ciPipelineMaterial,
74-
TriggeredBy: 1, // Automatic trigger, userId is 1
75+
TriggeredBy: bean2.SYSTEM_USER_ID, // Automatic trigger, system user
7576
ExtraEnvironmentVariables: gitWebhookRequest.ExtraEnvironmentVariables,
7677
})
7778
if err != nil {

pkg/deployment/trigger/devtronApps/PreStageTriggerService.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/devtron-labs/devtron/internal/util"
3333
bean6 "github.com/devtron-labs/devtron/pkg/app/bean"
3434
attributesBean "github.com/devtron-labs/devtron/pkg/attributes/bean"
35+
bean7 "github.com/devtron-labs/devtron/pkg/auth/user/bean"
3536
bean4 "github.com/devtron-labs/devtron/pkg/bean"
3637
"github.com/devtron-labs/devtron/pkg/bean/common"
3738
repository4 "github.com/devtron-labs/devtron/pkg/cluster/environment/repository"
@@ -177,7 +178,7 @@ func (impl *TriggerServiceImpl) TriggerPreStage(request bean.TriggerRequest) (*b
177178
return manifestPushTemplate, nil
178179
}
179180

180-
func (impl *TriggerServiceImpl) TriggerAutoCDOnPreStageSuccess(triggerContext bean.TriggerContext, cdPipelineId, ciArtifactId, workflowId int, triggerdBy int32) error {
181+
func (impl *TriggerServiceImpl) TriggerAutoCDOnPreStageSuccess(triggerContext bean.TriggerContext, cdPipelineId, ciArtifactId, workflowId int) error {
181182
pipeline, err := impl.pipelineRepository.FindById(cdPipelineId)
182183
if err != nil {
183184
return err
@@ -204,7 +205,7 @@ func (impl *TriggerServiceImpl) TriggerAutoCDOnPreStageSuccess(triggerContext be
204205
CdWf: cdWorkflow,
205206
Pipeline: pipeline,
206207
Artifact: ciArtifact,
207-
TriggeredBy: triggerdBy,
208+
TriggeredBy: bean7.SYSTEM_USER_ID,
208209
TriggerContext: triggerContext,
209210
}
210211

pkg/deployment/trigger/devtronApps/TriggerService.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ type TriggerService interface {
104104
TriggerPostStage(request bean.TriggerRequest) (*bean4.ManifestPushTemplate, error)
105105
TriggerPreStage(request bean.TriggerRequest) (*bean4.ManifestPushTemplate, error)
106106

107-
TriggerAutoCDOnPreStageSuccess(triggerContext bean.TriggerContext, cdPipelineId, ciArtifactId, workflowId int, triggerdBy int32) error
107+
TriggerAutoCDOnPreStageSuccess(triggerContext bean.TriggerContext, cdPipelineId, ciArtifactId, workflowId int) error
108108

109109
TriggerStageForBulk(triggerRequest bean.TriggerRequest) error
110110

@@ -646,7 +646,7 @@ func (impl *TriggerServiceImpl) TriggerAutomaticDeployment(request bean.TriggerR
646646
WorkflowType: bean3.CD_WORKFLOW_TYPE_DEPLOY,
647647
ExecutorType: cdWorkflow.WORKFLOW_EXECUTOR_TYPE_SYSTEM,
648648
Status: cdWorkflow.WorkflowInitiated, // deployment Initiated for auto trigger
649-
TriggeredBy: 1,
649+
TriggeredBy: triggeredBy,
650650
StartedOn: triggeredAt,
651651
Namespace: impl.config.GetDefaultNamespace(),
652652
CdWorkflowId: cdWf.Id,
@@ -677,13 +677,12 @@ func (impl *TriggerServiceImpl) TriggerAutomaticDeployment(request bean.TriggerR
677677
impl.logger.Errorw("error in fetching environment deployment config by appId and envId", "appId", pipeline.AppId, "envId", pipeline.EnvironmentId, "err", err)
678678
return err
679679
}
680-
// setting triggeredBy as 1(system user) since case of auto trigger
681-
validationErr := impl.validateDeploymentTriggerRequest(ctx, adapter.NewValidateDeploymentTriggerObj(runner, pipeline, artifact.ImageDigest, envDeploymentConfig, 1, false))
680+
validationErr := impl.validateDeploymentTriggerRequest(ctx, adapter.NewValidateDeploymentTriggerObj(runner, pipeline, artifact.ImageDigest, envDeploymentConfig, triggeredBy, false))
682681
if validationErr != nil {
683682
impl.logger.Errorw("validation error deployment request", "cdWfr", runner.Id, "err", validationErr)
684683
return validationErr
685684
}
686-
releaseErr := impl.TriggerCD(ctx, artifact, cdWf.Id, savedWfr.Id, pipeline, envDeploymentConfig, triggeredAt)
685+
releaseErr := impl.TriggerCD(ctx, artifact, cdWf.Id, savedWfr.Id, pipeline, envDeploymentConfig, triggeredAt, triggeredBy)
687686
// if releaseErr found, then the mark current deployment Failed and return
688687
if releaseErr != nil {
689688
err := impl.cdWorkflowCommonService.MarkCurrentDeploymentFailed(runner, releaseErr, triggeredBy)
@@ -695,38 +694,38 @@ func (impl *TriggerServiceImpl) TriggerAutomaticDeployment(request bean.TriggerR
695694
return nil
696695
}
697696

698-
func (impl *TriggerServiceImpl) TriggerCD(ctx context.Context, artifact *repository3.CiArtifact, cdWorkflowId, wfrId int, pipeline *pipelineConfig.Pipeline, envDeploymentConfig *bean9.DeploymentConfig, triggeredAt time.Time) error {
697+
func (impl *TriggerServiceImpl) TriggerCD(ctx context.Context, artifact *repository3.CiArtifact, cdWorkflowId, wfrId int, pipeline *pipelineConfig.Pipeline, envDeploymentConfig *bean9.DeploymentConfig, triggeredAt time.Time, triggeredBy int32) error {
699698
impl.logger.Debugw("automatic pipeline trigger attempt async", "artifactId", artifact.Id)
700-
err := impl.triggerReleaseAsync(ctx, artifact, cdWorkflowId, wfrId, pipeline, envDeploymentConfig, triggeredAt)
699+
err := impl.triggerReleaseAsync(ctx, artifact, cdWorkflowId, wfrId, pipeline, envDeploymentConfig, triggeredAt, triggeredBy)
701700
if err != nil {
702701
impl.logger.Errorw("error in cd trigger", "err", err)
703702
return err
704703
}
705704
return err
706705
}
707706

708-
func (impl *TriggerServiceImpl) triggerReleaseAsync(ctx context.Context, artifact *repository3.CiArtifact, cdWorkflowId, wfrId int, pipeline *pipelineConfig.Pipeline, envDeploymentConfig *bean9.DeploymentConfig, triggeredAt time.Time) error {
709-
err := impl.validateAndTrigger(ctx, pipeline, envDeploymentConfig, artifact, cdWorkflowId, wfrId, triggeredAt)
707+
func (impl *TriggerServiceImpl) triggerReleaseAsync(ctx context.Context, artifact *repository3.CiArtifact, cdWorkflowId, wfrId int, pipeline *pipelineConfig.Pipeline, envDeploymentConfig *bean9.DeploymentConfig, triggeredAt time.Time, triggeredBy int32) error {
708+
err := impl.validateAndTrigger(ctx, pipeline, envDeploymentConfig, artifact, cdWorkflowId, wfrId, triggeredAt, triggeredBy)
710709
if err != nil {
711710
impl.logger.Errorw("error in trigger for pipeline", "pipelineId", strconv.Itoa(pipeline.Id))
712711
}
713712
impl.logger.Debugw("trigger attempted for all pipeline ", "artifactId", artifact.Id)
714713
return err
715714
}
716715

717-
func (impl *TriggerServiceImpl) validateAndTrigger(ctx context.Context, p *pipelineConfig.Pipeline, envDeploymentConfig *bean9.DeploymentConfig, artifact *repository3.CiArtifact, cdWorkflowId, wfrId int, triggeredAt time.Time) error {
716+
func (impl *TriggerServiceImpl) validateAndTrigger(ctx context.Context, p *pipelineConfig.Pipeline, envDeploymentConfig *bean9.DeploymentConfig, artifact *repository3.CiArtifact, cdWorkflowId, wfrId int, triggeredAt time.Time, triggeredBy int32) error {
718717
//TODO: verify this logic
719718
object := impl.enforcerUtil.GetAppRBACNameByAppId(p.AppId)
720719
envApp := strings.Split(object, "/")
721720
if len(envApp) != 2 {
722721
impl.logger.Error("invalid req, app and env not found from rbac")
723722
return errors.New("invalid req, app and env not found from rbac")
724723
}
725-
err := impl.releasePipeline(ctx, p, envDeploymentConfig, artifact, cdWorkflowId, wfrId, triggeredAt)
724+
err := impl.releasePipeline(ctx, p, envDeploymentConfig, artifact, cdWorkflowId, wfrId, triggeredAt, triggeredBy)
726725
return err
727726
}
728727

729-
func (impl *TriggerServiceImpl) releasePipeline(ctx context.Context, pipeline *pipelineConfig.Pipeline, envDeploymentConfig *bean9.DeploymentConfig, artifact *repository3.CiArtifact, cdWorkflowId, wfrId int, triggeredAt time.Time) error {
728+
func (impl *TriggerServiceImpl) releasePipeline(ctx context.Context, pipeline *pipelineConfig.Pipeline, envDeploymentConfig *bean9.DeploymentConfig, artifact *repository3.CiArtifact, cdWorkflowId, wfrId int, triggeredAt time.Time, triggeredBy int32) error {
730729
startTime := time.Now()
731730
defer func() {
732731
impl.logger.Debugw("auto trigger release process completed", "timeTaken", time.Since(startTime), "cdPipelineId", pipeline.Id, "artifactId", artifact.Id, "wfrId", wfrId)
@@ -751,8 +750,7 @@ func (impl *TriggerServiceImpl) releasePipeline(ctx context.Context, pipeline *p
751750

752751
adapter.SetPipelineFieldsInOverrideRequest(request, pipeline, envDeploymentConfig)
753752

754-
// setting deployedBy as 1(system user) since case of auto trigger
755-
id, _, err := impl.handleCDTriggerRelease(ctx, request, envDeploymentConfig, triggeredAt, 1)
753+
id, _, err := impl.handleCDTriggerRelease(ctx, request, envDeploymentConfig, triggeredAt, triggeredBy)
756754
if err != nil {
757755
impl.logger.Errorw("error in auto cd pipeline trigger", "pipelineId", pipeline.Id, "artifactId", artifact.Id, "err", err)
758756
} else {

pkg/eventProcessor/in/WorkflowEventProcessorService.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ func (impl *WorkflowEventProcessorImpl) SubscribeTriggerBulkAction() error {
316316
CdWf: adapter.ConvertCdWorkflowDtoToDbObj(wf), //TODO: update object from db to dto
317317
Artifact: artifact,
318318
Pipeline: pipelineObj,
319-
TriggeredBy: cdWorkflow.CreatedBy,
319+
TriggeredBy: cdWorkflow.CreatedBy, //actual request sent by user who created initial workflow, and then nats event is sent
320320
ApplyAuth: false,
321321
TriggerContext: triggerContext,
322322
}

pkg/pipeline/CiHandler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/devtron-labs/common-lib/utils/workFlow"
2626
"github.com/devtron-labs/devtron/internal/sql/constants"
2727
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig/bean/workflow/cdWorkflow"
28+
bean6 "github.com/devtron-labs/devtron/pkg/auth/user/bean"
2829
"github.com/devtron-labs/devtron/pkg/bean/common"
2930
"github.com/devtron-labs/devtron/pkg/build/artifacts/imageTagging"
3031
bean4 "github.com/devtron-labs/devtron/pkg/build/pipeline/bean"
@@ -222,7 +223,7 @@ func (impl *CiHandlerImpl) reTriggerCi(retryCount int, refCiWorkflow *pipelineCo
222223
}
223224

224225
trigger := types.Trigger{}
225-
trigger.BuildTriggerObject(refCiWorkflow, ciMaterials, 1, true, nil, "")
226+
trigger.BuildTriggerObject(refCiWorkflow, ciMaterials, bean6.SYSTEM_USER_ID, true, nil, "")
226227
_, err = impl.ciService.TriggerCiPipeline(trigger)
227228

228229
if err != nil {

pkg/workflow/dag/WorkflowDagExecutor.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig/bean/workflow"
3636
cdWorkflow2 "github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig/bean/workflow/cdWorkflow"
3737
"github.com/devtron-labs/devtron/pkg/app/status"
38+
bean7 "github.com/devtron-labs/devtron/pkg/auth/user/bean"
3839
"github.com/devtron-labs/devtron/pkg/build/artifacts"
3940
bean5 "github.com/devtron-labs/devtron/pkg/build/pipeline/bean"
4041
common2 "github.com/devtron-labs/devtron/pkg/deployment/common"
@@ -240,7 +241,7 @@ func (impl *WorkflowDagExecutorImpl) HandleCdStageReTrigger(runner *pipelineConf
240241
CdWf: runner.CdWorkflow,
241242
Pipeline: runner.CdWorkflow.Pipeline,
242243
Artifact: runner.CdWorkflow.CiArtifact,
243-
TriggeredBy: 1,
244+
TriggeredBy: bean7.SYSTEM_USER_ID,
244245
ApplyAuth: false,
245246
RefCdWorkflowRunnerId: runner.Id,
246247
TriggerContext: triggerBean.TriggerContext{
@@ -519,7 +520,6 @@ func (impl *WorkflowDagExecutorImpl) deleteCorruptedPipelineStage(pipelineStage
519520
}
520521

521522
func (impl *WorkflowDagExecutorImpl) triggerIfAutoStageCdPipeline(request triggerBean.TriggerRequest) error {
522-
523523
preStage, err := impl.getPipelineStage(request.Pipeline.Id, repository4.PIPELINE_STAGE_TYPE_PRE_CD)
524524
if err != nil {
525525
return err
@@ -533,6 +533,8 @@ func (impl *WorkflowDagExecutorImpl) triggerIfAutoStageCdPipeline(request trigge
533533
}
534534

535535
request.TriggerContext.Context = context.Background()
536+
//for auto stage setting triggeredBy to system user no matter where the request came from
537+
request.TriggeredBy = bean7.SYSTEM_USER_ID
536538
if len(request.Pipeline.PreStageConfig) > 0 || (preStage != nil && !deleted) {
537539
// pre stage exists
538540
if request.Pipeline.PreTriggerType == pipelineConfig.TRIGGER_TYPE_AUTOMATIC {
@@ -613,7 +615,7 @@ func (impl *WorkflowDagExecutorImpl) HandlePreStageSuccessEvent(triggerContext t
613615
} else {
614616
ciArtifactId = cdStageCompleteEvent.CiArtifactDTO.Id
615617
}
616-
err = impl.cdTriggerService.TriggerAutoCDOnPreStageSuccess(triggerContext, cdStageCompleteEvent.CdPipelineId, ciArtifactId, cdStageCompleteEvent.WorkflowId, cdStageCompleteEvent.TriggeredBy)
618+
err = impl.cdTriggerService.TriggerAutoCDOnPreStageSuccess(triggerContext, cdStageCompleteEvent.CdPipelineId, ciArtifactId, cdStageCompleteEvent.WorkflowId)
617619
if err != nil {
618620
impl.logger.Errorw("error in triggering cd on pre cd succcess", "err", err)
619621
return err
@@ -637,11 +639,10 @@ func (impl *WorkflowDagExecutorImpl) HandleDeploymentSuccessEvent(triggerContext
637639
return err
638640
}
639641

640-
var triggeredByUser int32 = 1
641642
//handle corrupt data (https://github.com/devtron-labs/devtron/issues/3826)
642-
err, deleted := impl.deleteCorruptedPipelineStage(postStage, triggeredByUser)
643+
err, deleted := impl.deleteCorruptedPipelineStage(postStage, bean7.SYSTEM_USER_ID)
643644
if err != nil {
644-
impl.logger.Errorw("error in deleteCorruptedPipelineStage ", "err", err, "preStage", postStage, "triggeredBy", triggeredByUser)
645+
impl.logger.Errorw("error in deleteCorruptedPipelineStage ", "err", err, "preStage", postStage)
645646
return err
646647
}
647648

@@ -653,7 +654,7 @@ func (impl *WorkflowDagExecutorImpl) HandleDeploymentSuccessEvent(triggerContext
653654
triggerRequest := triggerBean.TriggerRequest{
654655
CdWf: cdWorkflow,
655656
Pipeline: pipelineOverride.Pipeline,
656-
TriggeredBy: triggeredByUser,
657+
TriggeredBy: bean7.SYSTEM_USER_ID,
657658
TriggerContext: triggerContext,
658659
RefCdWorkflowRunnerId: 0,
659660
}

wire_gen.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)