@@ -46,40 +46,17 @@ func NewCdStage(gitManager helper.GitManager, dockerHelper helper.DockerHelper,
4646 }
4747}
4848
49- func deferCDEvent ( cdRequest * helper.CommonWorkflowRequest , artifactUploaded bool , err error ) ( exitCode int ) {
50- log . Println ( util . DEVTRON , "defer CD stage data." , " err: " , err , "artifactUploaded: " , artifactUploaded )
49+ func ( impl * CdStage ) HandleCDEvent ( ciCdRequest * helper.CiCdTriggerEvent , exitCode * int ) {
50+ resp , err := impl . handleCDEvent ( ciCdRequest )
5151 if err != nil {
52- exitCode = workFlow .DefaultErrorCode
53- var stageError * helper.CdStageError
54- if errors .As (err , & stageError ) {
55- // update artifact uploaded status
56- if ! stageError .IsArtifactUploaded () {
57- stageError = stageError .WithArtifactUploaded (artifactUploaded )
58- }
59- } else {
60- stageError = helper .NewCdStageError (fmt .Errorf (workFlow .CdStageFailed .String (), cdRequest .GetCdStageType (), err )).
61- WithArtifactUploaded (artifactUploaded )
62- }
63- // send ci failure event, for ci failure notification
64- sendCDFailureEvent (cdRequest , stageError )
65- // populate stage error
66- util .PopulateStageError (stageError .ErrorMessage ())
52+ //log error and send completion event
53+ log .Println ("cd stage error: " , err )
6754 }
68- return exitCode
69- }
70-
71- func (impl * CdStage ) HandleCDEvent (ciCdRequest * helper.CiCdTriggerEvent , exitCode * int ) {
72- var artifactUploaded bool
73- var err error
74- defer func () {
75- * exitCode = deferCDEvent (ciCdRequest .CommonWorkflowRequest , artifactUploaded , err )
76- }()
77- //deferCDEvent() handles error and artifact upload status
78- artifactUploaded , err = impl .handleCDEvent (ciCdRequest )
55+ * exitCode = impl .sendCDCompletionEvent (ciCdRequest , resp , err )
7956 return
8057}
8158
82- func (impl * CdStage ) handleCDEvent (ciCdRequest * helper.CiCdTriggerEvent ) (bool , error ) {
59+ func (impl * CdStage ) handleCDEvent (ciCdRequest * helper.CiCdTriggerEvent ) (* helper. HandleCdEventResponse , error ) {
8360 var artifactUploaded bool
8461 var err error
8562 var allPluginArtifacts * helper.PluginArtifacts
@@ -98,14 +75,11 @@ func (impl *CdStage) handleCDEvent(ciCdRequest *helper.CiCdTriggerEvent) (bool,
9875 err = artifactUploadErr
9976 }
10077 }
101- // IsVirtualExecution run flag indicates that cd stage is running in virtual mode.
102- // specifically for isolated environment type, for IsVirtualExecution we don't send success event.
103- // but failure event is sent in case of error.
104- if err == nil && ! ciCdRequest .CommonWorkflowRequest .IsVirtualExecution {
105- //send cd success event
106- sendCDSuccessEvent (ciCdRequest .CommonWorkflowRequest , allPluginArtifacts , artifactUploaded )
107- }
108- return artifactUploaded , err
78+
79+ return & helper.HandleCdEventResponse {
80+ PluginArtifacts : allPluginArtifacts ,
81+ IsArtifactUploaded : artifactUploaded ,
82+ }, err
10983}
11084
11185func collectAndUploadCDArtifacts (cdRequest * helper.CommonWorkflowRequest ) (artifactUploaded bool , err error ) {
@@ -233,23 +207,42 @@ func (impl *CdStage) runCDStages(ciCdRequest *helper.CiCdTriggerEvent) (*helper.
233207 return allPluginArtifacts , nil
234208}
235209
236- func sendCDFailureEvent (ciRequest * helper.CommonWorkflowRequest , err * helper.CdStageError ) {
237- event := adaptor .NewCdCompleteEvent (ciRequest , true ).
238- WithIsArtifactUploaded (err .IsArtifactUploaded ())
239- e := helper .SendCDEvent (ciRequest , event )
240- if e != nil {
241- log .Println (e )
242- }
243- }
244-
245- func sendCDSuccessEvent (commonWorkflowRequest * helper.CommonWorkflowRequest , allPluginArtifacts * helper.PluginArtifacts , artifactUploaded bool ) {
246- log .Println (util .DEVTRON , " event" )
247- event := adaptor .NewCdCompleteEvent (commonWorkflowRequest , false ).
248- WithPluginArtifacts (allPluginArtifacts ).
249- WithIsArtifactUploaded (artifactUploaded )
250- err := helper .SendCDEvent (commonWorkflowRequest , event )
210+ func (impl * CdStage ) sendCDCompletionEvent (ciCdRequest * helper.CiCdTriggerEvent , handleCdEventResp * helper.HandleCdEventResponse , err error ) (exitCode int ) {
211+ log .Println (util .DEVTRON , "CD stage completion data." , "artifactUploaded: " , handleCdEventResp .IsArtifactUploaded , "err " , err )
251212 if err != nil {
252- log .Println (err )
213+ exitCode = workFlow .DefaultErrorCode
214+ var stageError * helper.CdStageError
215+ if errors .As (err , & stageError ) {
216+ // update artifact uploaded status
217+ if ! stageError .IsArtifactUploaded () {
218+ stageError = stageError .WithArtifactUploaded (handleCdEventResp .IsArtifactUploaded )
219+ }
220+ } else {
221+ stageError = helper .NewCdStageError (fmt .Errorf (workFlow .CdStageFailed .String (), ciCdRequest .CommonWorkflowRequest .GetCdStageType (), err )).
222+ WithArtifactUploaded (handleCdEventResp .IsArtifactUploaded )
223+ }
224+ // send cd failure event, for ci failure notification
225+ event := adaptor .NewCdCompleteEvent (ciCdRequest .CommonWorkflowRequest , true ).
226+ WithIsArtifactUploaded (handleCdEventResp .IsArtifactUploaded )
227+ e := helper .SendCDEvent (ciCdRequest .CommonWorkflowRequest , event )
228+ if e != nil {
229+ log .Println (e )
230+ }
231+ // populate stage error
232+ util .PopulateStageError (stageError .ErrorMessage ())
233+ } else if err == nil && ! ciCdRequest .CommonWorkflowRequest .IsVirtualExecution {
234+ // IsVirtualExecution run flag indicates that cd stage is running in virtual mode.
235+ // specifically for isolated environment type, for IsVirtualExecution we don't send success event.
236+ // but failure event is sent in case of error.
237+ // send cd success event
238+ event := adaptor .NewCdCompleteEvent (ciCdRequest .CommonWorkflowRequest , false ).
239+ WithPluginArtifacts (handleCdEventResp .PluginArtifacts ).
240+ WithIsArtifactUploaded (handleCdEventResp .IsArtifactUploaded )
241+ err := helper .SendCDEvent (ciCdRequest .CommonWorkflowRequest , event )
242+ if err != nil {
243+ log .Println (err )
244+ }
253245 }
254- log .Println (util .DEVTRON , " /event" )
246+ log .Println (util .DEVTRON , "cd stage completion event sent" )
247+ return exitCode
255248}
0 commit comments