Skip to content

Commit f6e3252

Browse files
authored
chore: move notifications models into notifications service (grafana#61638)
1 parent 8c826cd commit f6e3252

31 files changed

+108
-111
lines changed

pkg/api/alerting.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/grafana/grafana/pkg/services/datasources"
1515
"github.com/grafana/grafana/pkg/services/guardian"
1616
"github.com/grafana/grafana/pkg/services/ngalert/notifier/channels_config"
17+
"github.com/grafana/grafana/pkg/services/notifications"
1718
"github.com/grafana/grafana/pkg/services/search"
1819
"github.com/grafana/grafana/pkg/setting"
1920
"github.com/grafana/grafana/pkg/util"
@@ -645,7 +646,7 @@ func (hs *HTTPServer) NotificationTest(c *models.ReqContext) response.Response {
645646
}
646647

647648
if err := hs.AlertNotificationService.HandleNotificationTestCommand(c.Req.Context(), cmd); err != nil {
648-
if errors.Is(err, models.ErrSmtpNotEnabled) {
649+
if errors.Is(err, notifications.ErrSmtpNotEnabled) {
649650
return response.Error(412, err.Error(), err)
650651
}
651652
var alertingErr alerting.ValidationError

pkg/api/org_invite.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/grafana/grafana/pkg/infra/metrics"
1515
"github.com/grafana/grafana/pkg/models"
1616
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
17+
"github.com/grafana/grafana/pkg/services/notifications"
1718
"github.com/grafana/grafana/pkg/services/org"
1819
tempuser "github.com/grafana/grafana/pkg/services/temp_user"
1920
"github.com/grafana/grafana/pkg/services/user"
@@ -113,7 +114,7 @@ func (hs *HTTPServer) AddOrgInvite(c *models.ReqContext) response.Response {
113114

114115
// send invite email
115116
if inviteDto.SendEmail && util.IsEmail(inviteDto.LoginOrEmail) {
116-
emailCmd := models.SendEmailCommand{
117+
emailCmd := notifications.SendEmailCommand{
117118
To: []string{inviteDto.LoginOrEmail},
118119
Template: "new_user_invite",
119120
Data: map[string]interface{}{
@@ -126,7 +127,7 @@ func (hs *HTTPServer) AddOrgInvite(c *models.ReqContext) response.Response {
126127
}
127128

128129
if err := hs.AlertNG.NotificationService.SendEmailCommandHandler(c.Req.Context(), &emailCmd); err != nil {
129-
if errors.Is(err, models.ErrSmtpNotEnabled) {
130+
if errors.Is(err, notifications.ErrSmtpNotEnabled) {
130131
return response.Error(412, err.Error(), err)
131132
}
132133

@@ -155,7 +156,7 @@ func (hs *HTTPServer) inviteExistingUserToOrg(c *models.ReqContext, user *user.U
155156
}
156157

157158
if inviteDto.SendEmail && util.IsEmail(user.Email) {
158-
emailCmd := models.SendEmailCommand{
159+
emailCmd := notifications.SendEmailCommand{
159160
To: []string{user.Email},
160161
Template: "invited_to_org",
161162
Data: map[string]interface{}{

pkg/api/password.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/grafana/grafana/pkg/api/response"
1010
"github.com/grafana/grafana/pkg/models"
1111
"github.com/grafana/grafana/pkg/services/login"
12+
"github.com/grafana/grafana/pkg/services/notifications"
1213
"github.com/grafana/grafana/pkg/services/user"
1314
"github.com/grafana/grafana/pkg/setting"
1415
"github.com/grafana/grafana/pkg/util"
@@ -45,7 +46,7 @@ func (hs *HTTPServer) SendResetPasswordEmail(c *models.ReqContext) response.Resp
4546
}
4647
}
4748

48-
emailCmd := models.SendResetPasswordEmailCommand{User: usr}
49+
emailCmd := notifications.SendResetPasswordEmailCommand{User: usr}
4950
if err := hs.NotificationService.SendResetPasswordEmail(c.Req.Context(), &emailCmd); err != nil {
5051
return response.Error(500, "Failed to send email", err)
5152
}
@@ -58,7 +59,7 @@ func (hs *HTTPServer) ResetPassword(c *models.ReqContext) response.Response {
5859
if err := web.Bind(c.Req, &form); err != nil {
5960
return response.Error(http.StatusBadRequest, "bad request data", err)
6061
}
61-
query := models.ValidateResetPasswordCodeQuery{Code: form.Code}
62+
query := notifications.ValidateResetPasswordCodeQuery{Code: form.Code}
6263

6364
// For now the only way to know the username to clear login attempts for is
6465
// to set it in the function provided to NotificationService
@@ -71,7 +72,7 @@ func (hs *HTTPServer) ResetPassword(c *models.ReqContext) response.Response {
7172
}
7273

7374
if err := hs.NotificationService.ValidateResetPasswordCode(c.Req.Context(), &query, getUserByLogin); err != nil {
74-
if errors.Is(err, models.ErrInvalidEmailCode) {
75+
if errors.Is(err, notifications.ErrInvalidEmailCode) {
7576
return response.Error(400, "Invalid or expired reset password code", nil)
7677
}
7778
return response.Error(500, "Unknown error validating email code", err)

pkg/services/alerting/notifiers/alertmanager.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func (am *AlertmanagerNotifier) Notify(evalContext *alerting.EvalContext) error
175175
errCnt := 0
176176

177177
for _, url := range am.URL {
178-
cmd := &models.SendWebhookSync{
178+
cmd := &notifications.SendWebhookSync{
179179
Url: strings.TrimSuffix(url, "/") + "/api/v1/alerts",
180180
User: am.BasicAuthUser,
181181
Password: am.BasicAuthPassword,

pkg/services/alerting/notifiers/dingding.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (dd *DingDingNotifier) Notify(evalContext *alerting.EvalContext) error {
8686
return err
8787
}
8888

89-
cmd := &models.SendWebhookSync{
89+
cmd := &notifications.SendWebhookSync{
9090
Url: dd.URL,
9191
Body: string(body),
9292
}

pkg/services/alerting/notifiers/discord.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func (dn *DiscordNotifier) Notify(evalContext *alerting.EvalContext) error {
161161

162162
json, _ := bodyJSON.MarshalJSON()
163163

164-
cmd := &models.SendWebhookSync{
164+
cmd := &notifications.SendWebhookSync{
165165
Url: dn.WebhookURL,
166166
HttpMethod: "POST",
167167
ContentType: "application/json",
@@ -185,7 +185,7 @@ func (dn *DiscordNotifier) Notify(evalContext *alerting.EvalContext) error {
185185
return nil
186186
}
187187

188-
func (dn *DiscordNotifier) embedImage(cmd *models.SendWebhookSync, imagePath string, existingJSONBody []byte) error {
188+
func (dn *DiscordNotifier) embedImage(cmd *notifications.SendWebhookSync, imagePath string, existingJSONBody []byte) error {
189189
// nolint:gosec
190190
// We can ignore the gosec G304 warning on this one because `imagePath` comes
191191
// from the alert `evalContext` that generates the images.

pkg/services/alerting/notifiers/email.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ func (en *EmailNotifier) Notify(evalContext *alerting.EvalContext) error {
8282
error = evalContext.Error.Error()
8383
}
8484

85-
cmd := &models.SendEmailCommandSync{
86-
SendEmailCommand: models.SendEmailCommand{
85+
cmd := &notifications.SendEmailCommandSync{
86+
SendEmailCommand: notifications.SendEmailCommand{
8787
Subject: evalContext.GetNotificationTitle(),
8888
Data: map[string]interface{}{
8989
"Title": evalContext.GetNotificationTitle(),

pkg/services/alerting/notifiers/googlechat.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func (gcn *GoogleChatNotifier) Notify(evalContext *alerting.EvalContext) error {
220220
}
221221
body, _ := json.Marshal(res1D)
222222

223-
cmd := &models.SendWebhookSync{
223+
cmd := &notifications.SendWebhookSync{
224224
Url: gcn.URL,
225225
HttpMethod: "POST",
226226
HttpHeader: headers,

pkg/services/alerting/notifiers/hipchat.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func (hc *HipChatNotifier) Notify(evalContext *alerting.EvalContext) error {
173173
hipURL := fmt.Sprintf("%s/v2/room/%s/notification?auth_token=%s", hc.URL, hc.RoomID, hc.APIKey)
174174
data, _ := json.Marshal(&body)
175175
hc.log.Info("Request payload", "json", string(data))
176-
cmd := &models.SendWebhookSync{Url: hipURL, Body: string(data)}
176+
cmd := &notifications.SendWebhookSync{Url: hipURL, Body: string(data)}
177177

178178
if err := hc.NotificationService.SendWebhookSync(evalContext.Ctx, cmd); err != nil {
179179
hc.log.Error("Failed to send hipchat notification", "error", err, "webhook", hc.Name)

pkg/services/alerting/notifiers/kafka.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func (kn *KafkaNotifier) Notify(evalContext *alerting.EvalContext) error {
114114

115115
topicURL := kn.Endpoint + "/topics/" + kn.Topic
116116

117-
cmd := &models.SendWebhookSync{
117+
cmd := &notifications.SendWebhookSync{
118118
Url: topicURL,
119119
Body: string(body),
120120
HttpMethod: "POST",

pkg/services/alerting/notifiers/line.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (ln *LineNotifier) createAlert(evalContext *alerting.EvalContext) error {
8282
form.Add("imageFullsize", evalContext.ImagePublicURL)
8383
}
8484

85-
cmd := &models.SendWebhookSync{
85+
cmd := &notifications.SendWebhookSync{
8686
Url: lineNotifyURL,
8787
HttpMethod: "POST",
8888
HttpHeader: map[string]string{

pkg/services/alerting/notifiers/opsgenie.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func (on *OpsGenieNotifier) createAlert(evalContext *alerting.EvalContext) error
195195

196196
body, _ := bodyJSON.MarshalJSON()
197197

198-
cmd := &models.SendWebhookSync{
198+
cmd := &notifications.SendWebhookSync{
199199
Url: on.APIUrl,
200200
Body: string(body),
201201
HttpMethod: "POST",
@@ -219,7 +219,7 @@ func (on *OpsGenieNotifier) closeAlert(evalContext *alerting.EvalContext) error
219219
bodyJSON.Set("source", "Grafana")
220220
body, _ := bodyJSON.MarshalJSON()
221221

222-
cmd := &models.SendWebhookSync{
222+
cmd := &notifications.SendWebhookSync{
223223
Url: fmt.Sprintf("%s/alertId-%d/close?identifierType=alias", on.APIUrl, evalContext.Rule.ID),
224224
Body: string(body),
225225
HttpMethod: "POST",

pkg/services/alerting/notifiers/pagerduty.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ func (pn *PagerdutyNotifier) Notify(evalContext *alerting.EvalContext) error {
231231
return err
232232
}
233233

234-
cmd := &models.SendWebhookSync{
234+
cmd := &notifications.SendWebhookSync{
235235
Url: pagerdutyEventAPIURL,
236236
Body: string(body),
237237
HttpMethod: "POST",

pkg/services/alerting/notifiers/pushover.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ func (pn *PushoverNotifier) Notify(evalContext *alerting.EvalContext) error {
280280
return err
281281
}
282282

283-
cmd := &models.SendWebhookSync{
283+
cmd := &notifications.SendWebhookSync{
284284
Url: pushoverEndpoint,
285285
HttpMethod: "POST",
286286
HttpHeader: headers,

pkg/services/alerting/notifiers/sensu.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (sn *SensuNotifier) Notify(evalContext *alerting.EvalContext) error {
138138

139139
body, _ := bodyJSON.MarshalJSON()
140140

141-
cmd := &models.SendWebhookSync{
141+
cmd := &notifications.SendWebhookSync{
142142
Url: sn.URL,
143143
User: sn.User,
144144
Password: sn.Password,

pkg/services/alerting/notifiers/sensugo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func (sn *SensuGoNotifier) Notify(evalContext *alerting.EvalContext) error {
188188
return err
189189
}
190190

191-
cmd := &models.SendWebhookSync{
191+
cmd := &notifications.SendWebhookSync{
192192
Url: fmt.Sprintf("%s/api/core/v2/namespaces/%s/events", strings.TrimSuffix(sn.URL, "/"), namespace),
193193
Body: string(body),
194194
HttpMethod: "POST",

pkg/services/alerting/notifiers/slack.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ func (sn *SlackNotifier) slackFileUpload(evalContext *alerting.EvalContext, log
415415
if err != nil {
416416
return err
417417
}
418-
cmd := &models.SendWebhookSync{
418+
cmd := &notifications.SendWebhookSync{
419419
Url: "https://slack.com/api/files.upload", Body: uploadBody.String(), HttpHeader: headers, HttpMethod: "POST",
420420
}
421421
if err := sn.NotificationService.SendWebhookSync(evalContext.Ctx, cmd); err != nil {

pkg/services/alerting/notifiers/teams.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func (tn *TeamsNotifier) Notify(evalContext *alerting.EvalContext) error {
133133
}
134134

135135
data, _ := json.Marshal(&body)
136-
cmd := &models.SendWebhookSync{Url: tn.URL, Body: string(data)}
136+
cmd := &notifications.SendWebhookSync{Url: tn.URL, Body: string(data)}
137137

138138
if err := tn.NotificationService.SendWebhookSync(evalContext.Ctx, cmd); err != nil {
139139
tn.log.Error("Failed to send teams notification", "error", err, "webhook", tn.Name)

pkg/services/alerting/notifiers/telegram.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func NewTelegramNotifier(model *models.AlertNotification, fn alerting.GetDecrypt
8989
}, nil
9090
}
9191

92-
func (tn *TelegramNotifier) buildMessage(evalContext *alerting.EvalContext, sendImageInline bool) (*models.SendWebhookSync, error) {
92+
func (tn *TelegramNotifier) buildMessage(evalContext *alerting.EvalContext, sendImageInline bool) (*notifications.SendWebhookSync, error) {
9393
if sendImageInline {
9494
cmd, err := tn.buildMessageInlineImage(evalContext)
9595
if err == nil {
@@ -102,7 +102,7 @@ func (tn *TelegramNotifier) buildMessage(evalContext *alerting.EvalContext, send
102102
return tn.buildMessageLinkedImage(evalContext)
103103
}
104104

105-
func (tn *TelegramNotifier) buildMessageLinkedImage(evalContext *alerting.EvalContext) (*models.SendWebhookSync, error) {
105+
func (tn *TelegramNotifier) buildMessageLinkedImage(evalContext *alerting.EvalContext) (*notifications.SendWebhookSync, error) {
106106
message := fmt.Sprintf("<b>%s</b>\nState: %s\nMessage: %s\n", evalContext.GetNotificationTitle(), evalContext.Rule.Name, evalContext.Rule.Message)
107107

108108
ruleURL, err := evalContext.GetRuleURL()
@@ -132,7 +132,7 @@ func (tn *TelegramNotifier) buildMessageLinkedImage(evalContext *alerting.EvalCo
132132
})
133133
}
134134

135-
func (tn *TelegramNotifier) buildMessageInlineImage(evalContext *alerting.EvalContext) (*models.SendWebhookSync, error) {
135+
func (tn *TelegramNotifier) buildMessageInlineImage(evalContext *alerting.EvalContext) (*notifications.SendWebhookSync, error) {
136136
var imageFile *os.File
137137
var err error
138138

@@ -169,7 +169,7 @@ func (tn *TelegramNotifier) buildMessageInlineImage(evalContext *alerting.EvalCo
169169
})
170170
}
171171

172-
func (tn *TelegramNotifier) generateTelegramCmd(message string, messageField string, apiAction string, extraConf func(writer *multipart.Writer)) (*models.SendWebhookSync, error) {
172+
func (tn *TelegramNotifier) generateTelegramCmd(message string, messageField string, apiAction string, extraConf func(writer *multipart.Writer)) (*notifications.SendWebhookSync, error) {
173173
var body bytes.Buffer
174174
w := multipart.NewWriter(&body)
175175
defer func() {
@@ -203,7 +203,7 @@ func (tn *TelegramNotifier) generateTelegramCmd(message string, messageField str
203203
tn.log.Info("Sending telegram notification", "chat_id", tn.ChatID, "bot_token", tn.BotToken, "apiAction", apiAction)
204204
url := fmt.Sprintf(telegramAPIURL, tn.BotToken, apiAction)
205205

206-
cmd := &models.SendWebhookSync{
206+
cmd := &notifications.SendWebhookSync{
207207
Url: url,
208208
Body: body.String(),
209209
HttpMethod: "POST",
@@ -260,7 +260,7 @@ func appendIfPossible(tlog log.Logger, message string, extra string, sizeLimit i
260260

261261
// Notify send an alert notification to Telegram.
262262
func (tn *TelegramNotifier) Notify(evalContext *alerting.EvalContext) error {
263-
var cmd *models.SendWebhookSync
263+
var cmd *notifications.SendWebhookSync
264264
var err error
265265
if evalContext.ImagePublicURL == "" && tn.UploadImage {
266266
cmd, err = tn.buildMessage(evalContext, true)

pkg/services/alerting/notifiers/threema.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func (notifier *ThreemaNotifier) Notify(evalContext *alerting.EvalContext) error
152152
headers := map[string]string{
153153
"Content-Type": "application/x-www-form-urlencoded",
154154
}
155-
cmd := &models.SendWebhookSync{
155+
cmd := &notifications.SendWebhookSync{
156156
Url: url,
157157
Body: body,
158158
HttpMethod: "POST",

pkg/services/alerting/notifiers/victorops.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func (vn *VictoropsNotifier) Notify(evalContext *alerting.EvalContext) error {
154154
}
155155

156156
data, _ := bodyJSON.MarshalJSON()
157-
cmd := &models.SendWebhookSync{Url: vn.URL, Body: string(data)}
157+
cmd := &notifications.SendWebhookSync{Url: vn.URL, Body: string(data)}
158158

159159
if err := vn.NotificationService.SendWebhookSync(evalContext.Ctx, cmd); err != nil {
160160
vn.log.Error("Failed to send Victorops notification", "error", err, "webhook", vn.Name)

pkg/services/alerting/notifiers/webhook.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func (wn *WebhookNotifier) Notify(evalContext *alerting.EvalContext) error {
145145

146146
bodyJSON, _ := json.Marshal(body)
147147

148-
cmd := &models.SendWebhookSync{
148+
cmd := &notifications.SendWebhookSync{
149149
Url: wn.URL,
150150
User: wn.User,
151151
Password: wn.Password,

pkg/services/ngalert/notifier/email_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515

1616
"github.com/grafana/grafana/pkg/bus"
1717
"github.com/grafana/grafana/pkg/infra/tracing"
18-
"github.com/grafana/grafana/pkg/models"
1918
"github.com/grafana/grafana/pkg/services/notifications"
2019
"github.com/grafana/grafana/pkg/setting"
2120
)
@@ -240,15 +239,15 @@ func (e emailSender) SendWebhook(ctx context.Context, cmd *channels.SendWebhookS
240239
}
241240

242241
func (e emailSender) SendEmail(ctx context.Context, cmd *channels.SendEmailSettings) error {
243-
attached := make([]*models.SendEmailAttachFile, 0, len(cmd.AttachedFiles))
242+
attached := make([]*notifications.SendEmailAttachFile, 0, len(cmd.AttachedFiles))
244243
for _, file := range cmd.AttachedFiles {
245-
attached = append(attached, &models.SendEmailAttachFile{
244+
attached = append(attached, &notifications.SendEmailAttachFile{
246245
Name: file.Name,
247246
Content: file.Content,
248247
})
249248
}
250-
return e.ns.SendEmailCommandHandlerSync(ctx, &models.SendEmailCommandSync{
251-
SendEmailCommand: models.SendEmailCommand{
249+
return e.ns.SendEmailCommandHandlerSync(ctx, &notifications.SendEmailCommandSync{
250+
SendEmailCommand: notifications.SendEmailCommand{
252251
To: cmd.To,
253252
SingleEmail: cmd.SingleEmail,
254253
Template: cmd.Template,

pkg/services/ngalert/notifier/sender.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/grafana/alerting/alerting/notifier/channels"
77

8-
"github.com/grafana/grafana/pkg/models"
98
"github.com/grafana/grafana/pkg/services/notifications"
109
)
1110

@@ -14,7 +13,7 @@ type sender struct {
1413
}
1514

1615
func (s sender) SendWebhook(ctx context.Context, cmd *channels.SendWebhookSettings) error {
17-
return s.ns.SendWebhookSync(ctx, &models.SendWebhookSync{
16+
return s.ns.SendWebhookSync(ctx, &notifications.SendWebhookSync{
1817
Url: cmd.URL,
1918
User: cmd.User,
2019
Password: cmd.Password,
@@ -27,18 +26,18 @@ func (s sender) SendWebhook(ctx context.Context, cmd *channels.SendWebhookSettin
2726
}
2827

2928
func (s sender) SendEmail(ctx context.Context, cmd *channels.SendEmailSettings) error {
30-
var attached []*models.SendEmailAttachFile
29+
var attached []*notifications.SendEmailAttachFile
3130
if cmd.AttachedFiles != nil {
32-
attached = make([]*models.SendEmailAttachFile, 0, len(cmd.AttachedFiles))
31+
attached = make([]*notifications.SendEmailAttachFile, 0, len(cmd.AttachedFiles))
3332
for _, file := range cmd.AttachedFiles {
34-
attached = append(attached, &models.SendEmailAttachFile{
33+
attached = append(attached, &notifications.SendEmailAttachFile{
3534
Name: file.Name,
3635
Content: file.Content,
3736
})
3837
}
3938
}
40-
return s.ns.SendEmailCommandHandlerSync(ctx, &models.SendEmailCommandSync{
41-
SendEmailCommand: models.SendEmailCommand{
39+
return s.ns.SendEmailCommandHandlerSync(ctx, &notifications.SendEmailCommandSync{
40+
SendEmailCommand: notifications.SendEmailCommand{
4241
To: cmd.To,
4342
SingleEmail: cmd.SingleEmail,
4443
Template: cmd.Template,

0 commit comments

Comments
 (0)