Skip to content

Commit f6069dc

Browse files
chore: refactoring notification bean (#6085)
* added the adapter in notification pkg * refactored the notify bean * misc * updated teh beans and removed the duplicates * executed make after develop merge * added the scripts
1 parent 64f31bb commit f6069dc

15 files changed

+415
-327
lines changed

client/events/bean/bean.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package bean
2+
3+
import "github.com/devtron-labs/devtron/util/event"
4+
5+
type Provider struct {
6+
Destination util.Channel `json:"dest"`
7+
Rule string `json:"rule"`
8+
ConfigId int `json:"configId"`
9+
Recipient string `json:"recipient"`
10+
}

cmd/external-app/wire_gen.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/notifier/NotificationConfigBuilder.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package notifier
1919
import (
2020
"encoding/json"
2121
"errors"
22+
"github.com/devtron-labs/devtron/client/events/bean"
2223
"github.com/devtron-labs/devtron/internal/sql/repository"
2324
"github.com/devtron-labs/devtron/pkg/notifier/beans"
2425
"github.com/devtron-labs/devtron/util/event"
@@ -29,7 +30,7 @@ import (
2930
type NotificationConfigBuilder interface {
3031
BuildNotificationSettingsConfig(notificationSettingsRequest *beans.NotificationConfigRequest, existingNotificationSettingsConfig *repository.NotificationSettingsView, userId int32) (*repository.NotificationSettingsView, error)
3132
BuildNewNotificationSettings(notificationSettingsRequest *beans.NotificationConfigRequest, notificationSettingsView *repository.NotificationSettingsView) ([]repository.NotificationSettings, error)
32-
BuildNotificationSettingWithPipeline(teamId *int, envId *int, appId *int, pipelineId *int, clusterId *int, pipelineType util.PipelineType, eventTypeId int, viewId int, providers []*beans.Provider) (repository.NotificationSettings, error)
33+
BuildNotificationSettingWithPipeline(teamId *int, envId *int, appId *int, pipelineId *int, clusterId *int, pipelineType util.PipelineType, eventTypeId int, viewId int, providers []*bean.Provider) (repository.NotificationSettings, error)
3334
}
3435

3536
type NotificationConfigBuilderImpl struct {
@@ -112,7 +113,7 @@ func (impl NotificationConfigBuilderImpl) buildNotificationSetting(notificationS
112113
return notificationSetting, nil
113114
}
114115

115-
func (impl NotificationConfigBuilderImpl) BuildNotificationSettingWithPipeline(teamId *int, envId *int, appId *int, pipelineId *int, clusterId *int, pipelineType util.PipelineType, eventTypeId int, viewId int, providers []*beans.Provider) (repository.NotificationSettings, error) {
116+
func (impl NotificationConfigBuilderImpl) BuildNotificationSettingWithPipeline(teamId *int, envId *int, appId *int, pipelineId *int, clusterId *int, pipelineType util.PipelineType, eventTypeId int, viewId int, providers []*bean.Provider) (repository.NotificationSettings, error) {
116117

117118
if teamId == nil && appId == nil && envId == nil && pipelineId == nil && clusterId == nil {
118119
return repository.NotificationSettings{}, errors.New("no filter criteria is selected")

pkg/notifier/NotificationConfigService.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ type NotificationConfigServiceImpl struct {
6767
ciPipelineMaterialRepository pipelineConfig.CiPipelineMaterialRepository
6868
}
6969

70-
const allNonProdEnvsName = "All non-prod environments"
71-
const allProdEnvsName = "All prod environments"
72-
7370
func NewNotificationConfigServiceImpl(logger *zap.SugaredLogger, notificationSettingsRepository repository.NotificationSettingsRepository, notificationConfigBuilder NotificationConfigBuilder, ciPipelineRepository pipelineConfig.CiPipelineRepository,
7471
pipelineRepository pipelineConfig.PipelineRepository, slackRepository repository.SlackNotificationRepository, webhookRepository repository.WebhookNotificationRepository,
7572
sesRepository repository.SESNotificationRepository, smtpRepository repository.SMTPNotificationRepository,
@@ -149,15 +146,6 @@ func (impl *NotificationConfigServiceImpl) DeleteNotificationSettings(request be
149146
return nil
150147
}
151148

152-
type config struct {
153-
AppId int `json:"appId"`
154-
EnvId int `json:"envId"`
155-
Pipelines []int `json:"pipelineIds"`
156-
PipelineType util.PipelineType `json:"pipelineType" validate:"required"`
157-
EventTypeIds []int `json:"eventTypeIds" validate:"required"`
158-
Providers []beans.Provider `json:"providers" validate:"required"`
159-
}
160-
161149
func (impl *NotificationConfigServiceImpl) CreateOrUpdateNotificationSettings(notificationSettingsRequest *beans.NotificationRequest, userId int32) (int, error) {
162150
var configId int
163151
var err error
@@ -252,9 +240,9 @@ func (impl *NotificationConfigServiceImpl) BuildNotificationSettingsResponse(not
252240
envIds := make([]*int, 0)
253241
for _, envId := range config.EnvId {
254242
if *envId == resourceQualifiers.AllExistingAndFutureProdEnvsInt {
255-
envResponse = append(envResponse, &beans.EnvResponse{Id: envId, Name: allProdEnvsName})
243+
envResponse = append(envResponse, &beans.EnvResponse{Id: envId, Name: beans.AllProdEnvsName})
256244
} else if *envId == resourceQualifiers.AllExistingAndFutureNonProdEnvsInt {
257-
envResponse = append(envResponse, &beans.EnvResponse{Id: envId, Name: allNonProdEnvsName})
245+
envResponse = append(envResponse, &beans.EnvResponse{Id: envId, Name: beans.AllNonProdEnvsName})
258246
} else {
259247
envIds = append(envIds, envId)
260248
}
@@ -417,7 +405,7 @@ func (impl *NotificationConfigServiceImpl) BuildNotificationSettingsResponse(not
417405
return notificationSettingsResponses, deletedItemCount, nil
418406
}
419407

420-
func (impl *NotificationConfigServiceImpl) buildProvidersConfig(config config) ([]beans.ProvidersConfig, error) {
408+
func (impl *NotificationConfigServiceImpl) buildProvidersConfig(config beans.Config) ([]beans.ProvidersConfig, error) {
421409
var providerConfigs []beans.ProvidersConfig
422410
if len(config.Providers) > 0 {
423411
sesConfigNamesMap := map[int]string{}
@@ -506,7 +494,7 @@ func (impl *NotificationConfigServiceImpl) buildProvidersConfig(config config) (
506494
return providerConfigs, nil
507495
}
508496

509-
func (impl *NotificationConfigServiceImpl) buildPipelineResponses(config config, ciPipelines []*pipelineConfig.CiPipeline, cdPipelines []*pipelineConfig.Pipeline) (util.PipelineType, []beans.PipelineResponse, error) {
497+
func (impl *NotificationConfigServiceImpl) buildPipelineResponses(config beans.Config, ciPipelines []*pipelineConfig.CiPipeline, cdPipelines []*pipelineConfig.Pipeline) (util.PipelineType, []beans.PipelineResponse, error) {
510498
var pipelineType util.PipelineType
511499
var err error
512500

@@ -778,9 +766,9 @@ func (impl *NotificationConfigServiceImpl) FindNotificationSettingOptions(settin
778766
envIds := make([]*int, 0)
779767
for _, envId := range settingRequest.EnvId {
780768
if *envId == resourceQualifiers.AllExistingAndFutureProdEnvsInt {
781-
envResponse = append(envResponse, &beans.EnvResponse{Id: envId, Name: allProdEnvsName})
769+
envResponse = append(envResponse, &beans.EnvResponse{Id: envId, Name: beans.AllProdEnvsName})
782770
} else if *envId == resourceQualifiers.AllExistingAndFutureNonProdEnvsInt {
783-
envResponse = append(envResponse, &beans.EnvResponse{Id: envId, Name: allNonProdEnvsName})
771+
envResponse = append(envResponse, &beans.EnvResponse{Id: envId, Name: beans.AllNonProdEnvsName})
784772
} else {
785773
envIds = append(envIds, envId)
786774
}

pkg/notifier/SESNotificationService.go

Lines changed: 7 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ import (
2020
"fmt"
2121
"github.com/devtron-labs/devtron/internal/sql/repository"
2222
"github.com/devtron-labs/devtron/internal/util"
23+
"github.com/devtron-labs/devtron/pkg/notifier/adapter"
2324
"github.com/devtron-labs/devtron/pkg/notifier/beans"
24-
"github.com/devtron-labs/devtron/pkg/sql"
2525
"github.com/devtron-labs/devtron/pkg/team"
26+
eventUtil "github.com/devtron-labs/devtron/util/event"
2627
"github.com/go-pg/pg"
2728
"go.uber.org/zap"
2829
"time"
@@ -55,7 +56,7 @@ func NewSESNotificationServiceImpl(logger *zap.SugaredLogger, sesRepository repo
5556

5657
func (impl *SESNotificationServiceImpl) SaveOrEditNotificationConfig(channelReq []*beans.SESConfigDto, userId int32) ([]int, error) {
5758
var responseIds []int
58-
sesConfigs := buildSESNewConfigs(channelReq, userId)
59+
sesConfigs := adapter.BuildSESNewConfigs(channelReq, userId)
5960
for _, config := range sesConfigs {
6061
if config.Id != 0 {
6162

@@ -80,7 +81,7 @@ func (impl *SESNotificationServiceImpl) SaveOrEditNotificationConfig(channelReq
8081
impl.logger.Errorw("err while fetching ses config", "err", err)
8182
return []int{}, err
8283
}
83-
impl.buildConfigUpdateModel(config, model, userId)
84+
adapter.BuildConfigUpdateModelForSES(config, model, userId)
8485
model, uErr := impl.sesRepository.UpdateSESConfig(model)
8586
if uErr != nil {
8687
impl.logger.Errorw("err while updating ses config", "err", err)
@@ -121,7 +122,7 @@ func (impl *SESNotificationServiceImpl) FetchSESNotificationConfigById(id int) (
121122
impl.logger.Errorw("cannot find all slack config", "err", err)
122123
return nil, err
123124
}
124-
sesConfigDto := impl.adaptSESConfig(sesConfig)
125+
sesConfigDto := adapter.AdaptSESConfig(sesConfig)
125126
return sesConfigDto, nil
126127
}
127128

@@ -133,7 +134,7 @@ func (impl *SESNotificationServiceImpl) FetchAllSESNotificationConfig() ([]*bean
133134
return []*beans.SESConfigDto{}, err
134135
}
135136
for _, sesConfig := range sesConfigs {
136-
sesConfigDto := impl.adaptSESConfig(sesConfig)
137+
sesConfigDto := adapter.AdaptSESConfig(sesConfig)
137138
sesConfigDto.SecretKey = "**********"
138139
responseDto = append(responseDto, sesConfigDto)
139140
}
@@ -159,71 +160,13 @@ func (impl *SESNotificationServiceImpl) FetchAllSESNotificationConfigAutocomplet
159160
return responseDto, nil
160161
}
161162

162-
func (impl *SESNotificationServiceImpl) adaptSESConfig(sesConfig *repository.SESConfig) *beans.SESConfigDto {
163-
sesConfigDto := &beans.SESConfigDto{
164-
OwnerId: sesConfig.OwnerId,
165-
Region: sesConfig.Region,
166-
AccessKey: sesConfig.AccessKey,
167-
SecretKey: sesConfig.SecretKey,
168-
FromEmail: sesConfig.FromEmail,
169-
SessionToken: sesConfig.SessionToken,
170-
ConfigName: sesConfig.ConfigName,
171-
Description: sesConfig.Description,
172-
Id: sesConfig.Id,
173-
Default: sesConfig.Default,
174-
}
175-
return sesConfigDto
176-
}
177-
178-
func buildSESNewConfigs(sesReq []*beans.SESConfigDto, userId int32) []*repository.SESConfig {
179-
var sesConfigs []*repository.SESConfig
180-
for _, c := range sesReq {
181-
sesConfig := &repository.SESConfig{
182-
Id: c.Id,
183-
Region: c.Region,
184-
AccessKey: c.AccessKey,
185-
SecretKey: c.SecretKey,
186-
ConfigName: c.ConfigName,
187-
FromEmail: c.FromEmail,
188-
SessionToken: c.SessionToken,
189-
Description: c.Description,
190-
Default: c.Default,
191-
AuditLog: sql.AuditLog{
192-
CreatedBy: userId,
193-
CreatedOn: time.Now(),
194-
UpdatedOn: time.Now(),
195-
UpdatedBy: userId,
196-
},
197-
}
198-
199-
sesConfig.OwnerId = userId
200-
sesConfigs = append(sesConfigs, sesConfig)
201-
}
202-
return sesConfigs
203-
}
204-
205-
func (impl *SESNotificationServiceImpl) buildConfigUpdateModel(sesConfig *repository.SESConfig, model *repository.SESConfig, userId int32) {
206-
model.Id = sesConfig.Id
207-
model.OwnerId = sesConfig.OwnerId
208-
model.Region = sesConfig.Region
209-
model.AccessKey = sesConfig.AccessKey
210-
model.SecretKey = sesConfig.SecretKey
211-
model.FromEmail = sesConfig.FromEmail
212-
model.SessionToken = sesConfig.SessionToken
213-
model.ConfigName = sesConfig.ConfigName
214-
model.Description = sesConfig.Description
215-
model.Default = sesConfig.Default
216-
model.UpdatedOn = time.Now()
217-
model.UpdatedBy = userId
218-
}
219-
220163
func (impl *SESNotificationServiceImpl) DeleteNotificationConfig(deleteReq *beans.SESConfigDto, userId int32) error {
221164
existingConfig, err := impl.sesRepository.FindOne(deleteReq.Id)
222165
if err != nil {
223166
impl.logger.Errorw("No matching entry found for delete", "err", err, "id", deleteReq.Id)
224167
return err
225168
}
226-
notifications, err := impl.notificationSettingsRepository.FindNotificationSettingsByConfigIdAndConfigType(deleteReq.Id, beans.SES_CONFIG_TYPE)
169+
notifications, err := impl.notificationSettingsRepository.FindNotificationSettingsByConfigIdAndConfigType(deleteReq.Id, eventUtil.SES.String())
227170
if err != nil && err != pg.ErrNoRows {
228171
impl.logger.Errorw("error in deleting ses config", "config", deleteReq)
229172
return err

pkg/notifier/SMTPNotificationService.go

Lines changed: 7 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ import (
2020
"fmt"
2121
"github.com/devtron-labs/devtron/internal/sql/repository"
2222
"github.com/devtron-labs/devtron/internal/util"
23+
"github.com/devtron-labs/devtron/pkg/notifier/adapter"
2324
"github.com/devtron-labs/devtron/pkg/notifier/beans"
24-
"github.com/devtron-labs/devtron/pkg/sql"
2525
"github.com/devtron-labs/devtron/pkg/team"
26+
eventUtil "github.com/devtron-labs/devtron/util/event"
2627
"github.com/go-pg/pg"
2728
"go.uber.org/zap"
2829
"time"
@@ -55,7 +56,7 @@ func NewSMTPNotificationServiceImpl(logger *zap.SugaredLogger, smtpRepository re
5556

5657
func (impl *SMTPNotificationServiceImpl) SaveOrEditNotificationConfig(channelReq []*beans.SMTPConfigDto, userId int32) ([]int, error) {
5758
var responseIds []int
58-
smtpConfigs := buildSMTPNewConfigs(channelReq, userId)
59+
smtpConfigs := adapter.BuildSMTPNewConfigs(channelReq, userId)
5960
for _, config := range smtpConfigs {
6061
if config.Id != 0 {
6162

@@ -80,7 +81,7 @@ func (impl *SMTPNotificationServiceImpl) SaveOrEditNotificationConfig(channelReq
8081
impl.logger.Errorw("err while fetching smtp config", "err", err)
8182
return []int{}, err
8283
}
83-
impl.buildConfigUpdateModel(config, model, userId)
84+
adapter.BuildConfigUpdateModelForSMTP(config, model, userId)
8485
model, uErr := impl.smtpRepository.UpdateSMTPConfig(model)
8586
if uErr != nil {
8687
impl.logger.Errorw("err while updating smtp config", "err", err)
@@ -121,7 +122,7 @@ func (impl *SMTPNotificationServiceImpl) FetchSMTPNotificationConfigById(id int)
121122
impl.logger.Errorw("cannot find all smtp config", "err", err)
122123
return nil, err
123124
}
124-
smtpConfigDto := impl.adaptSMTPConfig(smtpConfig)
125+
smtpConfigDto := adapter.AdaptSMTPConfig(smtpConfig)
125126
return smtpConfigDto, nil
126127
}
127128

@@ -133,7 +134,7 @@ func (impl *SMTPNotificationServiceImpl) FetchAllSMTPNotificationConfig() ([]*be
133134
return []*beans.SMTPConfigDto{}, err
134135
}
135136
for _, smtpConfig := range smtpConfigs {
136-
smtpConfigDto := impl.adaptSMTPConfig(smtpConfig)
137+
smtpConfigDto := adapter.AdaptSMTPConfig(smtpConfig)
137138
smtpConfigDto.AuthPassword = "**********"
138139
responseDto = append(responseDto, smtpConfigDto)
139140
}
@@ -159,76 +160,13 @@ func (impl *SMTPNotificationServiceImpl) FetchAllSMTPNotificationConfigAutocompl
159160
return responseDto, nil
160161
}
161162

162-
func (impl *SMTPNotificationServiceImpl) adaptSMTPConfig(smtpConfig *repository.SMTPConfig) *beans.SMTPConfigDto {
163-
smtpConfigDto := &beans.SMTPConfigDto{
164-
OwnerId: smtpConfig.OwnerId,
165-
Port: smtpConfig.Port,
166-
Host: smtpConfig.Host,
167-
AuthType: smtpConfig.AuthType,
168-
AuthUser: smtpConfig.AuthUser,
169-
AuthPassword: smtpConfig.AuthPassword,
170-
FromEmail: smtpConfig.FromEmail,
171-
ConfigName: smtpConfig.ConfigName,
172-
Description: smtpConfig.Description,
173-
Id: smtpConfig.Id,
174-
Default: smtpConfig.Default,
175-
Deleted: false,
176-
}
177-
return smtpConfigDto
178-
}
179-
180-
func buildSMTPNewConfigs(smtpReq []*beans.SMTPConfigDto, userId int32) []*repository.SMTPConfig {
181-
var smtpConfigs []*repository.SMTPConfig
182-
for _, c := range smtpReq {
183-
smtpConfig := &repository.SMTPConfig{
184-
Id: c.Id,
185-
Port: c.Port,
186-
Host: c.Host,
187-
AuthType: c.AuthType,
188-
AuthUser: c.AuthUser,
189-
AuthPassword: c.AuthPassword,
190-
ConfigName: c.ConfigName,
191-
FromEmail: c.FromEmail,
192-
Deleted: false,
193-
Description: c.Description,
194-
Default: c.Default,
195-
AuditLog: sql.AuditLog{
196-
CreatedBy: userId,
197-
CreatedOn: time.Now(),
198-
UpdatedOn: time.Now(),
199-
UpdatedBy: userId,
200-
},
201-
}
202-
smtpConfig.OwnerId = userId
203-
smtpConfigs = append(smtpConfigs, smtpConfig)
204-
}
205-
return smtpConfigs
206-
}
207-
208-
func (impl *SMTPNotificationServiceImpl) buildConfigUpdateModel(smtpConfig *repository.SMTPConfig, model *repository.SMTPConfig, userId int32) {
209-
model.Id = smtpConfig.Id
210-
model.OwnerId = smtpConfig.OwnerId
211-
model.Port = smtpConfig.Port
212-
model.Host = smtpConfig.Host
213-
model.AuthUser = smtpConfig.AuthUser
214-
model.AuthType = smtpConfig.AuthType
215-
model.AuthPassword = smtpConfig.AuthPassword
216-
model.FromEmail = smtpConfig.FromEmail
217-
model.ConfigName = smtpConfig.ConfigName
218-
model.Description = smtpConfig.Description
219-
model.Default = smtpConfig.Default
220-
model.UpdatedOn = time.Now()
221-
model.UpdatedBy = userId
222-
model.Deleted = false
223-
}
224-
225163
func (impl *SMTPNotificationServiceImpl) DeleteNotificationConfig(deleteReq *beans.SMTPConfigDto, userId int32) error {
226164
existingConfig, err := impl.smtpRepository.FindOne(deleteReq.Id)
227165
if err != nil {
228166
impl.logger.Errorw("No matching entry found for delete", "err", err, "id", deleteReq.Id)
229167
return err
230168
}
231-
notifications, err := impl.notificationSettingsRepository.FindNotificationSettingsByConfigIdAndConfigType(deleteReq.Id, beans.SMTP_CONFIG_TYPE)
169+
notifications, err := impl.notificationSettingsRepository.FindNotificationSettingsByConfigIdAndConfigType(deleteReq.Id, eventUtil.SMTP.String())
232170
if err != nil && err != pg.ErrNoRows {
233171
impl.logger.Errorw("error in deleting smtp config", "config", deleteReq)
234172
return err

0 commit comments

Comments
 (0)