added OnSave() method for notifiers

pull/682/head
hunterlong 2020-06-20 23:52:07 -07:00
parent 386d9a49b9
commit 723ced48c1
18 changed files with 142 additions and 19 deletions

View File

@ -135,6 +135,7 @@ jobs:
TWILIO_SECRET: ${{ secrets.TWILIO_SECRET }}
TWILIO_FROM: ${{ secrets.TWILIO_FROM }}
TWILIO_TO: ${{ secrets.TWILIO_TO }}
TEST_EMAIL: ${{ secrets.TEST_EMAIL }}
- name: Coveralls Testing Coverage
run: |

View File

@ -135,6 +135,7 @@ jobs:
TWILIO_SECRET: ${{ secrets.TWILIO_SECRET }}
TWILIO_FROM: ${{ secrets.TWILIO_FROM }}
TWILIO_TO: ${{ secrets.TWILIO_TO }}
TEST_EMAIL: ${{ secrets.TEST_EMAIL }}
- name: Coveralls Testing Coverage
run: |

View File

@ -1,6 +1,9 @@
# 0.90.55 (06-18-2020)
- Added 404 page
- Modified Statping's PR process, dev -> master
- Fixed Discord notifier
- Modified email template for SMTP emails
- Added OnSave() method for all notifiers
# 0.90.54 (06-17-2020)
- Fixed Slack Notifier's failure/success data saving issue

View File

@ -53,7 +53,13 @@ func apiNotifierUpdateHandler(w http.ResponseWriter, r *http.Request) {
sendErrorJson(err, w, r)
return
}
//notifications.OnSave(notifer.Method)
notif := services.ReturnNotifier(notifer.Method)
if _, err := notif.OnSave(); err != nil {
sendErrorJson(err, w, r)
return
}
sendJsonAction(vars["notifier"], "update", w, r)
}

View File

@ -70,3 +70,8 @@ func (c *commandLine) OnTest() (string, error) {
utils.Log.Infoln(out)
return out, err
}
// OnSave will trigger when this notifier is saved
func (c *commandLine) OnSave() (string, error) {
return "", nil
}

View File

@ -81,6 +81,11 @@ func (d *discord) OnTest() (string, error) {
return string(contents), nil
}
// OnSave will trigger when this notifier is saved
func (d *discord) OnSave() (string, error) {
return "", nil
}
type discordTestJson struct {
Code int `json:"code"`
Message string `json:"message"`

View File

@ -148,6 +148,11 @@ func (e *emailer) OnTest() (string, error) {
return subject, e.dialSend(email)
}
// OnSave will trigger when this notifier is saved
func (e *emailer) OnSave() (string, error) {
return "", nil
}
func (e *emailer) dialSend(email *emailOutgoing) error {
mailer = mail.NewDialer(e.Host, e.Port, e.Username, e.Password)
m := mail.NewMessage()

View File

@ -71,3 +71,8 @@ func (l *lineNotifier) OnTest() (string, error) {
_, err := l.sendMessage(msg)
return msg, err
}
// OnSave will trigger when this notifier is saved
func (l *lineNotifier) OnSave() (string, error) {
return "", nil
}

View File

@ -129,6 +129,11 @@ func (m *mobilePush) Send(pushMessage *pushArray) error {
return nil
}
// OnSave will trigger when this notifier is saved
func (m *mobilePush) OnSave() (string, error) {
return "", nil
}
func pushRequest(msg *pushArray) ([]byte, error) {
body, err := json.Marshal(&PushNotification{[]*pushArray{msg}})
if err != nil {

View File

@ -90,3 +90,8 @@ func (t *pushover) OnTest() (string, error) {
content, err := t.sendMessage(msg)
return content, err
}
// OnSave will trigger when this notifier is saved
func (t *pushover) OnSave() (string, error) {
return "", nil
}

View File

@ -86,3 +86,8 @@ func (s *slack) OnSuccess(srv *services.Service) (string, error) {
out, err := s.sendSlack(msg)
return out, err
}
// OnSave will trigger when this notifier is saved
func (s *slack) OnSave() (string, error) {
return "", nil
}

View File

@ -3,7 +3,6 @@ package notifiers
import (
"bytes"
"encoding/json"
"errors"
"github.com/statping/statping/types/core"
"github.com/statping/statping/types/failures"
"github.com/statping/statping/types/notifications"
@ -35,8 +34,7 @@ var statpingMailer = &statpingEmailer{&notifications.Notification{
Author: "Hunter Long",
AuthorUrl: "https://github.com/hunterlong",
Delay: time.Duration(10 * time.Second),
Icon: "fab fa-slack",
RequestInfo: "Slack allows you to customize your own messages with many complex components. Checkout the <a target=\"_blank\" href=\"https://api.slack.com/reference/surfaces/formatting\">Slack Message API</a> to learn how you can create your own.",
Icon: "fas envelope-square",
Limits: 60,
Form: []notifications.NotificationForm{{
Type: "email",
@ -58,23 +56,13 @@ func (s *statpingEmailer) sendStatpingEmail(msg statpingMail) (string, error) {
}
func (s *statpingEmailer) OnTest() (string, error) {
example := services.Example(true)
testMsg := ReplaceVars(s.SuccessData, example, nil)
contents, resp, err := utils.HttpRequest(s.Host, "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 slack response was incorrect, check the URL")
}
return string(contents), nil
return "", nil
}
type statpingMail struct {
Email string `json:"email"`
Core *core.Core `json:"core"`
Service *services.Service `json:"service"`
Core *core.Core `json:"core,omitempty"`
Service *services.Service `json:"service,omitempty"`
Failure *failures.Failure `json:"failure,omitempty"`
}
@ -86,8 +74,7 @@ func (s *statpingEmailer) OnFailure(srv *services.Service, f *failures.Failure)
Service: srv,
Failure: f,
}
out, err := s.sendStatpingEmail(ee)
return out, err
return s.sendStatpingEmail(ee)
}
// OnSuccess will trigger successful service
@ -98,6 +85,18 @@ func (s *statpingEmailer) OnSuccess(srv *services.Service) (string, error) {
Service: srv,
Failure: nil,
}
return s.sendStatpingEmail(ee)
}
// OnSave will trigger when this notifier is saved
func (s *statpingEmailer) OnSave() (string, error) {
ee := statpingMail{
Email: s.Host,
Core: core.App,
Service: nil,
Failure: nil,
}
out, err := s.sendStatpingEmail(ee)
log.Println("statping emailer response", out)
return out, err
}

View File

@ -0,0 +1,61 @@
package notifiers
import (
"github.com/statping/statping/database"
"github.com/statping/statping/types/failures"
"github.com/statping/statping/types/notifications"
"github.com/statping/statping/types/null"
"github.com/statping/statping/types/services"
"github.com/statping/statping/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
"time"
)
var (
testEmail string
)
func TestStatpingEmailerNotifier(t *testing.T) {
err := utils.InitLogs()
require.Nil(t, err)
db, err := database.OpenTester()
require.Nil(t, err)
db.AutoMigrate(&notifications.Notification{})
notifications.SetDB(db)
testEmail = utils.Params.GetString("TEST_EMAIL")
statpingMailer.Host = testEmail
statpingMailer.Enabled = null.NewNullBool(true)
if testEmail == "" {
t.Log("statping email notifier testing skipped, missing TEST_EMAIL environment variable")
t.SkipNow()
}
t.Run("Load statping emailer", func(t *testing.T) {
statpingMailer.Host = testEmail
statpingMailer.Delay = time.Duration(100 * time.Millisecond)
statpingMailer.Limits = 3
Add(statpingMailer)
assert.Equal(t, "Hunter Long", statpingMailer.Author)
assert.Equal(t, testEmail, statpingMailer.Host)
})
t.Run("statping emailer Within Limits", func(t *testing.T) {
ok := statpingMailer.CanSend()
assert.True(t, ok)
})
t.Run("statping emailer OnFailure", func(t *testing.T) {
_, err := statpingMailer.OnFailure(services.Example(false), failures.Example())
assert.Nil(t, err)
})
t.Run("statping emailer OnSuccess", func(t *testing.T) {
_, err := statpingMailer.OnSuccess(services.Example(true))
assert.Nil(t, err)
})
}

View File

@ -94,6 +94,11 @@ func (t *telegram) OnTest() (string, error) {
return content, err
}
// OnSave will trigger when this notifier is saved
func (t *telegram) OnSave() (string, error) {
return "", nil
}
func telegramSuccess(res []byte) (bool, telegramResponse) {
var obj telegramResponse
json.Unmarshal(res, &obj)

View File

@ -107,6 +107,11 @@ func (t *twilio) OnTest() (string, error) {
return t.sendMessage(msg)
}
// OnSave will trigger when this notifier is saved
func (t *twilio) OnSave() (string, error) {
return "", nil
}
func twilioSuccess(res []byte) (bool, twilioResponse) {
var obj twilioResponse
json.Unmarshal(res, &obj)

View File

@ -148,3 +148,8 @@ func (w *webhooker) OnSuccess(s *services.Service) (string, error) {
content, err := ioutil.ReadAll(resp.Body)
return string(content), err
}
// OnSave will trigger when this notifier is saved
func (w *webhooker) OnSave() (string, error) {
return "", nil
}

View File

@ -10,4 +10,5 @@ type Notifier interface {
OnSuccess(*services.Service) (string, error) // OnSuccess is triggered when a service is successful
OnFailure(*services.Service, *failures.Failure) (string, error) // OnFailure is triggered when a service is failing
OnTest() (string, error) // OnTest is triggered for testing
OnSave() (string, error) // OnSave is triggered for when saved
}

View File

@ -29,5 +29,6 @@ type ServiceNotifier interface {
OnSuccess(*Service) (string, error) // OnSuccess is triggered when a service is successful
OnFailure(*Service, *failures.Failure) (string, error) // OnFailure is triggered when a service is failing
OnTest() (string, error) // OnTest is triggered for testing
OnSave() (string, error) // OnSave is triggered for testing
Select() *notifications.Notification // OnTest is triggered for testing
}