Skip to content

Commit 7737030

Browse files
committed
get NotificationSettings added
1 parent fc37484 commit 7737030

File tree

5 files changed

+301
-40
lines changed

5 files changed

+301
-40
lines changed

client/events/EventClient.go

Lines changed: 69 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,25 @@ type EventClient interface {
6161
}
6262

6363
type Event struct {
64-
EventTypeId int `json:"eventTypeId"`
65-
EventName string `json:"eventName"`
66-
PipelineId int `json:"pipelineId"`
67-
PipelineType string `json:"pipelineType"`
68-
CorrelationId string `json:"correlationId"`
69-
Payload *Payload `json:"payload"`
70-
EventTime string `json:"eventTime"`
71-
TeamId int `json:"teamId"`
72-
AppId int `json:"appId"`
73-
EnvId int `json:"envId"`
74-
IsProdEnv bool `json:"isProdEnv"`
75-
ClusterId int `json:"clusterId"`
76-
CdWorkflowType bean.WorkflowType `json:"cdWorkflowType,omitempty"`
77-
CdWorkflowRunnerId int `json:"cdWorkflowRunnerId"`
78-
CiWorkflowRunnerId int `json:"ciWorkflowRunnerId"`
79-
CiArtifactId int `json:"ciArtifactId"`
80-
BaseUrl string `json:"baseUrl"`
81-
UserId int `json:"-"`
64+
EventTypeId int `json:"eventTypeId"`
65+
EventName string `json:"eventName"`
66+
PipelineId int `json:"pipelineId"`
67+
PipelineType string `json:"pipelineType"`
68+
CorrelationId string `json:"correlationId"`
69+
Payload *Payload `json:"payload"`
70+
EventTime string `json:"eventTime"`
71+
TeamId int `json:"teamId"`
72+
AppId int `json:"appId"`
73+
EnvId int `json:"envId"`
74+
IsProdEnv bool `json:"isProdEnv"`
75+
ClusterId int `json:"clusterId"`
76+
CdWorkflowType bean.WorkflowType `json:"cdWorkflowType,omitempty"`
77+
CdWorkflowRunnerId int `json:"cdWorkflowRunnerId"`
78+
CiWorkflowRunnerId int `json:"ciWorkflowRunnerId"`
79+
CiArtifactId int `json:"ciArtifactId"`
80+
EnvIdsForCiPipeline []int `json:"envIdsForCiPipeline"`
81+
BaseUrl string `json:"baseUrl"`
82+
UserId int `json:"-"`
8283
}
8384

8485
type Payload struct {
@@ -120,22 +121,25 @@ type MaterialTriggerInfo struct {
120121
}
121122

122123
type EventRESTClientImpl struct {
123-
logger *zap.SugaredLogger
124-
client *http.Client
125-
config *EventClientConfig
126-
pubsubClient *pubsub.PubSubClientServiceImpl
127-
ciPipelineRepository pipelineConfig.CiPipelineRepository
128-
pipelineRepository pipelineConfig.PipelineRepository
129-
attributesRepository repository.AttributesRepository
130-
moduleService module.ModuleService
124+
logger *zap.SugaredLogger
125+
client *http.Client
126+
config *EventClientConfig
127+
pubsubClient *pubsub.PubSubClientServiceImpl
128+
ciPipelineRepository pipelineConfig.CiPipelineRepository
129+
pipelineRepository pipelineConfig.PipelineRepository
130+
attributesRepository repository.AttributesRepository
131+
moduleService module.ModuleService
132+
notificationSettingsRepository repository.NotificationSettingsRepository
131133
}
132134

133135
func NewEventRESTClientImpl(logger *zap.SugaredLogger, client *http.Client, config *EventClientConfig, pubsubClient *pubsub.PubSubClientServiceImpl,
134136
ciPipelineRepository pipelineConfig.CiPipelineRepository, pipelineRepository pipelineConfig.PipelineRepository,
135-
attributesRepository repository.AttributesRepository, moduleService module.ModuleService) *EventRESTClientImpl {
137+
attributesRepository repository.AttributesRepository, moduleService module.ModuleService,
138+
notificationSettingsRepository repository.NotificationSettingsRepository) *EventRESTClientImpl {
136139
return &EventRESTClientImpl{logger: logger, client: client, config: config, pubsubClient: pubsubClient,
137140
ciPipelineRepository: ciPipelineRepository, pipelineRepository: pipelineRepository,
138-
attributesRepository: attributesRepository, moduleService: moduleService}
141+
attributesRepository: attributesRepository, moduleService: moduleService,
142+
notificationSettingsRepository: notificationSettingsRepository}
139143
}
140144

141145
func (impl *EventRESTClientImpl) buildFinalPayload(event Event, cdPipeline *pipelineConfig.Pipeline, ciPipeline *pipelineConfig.CiPipeline) *Payload {
@@ -263,23 +267,52 @@ func (impl *EventRESTClientImpl) sendEvent(event Event) (bool, error) {
263267
var body string
264268
var destinationUrl string
265269
if impl.config.EnableNotifierV2 {
266-
// logic to get NotificationSettings from event
270+
impl.logger.Infow("sending event to notifier v2")
271+
// destination Url for v2
272+
destinationUrl = impl.config.DestinationURL + "/v2"
273+
// Get NotificationSettings from event
274+
notificationSettings, err := impl.notificationSettingsRepository.FindNotificationSettingsByEvent(
275+
event.PipelineType,
276+
event.PipelineId,
277+
event.EventTypeId,
278+
event.AppId,
279+
event.EnvId,
280+
event.TeamId,
281+
event.ClusterId,
282+
event.IsProdEnv,
283+
event.EnvIdsForCiPipeline,
284+
)
285+
if err != nil {
286+
impl.logger.Errorw("error while fetching notification settings", "err", err)
287+
return false, err
288+
}
289+
290+
// Create a combined payload with event and notification settings
291+
combinedPayload := map[string]interface{}{
292+
"event": event,
293+
"notificationSettings": notificationSettings,
294+
}
267295

268-
// embed event and NotificationSettings in body
296+
// Marshal the combined payload
297+
bodyBytes, err := json.Marshal(combinedPayload)
298+
if err != nil {
299+
impl.logger.Errorw("error while marshaling combined event request", "err", err)
300+
return false, err
301+
}
302+
body = string(bodyBytes)
269303

270-
// destination Url
271-
destinationUrl = impl.config.DestinationURL + "/v2"
272304
} else {
273305
destinationUrl = impl.config.DestinationURL
274-
body, err := json.Marshal(event)
306+
bodyBytes, err := json.Marshal(event)
275307
if err != nil {
276-
impl.logger.Errorw("error while marshaling event request ", "err", err)
308+
impl.logger.Errorw("error while marshaling event request", "err", err)
277309
return false, err
278310
}
311+
body = string(bodyBytes)
279312
if impl.config.NotificationMedium == PUB_SUB {
280-
err = impl.sendEventsOnNats(body)
313+
err = impl.sendEventsOnNats([]byte(body))
281314
if err != nil {
282-
impl.logger.Errorw("error while publishing event ", "err", err)
315+
impl.logger.Errorw("error while publishing event", "err", err)
283316
return false, err
284317
}
285318
return true, nil

env_gen.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

env_gen.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
## CI_RUNNER Related Environment Variables
2929
| Key | Type | Default Value | Description | Example | Deprecated |
3030
|-------|----------|-------------------|-------------------|-----------------------|------------------|
31-
| AZURE_ACCOUNT_KEY | string | | If blob storage is bieng used of azure then pass the secret key to access the bucket | | false |
31+
| AZURE_ACCOUNT_KEY | string | | If blob storage is being used of azure then pass the secret key to access the bucket | | false |
3232
| AZURE_ACCOUNT_NAME | string | | Account name for azure blob storage | | false |
3333
| AZURE_BLOB_CONTAINER_CI_CACHE | string | | Cache bucket name for azure blob storage | | false |
3434
| AZURE_BLOB_CONTAINER_CI_LOG | string | | Log bucket for azure blob storage | | false |
@@ -174,6 +174,7 @@
174174
| ECR_REPO_NAME_PREFIX | string |test/ | Prefix for ECR repo to be created in does not exist | | false |
175175
| ENABLE_ASYNC_ARGO_CD_INSTALL_DEVTRON_CHART | bool |false | To enable async installation of gitops application | | false |
176176
| ENABLE_ASYNC_INSTALL_DEVTRON_CHART | bool |false | To enable async installation of no-gitops application | | false |
177+
| ENABLE_NOTIFIER_V2 | bool |false | enable notifier v2 | | false |
177178
| EPHEMERAL_SERVER_VERSION_REGEX | string |v[1-9]\.\b(2[3-9]\|[3-9][0-9])\b.* | ephemeral containers support version regex that is compared with k8sServerVersion | | false |
178179
| EVENT_URL | string |http://localhost:3000/notify | Notifier service url | | false |
179180
| EXECUTE_WIRE_NIL_CHECKER | bool |false | checks for any nil pointer in wire.go | | false |

internal/sql/repository/NotificationSettingsRepository.go

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type NotificationSettingsRepository interface {
4444
FindNotificationSettingBuildOptions(settingRequest *SearchRequest) ([]*SettingOptionDTO, error)
4545
FetchNotificationSettingGroupBy(viewId int) ([]NotificationSettings, error)
4646
FindNotificationSettingsByConfigIdAndConfigType(configId int, configType string) ([]*NotificationSettings, error)
47+
FindNotificationSettingsByEvent(pipelineType string, pipelineId int, eventTypeId int, appId int, envId int, teamId int, clusterId int, isProdEnv bool, envIdsForCiPipeline []int) ([]NotificationSettings, error)
4748
}
4849

4950
type NotificationSettingsRepositoryImpl struct {
@@ -355,3 +356,229 @@ func (impl *NotificationSettingsRepositoryImpl) FindNotificationSettingsByConfig
355356
}
356357
return notificationSettings, nil
357358
}
359+
360+
// FindByEventSource finds notification settings based on event source parameters
361+
// note: if the query in this func is changed, please update the same query in notifier
362+
func (impl *NotificationSettingsRepositoryImpl) FindNotificationSettingsByEvent(pipelineType string, pipelineId int, eventTypeId int, appId int, envId int, teamId int, clusterId int, isProdEnv bool, envIdsForCiPipeline []int) ([]NotificationSettings, error) {
363+
// Handle special case for event type 6 (deployment blocked with auto trigger)
364+
if eventTypeId == 6 {
365+
// This is the case when deployment is blocked and pipeline is set to auto trigger
366+
eventTypeId = 3
367+
}
368+
369+
// Determine environment identifier based on isProdEnv flag
370+
envIdentifier := resourceQualifiers.AllExistingAndFutureNonProdEnvsInt
371+
if isProdEnv {
372+
envIdentifier = resourceQualifiers.AllExistingAndFutureProdEnvsInt
373+
}
374+
375+
// Build the query with all the complex conditions
376+
var notificationSettings []NotificationSettings
377+
query := impl.dbConnection.Model(&notificationSettings).
378+
Where("pipeline_type = ?", pipelineType).
379+
Where("event_type_id = ?", eventTypeId)
380+
381+
// Add all the OR conditions as WhereGroup
382+
query = query.WhereGroup(func(q *orm.Query) (*orm.Query, error) {
383+
// App specific, env/team/pipeline null
384+
q = q.WhereOrGroup(func(q *orm.Query) (*orm.Query, error) {
385+
q = q.Where("app_id = ?", appId).
386+
Where("env_id IS NULL").
387+
Where("team_id IS NULL").
388+
Where("pipeline_id IS NULL")
389+
return q, nil
390+
})
391+
392+
// Env specific, app/team/pipeline null
393+
q = q.WhereOrGroup(func(q *orm.Query) (*orm.Query, error) {
394+
q = q.Where("app_id IS NULL").
395+
Where("env_id = ?", envId).
396+
Where("team_id IS NULL").
397+
Where("pipeline_id IS NULL")
398+
return q, nil
399+
})
400+
401+
// Team specific, app/env/pipeline null
402+
q = q.WhereOrGroup(func(q *orm.Query) (*orm.Query, error) {
403+
q = q.Where("app_id IS NULL").
404+
Where("env_id IS NULL").
405+
Where("team_id = ?", teamId).
406+
Where("pipeline_id IS NULL")
407+
return q, nil
408+
})
409+
410+
// App and env specific, team/pipeline null
411+
q = q.WhereOrGroup(func(q *orm.Query) (*orm.Query, error) {
412+
q = q.Where("app_id = ?", appId).
413+
Where("team_id IS NULL").
414+
Where("env_id = ?", envId).
415+
Where("pipeline_id IS NULL")
416+
return q, nil
417+
})
418+
419+
// App and envIdentifier specific, team/pipeline null
420+
q = q.WhereOrGroup(func(q *orm.Query) (*orm.Query, error) {
421+
q = q.Where("app_id = ?", appId).
422+
Where("team_id IS NULL").
423+
Where("env_id = ?", envIdentifier).
424+
Where("pipeline_id IS NULL")
425+
return q, nil
426+
})
427+
428+
// App, env, team specific with pipeline
429+
q = q.WhereOrGroup(func(q *orm.Query) (*orm.Query, error) {
430+
q = q.Where("app_id = ?", appId).
431+
Where("env_id = ?", envId).
432+
Where("team_id = ?", teamId).
433+
WhereOr("pipeline_id = ?", pipelineId)
434+
return q, nil
435+
})
436+
437+
// All envs of cluster, env/app/team null
438+
q = q.WhereOrGroup(func(q *orm.Query) (*orm.Query, error) {
439+
q = q.Where("app_id IS NULL").
440+
Where("team_id IS NULL").
441+
Where("env_id IS NULL").
442+
Where("cluster_id = ?", clusterId)
443+
return q, nil
444+
})
445+
446+
// All envs of cluster in an app
447+
q = q.WhereOrGroup(func(q *orm.Query) (*orm.Query, error) {
448+
q = q.Where("app_id = ?", appId).
449+
Where("env_id IS NULL").
450+
Where("cluster_id = ?", clusterId)
451+
return q, nil
452+
})
453+
454+
// All envs of cluster in a team, app is null
455+
q = q.WhereOrGroup(func(q *orm.Query) (*orm.Query, error) {
456+
q = q.Where("app_id IS NULL").
457+
Where("team_id = ?", teamId).
458+
Where("env_id IS NULL").
459+
Where("cluster_id = ?", clusterId)
460+
return q, nil
461+
})
462+
463+
// For all prod/non-prod envs across for pipelines of a project, app/cluster/pipeline null
464+
q = q.WhereOrGroup(func(q *orm.Query) (*orm.Query, error) {
465+
q = q.Where("app_id IS NULL").
466+
Where("env_id = ?", envIdentifier).
467+
Where("team_id = ?", teamId).
468+
Where("pipeline_id IS NULL").
469+
Where("cluster_id IS NULL")
470+
return q, nil
471+
})
472+
473+
// For all prod/non-prod envs across for pipelines of an app, project/cluster/pipeline null
474+
q = q.WhereOrGroup(func(q *orm.Query) (*orm.Query, error) {
475+
q = q.Where("app_id = ?", appId).
476+
Where("team_id IS NULL").
477+
Where("env_id = ?", envIdentifier).
478+
Where("pipeline_id IS NULL").
479+
Where("cluster_id IS NULL")
480+
return q, nil
481+
})
482+
483+
// For all prod/non-prod envs across all clusters, cluster/app/team null
484+
q = q.WhereOrGroup(func(q *orm.Query) (*orm.Query, error) {
485+
q = q.Where("app_id IS NULL").
486+
Where("env_id = ?", envIdentifier).
487+
Where("team_id IS NULL").
488+
Where("pipeline_id IS NULL")
489+
return q, nil
490+
})
491+
492+
// All prod/non-prod envs of a cluster, app/team null
493+
q = q.WhereOrGroup(func(q *orm.Query) (*orm.Query, error) {
494+
q = q.Where("app_id IS NULL").
495+
Where("team_id IS NULL").
496+
Where("env_id = ?", envIdentifier).
497+
Where("cluster_id = ?", clusterId)
498+
return q, nil
499+
})
500+
501+
// All prod/non-prod envs of a cluster in an app
502+
q = q.WhereOrGroup(func(q *orm.Query) (*orm.Query, error) {
503+
q = q.Where("app_id = ?", appId).
504+
Where("env_id = ?", envIdentifier).
505+
Where("cluster_id = ?", clusterId)
506+
return q, nil
507+
})
508+
509+
// All prod/non-prod envs of a cluster in a team, app null
510+
q = q.WhereOrGroup(func(q *orm.Query) (*orm.Query, error) {
511+
q = q.Where("app_id IS NULL").
512+
Where("env_id = ?", envIdentifier).
513+
Where("team_id = ?", teamId).
514+
Where("cluster_id = ?", clusterId)
515+
return q, nil
516+
})
517+
518+
// Handle envIdsForCiPipeline if provided
519+
if len(envIdsForCiPipeline) > 0 {
520+
// For team with specific envs
521+
q = q.WhereOrGroup(func(q *orm.Query) (*orm.Query, error) {
522+
q = q.Where("app_id IS NULL").
523+
Where("env_id IN (?)", pg.In(envIdsForCiPipeline)).
524+
Where("team_id = ?", teamId).
525+
Where("pipeline_id IS NULL")
526+
return q, nil
527+
})
528+
529+
// For app with specific envs
530+
q = q.WhereOrGroup(func(q *orm.Query) (*orm.Query, error) {
531+
q = q.Where("app_id = ?", appId).
532+
Where("team_id IS NULL").
533+
Where("env_id IN (?)", pg.In(envIdsForCiPipeline)).
534+
Where("pipeline_id IS NULL")
535+
return q, nil
536+
})
537+
538+
// For app, team, pipeline with specific envs
539+
q = q.WhereOrGroup(func(q *orm.Query) (*orm.Query, error) {
540+
q = q.Where("app_id = ?", appId).
541+
Where("env_id IN (?)", pg.In(envIdsForCiPipeline)).
542+
Where("team_id = ?", teamId).
543+
Where("pipeline_id = ?", pipelineId)
544+
return q, nil
545+
})
546+
547+
// For cluster with specific envs
548+
q = q.WhereOrGroup(func(q *orm.Query) (*orm.Query, error) {
549+
q = q.Where("app_id IS NULL").
550+
Where("team_id IS NULL").
551+
Where("env_id IN (?)", pg.In(envIdsForCiPipeline)).
552+
Where("cluster_id = ?", clusterId)
553+
return q, nil
554+
})
555+
556+
// For app, cluster with specific envs
557+
q = q.WhereOrGroup(func(q *orm.Query) (*orm.Query, error) {
558+
q = q.Where("app_id = ?", appId).
559+
Where("env_id IN (?)", pg.In(envIdsForCiPipeline)).
560+
Where("cluster_id = ?", clusterId)
561+
return q, nil
562+
})
563+
564+
// For team, cluster with specific envs
565+
q = q.WhereOrGroup(func(q *orm.Query) (*orm.Query, error) {
566+
q = q.Where("app_id IS NULL").
567+
Where("env_id IN (?)", pg.In(envIdsForCiPipeline)).
568+
Where("team_id = ?", teamId).
569+
Where("cluster_id = ?", clusterId)
570+
return q, nil
571+
})
572+
}
573+
574+
return q, nil
575+
})
576+
577+
// Execute the query
578+
err := query.Select()
579+
if err != nil {
580+
return nil, err
581+
}
582+
583+
return notificationSettings, nil
584+
}

0 commit comments

Comments
 (0)