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_SECRET: ${{ secrets.TWILIO_SECRET }}
TWILIO_FROM: ${{ secrets.TWILIO_FROM }} TWILIO_FROM: ${{ secrets.TWILIO_FROM }}
TWILIO_TO: ${{ secrets.TWILIO_TO }} TWILIO_TO: ${{ secrets.TWILIO_TO }}
TEST_EMAIL: ${{ secrets.TEST_EMAIL }}
- name: Coveralls Testing Coverage - name: Coveralls Testing Coverage
run: | run: |

View File

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

View File

@ -1,6 +1,9 @@
# 0.90.55 (06-18-2020) # 0.90.55 (06-18-2020)
- Added 404 page - Added 404 page
- Modified Statping's PR process, dev -> master - 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) # 0.90.54 (06-17-2020)
- Fixed Slack Notifier's failure/success data saving issue - 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) sendErrorJson(err, w, r)
return 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) sendJsonAction(vars["notifier"], "update", w, r)
} }

View File

@ -70,3 +70,8 @@ func (c *commandLine) OnTest() (string, error) {
utils.Log.Infoln(out) utils.Log.Infoln(out)
return out, err 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 return string(contents), nil
} }
// OnSave will trigger when this notifier is saved
func (d *discord) OnSave() (string, error) {
return "", nil
}
type discordTestJson struct { type discordTestJson struct {
Code int `json:"code"` Code int `json:"code"`
Message string `json:"message"` Message string `json:"message"`

View File

@ -148,6 +148,11 @@ func (e *emailer) OnTest() (string, error) {
return subject, e.dialSend(email) 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 { func (e *emailer) dialSend(email *emailOutgoing) error {
mailer = mail.NewDialer(e.Host, e.Port, e.Username, e.Password) mailer = mail.NewDialer(e.Host, e.Port, e.Username, e.Password)
m := mail.NewMessage() m := mail.NewMessage()

View File

@ -71,3 +71,8 @@ func (l *lineNotifier) OnTest() (string, error) {
_, err := l.sendMessage(msg) _, err := l.sendMessage(msg)
return msg, err 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 return nil
} }
// OnSave will trigger when this notifier is saved
func (m *mobilePush) OnSave() (string, error) {
return "", nil
}
func pushRequest(msg *pushArray) ([]byte, error) { func pushRequest(msg *pushArray) ([]byte, error) {
body, err := json.Marshal(&PushNotification{[]*pushArray{msg}}) body, err := json.Marshal(&PushNotification{[]*pushArray{msg}})
if err != nil { if err != nil {

View File

@ -90,3 +90,8 @@ func (t *pushover) OnTest() (string, error) {
content, err := t.sendMessage(msg) content, err := t.sendMessage(msg)
return content, err 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) out, err := s.sendSlack(msg)
return out, err 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 ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors"
"github.com/statping/statping/types/core" "github.com/statping/statping/types/core"
"github.com/statping/statping/types/failures" "github.com/statping/statping/types/failures"
"github.com/statping/statping/types/notifications" "github.com/statping/statping/types/notifications"
@ -35,8 +34,7 @@ var statpingMailer = &statpingEmailer{&notifications.Notification{
Author: "Hunter Long", Author: "Hunter Long",
AuthorUrl: "https://github.com/hunterlong", AuthorUrl: "https://github.com/hunterlong",
Delay: time.Duration(10 * time.Second), Delay: time.Duration(10 * time.Second),
Icon: "fab fa-slack", Icon: "fas envelope-square",
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.",
Limits: 60, Limits: 60,
Form: []notifications.NotificationForm{{ Form: []notifications.NotificationForm{{
Type: "email", Type: "email",
@ -58,23 +56,13 @@ func (s *statpingEmailer) sendStatpingEmail(msg statpingMail) (string, error) {
} }
func (s *statpingEmailer) OnTest() (string, error) { func (s *statpingEmailer) OnTest() (string, error) {
example := services.Example(true) return "", nil
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
} }
type statpingMail struct { type statpingMail struct {
Email string `json:"email"` Email string `json:"email"`
Core *core.Core `json:"core"` Core *core.Core `json:"core,omitempty"`
Service *services.Service `json:"service"` Service *services.Service `json:"service,omitempty"`
Failure *failures.Failure `json:"failure,omitempty"` Failure *failures.Failure `json:"failure,omitempty"`
} }
@ -86,8 +74,7 @@ func (s *statpingEmailer) OnFailure(srv *services.Service, f *failures.Failure)
Service: srv, Service: srv,
Failure: f, Failure: f,
} }
out, err := s.sendStatpingEmail(ee) return s.sendStatpingEmail(ee)
return out, err
} }
// OnSuccess will trigger successful service // OnSuccess will trigger successful service
@ -98,6 +85,18 @@ func (s *statpingEmailer) OnSuccess(srv *services.Service) (string, error) {
Service: srv, Service: srv,
Failure: nil, 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) out, err := s.sendStatpingEmail(ee)
log.Println("statping emailer response", out)
return out, err 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 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) { func telegramSuccess(res []byte) (bool, telegramResponse) {
var obj telegramResponse var obj telegramResponse
json.Unmarshal(res, &obj) json.Unmarshal(res, &obj)

View File

@ -107,6 +107,11 @@ func (t *twilio) OnTest() (string, error) {
return t.sendMessage(msg) 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) { func twilioSuccess(res []byte) (bool, twilioResponse) {
var obj twilioResponse var obj twilioResponse
json.Unmarshal(res, &obj) 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) content, err := ioutil.ReadAll(resp.Body)
return string(content), err 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 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 OnFailure(*services.Service, *failures.Failure) (string, error) // OnFailure is triggered when a service is failing
OnTest() (string, error) // OnTest is triggered for testing 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 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 OnFailure(*Service, *failures.Failure) (string, error) // OnFailure is triggered when a service is failing
OnTest() (string, error) // OnTest is triggered for testing OnTest() (string, error) // OnTest is triggered for testing
OnSave() (string, error) // OnSave is triggered for testing
Select() *notifications.Notification // OnTest is triggered for testing Select() *notifications.Notification // OnTest is triggered for testing
} }