From 19193b49226bfd70049d16c5c37330300d5e4081 Mon Sep 17 00:00:00 2001 From: Adam Boutcher Date: Tue, 14 Sep 2021 11:22:29 +0100 Subject: [PATCH 1/8] First Mattermost Test --- notifiers/mattermost.go | 98 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 notifiers/mattermost.go diff --git a/notifiers/mattermost.go b/notifiers/mattermost.go new file mode 100644 index 00000000..b5813c74 --- /dev/null +++ b/notifiers/mattermost.go @@ -0,0 +1,98 @@ +package notifiers + +import ( + "bytes" + "errors" + "strings" + "time" + + "github.com/statping-ng/statping-ng/types/failures" + "github.com/statping-ng/statping-ng/types/notifications" + "github.com/statping-ng/statping-ng/types/notifier" + "github.com/statping-ng/statping-ng/types/null" + "github.com/statping-ng/statping-ng/types/services" + "github.com/statping-ng/statping-ng/utils" +) + +var _ notifier.Notifier = (*mattermost)(nil) + +const ( + mattermostMethod = "mattermost" +) + +type mattermost struct { + *notifications.Notification +} + +func (s *mattermost) Select() *notifications.Notification { + return s.Notification +} + +var mattermoster = &mattermost{¬ifications.Notification{ + Method: mattermostMethod, + Title: "Mattermost", + Description: "Send notifications to your mattermost channel when a service is offline. Insert your Incoming webhook URL for your channel to receive notifications. Based on the Mattermost API.", + Author: "Adam Boutcher", + AuthorUrl: "https://github.com/adamboutcher", + Delay: time.Duration(10 * time.Second), + Icon: "fab fa-comments", + SuccessData: null.NewNullString(`{ "blocks": [ { "type": "section", "text": { "type": "mrkdwn", "text": "The service {{.Service.Name}} is back online." } }, { "type": "actions", "elements": [ { "type": "button", "text": { "type": "plain_text", "text": "View Service", "emoji": true }, "style": "primary", "url": "{{.Core.Domain}}/service/{{.Service.Id}}" }, { "type": "button", "text": { "type": "plain_text", "text": "Go to Statping", "emoji": true }, "url": "{{.Core.Domain}}" } ] } ] }`), + FailureData: null.NewNullString(`{ "blocks": [ { "type": "section", "text": { "type": "mrkdwn", "text": ":warning: The service {{.Service.Name}} is currently offline! :warning:" } }, { "type": "divider" }, { "type": "section", "fields": [ { "type": "mrkdwn", "text": "*Service:*\n{{.Service.Name}}" }, { "type": "mrkdwn", "text": "*URL:*\n{{.Service.Domain}}" }, { "type": "mrkdwn", "text": "*Status Code:*\n{{.Service.LastStatusCode}}" }, { "type": "mrkdwn", "text": "*When:*\n{{.Failure.CreatedAt}}" }, { "type": "mrkdwn", "text": "*Downtime:*\n{{.Service.Downtime.Human}}" }, { "type": "plain_text", "text": "*Error:*\n{{.Failure.Issue}}" } ] }, { "type": "divider" }, { "type": "actions", "elements": [ { "type": "button", "text": { "type": "plain_text", "text": "View Offline Service", "emoji": true }, "style": "danger", "url": "{{.Core.Domain}}/service/{{.Service.Id}}" }, { "type": "button", "text": { "type": "plain_text", "text": "Go to Statping", "emoji": true }, "url": "{{.Core.Domain}}" } ] } ] }`), + DataType: "json", + RequestInfo: "Mattermost allows you to customize your own messages with many complex components.", + Limits: 60, + Form: []notifications.NotificationForm{{ + Type: "text", + Title: "Incoming Webhook Url", + Placeholder: "https://mattermost.example.com/hooks/abcd1234efgh5678ijkl9012xy", + SmallText: "Incoming Webhook URL generated on your mattermost server.", + DbField: "Host", + Required: true, + }}}, +} + +// Send will send a HTTP Post to the slack webhooker API. It accepts type: string +func (s *mattermost) sendMattermost(msg string) (string, error) { + resp, _, err := utils.HttpRequest(s.Host.String, "POST", "application/json", nil, strings.NewReader(msg), time.Duration(10*time.Second), true, nil) + if err != nil { + return "", err + } + return string(resp), nil +} + +func (s *mattermost) OnTest() (string, error) { + example := services.Example(true) + testMsg := ReplaceVars(s.SuccessData.String, example, failures.Failure{}) + contents, resp, err := utils.HttpRequest(s.Host.String, "POST", "application/json", nil, bytes.NewBuffer([]byte(testMsg)), time.Duration(10*time.Second), true, nil) + if err != nil { + return "", err + } + defer resp.Body.Close() + if string(contents) != "ok" { + return string(contents), errors.New("the mattermost response was incorrect, check the URL") + } + return string(contents), nil +} + +// OnFailure will trigger failing service +func (s *mattermost) OnFailure(srv services.Service, f failures.Failure) (string, error) { + msg := ReplaceVars(s.FailureData.String, srv, f) + out, err := s.sendMattermost(msg) + return out, err +} + +// OnSuccess will trigger successful service +func (s *mattermost) OnSuccess(srv services.Service) (string, error) { + msg := ReplaceVars(s.SuccessData.String, srv, failures.Failure{}) + out, err := s.sendMattermost(msg) + return out, err +} + +// OnSave will trigger when this notifier is saved +func (s *mattermost) OnSave() (string, error) { + return "", nil +} + +func (s *mattermost) Valid(values notifications.Values) error { + return nil +} From ac4b52dcdbf8c87e8e73d677bddda2948ba0eaf6 Mon Sep 17 00:00:00 2001 From: Adam Boutcher Date: Tue, 14 Sep 2021 11:24:20 +0100 Subject: [PATCH 2/8] Add mattermost to list --- notifiers/mattermost.go | 2 +- notifiers/notifiers.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/notifiers/mattermost.go b/notifiers/mattermost.go index b5813c74..02e48630 100644 --- a/notifiers/mattermost.go +++ b/notifiers/mattermost.go @@ -28,7 +28,7 @@ func (s *mattermost) Select() *notifications.Notification { return s.Notification } -var mattermoster = &mattermost{¬ifications.Notification{ +var mattermost = &mattermost{¬ifications.Notification{ Method: mattermostMethod, Title: "Mattermost", Description: "Send notifications to your mattermost channel when a service is offline. Insert your Incoming webhook URL for your channel to receive notifications. Based on the Mattermost API.", diff --git a/notifiers/notifiers.go b/notifiers/notifiers.go index 54fa7aa6..b46c9845 100644 --- a/notifiers/notifiers.go +++ b/notifiers/notifiers.go @@ -26,6 +26,7 @@ type replacer struct { func InitNotifiers() { Add( slacker, + mattermost, Command, Discorder, email, From e99e807958c18568a5fa369c4ec2e708e25d4f10 Mon Sep 17 00:00:00 2001 From: Adam Boutcher Date: Tue, 14 Sep 2021 11:48:08 +0100 Subject: [PATCH 3/8] Some notifier fixes --- notifiers/mattermost.go | 2 +- notifiers/notifiers.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/notifiers/mattermost.go b/notifiers/mattermost.go index 02e48630..b5813c74 100644 --- a/notifiers/mattermost.go +++ b/notifiers/mattermost.go @@ -28,7 +28,7 @@ func (s *mattermost) Select() *notifications.Notification { return s.Notification } -var mattermost = &mattermost{¬ifications.Notification{ +var mattermoster = &mattermost{¬ifications.Notification{ Method: mattermostMethod, Title: "Mattermost", Description: "Send notifications to your mattermost channel when a service is offline. Insert your Incoming webhook URL for your channel to receive notifications. Based on the Mattermost API.", diff --git a/notifiers/notifiers.go b/notifiers/notifiers.go index b46c9845..1ffc2848 100644 --- a/notifiers/notifiers.go +++ b/notifiers/notifiers.go @@ -26,7 +26,7 @@ type replacer struct { func InitNotifiers() { Add( slacker, - mattermost, + mattermoster, Command, Discorder, email, From 70ced8f8bfa3f2b52d7a94872be15e7a7dda8359 Mon Sep 17 00:00:00 2001 From: Adam Boutcher Date: Tue, 14 Sep 2021 12:17:11 +0100 Subject: [PATCH 4/8] Mattermost JSON --- notifiers/mattermost.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/notifiers/mattermost.go b/notifiers/mattermost.go index b5813c74..d7960168 100644 --- a/notifiers/mattermost.go +++ b/notifiers/mattermost.go @@ -35,9 +35,9 @@ var mattermoster = &mattermost{¬ifications.Notification{ Author: "Adam Boutcher", AuthorUrl: "https://github.com/adamboutcher", Delay: time.Duration(10 * time.Second), - Icon: "fab fa-comments", - SuccessData: null.NewNullString(`{ "blocks": [ { "type": "section", "text": { "type": "mrkdwn", "text": "The service {{.Service.Name}} is back online." } }, { "type": "actions", "elements": [ { "type": "button", "text": { "type": "plain_text", "text": "View Service", "emoji": true }, "style": "primary", "url": "{{.Core.Domain}}/service/{{.Service.Id}}" }, { "type": "button", "text": { "type": "plain_text", "text": "Go to Statping", "emoji": true }, "url": "{{.Core.Domain}}" } ] } ] }`), - FailureData: null.NewNullString(`{ "blocks": [ { "type": "section", "text": { "type": "mrkdwn", "text": ":warning: The service {{.Service.Name}} is currently offline! :warning:" } }, { "type": "divider" }, { "type": "section", "fields": [ { "type": "mrkdwn", "text": "*Service:*\n{{.Service.Name}}" }, { "type": "mrkdwn", "text": "*URL:*\n{{.Service.Domain}}" }, { "type": "mrkdwn", "text": "*Status Code:*\n{{.Service.LastStatusCode}}" }, { "type": "mrkdwn", "text": "*When:*\n{{.Failure.CreatedAt}}" }, { "type": "mrkdwn", "text": "*Downtime:*\n{{.Service.Downtime.Human}}" }, { "type": "plain_text", "text": "*Error:*\n{{.Failure.Issue}}" } ] }, { "type": "divider" }, { "type": "actions", "elements": [ { "type": "button", "text": { "type": "plain_text", "text": "View Offline Service", "emoji": true }, "style": "danger", "url": "{{.Core.Domain}}/service/{{.Service.Id}}" }, { "type": "button", "text": { "type": "plain_text", "text": "Go to Statping", "emoji": true }, "url": "{{.Core.Domain}}" } ] } ] }`), + Icon: "far fa-comments", + SuccessData: null.NewNullString(`{"text": "The service {{.Service.Name}} is back online."}`), + FailureData: null.NewNullString(`{"text": "The service {{.Service.Name}} has gone offline."}`), DataType: "json", RequestInfo: "Mattermost allows you to customize your own messages with many complex components.", Limits: 60, From af361e5eaf2638918af0ab5d89ee8c88d166b5c6 Mon Sep 17 00:00:00 2001 From: Adam Boutcher Date: Tue, 14 Sep 2021 12:46:32 +0100 Subject: [PATCH 5/8] Mattermost JSON Updates --- notifiers/mattermost.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/notifiers/mattermost.go b/notifiers/mattermost.go index d7960168..5829cbe9 100644 --- a/notifiers/mattermost.go +++ b/notifiers/mattermost.go @@ -36,8 +36,8 @@ var mattermoster = &mattermost{¬ifications.Notification{ AuthorUrl: "https://github.com/adamboutcher", Delay: time.Duration(10 * time.Second), Icon: "far fa-comments", - SuccessData: null.NewNullString(`{"text": "The service {{.Service.Name}} is back online."}`), - FailureData: null.NewNullString(`{"text": "The service {{.Service.Name}} has gone offline."}`), + SuccessData: null.NewNullString(`{"icon_emoji":":white_check_mark:", "text": "The service {{.Service.Name}} is back online."}`), + FailureData: null.NewNullString(`{"icon_emoji":":x:", "text": "The service {{.Service.Name}} has gone offline."}`), DataType: "json", RequestInfo: "Mattermost allows you to customize your own messages with many complex components.", Limits: 60, From 30f27aa19eb16bdc6b101b78e70c077cebdbb874 Mon Sep 17 00:00:00 2001 From: Adam Boutcher Date: Tue, 14 Sep 2021 12:50:48 +0100 Subject: [PATCH 6/8] Mattermost tester --- notifiers/mattermost_test.go | 70 ++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 notifiers/mattermost_test.go diff --git a/notifiers/mattermost_test.go b/notifiers/mattermost_test.go new file mode 100644 index 00000000..ac9cb400 --- /dev/null +++ b/notifiers/mattermost_test.go @@ -0,0 +1,70 @@ +package notifiers + +import ( + "github.com/statping-ng/statping-ng/database" + "github.com/statping-ng/statping-ng/types/core" + "github.com/statping-ng/statping-ng/types/failures" + "github.com/statping-ng/statping-ng/types/notifications" + "github.com/statping-ng/statping-ng/types/null" + "github.com/statping-ng/statping-ng/types/services" + "github.com/statping-ng/statping-ng/utils" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "testing" + "time" +) + +var ( + MATTERMOST_URL string +) + +func TestMattermostNotifier(t *testing.T) { + err := utils.InitLogs() + require.Nil(t, err) + + t.Parallel() + db, err := database.OpenTester() + require.Nil(t, err) + db.AutoMigrate(¬ifications.Notification{}) + notifications.SetDB(db) + core.Example() + + MATTERMOST_URL = utils.Params.GetString("MATTERMOST_URL") + if MATTERMOST_URL == "" { + t.Log("mattermost notifier testing skipped, missing MATTERMOST_URL environment variable") + t.SkipNow() + } + + mattermoster.Host = null.NewNullString(SLACK_URL) + mattermoster.Enabled = null.NewNullBool(true) + + t.Run("Load mattermost", func(t *testing.T) { + mattermoster.Host = null.NewNullString(MATTERMOST_URL) + mattermoster.Delay = 100 * time.Millisecond + mattermoster.Limits = 3 + Add(mattermoster) + assert.Equal(t, "Adam Boutcher", mattermoster.Author) + assert.Equal(t, MATTERMOST_URL, mattermoster.Host.String) + }) + + t.Run("mattermost Within Limits", func(t *testing.T) { + ok := mattermoster.CanSend() + assert.True(t, ok) + }) + + t.Run("mattermost OnSave", func(t *testing.T) { + _, err := mattermoster.OnSave() + assert.Nil(t, err) + }) + + t.Run("mattermost OnFailure", func(t *testing.T) { + _, err := mattermoster.OnFailure(services.Example(false), failures.Example()) + assert.Nil(t, err) + }) + + t.Run("mattermost OnSuccess", func(t *testing.T) { + _, err := mattermoster.OnSuccess(services.Example(true)) + assert.Nil(t, err) + }) + +} From f3cb5f8cad276792237374ae53820f814681f74b Mon Sep 17 00:00:00 2001 From: Adam Boutcher Date: Tue, 14 Sep 2021 13:26:23 +0100 Subject: [PATCH 7/8] Remove slack from all mattermost files --- notifiers/mattermost.go | 2 +- notifiers/mattermost_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/notifiers/mattermost.go b/notifiers/mattermost.go index 5829cbe9..dec0af20 100644 --- a/notifiers/mattermost.go +++ b/notifiers/mattermost.go @@ -51,7 +51,7 @@ var mattermoster = &mattermost{¬ifications.Notification{ }}}, } -// Send will send a HTTP Post to the slack webhooker API. It accepts type: string +// Send will send a HTTP Post to the mattermost webhooker API. It accepts type: string func (s *mattermost) sendMattermost(msg string) (string, error) { resp, _, err := utils.HttpRequest(s.Host.String, "POST", "application/json", nil, strings.NewReader(msg), time.Duration(10*time.Second), true, nil) if err != nil { diff --git a/notifiers/mattermost_test.go b/notifiers/mattermost_test.go index ac9cb400..e49366fe 100644 --- a/notifiers/mattermost_test.go +++ b/notifiers/mattermost_test.go @@ -35,7 +35,7 @@ func TestMattermostNotifier(t *testing.T) { t.SkipNow() } - mattermoster.Host = null.NewNullString(SLACK_URL) + mattermoster.Host = null.NewNullString(MATTERMOST_URL) mattermoster.Enabled = null.NewNullBool(true) t.Run("Load mattermost", func(t *testing.T) { From 2c55a09924fefb22fe248e4c7e79354a39e7b99b Mon Sep 17 00:00:00 2001 From: Adam Boutcher Date: Tue, 14 Sep 2021 13:30:51 +0100 Subject: [PATCH 8/8] Postman tests increate to 14 from 13. --- dev/postman.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dev/postman.json b/dev/postman.json index 712312f4..3e2f60ff 100644 --- a/dev/postman.json +++ b/dev/postman.json @@ -4205,7 +4205,7 @@ "", "pm.test(\"View All Notifiers\", function () {", " var jsonData = pm.response.json();", - " pm.expect(jsonData.length).to.eql(13);", + " pm.expect(jsonData.length).to.eql(14);", "});" ], "type": "text/javascript" @@ -6242,4 +6242,4 @@ } ], "protocolProfileBehavior": {} -} \ No newline at end of file +}