Skip to content

Commit 2e10cea

Browse files
committed
refact
1 parent e608152 commit 2e10cea

File tree

2 files changed

+41
-24
lines changed

2 files changed

+41
-24
lines changed

client/events/EventClient.go

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,25 @@ func (impl *EventRESTClientImpl) sendEventsOnNats(body []byte) error {
265265
// do not call this method if notification module is not installed
266266
func (impl *EventRESTClientImpl) sendEvent(event Event) (bool, error) {
267267
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) {
268280
var body string
269281
var destinationUrl string
282+
270283
if impl.config.EnableNotifierV2 {
271-
impl.logger.Infow("sending event to notifier v2")
272-
// destination Url for v2
284+
// V2 payload and URL
273285
destinationUrl = impl.config.DestinationURL + "/v2"
274-
// Get NotificationSettings from event
286+
275287
req := repository.GetRulesRequest{
276288
TeamId: event.TeamId,
277289
EnvId: event.EnvId,
@@ -282,26 +294,24 @@ func (impl *EventRESTClientImpl) sendEvent(event Event) (bool, error) {
282294
ClusterId: event.ClusterId,
283295
EnvIdsForCiPipeline: event.EnvIdsForCiPipeline,
284296
}
285-
// Get NotificationSettings from event
297+
286298
notificationSettings, err := impl.notificationSettingsRepository.FindNotificationSettingsWithRules(
287299
context.Background(),
288300
event.EventTypeId,
289301
req,
290302
)
291303
if err != nil {
292304
impl.logger.Errorw("error while fetching notification settings", "err", err)
293-
return false, err
305+
return "", "", err
294306
}
295307

296-
// convert notificationSetting to notificationSettingsBean
297308
notificationSettingsBean := make([]*repository.NotificationSettingsBean, 0)
298309
for _, item := range notificationSettings {
299310
config := make([]repository.ConfigEntry, 0)
300311
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 {
303313
impl.logger.Errorw("error while unmarshaling config", "err", err)
304-
return false, err
314+
return "", "", err
305315
}
306316
}
307317
notificationSettingsBean = append(notificationSettingsBean, &repository.NotificationSettingsBean{
@@ -317,51 +327,58 @@ func (impl *EventRESTClientImpl) sendEvent(event Event) (bool, error) {
317327
})
318328
}
319329

320-
// Create a combined payload with event and notification settings
321330
combinedPayload := map[string]interface{}{
322331
"event": event,
323332
"notificationSettings": notificationSettingsBean,
324333
}
325334

326-
// Marshal the combined payload
327335
bodyBytes, err := json.Marshal(combinedPayload)
328336
if err != nil {
329337
impl.logger.Errorw("error while marshaling combined event request", "err", err)
330-
return false, err
338+
return "", "", err
331339
}
332340
body = string(bodyBytes)
333-
334341
} else {
342+
// Default payload and URL
335343
destinationUrl = impl.config.DestinationURL
336344
bodyBytes, err := json.Marshal(event)
337345
if err != nil {
338346
impl.logger.Errorw("error while marshaling event request", "err", err)
339-
return false, err
347+
return "", "", err
340348
}
341349
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
349362
}
363+
return true, nil
350364
}
351365

352-
var reqBody = []byte(body)
366+
// Default to REST
367+
reqBody := []byte(body)
353368
req, err := http.NewRequest(http.MethodPost, destinationUrl, bytes.NewBuffer(reqBody))
354369
if err != nil {
355370
impl.logger.Errorw("error while writing event", "err", err)
356371
return false, err
357372
}
358373
req.Header.Set("Content-Type", "application/json")
374+
359375
resp, err := impl.client.Do(req)
360376
if err != nil {
361-
impl.logger.Errorw("error while UpdateJiraTransition request ", "err", err)
377+
impl.logger.Errorw("error while notifier request ", "err", err)
362378
return false, err
363379
}
364380
defer resp.Body.Close()
381+
365382
impl.logger.Debugw("event completed", "event resp", resp)
366383
return true, err
367384
}

pkg/pipeline/CiService.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ func (impl *CiServiceImpl) WriteCITriggerEvent(trigger types.Trigger, pipeline *
499499
event = impl.eventFactory.BuildExtraCIData(event, material)
500500

501501
// fetching all the envs which are directly or indirectly linked with the ci pipeline
502-
envs, _ := impl.envRepository.FindEnvLinkedWithCiPipelines(false, []int{trigger.PipelineId})
502+
envs, _ := impl.envRepository.FindEnvLinkedWithCiPipelines(pipeline.IsExternal, []int{trigger.PipelineId})
503503

504504
event.EnvIdsForCiPipeline = []int{}
505505
for _, env := range envs {

0 commit comments

Comments
 (0)