-
Notifications
You must be signed in to change notification settings - Fork 3
/
aloba.go
265 lines (217 loc) · 6.08 KB
/
aloba.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
package main
import (
"errors"
"fmt"
"os"
"strings"
"github.com/containous/flaeg"
"github.com/ogier/pflag"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/traefik/aloba/cmd"
"github.com/traefik/aloba/meta"
"github.com/traefik/aloba/options"
)
func main() {
rootCmd := createRootCommand()
flag := flaeg.New(rootCmd, os.Args[1:])
// Report
reportCmd := createReportCommand()
flag.AddCommand(reportCmd)
// Label
labelCmd := createLabelCommand()
flag.AddCommand(labelCmd)
// GitHubAction
ghaCmd := createGitHubActionCommand()
flag.AddCommand(ghaCmd)
// Run command
// version
versionCmd := createVersionCommand()
flag.AddCommand(versionCmd)
// Print help when the command is running without any parameters.
rootCmd.Run = func() error {
return flaeg.LoadWithCommand(rootCmd, []string{"-h"}, nil, []*flaeg.Command{rootCmd, reportCmd, labelCmd, ghaCmd, versionCmd})
}
// Run command
err := flag.Run()
if err != nil && !errors.Is(err, pflag.ErrHelp) {
log.Fatal().Err(err).Msg("unable to start aloba")
}
}
func createVersionCommand() *flaeg.Command {
return &flaeg.Command{
Name: "version",
Description: "Display the version.",
Config: &options.Empty{},
DefaultPointersConfig: &options.Empty{},
Run: func() error {
meta.DisplayVersion()
return nil
},
}
}
func createRootCommand() *flaeg.Command {
emptyConfig := &options.Empty{}
rootCmd := &flaeg.Command{
Name: "aloba",
Description: "Myrmica Aloba: Manage GitHub labels.",
Config: emptyConfig,
DefaultPointersConfig: &options.Empty{},
}
return rootCmd
}
func createReportCommand() *flaeg.Command {
reportOptions := &options.Report{
Slack: &options.Slack{
IconEmoji: ":captainpr:",
BotName: "CaptainPR",
},
DryRun: true,
ServerPort: 80,
}
reportCmd := &flaeg.Command{
Name: "report",
Description: "Create a report and publish on Slack.",
Config: reportOptions,
DefaultPointersConfig: &options.Report{Slack: &options.Slack{}, GitHub: &options.GitHub{}},
}
reportCmd.Run = func() error {
setupLogger(reportOptions.DryRun, reportOptions.LogLevel)
if reportOptions.DryRun {
log.Info().Msg("IMPORTANT: you are using the dry-run mode. Use `--dry-run=false` to disable this mode.")
}
if len(reportOptions.GitHub.Token) == 0 {
reportOptions.GitHub.Token = os.Getenv("GITHUB_TOKEN")
}
if len(reportOptions.Slack.Token) == 0 {
reportOptions.Slack.Token = os.Getenv("SLACK_TOKEN")
}
err := validateReportOptions(reportOptions)
if err != nil {
return err
}
return cmd.Report(reportOptions)
}
return reportCmd
}
func createLabelCommand() *flaeg.Command {
labelOptions := &options.Label{
RulesFilePath: "./rules.toml",
DryRun: true,
}
defaultPointerOptions := &options.Label{
GitHub: &options.GitHub{},
WebHook: &options.WebHook{
Port: 80,
},
}
labelCmd := &flaeg.Command{
Name: "label",
Description: "Add labels and milestone on pull requests and issues.",
Config: labelOptions,
DefaultPointersConfig: defaultPointerOptions,
}
labelCmd.Run = func() error {
setupLogger(labelOptions.DryRun, labelOptions.LogLevel)
if labelOptions.DryRun {
log.Info().Msg("IMPORTANT: you are using the dry-run mode. Use `--dry-run=false` to disable this mode.")
}
if len(labelOptions.GitHub.Token) == 0 {
labelOptions.GitHub.Token = os.Getenv("GITHUB_TOKEN")
}
if labelOptions.WebHook != nil && len(labelOptions.WebHook.Secret) == 0 {
labelOptions.WebHook.Secret = os.Getenv("WEBHOOK_SECRET")
}
err := validateLabelOptions(labelOptions)
if err != nil {
return err
}
return cmd.Label(labelOptions)
}
return labelCmd
}
func createGitHubActionCommand() *flaeg.Command {
ghaOptions := &options.GitHubAction{
DryRun: true,
}
ghaCmd := &flaeg.Command{
Name: "action",
Description: "GitHub Action",
Config: ghaOptions,
DefaultPointersConfig: &options.GitHubAction{},
}
ghaCmd.Run = func() error {
setupLogger(ghaOptions.DryRun, ghaOptions.LogLevel)
if ghaOptions.DryRun {
log.Info().Msg("IMPORTANT: you are using the dry-run mode. Use `--dry-run=false` to disable this mode.")
}
ghToken := os.Getenv("GITHUB_TOKEN")
err := required(ghToken, "GITHUB_TOKEN")
if err != nil {
return err
}
return cmd.RunGitHubAction(ghaOptions, ghToken)
}
return ghaCmd
}
func required(field, fieldName string) error {
if len(field) == 0 {
return fmt.Errorf("%s is mandatory", fieldName)
}
return nil
}
func validateReportOptions(reportOptions *options.Report) error {
err := required(reportOptions.GitHub.Token, "github.token")
if err != nil {
return err
}
err = required(reportOptions.GitHub.Owner, "github.owner")
if err != nil {
return err
}
err = required(reportOptions.GitHub.RepositoryName, "github.repo-name")
if err != nil {
return err
}
err = required(reportOptions.Slack.Token, "slack.token")
if err != nil {
return err
}
if !reportOptions.ServerMode {
errSlackChan := required(reportOptions.Slack.ChannelID, "slack.channel")
if errSlackChan != nil {
return errSlackChan
}
}
return nil
}
func validateLabelOptions(labelOptions *options.Label) error {
err := required(labelOptions.GitHub.Token, "github.token")
if err != nil {
return err
}
err = required(labelOptions.GitHub.Owner, "github.owner")
if err != nil {
return err
}
err = required(labelOptions.GitHub.RepositoryName, "github.repo-name")
if err != nil {
return err
}
return required(labelOptions.RulesFilePath, "rules-path")
}
// setupLogger is configuring the logger.
func setupLogger(dryRun bool, level string) {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Logger = zerolog.New(os.Stderr).With().Caller().Logger()
logLevel := zerolog.DebugLevel
if !dryRun {
var err error
logLevel, err = zerolog.ParseLevel(strings.ToLower(level))
if err != nil {
logLevel = zerolog.InfoLevel
}
}
zerolog.SetGlobalLevel(logLevel)
log.Trace().Msgf("Log level set to %s.", logLevel)
}