-
Notifications
You must be signed in to change notification settings - Fork 9
/
plugin.go
214 lines (184 loc) · 5.07 KB
/
plugin.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
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
const (
respFormat = "Webhook %d\n URL: %s\n RESPONSE STATUS: %s\n RESPONSE BODY: %s\n"
debugRespFormat = "Webhook %d\n URL: %s\n METHOD: %s\n HEADERS: %s\n REQUEST BODY: %s\n RESPONSE STATUS: %s\n RESPONSE BODY: %s\n"
)
type (
Repo struct {
Owner string `json:"owner"`
Name string `json:"name"`
}
Build struct {
Tag string `json:"tag"`
Event string `json:"event"`
Number int `json:"number"`
Commit string `json:"commit"`
Ref string `json:"ref"`
Branch string `json:"branch"`
Author string `json:"author"`
Message string `json:"message"`
Status string `json:"status"`
Link string `json:"link"`
Started int64 `json:"started"`
Created int64 `json:"created"`
}
Config struct {
Method string
CorpID string
CorpSecret string
Agentid int `json:"agentid"`
MsgType string `json:"msgtype"`
URL string
MsgURL string
BtnTxt string
ToUser string `json:"touser"`
ToParty string `json:"toparty"`
ToTag string `json:"totag"`
Safe int `json:"safe"`
ContentType string
Debug bool
SkipVerify bool
Title string `json:"title"`
Description string `json:"description"`
}
Response struct {
Errcode int `json:"errcode"`
Errmsg string `json:"errmsg"`
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
}
Job struct {
Started int64 `json:"started"`
}
Plugin struct {
Repo Repo
Build Build
Config Config
Response Response
Job Job
}
)
func getAccessToken(body []byte) (*Response, error) {
var s = new(Response)
err := json.Unmarshal(body, &s)
if err != nil {
fmt.Println("whoops:", err)
}
return s, err
}
func (p Plugin) Exec() error {
var buf bytes.Buffer
var b []byte
// construct URL to get access token
accessURL := "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + p.Config.CorpID + "&corpsecret=" + p.Config.CorpSecret
fmt.Println("URL:>", accessURL)
req, err := http.NewRequest("GET", accessURL, bytes.NewBuffer(b))
var client = http.DefaultClient
if p.Config.SkipVerify {
client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
}
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Error: Failed to execute the HTTP request. %s\n", err)
return err
}
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", string(body))
s, err := getAccessToken(body)
// POST Request to WeChat work
if p.Config.Title == "" {
data := struct {
Repo Repo `json:"repo"`
Build Build `json:"build"`
}{p.Repo, p.Build}
if err := json.NewEncoder(&buf).Encode(&data); err != nil {
fmt.Printf("Error: Failed to encode JSON payload. %s\n", err)
return err
}
b = buf.Bytes()
} else {
textCard := struct {
Title string `json:"title"`
Description string `json:"description"`
MsgURL string `json:"url"`
BtnTxt string `json:"btntext"`
}{p.Config.Title, p.Config.Description, p.Config.MsgURL, p.Config.BtnTxt}
data := struct {
ToUser string `json:"touser"`
ToParty string `json:"toparty"`
ToTag string `json:"totag"`
MsgType string `json:"msgtype"`
Agentid int `json:"agentid"`
Safe int `json:"safe"`
TextCard struct {
Title string `json:"title"`
Description string `json:"description"`
MsgURL string `json:"url"`
BtnTxt string `json:"btntext"`
} `json:"textcard"`
}{p.Config.ToUser, p.Config.ToParty, p.Config.ToTag, p.Config.MsgType, p.Config.Agentid, p.Config.Safe, textCard}
b, _ = json.Marshal(data) // []byte(data)
}
// POST URL
url := "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + s.AccessToken
request, err := http.NewRequest("POST", url, bytes.NewBuffer(b))
request.Header.Set("Content-Type", "application/json")
client = http.DefaultClient
if p.Config.SkipVerify {
client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
}
response, err := client.Do(request)
if err != nil {
fmt.Printf("Error: Failed to execute the HTTP request. %s\n", err)
return err
}
defer response.Body.Close()
fmt.Println("response Status:", response.Status)
fmt.Println("response Headers:", response.Header)
responseBody, _ := ioutil.ReadAll(response.Body)
fmt.Println("response Body:", string(responseBody))
if p.Config.Debug || resp.StatusCode >= http.StatusBadRequest {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Error: Failed to read the HTTP response body. %s\n", err)
}
if p.Config.Debug {
fmt.Printf(
debugRespFormat,
req.URL,
req.Method,
req.Header,
string(b),
resp.Status,
string(body),
)
} else {
fmt.Printf(
respFormat,
req.URL,
resp.Status,
string(body),
)
}
}
return nil
}