diff --git a/alertmanager/discord/discord.go b/alertmanager/discord/discord.go new file mode 100644 index 00000000..5d5a8fca --- /dev/null +++ b/alertmanager/discord/discord.go @@ -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 + } +} diff --git a/alertmanager/discord/discord_test.go b/alertmanager/discord/discord_test.go new file mode 100644 index 00000000..4398e5aa --- /dev/null +++ b/alertmanager/discord/discord_test.go @@ -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)) +}