-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
200 lines (177 loc) · 3.79 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
package main
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"text/template"
"github.com/certifi/gocertifi"
)
// Message interface of message
type Message interface {
Send()
}
// WebHook webhook address
type WebHook struct {
Address string
}
// set actions output
func setOutput(output string) {
fmt.Printf(`::set-output name=response::%s\n`, output)
}
// post to feishu
func (webhook *WebHook) post(body interface{}) {
buf, err := json.Marshal(body)
if err != nil {
setOutput(err.Error())
return
}
var caCerts *x509.CertPool
if rootCAS, err := gocertifi.CACerts(); err == nil {
caCerts = rootCAS
} else {
setOutput("Couldn't load CA Certificates")
return
}
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: caCerts,
},
},
}
resp, err := client.Post(webhook.Address, "application/json", bytes.NewBuffer(buf))
if resp != nil {
defer func() {
if err := resp.Body.Close(); err != nil {
setOutput("close body error")
}
}()
}
if err != nil {
setOutput(err.Error())
return
}
outout, err := ioutil.ReadAll(resp.Body)
if err != nil {
setOutput(fmt.Sprintf("read response error: %s", err.Error()))
return
}
setOutput(string(outout))
}
// TextMessage 文本类型消息
type TextMessage struct {
WebHook
Text string
}
// Send implement Message
func (m *TextMessage) Send() {
body := map[string]interface{}{
"msg_type": "text",
"content": map[string]interface{}{
"text": m.Text,
},
}
m.post(body)
}
// PostMessage Post 类型消息
type PostMessage struct {
WebHook
Title string
Content string
}
// Send implement Message
func (m *PostMessage) Send() {
body := map[string]interface{}{
"msg_type": "post",
"content": map[string]interface{}{
"post": map[string]interface{}{
"zh_cn": map[string]interface{}{
"title": m.Title,
"content": [][]map[string]interface{}{
{{
"tag": "text",
"text": m.Content,
},
},
},
},
},
},
}
m.post(body)
}
// TemplateMessage 用于构造并处理处理模版请求消息的类型,不属于飞书定义的任何消息类型
type TemplateMessage struct {
WebHook
TemplatePath string
TemplateValues map[string]interface{}
}
// Send implement Message
func (m *TemplateMessage) Send() {
tmpl, err := template.ParseFiles(m.TemplatePath)
if err != nil {
log.Fatal(err)
}
var rawData bytes.Buffer
err = tmpl.Execute(&rawData, m.TemplateValues)
if err != nil {
log.Fatal(err)
}
var body map[string]interface{}
err = json.Unmarshal(rawData.Bytes(), &body)
if err != nil {
log.Fatal(err)
}
m.post(body)
}
type noopMessage struct{}
func (m *noopMessage) Send() {}
// NoopMessage no-op message
var NoopMessage = &noopMessage{}
func parseInput() Message {
webhook := os.Getenv("INPUT_WEBHOOK")
if webhook == "" {
setOutput("webhook required")
return NoopMessage
}
w := WebHook{Address: webhook}
msgType := os.Getenv("INPUT_MESSAGE_TYPE")
switch msgType {
case "post":
return &PostMessage{
WebHook: w,
Title: os.Getenv("INPUT_TITLE"),
Content: os.Getenv("INPUT_CONTENT"),
}
case "text":
return &TextMessage{
WebHook: w,
Text: os.Getenv("INPUT_CONTENT"),
}
case "template":
rawTemplateValues := os.Getenv("INPUT_MSG_TEMPLATE_VALUES")
var templateValues map[string]interface{}
err := json.Unmarshal([]byte(rawTemplateValues), &templateValues)
if err != nil {
log.Fatalf("hint: msg_template_values must be a valid JSON string\n\nReason: %s", err.Error())
}
templatePath := os.Getenv("INPUT_MSG_TEMPLATE_PATH")
return &TemplateMessage{
WebHook: w,
TemplatePath: templatePath,
TemplateValues: templateValues,
}
default:
return NoopMessage
}
}
func main() {
msg := parseInput()
msg.Send()
}