/
main.go
179 lines (151 loc) · 4.32 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"github.com/antonholmquist/jason"
"github.com/fatih/structs"
"github.com/hoisie/mustache"
)
type Event struct {
Author string
Repo string
Branch string
Action string
Msg string
Date string
Type string
}
func NewEvent(author, repo, branch, action, msg, date, ty string) *Event {
return &Event{
Author: author,
Repo: repo,
Branch: branch,
Action: action,
Msg: msg,
Date: date,
Type: ty,
}
}
var pushTmpl, _ = mustache.ParseString("{{Date}} -- {{Author}} {{Action}} to {{Repo}}/{{Branch}}\n\t{{Msg}}\n")
var pqTmpl, _ = mustache.ParseString("{{Date}} -- {{Author}} {{Action}} Pull Request{{Branch}} to {{Repo}}\n\t{{Msg}}\n")
func (e *Event) String() string {
m := structs.Map(e)
if e.Type == "push" {
return pushTmpl.Render(m)
}
if e.Type == "pullrequest" {
return pqTmpl.Render(m)
}
return ""
}
type Server struct {
Port int
Path string
Secret string
Events chan Event
}
func NewServer() *Server {
return &Server{
Port: 80,
Path: "/hook",
Events: make(chan Event),
}
}
func (srv *Server) Listen() error {
return http.ListenAndServe(":"+strconv.Itoa(srv.Port), srv)
}
func (srv *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
if req.Method != "POST" {
msg := "Github hook request is POST only"
http.Error(w, msg, http.StatusMethodNotAllowed)
fmt.Println(msg)
return
}
if req.URL.Path != srv.Path {
msg := "Invalid Path"
http.Error(w, msg, http.StatusNotFound)
fmt.Println(msg)
return
}
eventType := req.Header.Get("X-GitHub-Event")
if eventType != "push" && eventType != "pull_request" {
w.Write([]byte(fmt.Sprintf("Evernt Type %s is not supported", eventType)))
return
}
body, err := ioutil.ReadAll(req.Body)
if err != nil {
http.Error(w, "Fail to read req.Body", http.StatusBadRequest)
}
if srv.Secret != "" {
sig := req.Header.Get("X-Hub-Signature")
if sig == "" {
http.Error(w, "Missing X-Hub-Signature", http.StatusForbidden)
return
}
mac := hmac.New(sha1.New, []byte(srv.Secret))
mac.Write(body)
expected := "sha1=" + hex.EncodeToString(mac.Sum(nil))
// hmac Equal won't leak through timing side-channel
if !hmac.Equal([]byte(expected), []byte(sig)) {
http.Error(w, "Secret not match", http.StatusForbidden)
return
}
}
request, err := jason.NewObjectFromBytes(body)
if err != nil {
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
// https://developer.github.com/v3/activity/events/types/#pushevent
// (*jason.Object)(0xc20803b590)({"added":["test.go"],"author":{"email":"naituida@163.com","name":"naituida","username":"dyzz"},"committer":{"email":"naituida@163.com","name":"naituida","username":"dyzz"},"distinct":true,"id":"76fa79bab843450020bd73fbba0b30741f99055f","message":"init push","modified":[],"removed":[],"timestamp":"2015-09-11T02:22:02-04:00","url":"https://github.com/dyzz/gothubhook/commit/76fa79bab843450020bd73fbba0b30741f99055f"}) }
var log string
if eventType == "push" {
repo, _ := request.GetString("repository", "name")
ref, _ := request.GetString("ref")
chunks := strings.Split(ref, "/")
branch := chunks[len(chunks)-1]
commits, _ := request.GetObjectArray("commits")
for _, commit := range commits {
author, _ := commit.GetString("author", "name")
msg, _ := commit.GetString("message")
date, _ := commit.GetString("timestamp")
event := NewEvent(author, repo, branch, "commit", msg, date, "push")
log = event.String()
w.Write([]byte(log))
}
}
// https://developer.github.com/v3/activity/events/types/#pullrequestevent
if eventType == "pull_request" {
action, _ := request.GetString("action")
number, _ := request.GetInt64("number")
pq, _ := request.GetObject("pull_request")
msg, _ := pq.GetString("title")
date, _ := pq.GetString("updated_at")
author, _ := pq.GetString("user", "login")
repo, _ := request.GetString("repository", "name")
event := NewEvent(author, repo, "#"+strconv.Itoa(int(number)), action, msg, date, "pullrequest")
log = event.String()
w.Write([]byte(log))
}
fmt.Println(log)
AppendLog(log)
}
func main() {
srv := NewServer()
err := srv.Listen()
if err != nil {
fmt.Println(err)
}
for {
var output string
fmt.Scanln(&output)
fmt.Println(output)
}
}