This repository has been archived by the owner on Oct 2, 2024. It is now read-only.
forked from mercari/tfnotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
326 lines (307 loc) · 8.13 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package main
import (
"fmt"
"os"
"strings"
"github.com/mercari/tfnotify/config"
"github.com/mercari/tfnotify/notifier"
"github.com/mercari/tfnotify/notifier/github"
"github.com/mercari/tfnotify/notifier/gitlab"
"github.com/mercari/tfnotify/notifier/slack"
"github.com/mercari/tfnotify/notifier/typetalk"
"github.com/mercari/tfnotify/terraform"
"github.com/urfave/cli"
)
const (
name = "tfnotify"
description = "Notify the execution result of terraform command"
version = "unset"
)
type tfnotify struct {
config config.Config
context *cli.Context
parser terraform.Parser
template terraform.Template
destroyWarningTemplate terraform.Template
warnDestroy bool
}
// Run sends the notification with notifier
func (t *tfnotify) Run() error {
ciname := t.config.CI
if t.context.GlobalString("ci") != "" {
ciname = t.context.GlobalString("ci")
}
ciname = strings.ToLower(ciname)
var ci CI
var err error
switch ciname {
case "circleci", "circle-ci":
ci, err = circleci()
if err != nil {
return err
}
case "travis", "travisci", "travis-ci":
ci, err = travisci()
if err != nil {
return err
}
case "codebuild":
ci, err = codebuild()
if err != nil {
return err
}
case "teamcity":
ci, err = teamcity()
if err != nil {
return err
}
case "drone":
ci, err = drone()
if err != nil {
return err
}
case "jenkins":
ci, err = jenkins()
if err != nil {
return err
}
case "gitlabci", "gitlab-ci":
ci, err = gitlabci()
if err != nil {
return err
}
case "github-actions":
ci, err = githubActions()
if err != nil {
return err
}
case "cloud-build", "cloudbuild":
ci, err = cloudbuild()
if err != nil {
return err
}
case "":
return fmt.Errorf("CI service: required (e.g. circleci)")
default:
return fmt.Errorf("CI service %v: not supported yet", ci)
}
selectedNotifier := t.config.GetNotifierType()
if t.context.GlobalString("notifier") != "" {
selectedNotifier = t.context.GlobalString("notifier")
}
var notifier notifier.Notifier
switch selectedNotifier {
case "github":
client, err := github.NewClient(github.Config{
Token: t.config.Notifier.Github.Token,
BaseURL: t.config.Notifier.Github.BaseURL,
Owner: t.config.Notifier.Github.Repository.Owner,
Repo: t.config.Notifier.Github.Repository.Name,
PR: github.PullRequest{
Revision: ci.PR.Revision,
Number: ci.PR.Number,
Title: t.context.String("title"),
Message: t.context.String("message"),
DestroyWarningTitle: t.context.String("destroy-warning-title"),
DestroyWarningMessage: t.context.String("destroy-warning-message"),
},
CI: ci.URL,
Parser: t.parser,
UseRawOutput: t.config.Terraform.UseRawOutput,
Template: t.template,
DestroyWarningTemplate: t.destroyWarningTemplate,
WarnDestroy: t.warnDestroy,
ResultLabels: github.ResultLabels{
AddOrUpdateLabel: t.config.Terraform.Plan.WhenAddOrUpdateOnly.Label,
DestroyLabel: t.config.Terraform.Plan.WhenDestroy.Label,
NoChangesLabel: t.config.Terraform.Plan.WhenNoChanges.Label,
PlanErrorLabel: t.config.Terraform.Plan.WhenPlanError.Label,
},
})
if err != nil {
return err
}
notifier = client.Notify
case "gitlab":
client, err := gitlab.NewClient(gitlab.Config{
Token: t.config.Notifier.Gitlab.Token,
BaseURL: t.config.Notifier.Gitlab.BaseURL,
NameSpace: t.config.Notifier.Gitlab.Repository.Owner,
Project: t.config.Notifier.Gitlab.Repository.Name,
MR: gitlab.MergeRequest{
Revision: ci.PR.Revision,
Number: ci.PR.Number,
Title: t.context.String("title"),
Message: t.context.String("message"),
},
CI: ci.URL,
Parser: t.parser,
Template: t.template,
})
if err != nil {
return err
}
notifier = client.Notify
case "slack":
client, err := slack.NewClient(slack.Config{
Token: t.config.Notifier.Slack.Token,
Channel: t.config.Notifier.Slack.Channel,
Botname: t.config.Notifier.Slack.Bot,
Title: t.context.String("title"),
Message: t.context.String("message"),
CI: ci.URL,
Parser: t.parser,
Template: t.template,
})
if err != nil {
return err
}
notifier = client.Notify
case "typetalk":
client, err := typetalk.NewClient(typetalk.Config{
Token: t.config.Notifier.Typetalk.Token,
TopicID: t.config.Notifier.Typetalk.TopicID,
Title: t.context.String("title"),
Message: t.context.String("message"),
CI: ci.URL,
Parser: t.parser,
Template: t.template,
})
if err != nil {
return err
}
notifier = client.Notify
case "":
return fmt.Errorf("notifier is missing")
default:
return fmt.Errorf("%s: not supported notifier yet", t.context.GlobalString("notifier"))
}
if notifier == nil {
return fmt.Errorf("no notifier specified at all")
}
return NewExitError(notifier.Notify(tee(os.Stdin, os.Stdout)))
}
func main() {
app := cli.NewApp()
app.Name = name
app.Usage = description
app.Version = version
app.Flags = []cli.Flag{
cli.StringFlag{Name: "ci", Usage: "name of CI to run tfnotify"},
cli.StringFlag{Name: "config", Usage: "config path"},
cli.StringFlag{Name: "notifier", Usage: "notification destination"},
}
app.Commands = []cli.Command{
{
Name: "fmt",
Usage: "Parse stdin as a fmt result",
Action: cmdFmt,
Flags: []cli.Flag{
cli.StringFlag{
Name: "title, t",
Usage: "Specify the title to use for notification",
},
cli.StringFlag{
Name: "message, m",
Usage: "Specify the message to use for notification",
},
},
},
{
Name: "plan",
Usage: "Parse stdin as a plan result",
Action: cmdPlan,
Flags: []cli.Flag{
cli.StringFlag{
Name: "title, t",
Usage: "Specify the title to use for notification",
},
cli.StringFlag{
Name: "message, m",
Usage: "Specify the message to use for notification",
},
cli.StringFlag{
Name: "destroy-warning-title",
Usage: "Specify the title to use for destroy warning notification",
},
cli.StringFlag{
Name: "destroy-warning-message",
Usage: "Specify the message to use for destroy warning notification",
},
},
},
{
Name: "apply",
Usage: "Parse stdin as a apply result",
Action: cmdApply,
Flags: []cli.Flag{
cli.StringFlag{
Name: "title, t",
Usage: "Specify the title to use for notification",
},
cli.StringFlag{
Name: "message, m",
Usage: "Specify the message to use for notification",
},
},
},
}
err := app.Run(os.Args)
os.Exit(HandleExit(err))
}
func newConfig(ctx *cli.Context) (cfg config.Config, err error) {
confPath, err := cfg.Find(ctx.GlobalString("config"))
if err != nil {
return cfg, err
}
if err := cfg.LoadFile(confPath); err != nil {
return cfg, err
}
if err := cfg.Validation(); err != nil {
return cfg, err
}
return cfg, nil
}
func cmdFmt(ctx *cli.Context) error {
cfg, err := newConfig(ctx)
if err != nil {
return err
}
t := &tfnotify{
config: cfg,
context: ctx,
parser: terraform.NewFmtParser(),
template: terraform.NewFmtTemplate(cfg.Terraform.Fmt.Template),
}
return t.Run()
}
func cmdPlan(ctx *cli.Context) error {
cfg, err := newConfig(ctx)
if err != nil {
return err
}
// If when_destroy is not defined in configuration, tfnotify should not notify it
warnDestroy := cfg.Terraform.Plan.WhenDestroy.Template != ""
t := &tfnotify{
config: cfg,
context: ctx,
parser: terraform.NewPlanParser(),
template: terraform.NewPlanTemplate(cfg.Terraform.Plan.Template),
destroyWarningTemplate: terraform.NewDestroyWarningTemplate(cfg.Terraform.Plan.WhenDestroy.Template),
warnDestroy: warnDestroy,
}
return t.Run()
}
func cmdApply(ctx *cli.Context) error {
cfg, err := newConfig(ctx)
if err != nil {
return err
}
t := &tfnotify{
config: cfg,
context: ctx,
parser: terraform.NewApplyParser(),
template: terraform.NewApplyTemplate(cfg.Terraform.Apply.Template),
}
return t.Run()
}