forked from rtCamp/action-slack-notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (100 loc) · 2.07 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
)
const (
EnvSlackChannel = "SLACK_CHANNEL"
EnvSlackFooter = "SLACK_FOOTER"
EnvSlackMessage = "SLACK_MESSAGE"
EnvSlackWebhook = "SLACK_WEBHOOK"
)
type Webhook struct {
AsUser bool `json:"as_user,omitempty"`
Blocks []Block `json:"blocks,omitempty"`
Channel string `json:"channel,omitempty"`
Text string `json:"text,omitempty"`
}
type Block struct {
Elements *[]Text `json:"elements,omitempty"`
Text *Text `json:"text,omitempty"`
Type string `json:"type"`
}
type Text struct {
Type string `json:"type,omitempty"`
Text string `json:"text,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)
}
footer := os.Getenv(EnvSlackFooter)
if strings.HasPrefix(os.Getenv("GITHUB_WORKFLOW"), ".github") {
_ = os.Setenv("GITHUB_WORKFLOW", "Link to action run")
}
blocks := []Block{
{
Text: &Text{
Text: text,
Type: "mrkdwn",
},
Type: "section",
},
}
if footer != "" {
blocks = append(blocks,
Block{
Text: nil,
Type: "divider",
},
Block{
Elements: &[]Text{
{
Text: footer,
Type: "mrkdwn",
},
},
Text: nil,
Type: "context",
},
)
}
msg := Webhook{
AsUser: false,
Blocks: blocks,
Channel: os.Getenv(EnvSlackChannel),
Text: text,
}
if err := send(endpoint, msg); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Error sending message: %s\n", err)
os.Exit(2)
}
}
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 {
data, _ := json.MarshalIndent(msg, "", "\t")
return fmt.Errorf("Error on message: %s, `%s`\n", res.Status, data)
}
fmt.Println(res.Status)
return nil
}