Skip to content

Commit 06d6c05

Browse files
authored
Updated ci artifact response for running artifact in parent cd data (#1233)
* updated ci artifact response for running artifact in parent cd data * file format * updated condition for getting parent CD id
1 parent ac60fec commit 06d6c05

File tree

2 files changed

+52
-30
lines changed

2 files changed

+52
-30
lines changed

pkg/bean/app.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -512,18 +512,19 @@ type Rollback struct {
512512
}
513513

514514
type CiArtifactBean struct {
515-
Id int `json:"id"`
516-
Image string `json:"image,notnull"`
517-
ImageDigest string `json:"image_digest,notnull"`
518-
MaterialInfo json.RawMessage `json:"material_info"` //git material metadata json array string
519-
DataSource string `json:"data_source,notnull"`
520-
DeployedTime string `json:"deployed_time"`
521-
Deployed bool `json:"deployed,notnull"`
522-
Latest bool `json:"latest,notnull"`
523-
RunningOnParent bool `json:"runningOnParent,notnull"`
524-
IsVulnerable bool `json:"vulnerable,notnull"`
525-
ScanEnabled bool `json:"scanEnabled,notnull"`
526-
Scanned bool `json:"scanned,notnull"`
515+
Id int `json:"id"`
516+
Image string `json:"image,notnull"`
517+
ImageDigest string `json:"image_digest,notnull"`
518+
MaterialInfo json.RawMessage `json:"material_info"` //git material metadata json array string
519+
DataSource string `json:"data_source,notnull"`
520+
DeployedTime string `json:"deployed_time"`
521+
Deployed bool `json:"deployed,notnull"`
522+
Latest bool `json:"latest,notnull"`
523+
LastSuccessfulTriggerOnParent bool `json:"lastSuccessfulTriggerOnParent,notnull"`
524+
RunningOnParentCd bool `json:"runningOnParentCd,omitempty"`
525+
IsVulnerable bool `json:"vulnerable,notnull"`
526+
ScanEnabled bool `json:"scanEnabled,notnull"`
527+
Scanned bool `json:"scanned,notnull"`
527528
}
528529

529530
type CiArtifactResponse struct {

pkg/pipeline/PipelineBuilder.go

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,11 @@ func (impl PipelineBuilderImpl) GetArtifactsByCDPipeline(cdPipelineId int, stage
16141614
impl.logger.Errorw("error in getting cd parent details", "err", err, "cdPipelineId", cdPipelineId, "stage", stage)
16151615
return ciArtifactsResponse, err
16161616
}
1617+
//setting parent cd id for checking latest image running on parent cd
1618+
parentCdId := 0
1619+
if parentType == bean2.CD_WORKFLOW_TYPE_POST || (parentType == bean2.CD_WORKFLOW_TYPE_DEPLOY && stage != bean2.CD_WORKFLOW_TYPE_POST) {
1620+
parentCdId = parentId
1621+
}
16171622
pipeline, err := impl.pipelineRepository.FindById(cdPipelineId)
16181623
if err != nil && err != pg.ErrNoRows {
16191624
impl.logger.Errorw("Error in getting cd pipeline details", err, "cdPipelineId", cdPipelineId)
@@ -1626,7 +1631,7 @@ func (impl PipelineBuilderImpl) GetArtifactsByCDPipeline(cdPipelineId int, stage
16261631
parentId = cdPipelineId
16271632
parentType = bean2.CD_WORKFLOW_TYPE_DEPLOY
16281633
}
1629-
ciArtifactsResponse, err = impl.GetArtifactsForCdStage(cdPipelineId, parentId, parentType, stage)
1634+
ciArtifactsResponse, err = impl.GetArtifactsForCdStage(cdPipelineId, parentId, parentType, stage, parentCdId)
16301635
if err != nil {
16311636
impl.logger.Errorw("error in getting artifacts for cd", "err", err, "stage", stage, "cdPipelineId", cdPipelineId)
16321637
return ciArtifactsResponse, err
@@ -1655,22 +1660,21 @@ func (impl PipelineBuilderImpl) GetCdParentDetails(cdPipelineId int) (parentId i
16551660
return parentId, bean2.CD_WORKFLOW_TYPE_DEPLOY, nil
16561661
}
16571662
}
1658-
// empty string used to denote CI pipeline
16591663
return parentId, bean2.CI_WORKFLOW_TYPE, nil
16601664
}
16611665

1662-
func (impl PipelineBuilderImpl) GetArtifactsForCdStage(cdPipelineId int, parentId int, parentType bean2.WorkflowType, stage bean2.WorkflowType) (bean.CiArtifactResponse, error) {
1666+
func (impl PipelineBuilderImpl) GetArtifactsForCdStage(cdPipelineId int, parentId int, parentType bean2.WorkflowType, stage bean2.WorkflowType, parentCdId int) (bean.CiArtifactResponse, error) {
16631667
var ciArtifacts []bean.CiArtifactBean
16641668
var ciArtifactsResponse bean.CiArtifactResponse
16651669
var err error
16661670
artifactMap := make(map[int]int)
16671671
limit := 30
1668-
ciArtifacts, artifactMap, err = impl.BuildArtifactsForCdStage(cdPipelineId, stage, ciArtifacts, artifactMap, false, limit)
1672+
ciArtifacts, artifactMap, err = impl.BuildArtifactsForCdStage(cdPipelineId, stage, ciArtifacts, artifactMap, false, limit, parentCdId)
16691673
if err != nil && err != pg.ErrNoRows {
16701674
impl.logger.Errorw("error in getting artifacts for child cd stage", "err", err, "stage", stage)
16711675
return ciArtifactsResponse, err
16721676
}
1673-
ciArtifacts, err = impl.BuildArtifactsForParentStage(cdPipelineId, parentId, parentType, ciArtifacts, artifactMap, limit)
1677+
ciArtifacts, err = impl.BuildArtifactsForParentStage(cdPipelineId, parentId, parentType, ciArtifacts, artifactMap, limit, parentCdId)
16741678
if err != nil && err != pg.ErrNoRows {
16751679
impl.logger.Errorw("error in getting artifacts for cd", "err", err, "parentStage", parentType, "stage", stage)
16761680
return ciArtifactsResponse, err
@@ -1689,19 +1693,29 @@ func (impl PipelineBuilderImpl) GetArtifactsForCdStage(cdPipelineId int, parentI
16891693
return ciArtifactsResponse, nil
16901694
}
16911695

1692-
func (impl PipelineBuilderImpl) BuildArtifactsForParentStage(cdPipelineId int, parentId int, parentType bean2.WorkflowType, ciArtifacts []bean.CiArtifactBean, artifactMap map[int]int, limit int) ([]bean.CiArtifactBean, error) {
1696+
func (impl PipelineBuilderImpl) BuildArtifactsForParentStage(cdPipelineId int, parentId int, parentType bean2.WorkflowType, ciArtifacts []bean.CiArtifactBean, artifactMap map[int]int, limit int, parentCdId int) ([]bean.CiArtifactBean, error) {
16931697
var ciArtifactsFinal []bean.CiArtifactBean
16941698
var err error
16951699
if parentType == bean2.CI_WORKFLOW_TYPE {
16961700
ciArtifactsFinal, err = impl.BuildArtifactsForCIParent(cdPipelineId, ciArtifacts, artifactMap, limit)
16971701
} else {
16981702
//parent type is PRE, POST or DEPLOY type
1699-
ciArtifactsFinal, _, err = impl.BuildArtifactsForCdStage(parentId, parentType, ciArtifacts, artifactMap, true, limit)
1703+
ciArtifactsFinal, _, err = impl.BuildArtifactsForCdStage(parentId, parentType, ciArtifacts, artifactMap, true, limit, parentCdId)
17001704
}
17011705
return ciArtifactsFinal, err
17021706
}
17031707

1704-
func (impl PipelineBuilderImpl) BuildArtifactsForCdStage(pipelineId int, stageType bean2.WorkflowType, ciArtifacts []bean.CiArtifactBean, artifactMap map[int]int, parent bool, limit int) ([]bean.CiArtifactBean, map[int]int, error) {
1708+
func (impl PipelineBuilderImpl) BuildArtifactsForCdStage(pipelineId int, stageType bean2.WorkflowType, ciArtifacts []bean.CiArtifactBean, artifactMap map[int]int, parent bool, limit int, parentCdId int) ([]bean.CiArtifactBean, map[int]int, error) {
1709+
//getting running artifact id for parent cd
1710+
parentCdRunningArtifactId := 0
1711+
if parentCdId > 0 && parent {
1712+
parentCdWfrList, err := impl.cdWorkflowRepository.FindArtifactByPipelineIdAndRunnerType(parentCdId, bean2.CD_WORKFLOW_TYPE_DEPLOY, 1)
1713+
if err != nil {
1714+
impl.logger.Errorw("error in getting artifact for parent cd", "parentCdPipelineId", parentCdId)
1715+
return ciArtifacts, artifactMap, err
1716+
}
1717+
parentCdRunningArtifactId = parentCdWfrList[0].CdWorkflow.CiArtifact.Id
1718+
}
17051719
//getting wfr for parent and updating artifacts
17061720
parentWfrList, err := impl.cdWorkflowRepository.FindArtifactByPipelineIdAndRunnerType(pipelineId, stageType, limit)
17071721
if err != nil {
@@ -1716,8 +1730,9 @@ func (impl PipelineBuilderImpl) BuildArtifactsForCdStage(pipelineId int, stageTy
17161730
}
17171731
for index, wfr := range parentWfrList {
17181732
if wfr.Status == acceptedStatus {
1719-
runningOnParent := parent && index == 0
1733+
lastSuccessfulTriggerOnParent := parent && index == 0
17201734
latest := !parent && index == 0
1735+
runningOnParentCd := parentCdRunningArtifactId == wfr.CdWorkflow.CiArtifact.Id
17211736
if ciArtifactIndex, ok := artifactMap[wfr.CdWorkflow.CiArtifact.Id]; !ok {
17221737
//entry not present, creating new entry
17231738
mInfo, err := parseMaterialInfo([]byte(wfr.CdWorkflow.CiArtifact.MaterialInfo), wfr.CdWorkflow.CiArtifact.DataSource)
@@ -1726,26 +1741,32 @@ func (impl PipelineBuilderImpl) BuildArtifactsForCdStage(pipelineId int, stageTy
17261741
impl.logger.Errorw("Error in parsing artifact material info", "err", err)
17271742
}
17281743
ciArtifact := bean.CiArtifactBean{
1729-
Id: wfr.CdWorkflow.CiArtifact.Id,
1730-
Image: wfr.CdWorkflow.CiArtifact.Image,
1731-
ImageDigest: wfr.CdWorkflow.CiArtifact.ImageDigest,
1732-
MaterialInfo: mInfo,
1733-
RunningOnParent: runningOnParent,
1734-
Latest: latest,
1735-
Scanned: wfr.CdWorkflow.CiArtifact.Scanned,
1736-
ScanEnabled: wfr.CdWorkflow.CiArtifact.ScanEnabled,
1744+
Id: wfr.CdWorkflow.CiArtifact.Id,
1745+
Image: wfr.CdWorkflow.CiArtifact.Image,
1746+
ImageDigest: wfr.CdWorkflow.CiArtifact.ImageDigest,
1747+
MaterialInfo: mInfo,
1748+
LastSuccessfulTriggerOnParent: lastSuccessfulTriggerOnParent,
1749+
Latest: latest,
1750+
Scanned: wfr.CdWorkflow.CiArtifact.Scanned,
1751+
ScanEnabled: wfr.CdWorkflow.CiArtifact.ScanEnabled,
17371752
}
17381753
if !parent {
17391754
ciArtifact.Deployed = true
17401755
ciArtifact.DeployedTime = formatDate(wfr.StartedOn, bean.LayoutRFC3339)
17411756
}
1757+
if runningOnParentCd {
1758+
ciArtifact.RunningOnParentCd = runningOnParentCd
1759+
}
17421760
ciArtifacts = append(ciArtifacts, ciArtifact)
17431761
//storing index of ci artifact for using when updating old entry
17441762
artifactMap[wfr.CdWorkflow.CiArtifact.Id] = len(ciArtifacts) - 1
17451763
} else {
17461764
//entry already present, updating running on parent
17471765
if parent {
1748-
ciArtifacts[ciArtifactIndex].RunningOnParent = runningOnParent
1766+
ciArtifacts[ciArtifactIndex].LastSuccessfulTriggerOnParent = lastSuccessfulTriggerOnParent
1767+
}
1768+
if runningOnParentCd {
1769+
ciArtifacts[ciArtifactIndex].RunningOnParentCd = runningOnParentCd
17491770
}
17501771
}
17511772
}

0 commit comments

Comments
 (0)