@@ -44,6 +44,7 @@ type NotificationSettingsRepository interface {
44
44
FindNotificationSettingBuildOptions (settingRequest * SearchRequest ) ([]* SettingOptionDTO , error )
45
45
FetchNotificationSettingGroupBy (viewId int ) ([]NotificationSettings , error )
46
46
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 )
47
48
}
48
49
49
50
type NotificationSettingsRepositoryImpl struct {
@@ -355,3 +356,229 @@ func (impl *NotificationSettingsRepositoryImpl) FindNotificationSettingsByConfig
355
356
}
356
357
return notificationSettings , nil
357
358
}
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