Skip to content

Commit 960470e

Browse files
committed
more refact
1 parent a186761 commit 960470e

File tree

1 file changed

+83
-72
lines changed

1 file changed

+83
-72
lines changed

client/events/EventClient.go

Lines changed: 83 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -277,108 +277,119 @@ func (impl *EventRESTClientImpl) sendEvent(event Event) (bool, error) {
277277
}
278278

279279
func (impl *EventRESTClientImpl) createPayloadAndDestination(event Event) ([]byte, string, error) {
280-
var bodyBytes []byte
281-
var destinationUrl string
282-
var err error
283-
284280
if impl.config.EnableNotifierV2 {
285-
// V2 payload and URL
286-
destinationUrl = impl.config.DestinationURL + "/v2"
287-
288-
req := repository.GetRulesRequest{
289-
TeamId: event.TeamId,
290-
EnvId: event.EnvId,
291-
AppId: event.AppId,
292-
PipelineId: event.PipelineId,
293-
PipelineType: event.PipelineType,
294-
IsProdEnv: &event.IsProdEnv,
295-
ClusterId: event.ClusterId,
296-
EnvIdsForCiPipeline: event.EnvIdsForCiPipeline,
297-
}
281+
return impl.createV2PayloadAndDestination(event)
282+
}
283+
return impl.createDefaultPayloadAndDestination(event)
284+
}
298285

299-
notificationSettings, err := impl.notificationSettingsRepository.FindNotificationSettingsWithRules(
300-
context.Background(),
301-
event.EventTypeId,
302-
req,
303-
)
304-
if err != nil {
305-
impl.logger.Errorw("error while fetching notification settings", "err", err)
306-
return nil, "", err
307-
}
286+
func (impl *EventRESTClientImpl) createV2PayloadAndDestination(event Event) ([]byte, string, error) {
287+
destinationUrl := impl.config.DestinationURL + "/v2"
288+
289+
// Fetch notification settings
290+
req := repository.GetRulesRequest{
291+
TeamId: event.TeamId,
292+
EnvId: event.EnvId,
293+
AppId: event.AppId,
294+
PipelineId: event.PipelineId,
295+
PipelineType: event.PipelineType,
296+
IsProdEnv: &event.IsProdEnv,
297+
ClusterId: event.ClusterId,
298+
EnvIdsForCiPipeline: event.EnvIdsForCiPipeline,
299+
}
300+
notificationSettings, err := impl.notificationSettingsRepository.FindNotificationSettingsWithRules(
301+
context.Background(), event.EventTypeId, req,
302+
)
303+
if err != nil {
304+
impl.logger.Errorw("error while fetching notification settings", "err", err)
305+
return nil, "", err
306+
}
308307

309-
notificationSettingsBean := make([]*repository.NotificationSettingsBean, 0)
310-
for _, item := range notificationSettings {
311-
config := make([]repository.ConfigEntry, 0)
312-
if item.Config != "" {
313-
if err := json.Unmarshal([]byte(item.Config), &config); err != nil {
314-
impl.logger.Errorw("error while unmarshaling config", "err", err)
315-
return nil, "", err
316-
}
317-
}
318-
notificationSettingsBean = append(notificationSettingsBean, &repository.NotificationSettingsBean{
319-
Id: item.Id,
320-
TeamId: item.TeamId,
321-
AppId: item.AppId,
322-
EnvId: item.EnvId,
323-
PipelineId: item.PipelineId,
324-
PipelineType: item.PipelineType,
325-
EventTypeId: item.EventTypeId,
326-
Config: config,
327-
ViewId: item.ViewId,
328-
})
329-
}
308+
// Process notification settings into beans
309+
notificationSettingsBean, err := impl.processNotificationSettings(notificationSettings)
310+
if err != nil {
311+
return nil, "", err
312+
}
330313

331-
combinedPayload := map[string]interface{}{
332-
"event": event,
333-
"notificationSettings": notificationSettingsBean,
334-
}
314+
// Create combined payload
315+
combinedPayload := map[string]interface{}{
316+
"event": event,
317+
"notificationSettings": notificationSettingsBean,
318+
}
335319

336-
bodyBytes, err = json.Marshal(combinedPayload)
337-
if err != nil {
338-
impl.logger.Errorw("error while marshaling combined event request", "err", err)
339-
return nil, "", err
340-
}
341-
} else {
342-
// Default payload and URL
343-
destinationUrl = impl.config.DestinationURL
344-
bodyBytes, err = json.Marshal(event)
345-
if err != nil {
346-
impl.logger.Errorw("error while marshaling event request", "err", err)
347-
return nil, "", err
348-
}
320+
bodyBytes, err := json.Marshal(combinedPayload)
321+
if err != nil {
322+
impl.logger.Errorw("error while marshaling combined event request", "err", err)
323+
return nil, "", err
349324
}
350325

351326
return bodyBytes, destinationUrl, nil
352327
}
353328

329+
func (impl *EventRESTClientImpl) createDefaultPayloadAndDestination(event Event) ([]byte, string, error) {
330+
bodyBytes, err := json.Marshal(event)
331+
if err != nil {
332+
impl.logger.Errorw("error while marshaling event request", "err", err)
333+
return nil, "", err
334+
}
335+
return bodyBytes, impl.config.DestinationURL, nil
336+
}
337+
338+
func (impl *EventRESTClientImpl) processNotificationSettings(notificationSettings []repository.NotificationSettings) ([]*repository.NotificationSettingsBean, error) {
339+
notificationSettingsBean := make([]*repository.NotificationSettingsBean, 0)
340+
for _, item := range notificationSettings {
341+
config := make([]repository.ConfigEntry, 0)
342+
if item.Config != "" {
343+
if err := json.Unmarshal([]byte(item.Config), &config); err != nil {
344+
impl.logger.Errorw("error while unmarshaling config", "err", err)
345+
return nil, err
346+
}
347+
}
348+
notificationSettingsBean = append(notificationSettingsBean, &repository.NotificationSettingsBean{
349+
Id: item.Id,
350+
TeamId: item.TeamId,
351+
AppId: item.AppId,
352+
EnvId: item.EnvId,
353+
PipelineId: item.PipelineId,
354+
PipelineType: item.PipelineType,
355+
EventTypeId: item.EventTypeId,
356+
Config: config,
357+
ViewId: item.ViewId,
358+
})
359+
}
360+
return notificationSettingsBean, nil
361+
}
362+
354363
func (impl *EventRESTClientImpl) deliverEvent(bodyBytes []byte, destinationUrl string) (bool, error) {
355-
// Check if it should use NATS
356364
if impl.config.NotificationMedium == PUB_SUB {
357-
err := impl.sendEventsOnNats(bodyBytes)
358-
if err != nil {
365+
if err := impl.sendEventsOnNats(bodyBytes); err != nil {
359366
impl.logger.Errorw("error while publishing event", "err", err)
360367
return false, err
361368
}
362369
return true, nil
363370
}
364371

365-
// Default to REST
366372
req, err := http.NewRequest(http.MethodPost, destinationUrl, bytes.NewBuffer(bodyBytes))
367373
if err != nil {
368-
impl.logger.Errorw("error while writing event", "err", err)
374+
impl.logger.Errorw("error while creating HTTP request", "err", err)
369375
return false, err
370376
}
371377
req.Header.Set("Content-Type", "application/json")
372378

373379
resp, err := impl.client.Do(req)
374380
if err != nil {
375-
impl.logger.Errorw("error while notifier request ", "err", err)
381+
impl.logger.Errorw("error while sending HTTP request", "err", err)
376382
return false, err
377383
}
378384
defer resp.Body.Close()
379385

380-
impl.logger.Debugw("event completed", "event resp", resp)
381-
return true, err
386+
if resp.StatusCode >= 300 {
387+
impl.logger.Errorw("unexpected response from notifier", "status", resp.StatusCode)
388+
return false, fmt.Errorf("unexpected response code: %d", resp.StatusCode)
389+
}
390+
391+
impl.logger.Debugw("event successfully delivered", "status", resp.StatusCode)
392+
return true, nil
382393
}
383394

384395
func (impl *EventRESTClientImpl) WriteNatsEvent(topic string, payload interface{}) error {

0 commit comments

Comments
 (0)