From 2f60929d1ee45de9cec57a93962f379db113db94 Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Fri, 4 Nov 2016 05:18:43 -0700 Subject: [PATCH 1/2] Added slack mention functionality --- pkg/services/alerting/notifiers/slack.go | 7 +++++-- .../features/alerting/partials/notification_edit.html | 11 +++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/pkg/services/alerting/notifiers/slack.go b/pkg/services/alerting/notifiers/slack.go index e7b6ab79456..f266c8cd095 100644 --- a/pkg/services/alerting/notifiers/slack.go +++ b/pkg/services/alerting/notifiers/slack.go @@ -23,11 +23,13 @@ func NewSlackNotifier(model *m.AlertNotification) (alerting.Notifier, error) { } recipient := model.Settings.Get("recipient").MustString() + mention := model.Settings.Get("mention").MustString() return &SlackNotifier{ NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings), Url: url, Recipient: recipient, + Mention: mention, log: log.New("alerting.notifier.slack"), }, nil } @@ -36,6 +38,7 @@ type SlackNotifier struct { NotifierBase Url string Recipient string + Mention string log log.Logger } @@ -70,9 +73,9 @@ func (this *SlackNotifier) Notify(evalContext *alerting.EvalContext) error { }) } - message := "" + message := this.Mention if evalContext.Rule.State != m.AlertStateOK { //dont add message when going back to alert state ok. - message = evalContext.Rule.Message + message += " " + evalContext.Rule.Message } body := map[string]interface{}{ diff --git a/public/app/features/alerting/partials/notification_edit.html b/public/app/features/alerting/partials/notification_edit.html index 47999b5289d..efb22e2e05a 100644 --- a/public/app/features/alerting/partials/notification_edit.html +++ b/public/app/features/alerting/partials/notification_edit.html @@ -74,6 +74,17 @@ Override default channel or user, use #channel-name or @username +
+ Mention + + + + Mention a user or a group using @ when notifying in a channel + +
From dc3a62da83755bad76309c1c346b0862cff6767b Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Fri, 4 Nov 2016 05:33:50 -0700 Subject: [PATCH 2/2] Added slack tests , fixed webhook tests --- pkg/services/alerting/notifiers/slack_test.go | 81 +++++++++++++++++++ .../alerting/notifiers/webhook_test.go | 8 +- 2 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 pkg/services/alerting/notifiers/slack_test.go diff --git a/pkg/services/alerting/notifiers/slack_test.go b/pkg/services/alerting/notifiers/slack_test.go new file mode 100644 index 00000000000..5b1763064aa --- /dev/null +++ b/pkg/services/alerting/notifiers/slack_test.go @@ -0,0 +1,81 @@ +package notifiers + +import ( + "testing" + + "github.com/grafana/grafana/pkg/components/simplejson" + m "github.com/grafana/grafana/pkg/models" + . "github.com/smartystreets/goconvey/convey" +) + +func TestSlackNotifier(t *testing.T) { + Convey("Slack notifier tests", t, func() { + + Convey("Parsing alert notification from settings", func() { + Convey("empty settings should return error", func() { + json := `{ }` + + settingsJSON, _ := simplejson.NewJson([]byte(json)) + model := &m.AlertNotification{ + Name: "ops", + Type: "slack", + Settings: settingsJSON, + } + + _, err := NewSlackNotifier(model) + So(err, ShouldNotBeNil) + }) + + Convey("from settings", func() { + json := ` + { + "url": "http://google.com" + }` + + settingsJSON, _ := simplejson.NewJson([]byte(json)) + model := &m.AlertNotification{ + Name: "ops", + Type: "slack", + Settings: settingsJSON, + } + + not, err := NewSlackNotifier(model) + slackNotifier := not.(*SlackNotifier) + + So(err, ShouldBeNil) + So(slackNotifier.Name, ShouldEqual, "ops") + So(slackNotifier.Type, ShouldEqual, "slack") + So(slackNotifier.Url, ShouldEqual, "http://google.com") + So(slackNotifier.Recipient, ShouldEqual, "") + So(slackNotifier.Mention, ShouldEqual, "") + }) + + Convey("from settings with Recipient and Mention", func() { + json := ` + { + "url": "http://google.com", + "recipient": "#ds-opentsdb", + "mention": "@carl" + }` + + settingsJSON, _ := simplejson.NewJson([]byte(json)) + model := &m.AlertNotification{ + Name: "ops", + Type: "slack", + Settings: settingsJSON, + } + + not, err := NewSlackNotifier(model) + slackNotifier := not.(*SlackNotifier) + + So(err, ShouldBeNil) + So(slackNotifier.Name, ShouldEqual, "ops") + So(slackNotifier.Type, ShouldEqual, "slack") + So(slackNotifier.Url, ShouldEqual, "http://google.com") + So(slackNotifier.Recipient, ShouldEqual, "#ds-opentsdb") + So(slackNotifier.Mention, ShouldEqual, "@carl") + }) + + }) + }) +} diff --git a/pkg/services/alerting/notifiers/webhook_test.go b/pkg/services/alerting/notifiers/webhook_test.go index 6147f54d773..eb25130e4e1 100644 --- a/pkg/services/alerting/notifiers/webhook_test.go +++ b/pkg/services/alerting/notifiers/webhook_test.go @@ -40,12 +40,12 @@ func TestWebhookNotifier(t *testing.T) { } not, err := NewWebHookNotifier(model) - emailNotifier := not.(*WebhookNotifier) + webhookNotifier := not.(*WebhookNotifier) So(err, ShouldBeNil) - So(emailNotifier.Name, ShouldEqual, "ops") - So(emailNotifier.Type, ShouldEqual, "email") - So(emailNotifier.Url, ShouldEqual, "http://google.com") + So(webhookNotifier.Name, ShouldEqual, "ops") + So(webhookNotifier.Type, ShouldEqual, "email") + So(webhookNotifier.Url, ShouldEqual, "http://google.com") }) }) })