-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin.go
170 lines (156 loc) · 3.85 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
package main
import (
"bytes"
"encoding/json"
"github.com/futuregadgetlabx/drone-feishu/consts"
"github.com/futuregadgetlabx/drone-feishu/request"
feishuTemplate "github.com/futuregadgetlabx/drone-feishu/template"
"github.com/google/uuid"
"html/template"
"io"
"log"
"net/http"
"strings"
"time"
)
type (
Repo struct {
Owner string
Name string
Url string
}
Build struct {
Tag string
Event string
Number int
Parent int
Commit string
CommitMessage string
CommitAuthor CommitAuthor
Ref string
Branch string
Pull string
PullRequestTitle string
Status string
Link string
Started int64
StartedFormatted string
Created int64
CreatedFormatted string
Finished int64
FinishedFormatted string
CostTime int64
FailedStages string
FailedSteps string
SourceBranch string
TargetBranch string
}
CommitAuthor struct {
Username string
Name string
Email string
Avatar string
}
CommitMessage struct {
msg string
Title string
Body string
}
Feishu struct {
UserID string
ChatID string
AppID string
AppSecret string
}
Plugin struct {
Repo Repo
Build Build
Feishu Feishu
}
)
var eventStatusMap = map[string]map[string]string{
"push": {
"success": feishuTemplate.PushSuccess,
"failure": feishuTemplate.PushFailure,
},
"pull_request": {
"success": feishuTemplate.PrSuccess,
"failure": feishuTemplate.PrFailure,
},
}
func buildCommitMessage(m string) string {
tmp := strings.ReplaceAll(m, "\r\n", "\\n")
return strings.ReplaceAll(tmp, "\n", "\\n")
}
func (p Plugin) Exec() {
// Get tenant access token
tokenReq := request.GetTokenReq{
AppID: p.Feishu.AppID,
AppSecret: p.Feishu.AppSecret,
}
reqBody, _ := json.Marshal(tokenReq)
resp, err := http.Post(consts.GetTenantToken, "application/json", bytes.NewBuffer(reqBody))
if err != nil {
log.Fatalf("request feishu tenant_access_token error: %v", err)
}
respBody, _ := io.ReadAll(resp.Body)
var tokenResp request.GetTokenResp
err = json.Unmarshal(respBody, &tokenResp)
if err != nil {
log.Fatalf("unmarshal data error: %v", err)
}
if tokenResp.Code != 0 {
log.Fatalf("request feishu tenant_access_token error: %v", tokenResp.Msg)
}
var originTmpl string
if statusMap, ok := eventStatusMap[p.Build.Event]; ok {
if originTmpl, ok = statusMap[p.Build.Status]; !ok {
log.Fatal("unknown status")
}
} else {
log.Fatal("unknown event")
}
p.Build.CreatedFormatted = time.Unix(p.Build.Created, 0).Format("2006-01-02 15:04:05")
p.Build.StartedFormatted = time.Unix(p.Build.Started, 0).Format("2006-01-02 15:04:05")
p.Build.FinishedFormatted = time.Unix(p.Build.Finished, 0).Format("2006-01-02 15:04:05")
p.Build.CostTime = p.Build.Finished - p.Build.Started
tmpl, err := template.New("template").Parse(originTmpl)
if err != nil {
log.Fatal(err)
}
var filledTemplate bytes.Buffer
err = tmpl.Execute(&filledTemplate, p)
if err != nil {
log.Fatal(err)
}
sendMsgReq := request.SendMsgReq{
ReceiveID: p.Feishu.UserID,
MsgType: "interactive",
Content: filledTemplate.String(),
UUID: uuid.New().String(),
}
sendMsgReqBody, _ := json.Marshal(sendMsgReq)
httpPost, err := http.NewRequest("POST", consts.SendMsg, bytes.NewBuffer(sendMsgReqBody))
if err != nil {
log.Fatal(err)
}
httpPost.Header.Set("Authorization", "Bearer "+tokenResp.TenantAccessToken)
// 发送请求
client := &http.Client{}
resp, err = client.Do(httpPost)
if err != nil {
log.Fatal(err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Fatal(err)
}
}(resp.Body)
// 读取响应的内容
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
log.Println(string(body))
}