-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
65 lines (58 loc) · 1.53 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
package main
import (
"fmt"
"github-webhook-server/config"
"github-webhook-server/issue"
"github-webhook-server/push"
"io/ioutil"
"log"
"net/http"
"github.com/google/go-github/github"
)
func parseWebHook(r *http.Request) (interface{}, error) {
// Read payload into a []byte buffer
payload, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, fmt.Errorf("error reading request body: %s", err)
}
defer r.Body.Close()
// Parse webhook into event
event, err := github.ParseWebHook(github.WebHookType(r), payload)
if err != nil {
return nil, fmt.Errorf("error parsing webhook: %s", err)
}
return event, nil
}
func handleWebHook(w http.ResponseWriter, r *http.Request) {
event, err := parseWebHook(r)
if err != nil {
log.Println(err)
}
// FIXME: remove switch, refactor function to return event
// and call it in specific handlers
switch e := event.(type) {
case *github.IssuesEvent:
handleIssueEvent(*e)
case *github.IssueCommentEvent:
handleIssueEvent(*e)
case *github.PushEvent:
handlePushEvent(*e)
default:
log.Printf("unknown event type %s\n", github.WebHookType(r))
}
}
func handleIssueEvent(e interface{}) {
issue.SaveIssueDataToDB(e)
}
func handlePushEvent(e github.PushEvent) {
log.Print("Handling push event...")
push.GetModifiedFiles(e)
}
func main() {
log.Println("Server started")
config.ReadConfig()
// FIXME: each endpoint should have its own webhook handler
http.HandleFunc("/issues", handleWebHook)
http.HandleFunc("/pushes", handleWebHook)
log.Fatal(http.ListenAndServe(":8080", nil))
}