This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
/
slack.go
134 lines (116 loc) · 3.98 KB
/
slack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package services
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"regexp"
texttemplate "text/template"
httputil "github.com/argoproj-labs/argocd-notifications/pkg/util/http"
log "github.com/sirupsen/logrus"
"github.com/slack-go/slack"
)
type SlackNotification struct {
Attachments string `json:"attachments,omitempty"`
Blocks string `json:"blocks,omitempty"`
}
func (n *SlackNotification) GetTemplater(name string, f texttemplate.FuncMap) (Templater, error) {
slackAttachments, err := texttemplate.New(name).Funcs(f).Parse(n.Attachments)
if err != nil {
return nil, err
}
slackBlocks, err := texttemplate.New(name).Funcs(f).Parse(n.Blocks)
if err != nil {
return nil, err
}
return func(notification *Notification, vars map[string]interface{}) error {
if notification.Slack == nil {
notification.Slack = &SlackNotification{}
}
var slackAttachmentsData bytes.Buffer
if err := slackAttachments.Execute(&slackAttachmentsData, vars); err != nil {
return err
}
notification.Slack.Attachments = slackAttachmentsData.String()
var slackBlocksData bytes.Buffer
if err := slackBlocks.Execute(&slackBlocksData, vars); err != nil {
return err
}
notification.Slack.Blocks = slackBlocksData.String()
return nil
}, nil
}
type SlackOptions struct {
Username string `json:"username"`
Icon string `json:"icon"`
Token string `json:"token"`
SigningSecret string `json:"signingSecret"`
Channels []string `json:"channels"`
InsecureSkipVerify bool `json:"insecureSkipVerify"`
ApiURL string `json:"apiURL"`
}
type slackService struct {
opts SlackOptions
}
var validIconEmoij = regexp.MustCompile(`^:.+:$`)
func NewSlackService(opts SlackOptions) NotificationService {
return &slackService{opts: opts}
}
func (s *slackService) Send(notification Notification, dest Destination) error {
apiURL := slack.APIURL
if s.opts.ApiURL != "" {
apiURL = s.opts.ApiURL
}
transport := httputil.NewTransport(apiURL, s.opts.InsecureSkipVerify)
client := &http.Client{
Transport: httputil.NewLoggingRoundTripper(transport, log.WithField("service", "slack")),
}
sl := slack.New(s.opts.Token, slack.OptionHTTPClient(client), slack.OptionAPIURL(apiURL))
msgOptions := []slack.MsgOption{slack.MsgOptionText(notification.Message, false)}
if s.opts.Username != "" {
msgOptions = append(msgOptions, slack.MsgOptionUsername(s.opts.Username))
}
if s.opts.Icon != "" {
if validIconEmoij.MatchString(s.opts.Icon) {
msgOptions = append(msgOptions, slack.MsgOptionIconEmoji(s.opts.Icon))
} else if isValidIconURL(s.opts.Icon) {
msgOptions = append(msgOptions, slack.MsgOptionIconURL(s.opts.Icon))
} else {
log.Warnf("Icon reference '%v' is not a valid emoij or url", s.opts.Icon)
}
}
if notification.Slack != nil {
attachments := make([]slack.Attachment, 0)
if notification.Slack.Attachments != "" {
if err := json.Unmarshal([]byte(notification.Slack.Attachments), &attachments); err != nil {
return fmt.Errorf("failed to unmarshal attachments '%s' : %v", notification.Slack.Attachments, err)
}
}
blocks := slack.Blocks{}
if notification.Slack.Blocks != "" {
if err := json.Unmarshal([]byte(notification.Slack.Blocks), &blocks); err != nil {
return fmt.Errorf("failed to unmarshal blocks '%s' : %v", notification.Slack.Blocks, err)
}
}
msgOptions = append(msgOptions, slack.MsgOptionAttachments(attachments...), slack.MsgOptionBlocks(blocks.BlockSet...))
}
_, _, err := sl.PostMessageContext(context.TODO(), dest.Recipient, msgOptions...)
return err
}
// GetSigningSecret exposes signing secret for slack bot
func (s *slackService) GetSigningSecret() string {
return s.opts.SigningSecret
}
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
}