Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
add icon emoij and icon url support for slack messages (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
sboschman authored Mar 1, 2020
1 parent c61921e commit f641c68
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/argocd-notifications-secret.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ stringData:
slack:
token: <my-token>
username: <override-username> # optional username
icon: <override-icon> # optional icon for the message (supports both emoij and url notation)
opsgenie:
apiUrl: api.opsgenie.com
apiKeys:
Expand Down
29 changes: 29 additions & 0 deletions notifiers/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import (
"crypto/tls"
"encoding/json"
"net/http"
"net/url"
"regexp"

"github.com/nlopes/slack"
log "github.com/sirupsen/logrus"
)

type SlackOptions struct {
Username string `json:"username"`
Icon string `json:"icon"`
Token string `json:"token"`
SigningSecret string `json:"signingSecret"`
Channels []string `json:"channels"`
Expand All @@ -21,6 +25,8 @@ type slackNotifier struct {
opts SlackOptions
}

var validIconEmoij = regexp.MustCompile(`^:.+:$`)

func NewSlackNotifier(opts SlackOptions) Notifier {
return &slackNotifier{opts: opts}
}
Expand All @@ -38,6 +44,15 @@ func (n *slackNotifier) Send(notification Notification, recipient string) error
if n.opts.Username != "" {
msgOptions = append(msgOptions, slack.MsgOptionUsername(n.opts.Username))
}
if n.opts.Icon != "" {
if validIconEmoij.MatchString(n.opts.Icon) {
msgOptions = append(msgOptions, slack.MsgOptionIconEmoji(n.opts.Icon))
} else if isValidIconURL(n.opts.Icon) {
msgOptions = append(msgOptions, slack.MsgOptionIconURL(n.opts.Icon))
} else {
log.Warnf("Icon reference '%v' is not a valid emoij or url", n.opts.Icon)
}
}

if notification.Slack != nil {
attachments := make([]slack.Attachment, 0)
Expand All @@ -59,3 +74,17 @@ func (n *slackNotifier) Send(notification Notification, recipient string) error
_, _, err := s.PostMessageContext(context.TODO(), recipient, msgOptions...)
return err
}

func isValidIconURL(iconURL string) bool {
_, err := url.ParseRequestURI(iconURL)
if err != nil {
return false
}

u, err := url.Parse(iconURL)
if err != nil || (u.Scheme == "" || !(u.Scheme == "http" || u.Scheme == "https")) || u.Host == "" {
return false
}

return true
}
21 changes: 21 additions & 0 deletions notifiers/slack_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package notifiers

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestValidIconEmoij(t *testing.T) {
assert.Equal(t, true, validIconEmoij.MatchString(":slack:"))
assert.Equal(t, true, validIconEmoij.MatchString(":chart_with_upwards_trend:"))
assert.Equal(t, false, validIconEmoij.MatchString("http://lorempixel.com/48/48"))
}

func TestValidIconURL(t *testing.T) {
assert.Equal(t, true, isValidIconURL("http://lorempixel.com/48/48"))
assert.Equal(t, true, isValidIconURL("https://lorempixel.com/48/48"))
assert.Equal(t, false, isValidIconURL("favicon.ico"))
assert.Equal(t, false, isValidIconURL("ftp://favicon.ico"))
assert.Equal(t, false, isValidIconURL("ftp://lorempixel.com/favicon.ico"))
}

0 comments on commit f641c68

Please sign in to comment.