-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
128 lines (110 loc) · 4.87 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
package main
import (
"net"
"net/http"
"os"
"strconv"
"time"
"github.com/gregjones/httpcache"
"github.com/palantir/go-baseapp/baseapp"
"github.com/palantir/go-githubapp/githubapp"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog"
"github.com/spf13/pflag"
)
func main() {
// Logging
zerolog.TimestampFieldName = "@timestamp"
logger := zerolog.New(os.Stdout).With().
Timestamp().
Str("log_type", "app").
Logger()
httpLogger := zerolog.New(os.Stdout).With().
Timestamp().
Str("log_type", "reqresp").
Logger()
// Flags
releaseManagerAuthToken := pflag.String("release-manager-auth-token", "", "auth token for accessing release manager")
releaseManagerURL := pflag.String("release-manager-url", "http://localhost:8080", "url to release manager; without '/' at the end")
var httpServerConfig baseapp.HTTPConfig
pflag.StringVar(&httpServerConfig.Address, "http-address", "localhost", "http listen address")
pflag.IntVar(&httpServerConfig.Port, "http-port", 8080, "http listen port")
pflag.StringVar(&httpServerConfig.PublicURL, "http-public-url", "https://localhost:8080", "http public url")
var githubappConfig githubapp.Config
pflag.StringVar(&githubappConfig.V3APIURL, "github-v3-api-url", "https://api.github.com/", "github v3 api url")
pflag.Int64Var(&githubappConfig.App.IntegrationID, "github-integration-id", 0, "github App ID (App->General->About->App ID)")
pflag.StringVar(&githubappConfig.App.WebhookSecret, "github-webhook-secret", "", "github webhook secret")
pflag.StringVar(&githubappConfig.App.PrivateKey, "github-private-key", "", "github app private key content")
githubWebhookRoute := pflag.String("github-webhook-route", "/webhook/github/bot", "route to listen for webhooks from Github")
messageTemplate := pflag.String("message-template", "'{{.Branch}}' will auto-release to: {{range .AutoReleaseEnvironments}}\n {{.}}{{end}}", "Template string used when commenting on pull requests on Github. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].")
repoFilter := pflag.StringSlice("ignored-repositories", []string{}, "Slice with names of repositories which the bot should not respond to")
repoToServiceMap := pflag.StringToString("map-repo-to-service", map[string]string{}, "Map where key is repo name and value is assigned/interpreted service name. Ex. usage: '--map-repo-to-service=repo1=service1,repo2=service2'")
metricsRoute := pflag.String("metrics-route", "/metrics", "route to expect prometheus requests from")
pflag.Parse()
// Flag validation
if *releaseManagerAuthToken == "" {
logger.Error().Msg("flag 'release-manager-auth-token' is empty")
os.Exit(1)
return
}
// Template validation, fail fast
_, err := BotMessage(BotMessageData{
Template: *messageTemplate,
Branch: "master",
AutoReleaseEnvironments: []string{"dev", "prod"},
})
if err != nil {
logger.Error().Msgf("flag 'message-template' parsing error recieved: %v", err)
os.Exit(1)
return
}
// Metrics
prometheusRegistry := prometheus.DefaultRegisterer
// Create Github client
cc, err := githubapp.NewDefaultCachingClientCreator(
githubappConfig,
githubapp.WithClientUserAgent("release-managar-bot/1.0.0"),
githubapp.WithClientTimeout(3*time.Second),
githubapp.WithClientCaching(false, func() httpcache.Cache { return httpcache.NewMemoryCache() }),
githubapp.WithClientMiddleware(
githubapp.ClientLogging(zerolog.DebugLevel),
clientMetricsMiddleware(prometheusRegistry, "github"),
githubMetricsMiddleware(prometheusRegistry),
),
)
if err != nil {
logger.Error().Msgf("Failed to instatiate Github client: %v", err)
os.Exit(1)
return
}
pullRequestHandler := &PRCreateHandler{
ClientCreator: cc,
releaseManagerMetricsMiddleware: clientMetricsMiddleware(prometheusRegistry, "release-manager")(http.DefaultTransport),
releaseManagerAuthToken: *releaseManagerAuthToken,
releaseManagerURL: *releaseManagerURL,
messageTemplate: *messageTemplate,
repoFilters: *repoFilter,
repoToServiceMap: *repoToServiceMap,
}
webhookHandler := githubapp.NewDefaultEventDispatcher(githubappConfig, pullRequestHandler)
// Create http server
mux := http.NewServeMux()
mux.Handle(*githubWebhookRoute, inboundMetricsMiddleware(prometheusRegistry, webhookHandler))
mux.Handle(*metricsRoute, promhttp.Handler())
// Middleware
httpHandler := loggerMiddleware(func(msg string, m map[string]interface{}) {
httpLogger.Info().Fields(m).Msg(msg)
}, mux)
httpHandler = loggingContextMiddleware(logger, httpHandler)
s := http.Server{
Addr: net.JoinHostPort("", strconv.Itoa(httpServerConfig.Port)),
Handler: httpHandler,
}
// Serve
err = s.ListenAndServe()
if err != nil {
logger.Error().Msgf("Failed to serve: %v", err)
os.Exit(1)
}
}