@@ -20,6 +20,7 @@ package pubsub
2020import (
2121 "encoding/json"
2222 "fmt"
23+ "github.com/aws/aws-sdk-go-v2/service/ecr/types"
2324 "github.com/caarlos0/env/v6"
2425 pubsub "github.com/devtron-labs/common-lib/pubsub-lib"
2526 "github.com/devtron-labs/devtron/internal/sql/repository"
@@ -28,6 +29,7 @@ import (
2829 bean2 "github.com/devtron-labs/devtron/pkg/pipeline/bean"
2930 "github.com/devtron-labs/devtron/util"
3031 "go.uber.org/zap"
32+ "time"
3133)
3234
3335type CiEventConfig struct {
@@ -53,6 +55,11 @@ type CiEventHandlerImpl struct {
5355 ciEventConfig * CiEventConfig
5456}
5557
58+ type ImageDetailsFromCR struct {
59+ ImageDetails []types.ImageDetail `json:"imageDetails"`
60+ Region string `json:"region"`
61+ }
62+
5663type CiCompleteEvent struct {
5764 CiProjectDetails []bean2.CiProjectDetails `json:"ciProjectDetails"`
5865 DockerImage string `json:"dockerImage" validate:"required,image-validator"`
@@ -67,6 +74,7 @@ type CiCompleteEvent struct {
6774 AppName string `json:"appName"`
6875 IsArtifactUploaded bool `json:"isArtifactUploaded"`
6976 FailureReason string `json:"failureReason"`
77+ ImageDetailsFromCR * ImageDetailsFromCR `json:"imageDetailsFromCR"`
7078}
7179
7280func NewCiEventHandlerImpl (logger * zap.SugaredLogger , pubsubClient * pubsub.PubSubClientServiceImpl , webhookService pipeline.WebhookService , ciEventConfig * CiEventConfig ) * CiEventHandlerImpl {
@@ -108,10 +116,26 @@ func (impl *CiEventHandlerImpl) Subscribe() error {
108116 ciCompleteEvent .PipelineId , "request: " , req , "error: " , err )
109117 return
110118 }
119+ } else if ciCompleteEvent .ImageDetailsFromCR != nil {
120+ if len (ciCompleteEvent .ImageDetailsFromCR .ImageDetails ) > 0 {
121+ detail := util .GetLatestImageAccToImagePushedAt (ciCompleteEvent .ImageDetailsFromCR .ImageDetails )
122+ request , err := impl .BuildCIArtifactRequestForImageFromCR (detail , ciCompleteEvent .ImageDetailsFromCR .Region , ciCompleteEvent )
123+ if err != nil {
124+ impl .logger .Error ("Error while creating request for pipelineID" , "pipelineId" , ciCompleteEvent .PipelineId , "err" , err )
125+ return
126+ }
127+ resp , err := impl .webhookService .HandleCiSuccessEvent (ciCompleteEvent .PipelineId , request , detail .ImagePushedAt )
128+ if err != nil {
129+ impl .logger .Error ("Error while sending event for CI success for pipelineID" , "pipelineId" ,
130+ ciCompleteEvent .PipelineId , "request" , request , "err" , err )
131+ return
132+ }
133+ impl .logger .Debug (resp )
134+ }
135+
111136 } else {
112137 util .TriggerCIMetrics (ciCompleteEvent .Metrics , impl .ciEventConfig .ExposeCiMetrics , ciCompleteEvent .PipelineName , ciCompleteEvent .AppName )
113-
114- resp , err := impl .webhookService .HandleCiSuccessEvent (ciCompleteEvent .PipelineId , req )
138+ resp , err := impl .webhookService .HandleCiSuccessEvent (ciCompleteEvent .PipelineId , req , & time.Time {})
115139 if err != nil {
116140 impl .logger .Error ("Error while sending event for CI success for pipelineID: " ,
117141 ciCompleteEvent .PipelineId , "request: " , req , "error: " , err )
@@ -195,6 +219,22 @@ func (impl *CiEventHandlerImpl) BuildCiArtifactRequest(event CiCompleteEvent) (*
195219 return request , nil
196220}
197221
222+ func (impl * CiEventHandlerImpl ) BuildCIArtifactRequestForImageFromCR (imageDetails types.ImageDetail , region string , event CiCompleteEvent ) (* pipeline.CiArtifactWebhookRequest , error ) {
223+ if event .TriggeredBy == 0 {
224+ event .TriggeredBy = 1 // system triggered event
225+ }
226+ request := & pipeline.CiArtifactWebhookRequest {
227+ Image : util .ExtractEcrImage (* imageDetails .RegistryId , region , * imageDetails .RepositoryName , imageDetails .ImageTags [0 ]),
228+ ImageDigest : * imageDetails .ImageDigest ,
229+ DataSource : event .DataSource ,
230+ PipelineName : event .PipelineName ,
231+ UserId : event .TriggeredBy ,
232+ WorkflowId : event .WorkflowId ,
233+ IsArtifactUploaded : event .IsArtifactUploaded ,
234+ }
235+ return request , nil
236+ }
237+
198238func (impl * CiEventHandlerImpl ) BuildCiArtifactRequestForWebhook (event CiCompleteEvent ) (* pipeline.CiArtifactWebhookRequest , error ) {
199239 ciMaterialInfos := make ([]repository.CiMaterialInfo , 0 )
200240 if event .MaterialType == "" {
0 commit comments