-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
128 lines (111 loc) · 3.13 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
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
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/litl/hookhub/fogbugz"
"log"
"net/http"
"os"
"strconv"
)
type HookHubHandler struct {
bindAddress string
bindPort int
repos map[string]*Repo
debug bool
fogbugzSession *fogbugz.Session
}
type ReleaseHandler interface {
Handle(repo *Repo, notification GithubReleaseEvent, debug bool) error
}
type PushHandler interface {
Handle(hookHubHandler *HookHubHandler, notification GithubPushEvent, debug bool) error
}
type Repo struct {
Name string
FullName string
releaseHandlers []ReleaseHandler
pushHandlers []PushHandler
}
func (handler *HookHubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
event := r.Header.Get("X-Github-Event")
if event == "" {
log.Println("No X-Github-Event header")
return
}
defer r.Body.Close()
var err error
if err = r.ParseForm(); err != nil {
fmt.Println("Failed to parse form values from request body", err)
}
var jsonStr = r.PostFormValue("payload")
if handler.debug {
log.Println("Received notification", jsonStr)
}
switch event {
case GITHUB_EVENT_RELEASE:
var notification GithubReleaseEvent
if err = json.Unmarshal([]byte(jsonStr), ¬ification); err != nil {
log.Println("Got errors parsing notification", err)
log.Println(jsonStr)
return
}
repo := handler.repos[notification.Repository.FullName]
if repo == nil {
log.Println("No repo configured for", notification.Repository.FullName)
return
}
for _, eventHandler := range repo.releaseHandlers {
if err = eventHandler.Handle(repo, notification, handler.debug); err != nil {
log.Println("Error when handling release", err)
} else {
log.Println("Successfully handled release")
}
}
break
case GITHUB_EVENT_PUSH:
var notification GithubPushEvent
if err = json.Unmarshal([]byte(jsonStr), ¬ification); err != nil {
log.Println("Got errors parsing notification", err)
log.Println(jsonStr)
return
}
repo := handler.repos[notification.Repository.FullName]
if repo == nil {
log.Println("No repo configured for", notification.Repository.FullName)
return
}
for _, eventHandler := range repo.pushHandlers {
if err = eventHandler.Handle(handler, notification, handler.debug); err != nil {
log.Println("Error when handling push", err)
} else {
log.Println("Successfully handled push")
}
}
break
}
}
func main() {
var configFile string
handler := new(HookHubHandler)
flag.BoolVar(&handler.debug, "debug", false, "Run in debug mode")
flag.StringVar(&configFile, "config", "hookhub.toml", "Path to configuration file")
flag.Parse()
if handler.debug {
log.Println("Debug mode enabled")
}
if err := handler.ParseConfig(configFile); err != nil {
log.Fatalln("Failed to initialize from config", err)
return
}
portEnv := os.Getenv("PORT")
if portEnv != "" {
if port, err := strconv.Atoi(portEnv); err == nil {
handler.bindPort = port
}
}
http.Handle("/github_webhook", handler)
log.Printf("Listening on %s:%d\n", handler.bindAddress, handler.bindPort)
http.ListenAndServe(fmt.Sprintf("%s:%d", handler.bindAddress, handler.bindPort), nil)
}