Skip to content

Commit

Permalink
migration for deprecated DataSourceType
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash-exp committed Jan 2, 2024
1 parent 118ce4b commit 9031282
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
13 changes: 13 additions & 0 deletions internal/sql/repository/CiArtifactRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ type CiArtifact struct {
type CiArtifactRepository interface {
Save(artifact *CiArtifact) error
Delete(artifact *CiArtifact) error

// Get returns the CiArtifact of the given id.
// Note: Use Get along with MigrateToWebHookDataSourceType. For webhook artifacts, migration is required for column DataSource from 'ext' to 'EXTERNAL'
Get(id int) (artifact *CiArtifact, err error)
GetArtifactParentCiAndWorkflowDetailsByIds(ids []int) ([]*CiArtifact, error)
GetByWfId(wfId int) (artifact *CiArtifact, err error)
Expand All @@ -109,6 +112,8 @@ type CiArtifactRepository interface {
GetArtifactsByDataSourceAndComponentId(dataSource string, componentId int) ([]CiArtifact, error)
FindCiArtifactByImagePaths(images []string) ([]CiArtifact, error)

// MigrateToWebHookDataSourceType is used for backward compatibility. It'll migrate the deprecated DataSource type
MigrateToWebHookDataSourceType(id int) error
UpdateLatestTimestamp(artifactIds []int) error
}

Expand All @@ -135,6 +140,14 @@ func (impl CiArtifactRepositoryImpl) SaveAll(artifacts []*CiArtifact) ([]*CiArti
return artifacts, err
}

func (impl CiArtifactRepositoryImpl) MigrateToWebHookDataSourceType(id int) error {
_, err := impl.dbConnection.Model(&CiArtifact{}).
Set("data_source = ?", WEBHOOK).
Where("id = ?", id).
Update()
return err
}

func (impl CiArtifactRepositoryImpl) UpdateLatestTimestamp(artifactIds []int) error {
if len(artifactIds) == 0 {
impl.logger.Debug("UpdateLatestTimestamp empty list of artifacts, not updating")
Expand Down
47 changes: 44 additions & 3 deletions pkg/pipeline/WorkflowDagExecutor.go
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,13 @@ func (impl *WorkflowDagExecutorImpl) HandlePreStageSuccessEvent(cdStageCompleteE
if err != nil {
return err
}
// Migration of deprecated DataSource Type
if ciArtifact.DataSource == repository.DEPRICATED_EXT {
migrationErr := impl.ciArtifactRepository.MigrateToWebHookDataSourceType(ciArtifact.Id)
if migrationErr != nil {
impl.logger.Warnw("unable to migrate deprecated DataSource", "artifactId", ciArtifact.Id)
}
}
PreCDArtifacts, err := impl.SavePluginArtifacts(ciArtifact, cdStageCompleteEvent.PluginRegistryArtifactDetails, pipeline.Id, repository.PRE_CD, cdStageCompleteEvent.TriggeredBy)
if err != nil {
impl.logger.Errorw("error in saving plugin artifacts", "err", err)
Expand Down Expand Up @@ -1278,6 +1285,13 @@ func (impl *WorkflowDagExecutorImpl) TriggerPostStage(cdWf *pipelineConfig.CdWor
return err
}
}
// Migration of deprecated DataSource Type
if cdWf.CiArtifact.DataSource == repository.DEPRICATED_EXT {
migrationErr := impl.ciArtifactRepository.MigrateToWebHookDataSourceType(cdWf.CiArtifact.Id)
if migrationErr != nil {
impl.logger.Warnw("unable to migrate deprecated DataSource", "artifactId", cdWf.CiArtifact.Id)
}
}
//checking vulnerability for the selected image
isVulnerable, err := impl.GetArtifactVulnerabilityStatus(cdWf.CiArtifact, pipeline, context.Background())
if err != nil {
Expand Down Expand Up @@ -1442,7 +1456,13 @@ func (impl *WorkflowDagExecutorImpl) buildWFRequest(runner *pipelineConfig.CdWor
if err != nil {
return nil, err
}

// Migration of deprecated DataSource Type
if artifact.DataSource == repository.DEPRICATED_EXT {
migrationErr := impl.ciArtifactRepository.MigrateToWebHookDataSourceType(artifact.Id)
if migrationErr != nil {
impl.logger.Warnw("unable to migrate deprecated DataSource", "artifactId", artifact.Id)
}
}
ciMaterialInfo, err := repository.GetCiMaterialInfo(artifact.MaterialInfo, artifact.DataSource)
if err != nil {
impl.logger.Errorw("parsing error", "err", err)
Expand Down Expand Up @@ -2326,6 +2346,13 @@ func (impl *WorkflowDagExecutorImpl) ManualCdTrigger(overrideRequest *bean.Value
impl.logger.Errorw("error in getting CiArtifact", "CiArtifactId", overrideRequest.CiArtifactId, "err", err)
return 0, err
}
// Migration of deprecated DataSource Type
if artifact.DataSource == repository.DEPRICATED_EXT {
migrationErr := impl.ciArtifactRepository.MigrateToWebHookDataSourceType(artifact.Id)
if migrationErr != nil {
impl.logger.Warnw("unable to migrate deprecated DataSource", "artifactId", artifact.Id)
}
}
_, span = otel.Tracer("orchestrator").Start(ctx, "TriggerPreStage")
err = impl.TriggerPreStage(ctx, nil, artifact, cdPipeline, overrideRequest.UserId, false, 0)
span.End()
Expand Down Expand Up @@ -2398,6 +2425,13 @@ func (impl *WorkflowDagExecutorImpl) ManualCdTrigger(overrideRequest *bean.Value
impl.logger.Errorw("error in getting ciArtifact, ManualCdTrigger", "CiArtifactId", overrideRequest.CiArtifactId, "err", err)
return 0, err
}
// Migration of deprecated DataSource Type
if artifact.DataSource == repository.DEPRICATED_EXT {
migrationErr := impl.ciArtifactRepository.MigrateToWebHookDataSourceType(artifact.Id)
if migrationErr != nil {
impl.logger.Warnw("unable to migrate deprecated DataSource", "artifactId", artifact.Id)
}
}
isVulnerable, err := impl.GetArtifactVulnerabilityStatus(artifact, cdPipeline, ctx)
if err != nil {
impl.logger.Errorw("error in getting Artifact vulnerability status, ManualCdTrigger", "err", err)
Expand Down Expand Up @@ -2619,14 +2653,21 @@ func (impl *WorkflowDagExecutorImpl) subscribeTriggerBulkAction() error {
impl.cdWorkflowRepository.UpdateWorkFlow(wf)
return
}
artefact, err := impl.ciArtifactRepository.Get(cdWorkflow.CiArtifactId)
artifact, err := impl.ciArtifactRepository.Get(cdWorkflow.CiArtifactId)
if err != nil {
impl.logger.Errorw("error in fetching artefact", "err", err)
wf.WorkflowStatus = pipelineConfig.TRIGGER_ERROR
impl.cdWorkflowRepository.UpdateWorkFlow(wf)
return
}
err = impl.triggerStageForBulk(wf, pipeline, artefact, false, false, cdWorkflow.CreatedBy)
// Migration of deprecated DataSource Type
if artifact.DataSource == repository.DEPRICATED_EXT {
migrationErr := impl.ciArtifactRepository.MigrateToWebHookDataSourceType(artifact.Id)
if migrationErr != nil {
impl.logger.Warnw("unable to migrate deprecated DataSource", "artifactId", artifact.Id)
}
}
err = impl.triggerStageForBulk(wf, pipeline, artifact, false, false, cdWorkflow.CreatedBy)
if err != nil {
impl.logger.Errorw("error in cd trigger ", "err", err)
wf.WorkflowStatus = pipelineConfig.TRIGGER_ERROR
Expand Down

0 comments on commit 9031282

Please sign in to comment.