Repository navigation
Expand file tree
/
Copy pathserver.go
More file actions
74 lines (62 loc) · 1.33 KB
/
Copy pathserver.go
File metadata and controls
74 lines (62 loc) · 1.33 KB
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
package main
import (
"io"
"log/slog"
"net/http"
"github.com/go-playground/webhooks/v6/github"
)
func NewServer(er *Repository) *Server {
return &Server{er}
}
type Server struct {
er *Repository
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
endpoint, ok := s.er.GetEndpoint(r.URL.Path)
if !ok {
w.WriteHeader(http.StatusNotFound)
return
}
signature := r.Header.Get("X-Hub-Signature")
if signature == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
event := r.Header.Get("X-GitHub-Event")
if event == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
slog.Info("Incomming webhook request", "endpoint", endpoint.Route, "event", event)
payload, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if !ValidatePayload(endpoint.Secret, payload, signature) {
w.WriteHeader(http.StatusUnauthorized)
return
}
cmds := endpoint.GetCommands(github.Event(event))
if len(cmds) > 0 {
slog.Info(
"Triggering endpoint actions",
"endpoint",
endpoint.Route,
"event",
event,
"actions_count",
len(cmds),
)
go s.er.HandleCommands(cmds, endpoint.Route, event)
} else {
slog.Info(
"Endpoint has no actions setted up for the event",
"endpoint",
endpoint.Route,
"event",
event,
)
}
w.WriteHeader(http.StatusOK)
}