Skip to content

Commit

Permalink
Merge pull request #192 from K-Phoen/alertmanager-discord
Browse files Browse the repository at this point in the history
Support discord alertmanager contact point type
  • Loading branch information
K-Phoen authored Jul 24, 2022
2 parents ceff5f0 + 0a51e7a commit ada0185
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
42 changes: 42 additions & 0 deletions alertmanager/discord/discord.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package discord

import (
"github.com/K-Phoen/grabana/alertmanager"
"github.com/K-Phoen/sdk"
)

// Option represents an option that can be used to configure a "discord"
// contact point type.
type Option func(contactType *discordType)

type discordType struct {
builder *sdk.ContactPointType
}

// With creates an Opsgenie contact point type with the given settings.
func With(webhookURL string, opts ...Option) alertmanager.ContactPointOption {
discord := &discordType{
builder: &sdk.ContactPointType{
Type: "discord",
Settings: map[string]interface{}{
"url": webhookURL,
},
},
}

for _, opt := range opts {
opt(discord)
}

return func(contact *alertmanager.Contact) {
contact.Builder.GrafanaManagedReceivers = append(contact.Builder.GrafanaManagedReceivers, *discord.builder)
}
}

// UseDiscordUsername uses the username configured in Discord's webhook settings.
// Otherwise, the username will be 'Grafana'.
func UseDiscordUsername() Option {
return func(contactType *discordType) {
contactType.builder.Settings["use_discord_username"] = true
}
}
31 changes: 31 additions & 0 deletions alertmanager/discord/discord_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package discord

import (
"testing"

"github.com/K-Phoen/grabana/alertmanager"
"github.com/stretchr/testify/require"
)

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

contactPoint := alertmanager.ContactPoint("", With("url"))

req.Len(contactPoint.Builder.GrafanaManagedReceivers, 1)

contactType := contactPoint.Builder.GrafanaManagedReceivers[0]
req.Equal("discord", contactType.Type)
req.Equal("url", contactType.Settings["url"].(string))
}

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

contactPoint := alertmanager.ContactPoint("", With("", UseDiscordUsername()))

req.Len(contactPoint.Builder.GrafanaManagedReceivers, 1)

contactType := contactPoint.Builder.GrafanaManagedReceivers[0]
req.True(contactType.Settings["use_discord_username"].(bool))
}

0 comments on commit ada0185

Please sign in to comment.