mirror of https://github.com/statping/statping
commit
c3e1d877b2
|
@ -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"
|
||||
|
|
|
@ -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 <a href=\"https://docs.mattermost.com/developer/webhooks-incoming.html\">Mattermost API</a>.",
|
||||
Author: "Adam Boutcher",
|
||||
AuthorUrl: "https://github.com/adamboutcher",
|
||||
Delay: time.Duration(10 * time.Second),
|
||||
Icon: "far fa-comments",
|
||||
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,
|
||||
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 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 {
|
||||
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
|
||||
}
|
|
@ -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(MATTERMOST_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)
|
||||
})
|
||||
|
||||
}
|
|
@ -26,6 +26,7 @@ type replacer struct {
|
|||
func InitNotifiers() {
|
||||
Add(
|
||||
slacker,
|
||||
mattermoster,
|
||||
Command,
|
||||
Discorder,
|
||||
email,
|
||||
|
|
Loading…
Reference in New Issue