Skip to content

Commit

Permalink
fix(notifier|mattermost) added plot sending (#897)
Browse files Browse the repository at this point in the history
* fix(notifier|mattermost) added plot sending
  • Loading branch information
kissken authored Aug 21, 2023
1 parent 31167c6 commit db5a20f
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 7 deletions.
16 changes: 16 additions & 0 deletions mock/notifier/mattermost/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions senders/mattermost/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ import "github.com/mattermost/mattermost-server/v6/model"
type Client interface {
SetToken(token string)
CreatePost(post *model.Post) (*model.Post, *model.Response, error)
UploadFile(data []byte, channelId string, filename string) (*model.FileUploadResponse, *model.Response, error)
}
52 changes: 47 additions & 5 deletions senders/mattermost/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type mattermost struct {
// You must call Init method before SendEvents method.
type Sender struct {
frontURI string
logger moira.Logger
location *time.Location
client Client
}
Expand Down Expand Up @@ -70,17 +71,28 @@ func (sender *Sender) Init(senderSettings interface{}, logger moira.Logger, loca
}
sender.frontURI = frontURI
sender.location = location
sender.logger = logger

return nil
}

// SendEvents implements moira.Sender interface.
func (sender *Sender) SendEvents(events moira.NotificationEvents, contact moira.ContactData, trigger moira.TriggerData, _ [][]byte, throttled bool) error {
func (sender *Sender) SendEvents(events moira.NotificationEvents, contact moira.ContactData, trigger moira.TriggerData, plots [][]byte, throttled bool) error {
message := sender.buildMessage(events, trigger, throttled)
err := sender.sendMessage(message, contact.Value, trigger.ID)
post, err := sender.sendMessage(message, contact.Value, trigger.ID)
if err != nil {
return err
}
if len(plots) > 0 {
err = sender.sendPlots(plots, contact.Value, post.Id, trigger.ID)
if err != nil {
sender.logger.Warning().
String("trigger_id", trigger.ID).
String("contact_value", contact.Value).
String("contact_type", contact.Type).
Error(err)
}
}

return nil
}
Expand Down Expand Up @@ -187,15 +199,45 @@ func (sender *Sender) buildEventsString(events moira.NotificationEvents, charsFo
return eventsString
}

func (sender *Sender) sendMessage(message string, contact string, triggerID string) error {
func (sender *Sender) sendMessage(message string, contact string, triggerID string) (*model.Post, error) {
post := model.Post{
ChannelId: contact,
Message: message,
}

_, _, err := sender.client.CreatePost(&post)
sentPost, _, err := sender.client.CreatePost(&post)
if err != nil {
return fmt.Errorf("failed to send %s event message to Mattermost [%s]: %s", triggerID, contact, err)
return nil, fmt.Errorf("failed to send %s event message to Mattermost [%s]: %s", triggerID, contact, err)
}

return sentPost, nil
}

func (sender *Sender) sendPlots(plots [][]byte, channelID, postID, triggerID string) error {
var filesID []string

filename := fmt.Sprintf("%s.png", triggerID)
for _, plot := range plots {
file, _, err := sender.client.UploadFile(plot, channelID, filename)
if err != nil {
return err
}
for _, info := range file.FileInfos {
filesID = append(filesID, info.Id)
}
}

if len(filesID) > 0 {
_, _, err := sender.client.CreatePost(
&model.Post{
ChannelId: channelID,
RootId: postID,
FileIds: filesID,
},
)
if err != nil {
return err
}
}

return nil
Expand Down
24 changes: 22 additions & 2 deletions senders/mattermost/sender_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"
"time"

"github.com/mattermost/mattermost-server/v6/model"

"github.com/moira-alert/moira"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -39,16 +41,34 @@ func TestSendEvents(t *testing.T) {
So(err, ShouldNotBeNil)
})

Convey("When client CreatePost is success, SendEvents should not return error", func() {
Convey("When client CreatePost is success and no plots, SendEvents should not return error", func() {
ctrl := gomock.NewController(t)
client := mock.NewMockClient(ctrl)
client.EXPECT().CreatePost(gomock.Any()).Return(nil, nil, nil)
client.EXPECT().CreatePost(gomock.Any()).Return(&model.Post{Id: "postID"}, nil, nil)
sender.client = client

events, contact, trigger, plots, throttled := moira.NotificationEvents{}, moira.ContactData{}, moira.TriggerData{}, make([][]byte, 0), false
err = sender.SendEvents(events, contact, trigger, plots, throttled)
So(err, ShouldBeNil)
})

Convey("When client CreatePost is success and have succeeded sent plots, SendEvents should not return error", func() {
ctrl := gomock.NewController(t)
client := mock.NewMockClient(ctrl)
client.EXPECT().CreatePost(gomock.Any()).Return(&model.Post{Id: "postID"}, nil, nil).Times(2)
client.EXPECT().UploadFile(gomock.Any(), "contactDataID", "triggerID.png").
Return(
&model.FileUploadResponse{
FileInfos: []*model.FileInfo{{Id: "fileID"}},
}, nil, nil)
sender.client = client

plots := make([][]byte, 0)
plots = append(plots, []byte("my_awesome_plot"))
events, contact, trigger, throttled := moira.NotificationEvents{}, moira.ContactData{Value: "contactDataID"}, moira.TriggerData{ID: "triggerID"}, false
err = sender.SendEvents(events, contact, trigger, plots, throttled)
So(err, ShouldBeNil)
})
})
}

Expand Down

0 comments on commit db5a20f

Please sign in to comment.