Skip to content
2 changes: 1 addition & 1 deletion internal/sql/repository/CiArtifactRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (impl CiArtifactRepositoryImpl) GetArtifactParentCiAndWorkflowDetailsByIds(
}

err := impl.dbConnection.Model(&artifacts).
Column("ci_artifact.ci_workflow_id", "ci_artifact.parent_ci_artifact", "ci_artifact.external_ci_pipeline_id").
Column("ci_artifact.ci_workflow_id", "ci_artifact.parent_ci_artifact", "ci_artifact.external_ci_pipeline_id", "ci_artifact.id").
Where("ci_artifact.id in (?)", pg.In(ids)).
Select()

Expand Down
61 changes: 41 additions & 20 deletions internal/sql/repository/pipelineConfig/CdWorfkflowRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/argoproj/gitops-engine/pkg/health"
"github.com/devtron-labs/devtron/api/bean"
"github.com/devtron-labs/devtron/client/argocdServer/application"
"github.com/devtron-labs/devtron/client/gitSensor"
"github.com/devtron-labs/devtron/internal/sql/repository"
"github.com/devtron-labs/devtron/internal/util"
"github.com/devtron-labs/devtron/pkg/sql"
Expand Down Expand Up @@ -161,27 +162,47 @@ type CdWorkflowRunner struct {
sql.AuditLog
}

type CiPipelineMaterialResponse struct {
Id int `json:"id"`
GitMaterialId int `json:"gitMaterialId"`
GitMaterialUrl string `json:"gitMaterialUrl"`
GitMaterialName string `json:"gitMaterialName"`
Type string `json:"type"`
Value string `json:"value"`
Active bool `json:"active"`
History []*gitSensor.GitCommit `json:"history,omitempty"`
LastFetchTime time.Time `json:"lastFetchTime"`
IsRepoError bool `json:"isRepoError"`
RepoErrorMsg string `json:"repoErrorMsg"`
IsBranchError bool `json:"isBranchError"`
BranchErrorMsg string `json:"branchErrorMsg"`
Url string `json:"url"`
Regex string `json:"regex"`
}

type CdWorkflowWithArtifact struct {
Id int `json:"id"`
CdWorkflowId int `json:"cd_workflow_id"`
Name string `json:"name"`
Status string `json:"status"`
PodStatus string `json:"pod_status"`
Message string `json:"message"`
StartedOn time.Time `json:"started_on"`
FinishedOn time.Time `json:"finished_on"`
PipelineId int `json:"pipeline_id"`
Namespace string `json:"namespace"`
LogFilePath string `json:"log_file_path"`
TriggeredBy int32 `json:"triggered_by"`
EmailId string `json:"email_id"`
Image string `json:"image"`
MaterialInfo string `json:"material_info,omitempty"`
DataSource string `json:"data_source,omitempty"`
CiArtifactId int `json:"ci_artifact_id,omitempty"`
WorkflowType string `json:"workflow_type,omitempty"`
ExecutorType string `json:"executor_type,omitempty"`
BlobStorageEnabled bool `json:"blobStorageEnabled"`
Id int `json:"id"`
CdWorkflowId int `json:"cd_workflow_id"`
Name string `json:"name"`
Status string `json:"status"`
PodStatus string `json:"pod_status"`
Message string `json:"message"`
StartedOn time.Time `json:"started_on"`
FinishedOn time.Time `json:"finished_on"`
PipelineId int `json:"pipeline_id"`
Namespace string `json:"namespace"`
LogFilePath string `json:"log_file_path"`
TriggeredBy int32 `json:"triggered_by"`
EmailId string `json:"email_id"`
Image string `json:"image"`
MaterialInfo string `json:"material_info,omitempty"`
DataSource string `json:"data_source,omitempty"`
CiArtifactId int `json:"ci_artifact_id,omitempty"`
WorkflowType string `json:"workflow_type,omitempty"`
ExecutorType string `json:"executor_type,omitempty"`
BlobStorageEnabled bool `json:"blobStorageEnabled"`
GitTriggers map[int]GitCommit `json:"gitTriggers"`
CiMaterials []CiPipelineMaterialResponse `json:"ciMaterials"`
}

type TriggerWorkflowStatus struct {
Expand Down
10 changes: 10 additions & 0 deletions internal/sql/repository/pipelineConfig/CiWorkflowRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type CiWorkflowRepository interface {

FindLastTriggeredWorkflowByCiIds(pipelineId []int) (ciWorkflow []*CiWorkflow, err error)
FindLastTriggeredWorkflowByArtifactId(ciArtifactId int) (ciWorkflow *CiWorkflow, err error)
FindAllLastTriggeredWorkflowByArtifactId(ciArtifactId []int) (ciWorkflow []*CiWorkflow, err error)
FindLastTriggeredWorkflowGitTriggersByArtifactId(ciArtifactId int) (ciWorkflow *CiWorkflow, err error)
ExistsByStatus(status string) (bool, error)
FindBuildTypeAndStatusDataOfLast1Day() []*BuildTypeCount
Expand Down Expand Up @@ -238,6 +239,15 @@ func (impl *CiWorkflowRepositoryImpl) FindLastTriggeredWorkflowByArtifactId(ciAr
return workflow, err
}

func (impl *CiWorkflowRepositoryImpl) FindAllLastTriggeredWorkflowByArtifactId(ciArtifactIds []int) (ciWorkflows []*CiWorkflow, err error) {
err = impl.dbConnection.Model(&ciWorkflows).
Column("ci_workflow.git_triggers", "ci_workflow.ci_pipeline_id", "CiPipeline", "cia.id").
Join("inner join ci_artifact cia on cia.ci_workflow_id = ci_workflow.id").
Where("cia.id in (?) ", pg.In(ciArtifactIds)).
Select()
return ciWorkflows, err
}

func (impl *CiWorkflowRepositoryImpl) FindLastTriggeredWorkflowGitTriggersByArtifactId(ciArtifactId int) (ciWorkflow *CiWorkflow, err error) {
workflow := &CiWorkflow{}
err = impl.dbConnection.Model(workflow).
Expand Down
86 changes: 84 additions & 2 deletions pkg/pipeline/CdHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,88 @@ func (impl *CdHandlerImpl) GetCdBuildHistory(appId int, environmentId int, pipel
return cdWorkflowArtifact, err
}
cdWorkflowArtifact = impl.converterWFRList(wfrList)
if err == pg.ErrNoRows || wfrList == nil {
return cdWorkflowArtifact, nil
}
var ciArtifactIds []int
for _, cdWfA := range cdWorkflowArtifact {
ciArtifactIds = append(ciArtifactIds, cdWfA.CiArtifactId)
}
parentCiArtifact := make(map[int]int)
isLinked := false
ciArtifacts, err := impl.ciArtifactRepository.GetArtifactParentCiAndWorkflowDetailsByIds(ciArtifactIds)
if err != nil || len(ciArtifacts) == 0 {
impl.Logger.Errorw("error fetching artifact data", "err", err)
return cdWorkflowArtifact, err
}
var newCiArtifactIds []int
for _, ciArtifact := range ciArtifacts {
if ciArtifact.ParentCiArtifact > 0 && ciArtifact.WorkflowId == nil {
isLinked = true
newCiArtifactIds = append(newCiArtifactIds, ciArtifact.ParentCiArtifact)
parentCiArtifact[ciArtifact.Id] = ciArtifact.ParentCiArtifact
} else {
newCiArtifactIds = append(newCiArtifactIds, ciArtifact.Id)
}
}
// handling linked ci pipeline
if isLinked {
ciArtifactIds = newCiArtifactIds
}

ciWfs, err := impl.ciWorkflowRepository.FindAllLastTriggeredWorkflowByArtifactId(ciArtifactIds)
if err != nil && err != pg.ErrNoRows {
impl.Logger.Errorw("error in fetching ci wfs", "artifactIds", ciArtifactIds, "err", err)
return cdWorkflowArtifact, err
} else if len(ciWfs) == 0 {
return cdWorkflowArtifact, nil
}

wfGitTriggers := make(map[int]map[int]pipelineConfig.GitCommit)
var ciPipelineId int
for _, ciWf := range ciWfs {
ciPipelineId = ciWf.CiPipelineId
wfGitTriggers[ciWf.Id] = ciWf.GitTriggers
}
ciMaterials, err := impl.ciPipelineMaterialRepository.GetByPipelineIdForRegexAndFixed(ciPipelineId)
if err != nil && err != pg.ErrNoRows {
impl.Logger.Errorw("err in fetching ci materials", "ciMaterials", ciMaterials, "err", err)
return cdWorkflowArtifact, err
}

var ciMaterialsArr []pipelineConfig.CiPipelineMaterialResponse
for _, ciMaterial := range ciMaterials {
res := pipelineConfig.CiPipelineMaterialResponse{
Id: ciMaterial.Id,
GitMaterialId: ciMaterial.GitMaterialId,
GitMaterialName: ciMaterial.GitMaterial.Name[strings.Index(ciMaterial.GitMaterial.Name, "-")+1:],
Type: string(ciMaterial.Type),
Value: ciMaterial.Value,
Active: ciMaterial.Active,
Url: ciMaterial.GitMaterial.Url,
}
ciMaterialsArr = append(ciMaterialsArr, res)
}
var newCdWorkflowArtifact []pipelineConfig.CdWorkflowWithArtifact
for _, cdWfA := range cdWorkflowArtifact {

gitTriggers := make(map[int]pipelineConfig.GitCommit)
if isLinked {
if gitTriggerVal, ok := wfGitTriggers[parentCiArtifact[cdWfA.CiArtifactId]]; ok {
gitTriggers = gitTriggerVal
}
} else {
if gitTriggerVal, ok := wfGitTriggers[cdWfA.CiArtifactId]; ok {
gitTriggers = gitTriggerVal
}
}

cdWfA.GitTriggers = gitTriggers
cdWfA.CiMaterials = ciMaterialsArr
newCdWorkflowArtifact = append(newCdWorkflowArtifact, cdWfA)

}
cdWorkflowArtifact = newCdWorkflowArtifact
}

return cdWorkflowArtifact, nil
Expand Down Expand Up @@ -867,9 +949,9 @@ func (impl *CdHandlerImpl) FetchCdWorkflowDetails(appId int, environmentId int,
return WorkflowResponse{}, err
}

var ciMaterialsArr []CiPipelineMaterialResponse
var ciMaterialsArr []pipelineConfig.CiPipelineMaterialResponse
for _, m := range ciMaterials {
res := CiPipelineMaterialResponse{
res := pipelineConfig.CiPipelineMaterialResponse{
Id: m.Id,
GitMaterialId: m.GitMaterialId,
GitMaterialName: m.GitMaterial.Name[strings.Index(m.GitMaterial.Name, "-")+1:],
Expand Down
2 changes: 0 additions & 2 deletions pkg/pipeline/CiCdPipelineOrchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1176,8 +1176,6 @@ func (impl CiCdPipelineOrchestratorImpl) CreateCDPipelines(pipelineRequest *bean
return 0, err
}



env, err := impl.envRepository.FindById(pipelineRequest.EnvironmentId)
if err != nil {
impl.logger.Errorw("error in getting environment by id", "err", err)
Expand Down
Loading