-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmain.go
297 lines (288 loc) · 7.96 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
package main
import (
"fmt"
"log"
"os"
"time"
"github.com/joho/godotenv"
"github.com/urfave/cli"
)
// Version set at compile-time
var Version string
func main() {
// Load env-file if it exists first
if filename, found := os.LookupEnv("PLUGIN_ENV_FILE"); found {
_ = godotenv.Load(filename)
}
if _, err := os.Stat("/run/drone/env"); err == nil {
_ = godotenv.Overload("/run/drone/env")
}
year := fmt.Sprintf("%v", time.Now().Year())
app := cli.NewApp()
app.Name = "Drone Discord"
app.Usage = "Sending message to Discord channel using Webhook"
app.Copyright = "Copyright (c) " + year + " Bo-Yi Wu"
app.Authors = []cli.Author{
{
Name: "Bo-Yi Wu",
Email: "appleboy.tw@gmail.com",
},
}
app.Action = run
app.Version = Version
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "webhook-id",
Usage: "discord webhook id",
EnvVar: "PLUGIN_WEBHOOK_ID,WEBHOOK_ID,DISCORD_WEBHOOK_ID,INPUT_WEBHOOK_ID",
},
cli.StringFlag{
Name: "webhook-token",
Usage: "discord webhook token",
EnvVar: "PLUGIN_WEBHOOK_TOKEN,WEBHOOK_TOKEN,DISCORD_WEBHOOK_TOKEN,INPUT_WEBHOOK_TOKEN",
},
cli.StringSliceFlag{
Name: "message",
Usage: "the message contents (up to 2000 characters)",
EnvVar: "PLUGIN_MESSAGE,DISCORD_MESSAGE,MESSAGE,INPUT_MESSAGE",
},
cli.StringSliceFlag{
Name: "file",
Usage: "the contents of the file being sent",
EnvVar: "PLUGIN_FILE,DISCORD_FILE,FILE,INPUT_FILE",
},
cli.StringFlag{
Name: "color",
Usage: "color code of the embed",
EnvVar: "PLUGIN_COLOR,COLOR,INPUT_COLOR",
},
cli.BoolFlag{
Name: "wait",
Usage: "waits for server confirmation of message send before response, and returns the created message body",
EnvVar: "PLUGIN_WAIT,WAIT,INPUT_WAIT",
},
cli.BoolFlag{
Name: "tts",
Usage: "true if this is a TTS message",
EnvVar: "PLUGIN_TTS,TTS,INPUT_TTS",
},
cli.StringFlag{
Name: "username",
Usage: "override the default username of the webhook",
EnvVar: "PLUGIN_USERNAME,USERNAME,INPUT_USERNAME",
},
cli.StringFlag{
Name: "avatar-url",
Usage: "override the default avatar of the webhook",
EnvVar: "PLUGIN_AVATAR_URL,AVATAR_URL,INPUT_AVATAR_URL",
},
cli.BoolFlag{
Name: "drone",
Usage: "environment is drone",
EnvVar: "DRONE",
},
cli.StringFlag{
Name: "ci.environment",
Usage: "ci environment name",
EnvVar: "CI",
},
cli.StringFlag{
Name: "repo",
Usage: "repository owner and repository name",
EnvVar: "DRONE_REPO,CI_REPO,GITHUB_REPOSITORY",
},
cli.StringFlag{
Name: "repo.namespace",
Usage: "repository namespace",
EnvVar: "DRONE_REPO_OWNER,DRONE_REPO_NAMESPACE,CI_REPO_OWNER,GITHUB_ACTOR",
},
cli.StringFlag{
Name: "repo.name",
Usage: "repository name",
EnvVar: "DRONE_REPO_NAME,CI_REPO_NAME",
},
cli.StringFlag{
Name: "commit.sha",
Usage: "git commit sha",
EnvVar: "DRONE_COMMIT_SHA,CI_COMMIT_SHA,GITHUB_SHA",
},
cli.StringFlag{
Name: "commit.ref",
Usage: "git commit ref",
EnvVar: "DRONE_COMMIT_REF,CI_COMMIT_REF,GITHUB_REF",
},
cli.StringFlag{
Name: "commit.branch",
Value: "master",
Usage: "git commit branch",
EnvVar: "DRONE_COMMIT_BRANCH,CI_COMMIT_BRANCH",
},
cli.StringFlag{
Name: "commit.link",
Usage: "git commit link",
EnvVar: "DRONE_COMMIT_LINK,CI_PIPELINE_FORGE_URL",
},
cli.StringFlag{
Name: "commit.author",
Usage: "git author name",
EnvVar: "DRONE_COMMIT_AUTHOR,CI_COMMIT_AUTHOR",
},
cli.StringFlag{
Name: "commit.author.email",
Usage: "git author email",
EnvVar: "DRONE_COMMIT_AUTHOR_EMAIL,CI_COMMIT_AUTHOR_EMAIL",
},
cli.StringFlag{
Name: "commit.author.avatar",
Usage: "git author avatar",
EnvVar: "DRONE_COMMIT_AUTHOR_AVATAR,CI_COMMIT_AUTHOR_AVATAR",
},
cli.StringFlag{
Name: "commit.message",
Usage: "commit message",
EnvVar: "DRONE_COMMIT_MESSAGE,CI_COMMIT_MESSAGE",
},
cli.StringFlag{
Name: "source.branch",
Value: "develop",
Usage: "git source branch",
EnvVar: "DRONE_SOURCE_BRANCH,CI_COMMIT_SOURCE_BRANCH",
},
cli.StringFlag{
Name: "build.event",
Value: "push",
Usage: "build event",
EnvVar: "DRONE_BUILD_EVENT,CI_PIPELINE_EVENT",
},
cli.IntFlag{
Name: "build.number",
Usage: "build number",
EnvVar: "DRONE_BUILD_NUMBER,CI_PIPELINE_NUMBER",
},
cli.StringFlag{
Name: "build.status",
Usage: "build status",
Value: "success",
EnvVar: "DRONE_BUILD_STATUS,CI_PIPELINE_STATUS",
},
cli.StringFlag{
Name: "build.link",
Usage: "build link",
EnvVar: "DRONE_BUILD_LINK,CI_PIPELINE_URL",
},
cli.StringFlag{
Name: "build.tag",
Usage: "build tag",
EnvVar: "DRONE_TAG,CI_COMMIT_TAG",
},
cli.StringFlag{
Name: "pull.request",
Usage: "pull request",
EnvVar: "DRONE_PULL_REQUEST,CI_COMMIT_PULL_REQUEST",
},
cli.Int64Flag{
Name: "build.started",
Usage: "build started",
EnvVar: "DRONE_BUILD_STARTED,CI_PIPELINE_STARTED",
},
cli.Int64Flag{
Name: "build.finished",
Usage: "build finished",
EnvVar: "DRONE_BUILD_FINISHED,CI_PIPELINE_FINISHED",
},
cli.BoolFlag{
Name: "github",
Usage: "Boolean value, indicates the runtime environment is GitHub Action.",
EnvVar: "PLUGIN_GITHUB,GITHUB",
},
cli.StringFlag{
Name: "github.workflow",
Usage: "The name of the workflow.",
EnvVar: "GITHUB_WORKFLOW",
},
cli.StringFlag{
Name: "github.action",
Usage: "The name of the action.",
EnvVar: "GITHUB_ACTION",
},
cli.StringFlag{
Name: "github.event.name",
Usage: "The webhook name of the event that triggered the workflow.",
EnvVar: "GITHUB_EVENT_NAME",
},
cli.StringFlag{
Name: "github.event.path",
Usage: "The path to a file that contains the payload of the event that triggered the workflow. Value: /github/workflow/event.json.",
EnvVar: "GITHUB_EVENT_PATH",
},
cli.StringFlag{
Name: "github.workspace",
Usage: "The GitHub workspace path. Value: /github/workspace.",
EnvVar: "GITHUB_WORKSPACE",
},
cli.StringFlag{
Name: "deploy.to",
Usage: "Provides the target deployment environment for the running build. This value is only available to promotion and rollback pipelines.",
EnvVar: "DRONE_DEPLOY_TO,CI_PIPELINE_DEPLOY_TARGET",
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func run(c *cli.Context) error {
plugin := Plugin{
GitHub: GitHub{
Workflow: c.String("github.workflow"),
Workspace: c.String("github.workspace"),
Action: c.String("github.action"),
EventName: c.String("github.event.name"),
EventPath: c.String("github.event.path"),
},
Repo: Repo{
FullName: c.String("repo"),
Namespace: c.String("repo.namespace"),
Name: c.String("repo.name"),
},
Commit: Commit{
Sha: c.String("commit.sha"),
Ref: c.String("commit.ref"),
Branch: c.String("commit.branch"),
Link: c.String("commit.link"),
Author: c.String("commit.author"),
Email: c.String("commit.author.email"),
Avatar: c.String("commit.author.avatar"),
Message: c.String("commit.message"),
},
Source: Source{
Branch: c.String("source.branch"),
},
Build: Build{
Tag: c.String("build.tag"),
Number: c.Int("build.number"),
Event: c.String("build.event"),
Status: c.String("build.status"),
Link: c.String("build.link"),
Started: c.Int64("build.started"),
Finished: c.Int64("build.finished"),
PR: c.String("pull.request"),
DeployTo: c.String("deploy.to"),
},
Config: Config{
WebhookID: c.String("webhook-id"),
WebhookToken: c.String("webhook-token"),
Message: c.StringSlice("message"),
File: c.StringSlice("file"),
Color: c.String("color"),
Drone: c.Bool("drone") || c.String("ci.environment") == "woodpecker",
GitHub: c.Bool("github"),
},
Payload: Payload{
Wait: c.Bool("wait"),
Username: c.String("username"),
AvatarURL: c.String("avatar-url"),
TTS: c.Bool("tts"),
},
}
return plugin.Exec()
}