Chore: Remove bus from ngalert (#44465)
* pass notification service down to the notifiers * add ns to all notifiers * remove bus from ngalert notifiers * use smaller interfaces for notificationservice * attempt to fix the tests * remove unused struct field * simplify notification service mock * trying to resolve issues in the tests * make linter happy * make linter even happier * linter, you are annoying
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package notifications
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
)
|
||||
|
||||
type NotificationServiceMock struct {
|
||||
Webhook models.SendWebhookSync
|
||||
Email models.SendEmailCommandSync
|
||||
ShouldError error
|
||||
|
||||
WebhookHandler func(context.Context, *models.SendWebhookSync) error
|
||||
EmailHandler func(context.Context, *models.SendEmailCommandSync) error
|
||||
}
|
||||
|
||||
func (ns *NotificationServiceMock) SendWebhookSync(ctx context.Context, cmd *models.SendWebhookSync) error {
|
||||
ns.Webhook = *cmd
|
||||
if ns.WebhookHandler != nil {
|
||||
return ns.WebhookHandler(ctx, cmd)
|
||||
}
|
||||
return ns.ShouldError
|
||||
}
|
||||
func (ns *NotificationServiceMock) SendEmailCommandHandlerSync(ctx context.Context, cmd *models.SendEmailCommandSync) error {
|
||||
ns.Email = *cmd
|
||||
if ns.EmailHandler != nil {
|
||||
return ns.EmailHandler(ctx, cmd)
|
||||
}
|
||||
return ns.ShouldError
|
||||
}
|
||||
|
||||
func MockNotificationService() *NotificationServiceMock { return &NotificationServiceMock{} }
|
||||
@@ -17,6 +17,17 @@ import (
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
|
||||
type WebhookSender interface {
|
||||
SendWebhookSync(ctx context.Context, cmd *models.SendWebhookSync) error
|
||||
}
|
||||
type EmailSender interface {
|
||||
SendEmailCommandHandlerSync(ctx context.Context, cmd *models.SendEmailCommandSync) error
|
||||
}
|
||||
type Service interface {
|
||||
WebhookSender
|
||||
EmailSender
|
||||
}
|
||||
|
||||
var mailTemplates *template.Template
|
||||
var tmplResetPassword = "reset_password"
|
||||
var tmplSignUpStarted = "signup_started"
|
||||
@@ -36,7 +47,7 @@ func ProvideService(bus bus.Bus, cfg *setting.Cfg, mailer Mailer) (*Notification
|
||||
ns.Bus.AddHandler(ns.validateResetPasswordCode)
|
||||
ns.Bus.AddHandler(ns.sendEmailCommandHandler)
|
||||
|
||||
ns.Bus.AddHandler(ns.sendEmailCommandHandlerSync)
|
||||
ns.Bus.AddHandler(ns.SendEmailCommandHandlerSync)
|
||||
ns.Bus.AddHandler(ns.SendWebhookSync)
|
||||
|
||||
ns.Bus.AddEventListener(ns.signUpStartedHandler)
|
||||
@@ -119,7 +130,7 @@ func subjectTemplateFunc(obj map[string]interface{}, value string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (ns *NotificationService) sendEmailCommandHandlerSync(ctx context.Context, cmd *models.SendEmailCommandSync) error {
|
||||
func (ns *NotificationService) SendEmailCommandHandlerSync(ctx context.Context, cmd *models.SendEmailCommandSync) error {
|
||||
message, err := ns.buildEmailMessage(&models.SendEmailCommand{
|
||||
Data: cmd.Data,
|
||||
Info: cmd.Info,
|
||||
|
||||
Reference in New Issue
Block a user