-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
310 lines (304 loc) · 8.21 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package main
import (
"fmt"
_ "github.com/joho/godotenv/autoload"
"github.com/urfave/cli"
"log"
"os"
)
var Version = "0.1.1"
func main() {
app := cli.NewApp()
app.Name = "Drone Dingtalk Message Plugin"
app.Usage = "Sending message to Dingtalk group by robot using webhook"
app.Action = run
app.Version = Version
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "config.debug",
Usage: "debug mode",
EnvVar: "PLUGIN_DEBUG",
},
cli.StringFlag{
Name: "config.token,access_token,token",
Usage: "dingtalk webhook access token",
EnvVar: "PLUGIN_ACCESS_TOKEN,PLUGIN_TOKEN",
},
cli.StringFlag{
Name: "config.secret,access_secret,secret",
Usage: "dingtalk webhook secret",
EnvVar: "PLUGIN_ACCESS_SECRET,PLUGIN_SECRET",
},
cli.StringFlag{
Name: "config.lang",
Value: "zh_CN",
Usage: "the lang display (zh_CN or en_US, zh_CN is default)",
EnvVar: "PLUGIN_LANG",
},
cli.StringFlag{
Name: "config.message.type,message_type",
Usage: "dingtalk message type, like text, markdown, action card, link and feed card...",
EnvVar: "PLUGIN_MSG_TYPE,PLUGIN_TYPE,PLUGIN_MESSAGE_TYPE",
},
cli.StringFlag{
Name: "config.message.at.all",
Usage: "at all in a message(only text and markdown type message can at)",
EnvVar: "PLUGIN_MSG_AT_ALL",
},
cli.StringFlag{
Name: "config.message.at.mobiles",
Usage: "at someone in a dingtalk group need this guy bind's mobile",
EnvVar: "PLUGIN_MSG_AT_MOBILES",
},
cli.StringFlag{
Name: "commit.author.avatar",
Usage: "providers the author avatar url for the current commit",
EnvVar: "DRONE_COMMIT_AUTHOR_AVATAR",
},
cli.StringFlag{
Name: "commit.author.email",
Usage: "providers the author email for the current commit",
EnvVar: "DRONE_COMMIT_AUTHOR_EMAIL",
},
cli.StringFlag{
Name: "commit.author.name",
Usage: "providers the author name for the current commit",
EnvVar: "DRONE_COMMIT_AUTHOR",
},
cli.StringFlag{
Name: "commit.branch",
Usage: "providers the branch for the current build",
EnvVar: "DRONE_COMMIT_BRANCH",
Value: "master",
},
cli.StringFlag{
Name: "commit.link",
Usage: "providers the http link to the current commit in the remote source code management system(e.g.GitHub)",
EnvVar: "DRONE_COMMIT_LINK",
},
cli.StringFlag{
Name: "commit.message",
Usage: "providers the commit message for the current build",
EnvVar: "DRONE_COMMIT_MESSAGE",
},
cli.StringFlag{
Name: "commit.sha",
Usage: "providers the commit sha for the current build",
EnvVar: "DRONE_COMMIT_SHA",
},
cli.StringFlag{
Name: "repo.fullname",
Usage: "providers the full name of the repository",
EnvVar: "DRONE_REPO",
},
// DRONE_TAG
cli.StringFlag{
Name: "repo.tag",
Usage: "providers the tag of the repository",
EnvVar: "DRONE_TAG",
},
cli.StringFlag{
Name: "build.status",
Usage: "build status",
Value: "success",
EnvVar: "DRONE_BUILD_STATUS",
},
cli.StringFlag{
Name: "build.link",
Usage: "build link",
EnvVar: "DRONE_BUILD_LINK",
},
cli.StringFlag{
Name: "build.port",
Usage: "drone build port",
EnvVar: "PLUGIN_DRONE_PORT",
},
cli.StringFlag{
Name: "plugin.build.reponamespace",
Usage: "build step docker repository",
EnvVar: "PLUGIN_REPO_NAMESPACE",
},
cli.StringFlag{
Name: "plugin.build.imagename",
Usage: "build step docker repository",
EnvVar: "PLUGIN_IMAGE_NAME",
},
cli.StringFlag{
Name: "config.success.pic.url",
Usage: "config success picture url",
EnvVar: "SUCCESS_PICTURE_URL,PLUGIN_SUCCESS_PIC",
},
cli.StringFlag{
Name: "config.failure.pic.url",
Usage: "config failure picture url",
EnvVar: "FAILURE_PICTURE_URL,PLUGIN_FAILURE_PIC",
},
cli.StringFlag{
Name: "config.success.color",
Usage: "config success color for title in markdown",
EnvVar: "SUCCESS_COLOR,PLUGIN_SUCCESS_COLOR",
},
cli.StringFlag{
Name: "config.failure.color",
Usage: "config failure color for title in markdown",
EnvVar: "FAILURE_COLOR,PLUGIN_FAILURE_COLOR",
},
cli.BoolFlag{
Name: "config.message.color",
Usage: "configure the message with color or not",
EnvVar: "PLUGIN_COLOR,PLUGIN_MESSAGE_COLOR",
},
cli.BoolFlag{
Name: "config.message.pic",
Usage: "configure the message with picture or not",
EnvVar: "PLUGIN_PIC,PLUGIN_MESSAGE_PIC",
},
cli.BoolFlag{
Name: "config.message.sha.link",
Usage: "link sha source page or not",
EnvVar: "PLUGIN_SHA_LINK,PLUGIN_MESSAGE_SHA_LINK",
},
cli.BoolFlag{
Name: "config.db.log",
Usage: "write db log",
EnvVar: "PLUGIN_DB_LOG",
},
cli.StringFlag{
Name: "config.db.type",
Usage: " db type",
EnvVar: "PLUGIN_DB_TYPE",
},
cli.StringFlag{
Name: "config.db.name",
Usage: " db name",
EnvVar: "PLUGIN_DB_NAME",
},
cli.StringFlag{
Name: "config.db.host",
Usage: " db host",
EnvVar: "PLUGIN_DB_HOST",
}, cli.Int64Flag{
Name: "config.db.port",
Usage: " db port",
EnvVar: "PLUGIN_DB_PORT",
},
cli.StringFlag{
Name: "config.db.username",
Usage: " db name",
EnvVar: "PLUGIN_DB_USERNAME",
},
cli.StringFlag{
Name: "config.db.password",
Usage: " db name",
EnvVar: "PLUGIN_DB_PASSWORD",
},
cli.StringFlag{
Name: "config.db.table",
Usage: " db table",
EnvVar: "PLUGIN_DB_TABLE",
},
cli.StringFlag{
Name: "module.name",
Usage: "git update package name",
EnvVar: "PLUGIN_MODNAME",
},
cli.Int64Flag{
Name: "build.start",
Usage: "drone build start at",
EnvVar: "DRONE_STAGE_STARTED",
},
cli.Int64Flag{
Name: "build.end",
Usage: "drone build finish at",
EnvVar: "DRONE_STAGE_FINISHED",
},
cli.StringFlag{
Name: "build.stage",
Usage: "drone build stage name",
EnvVar: "DRONE_STAGE_NAME",
},
cli.StringFlag{
Name: "build.event",
Usage: "drone build event",
EnvVar: "DRONE_BUILD_EVENT",
},
}
if err := app.Run(os.Args); nil != err {
log.Println(err)
}
}
// run with args
func run(c *cli.Context) {
plugin := Plugin{
Drone: Drone{
// repo info
Repo: Repo{
FullName: c.String("repo.fullname"),
ModName: c.String("module.name"),
},
// build info
Build: Build{
Status: c.String("build.status"),
Link: c.String("build.link"),
RepoName: c.String("plugin.build.reponamespace"),
Image: c.String("plugin.build.imagename"),
Tag: c.String("repo.tag"),
Port: c.String("build.port"),
StartAt: c.Int64("build.start"),
FinishedAt: c.Int64("build.end"),
Stage: c.String("build.stage"),
Event: c.String("build.event"),
},
Commit: Commit{
Sha: c.String("commit.sha"),
Branch: c.String("commit.branch"),
Message: c.String("commit.message"),
Link: c.String("commit.link"),
Authors: struct {
Avatar string
Email string
Name string
}{
Avatar: c.String("commit.author.avatar"),
Email: c.String("commit.author.email"),
Name: c.String("commit.author.name"),
},
},
},
// custom config
Config: Config{
AccessToken: c.String("config.token"),
Secret: c.String("config.secret"),
IsAtALL: c.Bool("config.message.at.all"),
MsgType: c.String("config.message.type"),
Mobiles: c.String("config.message.at.mobiles"),
Debug: c.Bool("config.debug"),
},
Extra: Extra{
Pic: ExtraPic{
WithPic: c.Bool("config.message.pic"),
SuccessPicURL: c.String("config.success.pic.url"),
FailurePicURL: c.String("config.failure.pic.url"),
},
Color: ExtraColor{
SuccessColor: c.String("config.success.color"),
FailureColor: c.String("config.failure.color"),
WithColor: c.Bool("config.message.color"),
},
Db: ExtraDb{
DbLog: c.Bool("config.db.log"),
DbType: c.String("config.db.type"),
DbName: c.String("config.db.name"),
DbHost: c.String("config.db.host"),
DbPort: c.Int64("config.db.port"),
DbUsername: c.String("config.db.username"),
DbPassword: c.String("config.db.password"),
DbTable: c.String("config.db.table"),
},
LinkSha: c.Bool("config.message.sha.link"),
},
}
if err := plugin.Exec(); nil != err {
fmt.Println(err)
}
}