Implement Discord notifier with Slack formatting

This commit is contained in:
stefanprodan
2020-02-10 11:39:57 +02:00
parent 86e813f6b7
commit 35cf634d89
4 changed files with 141 additions and 10 deletions
+81
View File
@@ -0,0 +1,81 @@
package notifier
import (
"errors"
"fmt"
"net/url"
"path"
"strings"
)
// Discord holds the hook URL
type Discord struct {
URL string
Username string
Channel string
}
// NewDiscord validates the URL and returns a Discord object
func NewDiscord(hookURL string, username string, channel string) (*Discord, error) {
webhook, err := url.ParseRequestURI(hookURL)
if err != nil {
return nil, fmt.Errorf("invalid Discord hook URL %s", hookURL)
}
// use Slack formatting
// https://birdie0.github.io/discord-webhooks-guide/other/slack_formatting.html
if !strings.HasSuffix(hookURL, "/slack") {
webhook.Path = path.Join(webhook.Path, "slack")
hookURL = webhook.String()
}
if username == "" {
return nil, errors.New("empty Discord username")
}
if channel == "" {
return nil, errors.New("empty Discord channel")
}
return &Discord{
Channel: channel,
URL: hookURL,
Username: username,
}, nil
}
// Post Discord message
func (s *Discord) Post(workload string, namespace string, message string, fields []Field, warn bool) error {
payload := SlackPayload{
Channel: s.Channel,
Username: s.Username,
IconEmoji: ":rocket:",
}
color := "good"
if warn {
color = "danger"
}
sfields := make([]SlackField, 0, len(fields))
for _, f := range fields {
sfields = append(sfields, SlackField{f.Name, f.Value, false})
}
a := SlackAttachment{
Color: color,
AuthorName: fmt.Sprintf("%s.%s", workload, namespace),
Text: message,
MrkdwnIn: []string{"text"},
Fields: sfields,
}
payload.Attachments = []SlackAttachment{a}
err := postMessage(s.URL, payload)
if err != nil {
return err
}
return nil
}
+49
View File
@@ -0,0 +1,49 @@
package notifier
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestDiscord_Post(t *testing.T) {
fields := []Field{
{Name: "name1", Value: "value1"},
{Name: "name2", Value: "value2"},
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
var payload = SlackPayload{}
err = json.Unmarshal(b, &payload)
if payload.Attachments[0].AuthorName != "podinfo.test" {
t.Fatal("wrong author name")
}
if len(payload.Attachments[0].Fields) != len(fields) {
t.Fatal("wrong facts")
}
}))
defer ts.Close()
discord, err := NewDiscord(ts.URL, "test", "test")
if err != nil {
t.Fatal(err)
}
if !strings.HasSuffix(discord.URL, "/slack") {
t.Error("Invalid Discord URL, expected to have /slack prefix")
}
err = discord.Post("podinfo", "test", "test", fields, true)
if err != nil {
t.Fatal(err)
}
}
+2
View File
@@ -18,6 +18,8 @@ func (f Factory) Notifier(provider string) (Interface, error) {
switch {
case provider == "slack":
return NewSlack(f.URL, f.Username, f.Channel)
case provider == "discord":
return NewDiscord(f.URL, f.Username, f.Channel)
case provider == "msteams":
return NewMSTeams(f.URL)
}
+9 -10
View File
@@ -8,10 +8,9 @@ import (
// Slack holds the hook URL
type Slack struct {
URL string
Username string
Channel string
IconEmoji string
URL string
Username string
Channel string
}
// SlackPayload holds the channel and attachments
@@ -55,18 +54,18 @@ func NewSlack(hookURL string, username string, channel string) (*Slack, error) {
}
return &Slack{
Channel: channel,
URL: hookURL,
Username: username,
IconEmoji: ":rocket:",
Channel: channel,
URL: hookURL,
Username: username,
}, nil
}
// Post Slack message
func (s *Slack) Post(workload string, namespace string, message string, fields []Field, warn bool) error {
payload := SlackPayload{
Channel: s.Channel,
Username: s.Username,
Channel: s.Channel,
Username: s.Username,
IconEmoji: ":rocket:",
}
color := "good"