Skip to content

Commit

Permalink
Merge pull request #73 from K-Phoen/notify-channels
Browse files Browse the repository at this point in the history
Add a way to setup notification channels in bulk
  • Loading branch information
K-Phoen authored Jun 22, 2020
2 parents 971b164 + a1ea7a6 commit 388b23f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
12 changes: 12 additions & 0 deletions alert/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ func Notify(channel *Channel) Option {
}
}

// NotifyChannels appends the given notification channels to the list of
// channels for this alert.
func NotifyChannels(channels ...*Channel) Option {
return func(alert *Alert) {
for _, channel := range channels {
alert.Builder.Notifications = append(alert.Builder.Notifications, sdk.AlertNotification{
UID: channel.UID,
})
}
}
}

// NotifyChannel adds a notification for this alert given a channel UID.
func NotifyChannel(channelUID string) Option {
return func(alert *Alert) {
Expand Down
13 changes: 13 additions & 0 deletions alert/alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ func TestNotificationCanBeSet(t *testing.T) {
req.Empty(a.Builder.Notifications[0].ID)
}

func TestNotificationCanBeSetInBulk(t *testing.T) {
req := require.New(t)
channel := &Channel{ID: 1, UID: "channel"}
otherChannel := &Channel{ID: 2, UID: "other-channel"}

a := New("", NotifyChannels(channel, otherChannel))

req.Len(a.Builder.Notifications, 2)
req.ElementsMatch([]string{"channel", "other-channel"}, []string{a.Builder.Notifications[0].UID, a.Builder.Notifications[1].UID})
req.Empty(a.Builder.Notifications[0].ID)
req.Empty(a.Builder.Notifications[1].ID)
}

func TestNotificationCanBeSetByChannelID(t *testing.T) {
req := require.New(t)

Expand Down

0 comments on commit 388b23f

Please sign in to comment.