forked from skindar/action-slack-notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
164 lines (149 loc) · 4.12 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
)
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"
EnvGithubActor = "GITHUB_ACTOR"
EnvSiteName = "SITE_NAME"
EnvHostName = "HOST_NAME"
EnvDepolyPath = "DEPLOY_PATH"
EnvCoverageURL = "COVERAGE_URL"
)
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"`
UnfurlLinks bool `json:"unfurl_links"`
Attachments []Attachment `json:"attachments,omitmepty"`
}
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 == "" {
fmt.Fprintln(os.Stderr, "URL is required")
os.Exit(1)
}
text := os.Getenv(EnvSlackMessage)
if text == "" {
fmt.Fprintln(os.Stderr, "Message is required")
os.Exit(1)
}
fields:= []Field{
{
Title: "Ref",
Value: os.Getenv("GITHUB_REF"),
Short: true,
}, {
Title: "Event",
Value: os.Getenv("GITHUB_EVENT_NAME"),
Short: true,
},
{
Title: "Actions URL",
Value: "https://github.com/" + os.Getenv("GITHUB_REPOSITORY") + "/commit/" + os.Getenv("GITHUB_SHA") + "/checks",
Short: false,
},
{
Title: os.Getenv(EnvSlackTitle),
Value: envOr(EnvSlackMessage, "EOM"),
Short: false,
},
}
coverageUrl := os.Getenv(EnvCoverageURL)
if coverageUrl != "" {
newfields:= []Field{
{
Title: "Test coverage URL",
Value: os.Getenv(EnvCoverageURL),
Short: false,
},
}
fields = append(newfields, 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...)
}
msg := Webhook{
UserName: os.Getenv(EnvSlackUserName),
IconURL: os.Getenv(EnvSlackIcon),
IconEmoji: os.Getenv(EnvSlackIconEmoji),
Channel: os.Getenv(EnvSlackChannel),
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: envOr(EnvSlackColor, "good"),
AuthorName: envOr(EnvGithubActor, ""),
AuthorLink: "http://github.com/" + os.Getenv(EnvGithubActor),
AuthorIcon: "http://github.com/" + os.Getenv(EnvGithubActor) + ".png?size=32",
Fields: fields,
},
},
}
if err := send(endpoint, msg); err != nil {
fmt.Fprintf(os.Stderr, "Error sending message: %s\n", err)
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
}