Skip to content

Commit 35e6aff

Browse files
committed
Add the slack notification binary in the scripts/ folder
1 parent f1f85ff commit 35e6aff

File tree

1 file changed

+246
-0
lines changed

1 file changed

+246
-0
lines changed

scripts/slack_notification_action.go

+246
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
// Code taken from https://github.com/rtCamp/action-slack-notify
2+
// at revision c753c784. MIT Licensed, Copyright 2019
3+
4+
package main
5+
6+
import (
7+
"bytes"
8+
"encoding/json"
9+
"fmt"
10+
"net/http"
11+
"os"
12+
"strings"
13+
)
14+
15+
const (
16+
EnvSlackWebhook = "SLACK_WEBHOOK"
17+
EnvSlackIcon = "SLACK_ICON"
18+
EnvSlackIconEmoji = "SLACK_ICON_EMOJI"
19+
EnvSlackChannel = "SLACK_CHANNEL"
20+
EnvSlackTitle = "SLACK_TITLE"
21+
EnvSlackMessage = "SLACK_MESSAGE"
22+
EnvSlackColor = "SLACK_COLOR"
23+
EnvSlackUserName = "SLACK_USERNAME"
24+
EnvSlackFooter = "SLACK_FOOTER"
25+
EnvGithubActor = "GITHUB_ACTOR"
26+
EnvSiteName = "SITE_NAME"
27+
EnvHostName = "HOST_NAME"
28+
EnvMinimal = "MSG_MINIMAL"
29+
EnvSlackLinkNames = "SLACK_LINK_NAMES"
30+
)
31+
32+
type Webhook struct {
33+
Text string `json:"text,omitempty"`
34+
UserName string `json:"username,omitempty"`
35+
IconURL string `json:"icon_url,omitempty"`
36+
IconEmoji string `json:"icon_emoji,omitempty"`
37+
Channel string `json:"channel,omitempty"`
38+
LinkNames string `json:"link_names,omitempty"`
39+
UnfurlLinks bool `json:"unfurl_links"`
40+
Attachments []Attachment `json:"attachments,omitempty"`
41+
}
42+
43+
type Attachment struct {
44+
Fallback string `json:"fallback"`
45+
Pretext string `json:"pretext,omitempty"`
46+
Color string `json:"color,omitempty"`
47+
AuthorName string `json:"author_name,omitempty"`
48+
AuthorLink string `json:"author_link,omitempty"`
49+
AuthorIcon string `json:"author_icon,omitempty"`
50+
Footer string `json:"footer,omitempty"`
51+
Fields []Field `json:"fields,omitempty"`
52+
}
53+
54+
type Field struct {
55+
Title string `json:"title,omitempty"`
56+
Value string `json:"value,omitempty"`
57+
Short bool `json:"short,omitempty"`
58+
}
59+
60+
func main() {
61+
endpoint := os.Getenv(EnvSlackWebhook)
62+
if endpoint == "" {
63+
fmt.Fprintln(os.Stderr, "URL is required")
64+
os.Exit(1)
65+
}
66+
text := os.Getenv(EnvSlackMessage)
67+
if text == "" {
68+
fmt.Fprintln(os.Stderr, "Message is required")
69+
os.Exit(1)
70+
}
71+
if strings.HasPrefix(os.Getenv("GITHUB_WORKFLOW"), ".github") {
72+
os.Setenv("GITHUB_WORKFLOW", "Link to action run")
73+
}
74+
75+
long_sha := os.Getenv("GITHUB_SHA")
76+
commit_sha := long_sha[0:6]
77+
78+
minimal := os.Getenv(EnvMinimal)
79+
fields := []Field{}
80+
if minimal == "true" {
81+
mainFields := []Field{
82+
{
83+
Title: os.Getenv(EnvSlackTitle),
84+
Value: envOr(EnvSlackMessage, "EOM"),
85+
Short: false,
86+
},
87+
}
88+
fields = append(mainFields, fields...)
89+
} else if minimal != "" {
90+
requiredFields := strings.Split(minimal, ",")
91+
mainFields := []Field{
92+
{
93+
Title: os.Getenv(EnvSlackTitle),
94+
Value: envOr(EnvSlackMessage, "EOM"),
95+
Short: false,
96+
},
97+
}
98+
for _, requiredField := range requiredFields {
99+
switch strings.ToLower(requiredField) {
100+
case "ref":
101+
field := []Field{
102+
{
103+
Title: "Ref",
104+
Value: os.Getenv("GITHUB_REF"),
105+
Short: true,
106+
},
107+
}
108+
mainFields = append(field, mainFields...)
109+
case "event":
110+
field := []Field{
111+
{
112+
Title: "Event",
113+
Value: os.Getenv("GITHUB_EVENT_NAME"),
114+
Short: true,
115+
},
116+
}
117+
mainFields = append(field, mainFields...)
118+
case "actions url":
119+
field := []Field{
120+
{
121+
Title: "Actions URL",
122+
Value: "<" + os.Getenv("GITHUB_SERVER_URL") + "/" + os.Getenv("GITHUB_REPOSITORY") + "/commit/" + os.Getenv("GITHUB_SHA") + "/checks|" + os.Getenv("GITHUB_WORKFLOW") + ">",
123+
Short: true,
124+
},
125+
}
126+
mainFields = append(field, mainFields...)
127+
case "commit":
128+
field := []Field{
129+
{
130+
Title: "Commit",
131+
Value: "<" + os.Getenv("GITHUB_SERVER_URL") + "/" + os.Getenv("GITHUB_REPOSITORY") + "/commit/" + os.Getenv("GITHUB_SHA") + "|" + commit_sha + ">",
132+
Short: true,
133+
},
134+
}
135+
mainFields = append(field, mainFields...)
136+
}
137+
}
138+
fields = append(mainFields, fields...)
139+
} else {
140+
mainFields := []Field{
141+
{
142+
Title: "Ref",
143+
Value: os.Getenv("GITHUB_REF"),
144+
Short: true,
145+
}, {
146+
Title: "Event",
147+
Value: os.Getenv("GITHUB_EVENT_NAME"),
148+
Short: true,
149+
},
150+
{
151+
Title: "Actions URL",
152+
Value: "<" + os.Getenv("GITHUB_SERVER_URL") + "/" + os.Getenv("GITHUB_REPOSITORY") + "/commit/" + os.Getenv("GITHUB_SHA") + "/checks|" + os.Getenv("GITHUB_WORKFLOW") + ">",
153+
Short: true,
154+
},
155+
{
156+
Title: "Commit",
157+
Value: "<" + os.Getenv("GITHUB_SERVER_URL") + "/" + os.Getenv("GITHUB_REPOSITORY") + "/commit/" + os.Getenv("GITHUB_SHA") + "|" + commit_sha + ">",
158+
Short: true,
159+
},
160+
{
161+
Title: os.Getenv(EnvSlackTitle),
162+
Value: envOr(EnvSlackMessage, "EOM"),
163+
Short: false,
164+
},
165+
}
166+
fields = append(mainFields, fields...)
167+
}
168+
169+
hostName := os.Getenv(EnvHostName)
170+
if hostName != "" {
171+
newfields := []Field{
172+
{
173+
Title: os.Getenv("SITE_TITLE"),
174+
Value: os.Getenv(EnvSiteName),
175+
Short: true,
176+
},
177+
{
178+
Title: os.Getenv("HOST_TITLE"),
179+
Value: os.Getenv(EnvHostName),
180+
Short: true,
181+
},
182+
}
183+
fields = append(newfields, fields...)
184+
}
185+
186+
color := ""
187+
switch os.Getenv(EnvSlackColor) {
188+
case "success":
189+
color = "good"
190+
case "cancelled":
191+
color = "#808080"
192+
case "failure":
193+
color = "danger"
194+
default:
195+
color = envOr(EnvSlackColor, "good")
196+
}
197+
198+
msg := Webhook{
199+
UserName: os.Getenv(EnvSlackUserName),
200+
IconURL: os.Getenv(EnvSlackIcon),
201+
IconEmoji: os.Getenv(EnvSlackIconEmoji),
202+
Channel: os.Getenv(EnvSlackChannel),
203+
LinkNames: os.Getenv(EnvSlackLinkNames),
204+
Attachments: []Attachment{
205+
{
206+
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")),
207+
Color: color,
208+
AuthorName: envOr(EnvGithubActor, ""),
209+
AuthorLink: os.Getenv("GITHUB_SERVER_URL") + "/" + os.Getenv(EnvGithubActor),
210+
AuthorIcon: os.Getenv("GITHUB_SERVER_URL") + "/" + os.Getenv(EnvGithubActor) + ".png?size=32",
211+
Footer: envOr(EnvSlackFooter, "<https://github.com/rtCamp/github-actions-library|Powered By rtCamp's GitHub Actions Library>"),
212+
Fields: fields,
213+
},
214+
},
215+
}
216+
217+
if err := send(endpoint, msg); err != nil {
218+
fmt.Fprintf(os.Stderr, "Error sending message: %s\n", err)
219+
os.Exit(2)
220+
}
221+
}
222+
223+
func envOr(name, def string) string {
224+
if d, ok := os.LookupEnv(name); ok {
225+
return d
226+
}
227+
return def
228+
}
229+
230+
func send(endpoint string, msg Webhook) error {
231+
enc, err := json.Marshal(msg)
232+
if err != nil {
233+
return err
234+
}
235+
b := bytes.NewBuffer(enc)
236+
res, err := http.Post(endpoint, "application/json", b)
237+
if err != nil {
238+
return err
239+
}
240+
241+
if res.StatusCode >= 299 {
242+
return fmt.Errorf("Error on message: %s\n", res.Status)
243+
}
244+
fmt.Println(res.Status)
245+
return nil
246+
}

0 commit comments

Comments
 (0)