-
Notifications
You must be signed in to change notification settings - Fork 2
/
http.go
96 lines (88 loc) · 2.3 KB
/
http.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
package main
import (
"MuseBot/models"
"MuseBot/utils"
"crypto"
"crypto/rsa"
"encoding/base64"
"encoding/json"
"io/ioutil"
"net/http"
"strings"
)
func HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
return
}
func GitHubHookHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("token") == "" {
log.Debugf("GitHub hook with empty token")
w.WriteHeader(401)
return
}
if r.URL.Query().Get("token") != Config.GitHubHookToken {
log.Debugf("GitHub hook with wrong token")
w.WriteHeader(401)
return
}
event := r.Header.Get("X-GitHub-Event")
data, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Debugf("GitHub hook body read error: %s", err)
w.WriteHeader(500)
return
}
switch strings.ToLower(event) {
case "pull_request":
payload := models.GitHubPRPayload{}
if err := json.Unmarshal(data, &payload); err != nil {
log.Debugf("GitHub %s hook unmarshal error: %s", event, err)
w.WriteHeader(500)
return
}
BotMulticast <- GitHubHookPRMessage{Payload: payload}
case "push":
payload := models.GitHubPushPayload{}
if err := json.Unmarshal(data, &payload); err != nil {
log.Debugf("GitHub %s hook unmarshal error: %s", event, err)
w.WriteHeader(500)
return
}
BotMulticast <- GitHubHookPushMessage{Payload: payload}
default:
log.Debugf("GitHub hook with unknown event type: %s", event)
w.WriteHeader(404)
return
}
w.WriteHeader(200)
return
}
func TravisHookHandler(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Signature") == "" {
log.Debugf("Travis hook with empty Signature")
w.WriteHeader(400)
return
}
sign, err := base64.StdEncoding.DecodeString(r.Header.Get("Signature"))
if err != nil {
log.Debugf("Travis hook with wrong Signature, error: %s", err)
w.WriteHeader(400)
return
}
data := []byte(r.FormValue("payload"))
digest := utils.DataDigest(data)
if err := rsa.VerifyPKCS1v15(Config.TravisPublicKey, crypto.SHA1, digest, sign); err != nil {
log.Debugf("Travis hook sign error: %s", err)
w.WriteHeader(401)
return
}
payload := models.TravisHookPayload{}
if err := json.Unmarshal(data, &payload); err != nil {
log.Debugf("Travis hook unmarshal error: %s", err)
w.WriteHeader(500)
return
}
BotMulticast <- TravisHookMessage{Payload: payload}
w.WriteHeader(200)
return
}