This repository was archived by the owner on Dec 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.go
More file actions
104 lines (88 loc) · 2.62 KB
/
server.go
File metadata and controls
104 lines (88 loc) · 2.62 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
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
package validator
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/bradleyfalzon/ghinstallation"
"github.com/google/go-github/github"
)
// Server contains the logic to process webhooks, kinda like probot
type Server struct {
Port int
WebhookSecret string
PrivateKeyFile string
AppID int
GitHubAppClient *github.Client
tr *http.RoundTripper
ctx *context.Context
}
// GenericEvent contains just enough inforamation about webhook to handle
// authentication
type GenericEvent struct {
// Repo *github.Repository `json:"repository,omitempty"`
// Org *github.Organization `json:"organization,omitempty"`
// Sender *github.User `json:"sender,omitempty"`
Installation *github.Installation `json:"installation,omitempty"`
}
// Run starts a http server on the configured port
func (s *Server) Run(ctx context.Context) error {
s.tr = &http.DefaultTransport
itr, err := ghinstallation.NewAppsTransportKeyFromFile(*s.tr, s.AppID, s.PrivateKeyFile)
if err != nil {
return err
}
s.ctx = &ctx
s.GitHubAppClient = github.NewClient(&http.Client{Transport: itr})
http.HandleFunc("/webhook", s.handle)
http.HandleFunc("/healthz", s.health)
http.HandleFunc("/", s.redirect)
log.Println("hi")
return http.ListenAndServe(fmt.Sprintf(":%d", s.Port), nil)
}
func (s *Server) handle(w http.ResponseWriter, r *http.Request) {
payload, err := github.ValidatePayload(r, []byte(s.WebhookSecret))
if err != nil {
log.Println(err)
return
}
defer r.Body.Close()
event, err := github.ParseWebHook(github.WebHookType(r), payload)
if err != nil {
log.Println(err)
return
}
ge := &GenericEvent{}
err = json.Unmarshal(payload, &ge)
if err != nil {
log.Println(err)
return
}
var installationTransport *ghinstallation.Transport
if ge.Installation != nil {
installationTransport, err = ghinstallation.NewKeyFromFile(*s.tr, s.AppID, int(ge.Installation.GetID()), s.PrivateKeyFile)
if err != nil {
log.Println(err)
return
}
}
c := &Context{
Event: event,
Ctx: s.ctx,
AppID: &s.AppID,
Github: github.NewClient(&http.Client{Transport: installationTransport}),
AppGitHub: s.GitHubAppClient,
}
// TODO Return a 500 if we don't make it through the complete CheckRun cycle
c.Process()
return
}
func (s *Server) health(w http.ResponseWriter, r *http.Request) {
// TODO better health checks
fmt.Fprintf(w, "hi")
}
func (s *Server) redirect(w http.ResponseWriter, r *http.Request) {
// TODO automatically generate this redirect
http.Redirect(w, r, "http://github.com/urcomputeringpal/kubevalidator", 301)
}