-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
255 lines (223 loc) · 6.41 KB
/
server.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
package golangci_lint_runner
import (
"errors"
"fmt"
"net/http"
"time"
"context"
"sync"
"crypto/rsa"
"github.com/google/go-github/github"
"github.com/talon-one/golangci-lint-runner/internal"
)
type Server struct {
Options *ServerOptions
queueStarter sync.Once
queueSize int
queue chan *Runner
}
type ServerOptions struct {
PrivateKey *rsa.PrivateKey
WebhookSecret string
webHookSecret []byte
AppID int64
QueueSize int
*Options
}
type Logger interface {
Debug(string, ...interface{})
Error(string, ...interface{})
Warn(string, ...interface{})
Info(string, ...interface{})
}
type LinterOptions struct {
Linters []string
IncludeLinterName bool
}
func NewServer(options *ServerOptions) (*Server, error) {
if options == nil {
return nil, errors.New("Options must be specified")
}
if options.PrivateKey == nil {
return nil, errors.New("PrivateKey must be specified")
}
if options.WebhookSecret == "" {
return nil, errors.New("WebhookSecret must be specified")
}
options.webHookSecret = []byte(options.WebhookSecret)
if options.AppID == 0 {
return nil, errors.New("AppID must be specified")
}
if options.Logger == nil {
return nil, errors.New("Logger must be specified")
}
if options.Timeout <= 0 {
options.Timeout = time.Minute * 10
}
return &Server{
queue: make(chan *Runner, options.QueueSize),
queueSize: options.QueueSize,
Options: options,
}, nil
}
func (srv *Server) HttpHandler() http.Handler {
srv.startQueue()
mux := http.NewServeMux()
mux.HandleFunc("/", srv.handler(srv.handleEvent))
return mux
}
func (srv *Server) startQueue() {
srv.queueStarter.Do(func() {
go srv.workQueue()
})
}
func (srv *Server) workQueue() {
for runner := range srv.queue {
runner.Options.Context, _ = context.WithTimeout(context.Background(), srv.Options.Timeout)
if err := runner.Run(); err != nil {
srv.Options.Logger.Error("runner failed: %s", err.Error())
}
}
}
func (srv *Server) Close() error {
close(srv.queue)
return nil
}
type Writer struct {
http.ResponseWriter
writtenStatus bool
}
func (w *Writer) WriteHeader(statusCode int) {
if w.writtenStatus {
return
}
w.writtenStatus = true
w.ResponseWriter.WriteHeader(statusCode)
}
func (srv *Server) handler(h func(http.ResponseWriter, *http.Request) error) http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
w := &Writer{
ResponseWriter: writer,
writtenStatus: false,
}
if err := h(w, request); err != nil {
switch e := err.(type) {
case internal.WireError:
if e.StatusCode == 0 {
e.StatusCode = http.StatusInternalServerError
}
if e.PublicError == nil {
e.PublicError = errors.New("error")
}
srv.Options.Logger.Error("%d: %s\n%s", e.StatusCode, e.PublicError, e.PrivateError)
http.Error(writer, e.PublicError.Error(), e.StatusCode)
return
}
s := err.Error()
srv.Options.Logger.Error("%d: %s\n", http.StatusInternalServerError, s)
http.Error(writer, s, http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
}
func (srv *Server) handleEvent(writer http.ResponseWriter, request *http.Request) error {
srv.Options.Logger.Debug("got event from %s", request.RemoteAddr)
payload, err := github.ValidatePayload(request, srv.Options.webHookSecret)
if err != nil {
return internal.WireError{
StatusCode: http.StatusBadRequest,
PublicError: errors.New("unable to validate payload"),
PrivateError: errors.New("invalid payload"),
}
}
event, err := github.ParseWebHook(github.WebHookType(request), payload)
switch e := event.(type) {
case *github.PullRequestEvent:
return srv.handlePullRequest(writer, request, e)
case *github.PingEvent, *github.InstallationEvent:
return nil
}
srv.Options.Logger.Warn("unhandled event %T", event)
return internal.WireError{
StatusCode: http.StatusBadRequest,
PublicError: errors.New("unknown event"),
PrivateError: fmt.Errorf("unknown event %T", event),
}
}
func (srv *Server) handlePullRequest(writer http.ResponseWriter, request *http.Request, event *github.PullRequestEvent) error {
switch event.GetAction() {
case "opened", "reopened", "synchronize":
return srv.handlePullRequestOpened(writer, request, event)
}
srv.Options.Logger.Warn("unhandled action %s", event.GetAction())
return nil
}
func (srv *Server) handlePullRequestOpened(writer http.ResponseWriter, request *http.Request, event *github.PullRequestEvent) error {
pr := event.GetPullRequest()
if pr == nil {
return internal.WireError{
PrivateError: errors.New("unable to get pull request from event"),
}
}
installation := event.GetInstallation()
if installation == nil {
return internal.WireError{
PrivateError: errors.New("unable to get installation from event"),
}
}
appClient, err := makeAppClient(srv.Options.AppID, srv.Options.PrivateKey)
if err != nil {
return internal.WireError{
PrivateError: fmt.Errorf("unable to create client"),
}
}
installationID := installation.GetID()
if installationID == 0 {
return internal.WireError{
PrivateError: errors.New("unable to get id from installation"),
}
}
ctx, _ := context.WithTimeout(context.Background(), srv.Options.Timeout)
srv.Options.Logger.Debug("creating installation token")
// todo: we can store this token for a later use
installationToken, _, err := appClient.Apps.CreateInstallationToken(ctx, installationID)
if err != nil {
return internal.WireError{
PrivateError: fmt.Errorf("unable to create installation token: %w", err),
}
}
opts := *srv.Options.Options
opts.Context = ctx
opts.CloneToken = installationToken.GetToken()
if opts.CloneToken == "" {
return internal.WireError{
PrivateError: errors.New("unable to get installation token"),
}
}
opts.Client, err = makeInstallationClient(opts.CloneToken)
if err != nil {
return internal.WireError{
PrivateError: fmt.Errorf("unable to create client"),
}
}
opts.PullRequest = pr
runner, err := NewRunner(opts)
if err != nil {
return internal.WireError{
PublicError: errors.New("unable to create runner"),
PrivateError: fmt.Errorf("unable to create runner: %w", err),
}
}
select {
case srv.queue <- runner:
srv.Options.Logger.Debug("added job to queue (%d/%d)", len(srv.queue), srv.queueSize)
return nil
default:
return internal.WireError{
StatusCode: http.StatusServiceUnavailable,
PublicError: errors.New("try again later"),
PrivateError: errors.New("queue is full"),
}
}
}