This repository has been archived by the owner on Nov 24, 2022. It is now read-only.
/
slack.go
171 lines (139 loc) · 4.62 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package slack
import (
"fmt"
"time"
"github.com/nlopes/slack"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
eventerspec "github.com/giantswarm/draughtsman/service/eventer/spec"
"github.com/giantswarm/draughtsman/service/notifier/spec"
slackspec "github.com/giantswarm/draughtsman/service/slack"
)
const (
// goodColour is the colour to use for success Slack messages.
goodColour = "good"
// dangerColour is the colour to use for failure Slack messages.
dangerColour = "danger"
// titleFormat is the format for titles for Slack messages.
// Templated with the repository name, and sha.
// e.g: "api - 12345"
titleFormat = "%v - %v"
// successMessage is the message for success Slack messages.
successMessage = "Successfully deployed"
// failedMessageFormat is the format for failure Slack messages.
// Templated with the error message itself.
failedMessageFormat = "Encountered an error ```%v```"
// footerFormat is the format for footers for Slack messages.
// Templated with the environment name, and the deployment ID.
// e.g: "jabberwocky (12345)"
footerFormat = "%v (%v)"
)
// SlackNotifierType is an Notifier that uses Slack.
var SlackNotifierType spec.NotifierType = "SlackNotifier"
// Config represents the configuration used to create a Slack Notifier..
type Config struct {
// Dependencies.
Logger micrologger.Logger
SlackClient slackspec.Client
// Settings.
Channel string
Emoji string
Environment string
Username string
}
// DefaultConfig provides a default configuration to create a new Slack
// Notifier by best effort.
func DefaultConfig() Config {
return Config{
// Dependencies.
Logger: nil,
SlackClient: nil,
}
}
// New creates a new configured Slack Notifier.
func New(config Config) (*SlackNotifier, error) {
if config.Logger == nil {
return nil, microerror.Maskf(invalidConfigError, "logger must not be empty")
}
if config.SlackClient == nil {
return nil, microerror.Maskf(invalidConfigError, "slack client must not be empty")
}
if config.Channel == "" {
return nil, microerror.Maskf(invalidConfigError, "channel must not be empty")
}
if config.Emoji == "" {
return nil, microerror.Maskf(invalidConfigError, "emoji must not be empty")
}
if config.Environment == "" {
return nil, microerror.Maskf(invalidConfigError, "environment must not be empty")
}
if config.Username == "" {
return nil, microerror.Maskf(invalidConfigError, "username must not be empty")
}
config.Logger.Log("debug", "checking connection to Slack")
if _, err := config.SlackClient.AuthTest(); err != nil {
return nil, microerror.Maskf(slackError, "could not authenticate with slack: %s", err.Error())
}
notifier := &SlackNotifier{
// Dependencies.
client: config.SlackClient,
logger: config.Logger,
// Settings.
channel: config.Channel,
emoji: config.Emoji,
environment: config.Environment,
username: config.Username,
}
return notifier, nil
}
// SlackNotifier is an implementation of the Notifier interface,
// that uses Slack.
type SlackNotifier struct {
// Dependencies.
client slackspec.Client
logger micrologger.Logger
// Settings.
channel string
emoji string
environment string
username string
}
// postSlackMessage takes a DeploymentEvent and a possible error message,
// and posts a helpful message to the configured Slack channel.
func (n *SlackNotifier) postSlackMessage(event eventerspec.DeploymentEvent, errorMessage string) error {
startTime := time.Now()
defer updateSlackMetrics(startTime)
success := false
if len(errorMessage) == 0 {
success = true
}
attachment := slack.Attachment{}
attachment.Color = dangerColour
if success {
attachment.Color = goodColour
}
attachment.MarkdownIn = []string{"text"}
attachment.Title = fmt.Sprintf(titleFormat, event.Name, event.Sha)
attachment.Text = fmt.Sprintf(failedMessageFormat, errorMessage)
if success {
attachment.Text = successMessage
}
attachment.Footer = fmt.Sprintf(footerFormat, n.environment, event.ID)
params := slack.PostMessageParameters{}
params.Username = n.username
params.IconEmoji = n.emoji
params.Attachments = []slack.Attachment{attachment}
_, _, err := n.client.PostMessage(n.channel, "", params)
if err != nil {
return microerror.Mask(err)
}
return nil
}
func (n *SlackNotifier) Success(event eventerspec.DeploymentEvent) error {
n.logger.Log("debug", "sending success message to slack")
return n.postSlackMessage(event, "")
}
func (n *SlackNotifier) Failed(event eventerspec.DeploymentEvent, errorMessage string) error {
n.logger.Log("debug", "sending failed message to slack")
return n.postSlackMessage(event, errorMessage)
}