/
handlers.go
85 lines (74 loc) · 2.49 KB
/
handlers.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
package controller
import (
"fmt"
"io/ioutil"
"net/http"
"reflect"
"github.com/heptiolabs/healthcheck"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"github.com/xanzy/go-gitlab"
)
// HealthCheckHandler ..
func (c *Controller) HealthCheckHandler() (h healthcheck.Handler) {
h = healthcheck.NewHandler()
if c.Config.Gitlab.EnableHealthCheck {
h.AddReadinessCheck("gitlab-reachable", c.Gitlab.ReadinessCheck())
} else {
log.Warn("GitLab health check has been disabled. Readiness checks won't be operated.")
}
return
}
// MetricsHandler ..
func (c *Controller) MetricsHandler(w http.ResponseWriter, r *http.Request) {
registry := NewRegistry()
metrics, err := c.Store.Metrics()
if err != nil {
log.Error(err.Error())
}
registry.ExportMetrics(metrics)
promhttp.HandlerFor(registry, promhttp.HandlerOpts{
Registry: registry,
EnableOpenMetrics: c.Config.Server.Metrics.EnableOpenmetricsEncoding,
}).ServeHTTP(w, r)
}
// WebhookHandler ..
func (c *Controller) WebhookHandler(w http.ResponseWriter, r *http.Request) {
logFields := log.Fields{
"ip-address": r.RemoteAddr,
"user-agent": r.UserAgent(),
}
log.WithFields(logFields).Debug("webhook request")
if r.Header.Get("X-Gitlab-Token") != c.Config.Server.Webhook.SecretToken {
log.WithFields(logFields).Debug("invalid token provided for a webhook request")
w.WriteHeader(http.StatusForbidden)
fmt.Fprint(w, "{\"error\": \"invalid token\"")
return
}
if r.Body == http.NoBody {
log.WithFields(logFields).WithField("error", "nil body").Warn("unable to read body of a received webhook")
w.WriteHeader(http.StatusBadRequest)
return
}
payload, err := ioutil.ReadAll(r.Body)
if err != nil {
log.WithFields(logFields).WithField("error", err.Error()).Warn("unable to read body of a received webhook")
w.WriteHeader(http.StatusBadRequest)
return
}
event, err := gitlab.ParseHook(gitlab.HookEventType(r), payload)
if err != nil {
log.WithFields(logFields).WithFields(logFields).WithField("error", err.Error()).Warn("unable to parse body of a received webhook")
w.WriteHeader(http.StatusBadRequest)
return
}
switch event := event.(type) {
case *gitlab.PipelineEvent:
go c.processPipelineEvent(*event)
case *gitlab.DeploymentEvent:
go c.processDeploymentEvent(*event)
default:
log.WithFields(logFields).WithField("event-type", reflect.TypeOf(event).String()).Warn("received a non supported event type as a webhook")
w.WriteHeader(http.StatusUnprocessableEntity)
}
}