forked from bitrise-steplib/steps-slack-message
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
302 lines (274 loc) · 8.08 KB
/
main.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"github.com/bitrise-io/go-utils/colorstring"
)
// ConfigsModel ...
type ConfigsModel struct {
// Slack Inputs
WebhookURL string
Channel string
FromUsername string
FromUsernameOnError string
SendIfSucceeded bool
Message string
MessageOnError string
Color string
ColorOnError string
ImageURL string
ImageURLOnError string
Emoji string
EmojiOnError string
IconURL string
IconURLOnError string
IsLinkNames bool
// Other Inputs
IsDebugMode bool
// Other configs
IsBuildFailed bool
}
func createConfigsModelFromEnvs() ConfigsModel {
return ConfigsModel{
WebhookURL: os.Getenv("webhook_url"),
Channel: os.Getenv("channel"),
FromUsername: os.Getenv("from_username"),
FromUsernameOnError: os.Getenv("from_username_on_error"),
SendIfSucceeded: os.Getenv("send_if_succeeded") == "yes",
Message: os.Getenv("message"),
MessageOnError: os.Getenv("message_on_error"),
Emoji: os.Getenv("emoji"),
EmojiOnError: os.Getenv("emoji_on_error"),
Color: os.Getenv("color"),
ColorOnError: os.Getenv("color_on_error"),
ImageURL: os.Getenv("image_url"),
ImageURLOnError: os.Getenv("image_url_on_error"),
IconURL: os.Getenv("icon_url"),
IconURLOnError: os.Getenv("icon_url_on_error"),
IsLinkNames: os.Getenv("link_names") == "yes",
//
IsDebugMode: (os.Getenv("is_debug_mode") == "yes"),
//
IsBuildFailed: (os.Getenv("STEPLIB_BUILD_STATUS") != "0"),
}
}
func (configs ConfigsModel) print() {
fmt.Println("")
fmt.Println(colorstring.Blue("Slack configs:"))
fmt.Println(" - WebhookURL:", configs.WebhookURL)
fmt.Println(" - Channel:", configs.Channel)
fmt.Println(" - FromUsername:", configs.FromUsername)
fmt.Println(" - FromUsernameOnError:", configs.FromUsernameOnError)
fmt.Println(" - SendIfSucceeded:", configs.SendIfSucceeded)
fmt.Println(" - Message:", configs.Message)
fmt.Println(" - MessageOnError:", configs.MessageOnError)
fmt.Println(" - Color:", configs.Color)
fmt.Println(" - ColorOnError:", configs.ColorOnError)
fmt.Println(" - ImageURL:", configs.ImageURL)
fmt.Println(" - ImageURLOnError:", configs.ImageURLOnError)
fmt.Println(" - Emoji:", configs.Emoji)
fmt.Println(" - EmojiOnError:", configs.EmojiOnError)
fmt.Println(" - IconURL:", configs.IconURL)
fmt.Println(" - IconURLOnError:", configs.IconURLOnError)
fmt.Println(" - IsLinkNames:", configs.IsLinkNames)
fmt.Println("")
fmt.Println(colorstring.Blue("Other configs:"))
fmt.Println(" - IsDebugMode:", configs.IsDebugMode)
fmt.Println(" - IsBuildFailed:", configs.IsBuildFailed)
fmt.Println("")
}
func (configs ConfigsModel) validate() error {
// required
if configs.WebhookURL == "" {
return errors.New("No Webhook URL parameter specified")
}
if configs.Message == "" {
return errors.New("No Message parameter specified")
}
if configs.Color == "" {
return errors.New("No Color parameter specified")
}
return nil
}
// AttachmentItemModel ...
type AttachmentItemModel struct {
Fallback string `json:"fallback"`
Text string `json:"text"`
Color string `json:"color,omitempty"`
ImageURL string `json:"image_url"`
MrkdwnIn []string `json:"mrkdwn_in,omitempty"`
}
// RequestParams ...
type RequestParams struct {
// - required
Text string `json:"text"`
// OR use attachment instead of text, for better formatting
Attachments []AttachmentItemModel `json:"attachments,omitempty"`
// - optional
Channel *string `json:"channel"`
Username *string `json:"username"`
EmojiIcon *string `json:"icon_emoji"`
IconURL *string `json:"icon_url"`
LinkNames int `json:"link_names"`
}
// ensureNewlineEscapeChar replaces the "\" + "n" char sequences with the "\n" newline char
func ensureNewlineEscapeChar(s string) string {
return strings.Replace(s, "\\"+"n", "\n", -1)
}
// CreatePayloadParam ...
func CreatePayloadParam(configs ConfigsModel) (string, error) {
// - required
msgColor := configs.Color
if configs.IsBuildFailed {
if configs.ColorOnError == "" {
fmt.Println(colorstring.Yellow(" (i) Build failed but no color_on_error defined, using default."))
} else {
msgColor = configs.ColorOnError
}
}
msgText := configs.Message
if configs.IsBuildFailed {
if configs.MessageOnError == "" {
fmt.Println(colorstring.Yellow(" (i) Build failed but no message_on_error defined, using default."))
} else {
msgText = configs.MessageOnError
}
}
msgText = ensureNewlineEscapeChar(msgText)
// - optional attachment params
msgImage := configs.ImageURL
if configs.IsBuildFailed {
if configs.ImageURLOnError == "" {
fmt.Println(colorstring.Yellow("(i) Build failed but no image_url_on_error defined, using default."))
} else {
msgImage = configs.ImageURLOnError
}
}
reqParams := RequestParams{
Attachments: []AttachmentItemModel{
{
Text: msgText, Fallback: msgText,
Color: msgColor,
ImageURL: msgImage,
MrkdwnIn: []string{"text", "pretext", "fields"},
},
},
}
// - optional
reqChannel := configs.Channel
if reqChannel != "" {
reqParams.Channel = &reqChannel
}
reqUsername := configs.FromUsername
if reqUsername != "" {
reqParams.Username = &reqUsername
}
if configs.IsBuildFailed {
if configs.FromUsernameOnError == "" {
fmt.Println(colorstring.Yellow(" (i) Build failed but no from_username_on_error defined, using default."))
} else {
reqParams.Username = &configs.FromUsernameOnError
}
}
reqEmojiIcon := configs.Emoji
if reqEmojiIcon != "" {
reqParams.EmojiIcon = &reqEmojiIcon
}
if configs.IsBuildFailed {
if configs.EmojiOnError == "" {
fmt.Println(colorstring.Yellow(" (i) Build failed but no emoji_on_error defined, using default."))
} else {
reqParams.EmojiIcon = &configs.EmojiOnError
}
}
reqIconURL := configs.IconURL
if reqIconURL != "" {
reqParams.IconURL = &reqIconURL
}
if configs.IsBuildFailed {
if configs.IconURLOnError == "" {
fmt.Println(colorstring.Yellow(" (i) Build failed but no icon_url_on_error defined, using default."))
} else {
reqParams.IconURL = &configs.IconURLOnError
}
}
// if Icon URL defined ignore the emoji input
if reqParams.IconURL != nil {
reqParams.EmojiIcon = nil
}
if configs.IsLinkNames {
reqParams.LinkNames = 1
}
if configs.IsDebugMode {
fmt.Printf("Parameters: %#v\n", reqParams)
}
// JSON serialize the request params
reqParamsJSONBytes, err := json.Marshal(reqParams)
if err != nil {
return "", nil
}
reqParamsJSONString := string(reqParamsJSONBytes)
return reqParamsJSONString, nil
}
func main() {
configs := createConfigsModelFromEnvs()
configs.print()
if err := configs.validate(); err != nil {
fmt.Println()
fmt.Println(colorstring.Red("Issue with input:"), err)
fmt.Println()
os.Exit(1)
}
if !configs.IsBuildFailed && !configs.SendIfSucceeded {
fmt.Println("Successful build, not sending message.")
os.Exit(0)
}
//
// request URL
requestURL := configs.WebhookURL
//
// request parameters
reqParamsJSONString, err := CreatePayloadParam(configs)
if err != nil {
fmt.Println(colorstring.Red("Failed to create JSON payload:"), err)
os.Exit(1)
}
if configs.IsDebugMode {
fmt.Println()
fmt.Println("JSON payload: ", reqParamsJSONString)
}
//
// send request
resp, err := http.PostForm(requestURL,
url.Values{"payload": []string{reqParamsJSONString}})
if err != nil {
fmt.Println(colorstring.Red("Failed to send the request:"), err)
os.Exit(1)
}
//
// process the response
body, err := ioutil.ReadAll(resp.Body)
bodyStr := string(body)
resp.Body.Close()
if resp.StatusCode != 200 || bodyStr != "ok" {
fmt.Println()
fmt.Println(colorstring.Red("Request failed"))
fmt.Println("Response from Slack: ", bodyStr)
fmt.Println()
os.Exit(1)
}
if configs.IsDebugMode {
fmt.Println()
fmt.Println("Response from Slack: ", bodyStr)
}
fmt.Println()
fmt.Println(colorstring.Green("Slack message successfully sent! 🚀"))
fmt.Println()
os.Exit(0)
}