-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
269 lines (240 loc) · 6.53 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
)
const (
EnvSlackWebhook = "SLACK_WEBHOOK"
EnvSlackIcon = "SLACK_ICON"
EnvSlackIconEmoji = "SLACK_ICON_EMOJI"
EnvSlackChannel = "SLACK_CHANNEL"
EnvSlackTitle = "SLACK_TITLE"
EnvSlackMessage = "SLACK_MESSAGE"
EnvSlackColor = "SLACK_COLOR"
EnvSlackUserName = "SLACK_USERNAME"
EnvSlackFooter = "SLACK_FOOTER"
EnvGithubActor = "GITHUB_ACTOR"
EnvSiteName = "SITE_NAME"
EnvHostName = "HOST_NAME"
EnvMinimal = "MSG_MINIMAL"
EnvSlackLinkNames = "SLACK_LINK_NAMES"
)
type Webhook struct {
Text string `json:"text,omitempty"`
UserName string `json:"username,omitempty"`
IconURL string `json:"icon_url,omitempty"`
IconEmoji string `json:"icon_emoji,omitempty"`
Channel string `json:"channel,omitempty"`
LinkNames string `json:"link_names,omitempty"`
UnfurlLinks bool `json:"unfurl_links"`
Attachments []Attachment `json:"attachments,omitempty"`
}
type Attachment struct {
Fallback string `json:"fallback"`
Pretext string `json:"pretext,omitempty"`
Color string `json:"color,omitempty"`
AuthorName string `json:"author_name,omitempty"`
AuthorLink string `json:"author_link,omitempty"`
AuthorIcon string `json:"author_icon,omitempty"`
Footer string `json:"footer,omitempty"`
Fields []Field `json:"fields,omitempty"`
}
type Field struct {
Title string `json:"title,omitempty"`
Value string `json:"value,omitempty"`
Short bool `json:"short,omitempty"`
}
func main() {
endpoint := os.Getenv(EnvSlackWebhook)
if endpoint == "" {
_, err := fmt.Fprintln(os.Stderr, "URL is required")
if err != nil {
return
}
os.Exit(1)
}
text := os.Getenv(EnvSlackMessage)
if text == "" {
_, err := fmt.Fprintln(os.Stderr, "Message is required")
if err != nil {
return
}
os.Exit(1)
}
if strings.HasPrefix(os.Getenv("GITHUB_WORKFLOW"), ".github") {
err := os.Setenv("GITHUB_WORKFLOW", "Link to action run")
if err != nil {
return
}
}
longSha := os.Getenv("GITHUB_SHA")
commitSha := longSha[0:6]
minimal := os.Getenv(EnvMinimal)
var fields []Field
if minimal == "true" {
mainFields := []Field{
{
Title: os.Getenv(EnvSlackTitle),
Value: envOr(EnvSlackMessage, "EOM"),
Short: false,
},
}
fields = append(mainFields, fields...)
} else if minimal != "" {
requiredFields := strings.Split(minimal, ",")
mainFields := []Field{
{
Title: os.Getenv(EnvSlackTitle),
Value: envOr(EnvSlackMessage, "EOM"),
Short: false,
},
}
for _, requiredField := range requiredFields {
switch strings.ToLower(requiredField) {
case "ref":
field := []Field{
{
Title: "Ref",
Value: os.Getenv("GITHUB_REF"),
Short: true,
},
}
mainFields = append(field, mainFields...)
case "event":
field := []Field{
{
Title: "Event",
Value: os.Getenv("GITHUB_EVENT_NAME"),
Short: true,
},
}
mainFields = append(field, mainFields...)
case "actions url":
field := []Field{
{
Title: "Actions URL",
Value: "<" + os.Getenv("GITHUB_SERVER_URL") + "/" + os.Getenv("GITHUB_REPOSITORY") + "/commit/" + os.Getenv("GITHUB_SHA") + "/checks|" + os.Getenv("GITHUB_WORKFLOW") + ">",
Short: true,
},
}
mainFields = append(field, mainFields...)
case "commit":
field := []Field{
{
Title: "Commit",
Value: "<" + os.Getenv("GITHUB_SERVER_URL") + "/" + os.Getenv("GITHUB_REPOSITORY") + "/commit/" + os.Getenv("GITHUB_SHA") + "|" + commitSha + ">",
Short: true,
},
}
mainFields = append(field, mainFields...)
}
}
fields = append(mainFields, fields...)
} else {
mainFields := []Field{
{
Title: "Ref",
Value: os.Getenv("GITHUB_REF"),
Short: true,
}, {
Title: "Event",
Value: os.Getenv("GITHUB_EVENT_NAME"),
Short: true,
},
{
Title: "Actions URL",
Value: "<" + os.Getenv("GITHUB_SERVER_URL") + "/" + os.Getenv("GITHUB_REPOSITORY") + "/commit/" + os.Getenv("GITHUB_SHA") + "/checks|" + os.Getenv("GITHUB_WORKFLOW") + ">",
Short: true,
},
{
Title: "Commit",
Value: "<" + os.Getenv("GITHUB_SERVER_URL") + "/" + os.Getenv("GITHUB_REPOSITORY") + "/commit/" + os.Getenv("GITHUB_SHA") + "|" + commitSha + ">",
Short: true,
},
{
Title: os.Getenv(EnvSlackTitle),
Value: envOr(EnvSlackMessage, "EOM"),
Short: false,
},
}
fields = append(mainFields, fields...)
}
hostName := os.Getenv(EnvHostName)
if hostName != "" {
newFields := []Field{
{
Title: os.Getenv("SITE_TITLE"),
Value: os.Getenv(EnvSiteName),
Short: true,
},
{
Title: os.Getenv("HOST_TITLE"),
Value: os.Getenv(EnvHostName),
Short: true,
},
}
fields = append(newFields, fields...)
}
color := ""
switch os.Getenv(EnvSlackColor) {
case "success":
color = "good"
case "cancelled":
color = "#808080"
case "failure":
color = "danger"
default:
color = envOr(EnvSlackColor, "good")
}
msg := Webhook{
UserName: os.Getenv(EnvSlackUserName),
IconURL: os.Getenv(EnvSlackIcon),
IconEmoji: os.Getenv(EnvSlackIconEmoji),
Channel: os.Getenv(EnvSlackChannel),
LinkNames: os.Getenv(EnvSlackLinkNames),
Attachments: []Attachment{
{
Fallback: envOr(EnvSlackMessage, "GITHUB_ACTION="+os.Getenv("GITHUB_ACTION")+" \n GITHUB_ACTOR="+os.Getenv("GITHUB_ACTOR")+" \n GITHUB_EVENT_NAME="+os.Getenv("GITHUB_EVENT_NAME")+" \n GITHUB_REF="+os.Getenv("GITHUB_REF")+" \n GITHUB_REPOSITORY="+os.Getenv("GITHUB_REPOSITORY")+" \n GITHUB_WORKFLOW="+os.Getenv("GITHUB_WORKFLOW")),
Color: color,
AuthorName: envOr(EnvGithubActor, ""),
AuthorLink: os.Getenv("GITHUB_SERVER_URL") + "/" + os.Getenv(EnvGithubActor),
AuthorIcon: os.Getenv("GITHUB_SERVER_URL") + "/" + os.Getenv(EnvGithubActor) + ".png?size=32",
Footer: envOr(EnvSlackFooter, ""),
Fields: fields,
},
},
}
if err := send(endpoint, msg); err != nil {
_, err := fmt.Fprintf(os.Stderr, "Error sending message: %s\n", err)
if err != nil {
return
}
os.Exit(2)
}
}
func envOr(name, def string) string {
if d, ok := os.LookupEnv(name); ok {
return d
}
return def
}
func send(endpoint string, msg Webhook) error {
enc, err := json.Marshal(msg)
if err != nil {
return err
}
b := bytes.NewBuffer(enc)
res, err := http.Post(endpoint, "application/json", b)
if err != nil {
return err
}
if res.StatusCode >= 299 {
return fmt.Errorf("Error on message: %s\n", res.Status)
}
fmt.Println(res.Status)
return nil
}