-
Notifications
You must be signed in to change notification settings - Fork 4
/
bibikoffi.go
164 lines (134 loc) Β· 3.78 KB
/
bibikoffi.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
package main
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"github.com/BurntSushi/toml"
"github.com/containous/flaeg"
"github.com/ogier/pflag"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/traefik/bibikoffi/internal/gh"
"github.com/traefik/bibikoffi/mjolnir"
"github.com/traefik/bibikoffi/types"
)
func main() {
options := &types.Options{
DryRun: true,
ConfigFilePath: "./bibikoffi.toml",
ServerPort: 80,
}
rootCmd := &flaeg.Command{
Name: "bibikoffi",
Description: `Myrmica Bibikoffi: Closes stale issues.`,
DefaultPointersConfig: &types.Options{},
Config: options,
Run: runCmd(options),
}
flag := flaeg.New(rootCmd, os.Args[1:])
// version
versionCmd := &flaeg.Command{
Name: "version",
Description: "Display the version.",
Config: &types.NoOption{},
DefaultPointersConfig: &types.NoOption{},
Run: func() error {
displayVersion()
return nil
},
}
flag.AddCommand(versionCmd)
// Run command
err := flag.Run()
if err != nil && !errors.Is(err, pflag.ErrHelp) {
log.Fatal().Err(err).Msg("unable to start bibikoffi")
}
}
func runCmd(options *types.Options) func() error {
return func() error {
setupLogger(options.DryRun, options.LogLevel)
log.Debug().Msgf("Run bibikoffi command with config : %+v", options)
if len(options.GitHubToken) == 0 {
options.GitHubToken = os.Getenv("GITHUB_TOKEN")
}
err := required(options.GitHubToken, "token")
if err != nil {
return err
}
err = required(options.ConfigFilePath, "config-path")
if err != nil {
return err
}
if options.DryRun {
log.Debug().Msg("IMPORTANT: you are using the dry-run mode. Use `--dry-run=false` to disable this mode.")
}
return process(options)
}
}
func process(options *types.Options) error {
if options.ServerMode {
server := &server{options: options}
return server.ListenAndServe()
}
return launch(options)
}
func launch(options *types.Options) error {
config := &types.Configuration{}
metadata, err := toml.DecodeFile(options.ConfigFilePath, config)
if err != nil {
return err
}
log.Debug().Msgf("configuration: %+v", metadata)
ctx := context.Background()
client := gh.NewGitHubClient(ctx, options.GitHubToken)
err = mjolnir.CloseIssues(ctx, client, config.Owner, config.RepositoryName, config.Rules, options.DryRun)
if err != nil {
return err
}
return mjolnir.LockIssues(ctx, client, config.Owner, config.RepositoryName, config.Locks, options.DryRun)
}
func required(field, fieldName string) error {
if len(field) == 0 {
return fmt.Errorf("%s is mandatory", fieldName)
}
return nil
}
type server struct {
options *types.Options
}
func (s *server) ListenAndServe() error {
return http.ListenAndServe(":"+strconv.Itoa(s.options.ServerPort), s)
}
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
log.Error().Msgf("Invalid http method: %s", r.Method)
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
err := launch(s.options)
if err != nil {
log.Error().Err(err).Msg("Report error")
http.Error(w, "Report error.", http.StatusInternalServerError)
return
}
fmt.Fprint(w, "Myrmica Bibikoffi: Scheduled.\n")
}
// 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)
}