@@ -265,13 +265,25 @@ func (impl *EventRESTClientImpl) sendEventsOnNats(body []byte) error {
265
265
// do not call this method if notification module is not installed
266
266
func (impl * EventRESTClientImpl ) sendEvent (event Event ) (bool , error ) {
267
267
impl .logger .Debugw ("event before send" , "event" , event )
268
+
269
+ // Step 1: Create payload and destination URL based on config
270
+ body , destinationUrl , err := impl .createPayloadAndDestination (event )
271
+ if err != nil {
272
+ return false , err
273
+ }
274
+
275
+ // Step 2: Send via appropriate medium (NATS or REST)
276
+ return impl .deliverEvent (body , destinationUrl )
277
+ }
278
+
279
+ func (impl * EventRESTClientImpl ) createPayloadAndDestination (event Event ) (string , string , error ) {
268
280
var body string
269
281
var destinationUrl string
282
+
270
283
if impl .config .EnableNotifierV2 {
271
- impl .logger .Infow ("sending event to notifier v2" )
272
- // destination Url for v2
284
+ // V2 payload and URL
273
285
destinationUrl = impl .config .DestinationURL + "/v2"
274
- // Get NotificationSettings from event
286
+
275
287
req := repository.GetRulesRequest {
276
288
TeamId : event .TeamId ,
277
289
EnvId : event .EnvId ,
@@ -282,26 +294,24 @@ func (impl *EventRESTClientImpl) sendEvent(event Event) (bool, error) {
282
294
ClusterId : event .ClusterId ,
283
295
EnvIdsForCiPipeline : event .EnvIdsForCiPipeline ,
284
296
}
285
- // Get NotificationSettings from event
297
+
286
298
notificationSettings , err := impl .notificationSettingsRepository .FindNotificationSettingsWithRules (
287
299
context .Background (),
288
300
event .EventTypeId ,
289
301
req ,
290
302
)
291
303
if err != nil {
292
304
impl .logger .Errorw ("error while fetching notification settings" , "err" , err )
293
- return false , err
305
+ return "" , "" , err
294
306
}
295
307
296
- // convert notificationSetting to notificationSettingsBean
297
308
notificationSettingsBean := make ([]* repository.NotificationSettingsBean , 0 )
298
309
for _ , item := range notificationSettings {
299
310
config := make ([]repository.ConfigEntry , 0 )
300
311
if item .Config != "" {
301
- err := json .Unmarshal ([]byte (item .Config ), & config )
302
- if err != nil {
312
+ if err := json .Unmarshal ([]byte (item .Config ), & config ); err != nil {
303
313
impl .logger .Errorw ("error while unmarshaling config" , "err" , err )
304
- return false , err
314
+ return "" , "" , err
305
315
}
306
316
}
307
317
notificationSettingsBean = append (notificationSettingsBean , & repository.NotificationSettingsBean {
@@ -317,51 +327,58 @@ func (impl *EventRESTClientImpl) sendEvent(event Event) (bool, error) {
317
327
})
318
328
}
319
329
320
- // Create a combined payload with event and notification settings
321
330
combinedPayload := map [string ]interface {}{
322
331
"event" : event ,
323
332
"notificationSettings" : notificationSettingsBean ,
324
333
}
325
334
326
- // Marshal the combined payload
327
335
bodyBytes , err := json .Marshal (combinedPayload )
328
336
if err != nil {
329
337
impl .logger .Errorw ("error while marshaling combined event request" , "err" , err )
330
- return false , err
338
+ return "" , "" , err
331
339
}
332
340
body = string (bodyBytes )
333
-
334
341
} else {
342
+ // Default payload and URL
335
343
destinationUrl = impl .config .DestinationURL
336
344
bodyBytes , err := json .Marshal (event )
337
345
if err != nil {
338
346
impl .logger .Errorw ("error while marshaling event request" , "err" , err )
339
- return false , err
347
+ return "" , "" , err
340
348
}
341
349
body = string (bodyBytes )
342
- if impl .config .NotificationMedium == PUB_SUB {
343
- err = impl .sendEventsOnNats ([]byte (body ))
344
- if err != nil {
345
- impl .logger .Errorw ("error while publishing event" , "err" , err )
346
- return false , err
347
- }
348
- return true , nil
350
+ }
351
+
352
+ return body , destinationUrl , nil
353
+ }
354
+
355
+ func (impl * EventRESTClientImpl ) deliverEvent (body string , destinationUrl string ) (bool , error ) {
356
+ // Check if it should use NATS
357
+ if impl .config .NotificationMedium == PUB_SUB {
358
+ err := impl .sendEventsOnNats ([]byte (body ))
359
+ if err != nil {
360
+ impl .logger .Errorw ("error while publishing event" , "err" , err )
361
+ return false , err
349
362
}
363
+ return true , nil
350
364
}
351
365
352
- var reqBody = []byte (body )
366
+ // Default to REST
367
+ reqBody := []byte (body )
353
368
req , err := http .NewRequest (http .MethodPost , destinationUrl , bytes .NewBuffer (reqBody ))
354
369
if err != nil {
355
370
impl .logger .Errorw ("error while writing event" , "err" , err )
356
371
return false , err
357
372
}
358
373
req .Header .Set ("Content-Type" , "application/json" )
374
+
359
375
resp , err := impl .client .Do (req )
360
376
if err != nil {
361
- impl .logger .Errorw ("error while UpdateJiraTransition request " , "err" , err )
377
+ impl .logger .Errorw ("error while notifier request " , "err" , err )
362
378
return false , err
363
379
}
364
380
defer resp .Body .Close ()
381
+
365
382
impl .logger .Debugw ("event completed" , "event resp" , resp )
366
383
return true , err
367
384
}
0 commit comments