-
Notifications
You must be signed in to change notification settings - Fork 7
/
github.go
229 lines (175 loc) · 4.6 KB
/
github.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package github
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"regexp"
"strings"
"github.com/urfave/cli"
"github.com/sensiblecodeio/hookbot/pkg/hookbot"
"github.com/sensiblecodeio/hookbot/pkg/listen"
)
var RegexParseHeader = regexp.MustCompile("^\\s*([^\\:]+)\\s*:\\s*(.*)$")
func MustParseHeader(header string) (string, string) {
if !RegexParseHeader.MatchString(header) {
log.Fatalf("Unable to parse header: %v (re: %v)", header,
RegexParseHeader.String())
return "", ""
}
parts := RegexParseHeader.FindStringSubmatch(header)
return parts[1], parts[2]
}
func MustParseHeaders(headerStrings []string) http.Header {
headers := http.Header{}
for _, h := range headerStrings {
key, value := MustParseHeader(h)
headers.Set(key, value)
}
return headers
}
func MustMakeHeader(
target *url.URL, origin string, headerStrings []string,
) http.Header {
header := MustParseHeaders(headerStrings)
if origin == "samehost" {
origin = "//" + target.Host
}
header.Add("Origin", origin)
header.Add("X-Hookbot-Unsafe-Is-Ok",
"I understand the security implications")
return header
}
func ActionRoute(c *cli.Context) {
target, err := url.Parse(c.String("monitor-url"))
if err != nil {
log.Fatalf("Failed to parse %q as URL: %v", c.String("monitor-url"), err)
}
origin := c.String("origin")
header := MustMakeHeader(target, origin, c.StringSlice("header"))
finish := make(chan struct{})
messages, errors := listen.RetryingWatch(target.String(), header, finish)
outbound := make(chan listen.Message, 1)
publish := func(m hookbot.Message) bool {
token := Sha1HMAC(c.GlobalString("key"), []byte(m.Topic))
outURL := fmt.Sprintf("https://%v@%v/pub/%s", token, target.Host, m.Topic)
body := ioutil.NopCloser(bytes.NewBuffer(m.Body))
out, err := http.NewRequest("POST", outURL, body)
if err != nil {
log.Printf("Failed to construct outbound req: %v", err)
return false
}
out.SetBasicAuth(token, "")
resp, err := http.DefaultClient.Do(out)
if err != nil {
log.Printf("Failed to transmit: %v", err)
return false
}
log.Printf("Transmit: %v %v", resp.StatusCode, outURL)
return true
}
go func() {
for err := range errors {
log.Printf("Encountered error in Watch: %v", err)
}
}()
router := &Router{}
for mBytes := range messages {
log.Printf("Receive message")
parts := bytes.Split(mBytes, []byte{0})
topic := parts[0]
body := parts[1]
if !IsValidGithubSignature(c.GlobalString("github-secret"), body) {
log.Printf("Reject github signature")
continue
}
m := hookbot.Message{Topic: string(topic), Body: body}
router.Route(m, publish)
}
close(outbound)
}
type Event struct {
Type string
Repository *Repository `json:"repository"`
Pusher *Pusher `json:"pusher"`
Ref string `json:"ref"`
Before string `json:"before"`
After string `json:"after"`
}
func (e *Event) Branch() string {
return strings.TrimPrefix(e.Ref, "refs/heads/")
}
type Pusher struct {
Name string `json:"name"`
Email string `json:"email"`
}
type Repository struct {
FullName string `json:"full_name"`
}
type Router struct{}
func (r *Router) Name() string {
return "github"
}
func (r *Router) Topics() []string {
return []string{"/unsafe/github.com/"}
}
func (r *Router) Route(in hookbot.Message, publish func(hookbot.Message) bool) {
log.Printf("route github: %q", in.Topic)
type GithubMessage struct {
Event, Signature string
Payload []byte
}
var m GithubMessage
err := json.Unmarshal(in.Body, &m)
if err != nil {
log.Printf("Failed to unmarshal message in IsValidGithubSignature: %v",
err)
return
}
var event Event
event.Type = m.Event
err = json.Unmarshal(m.Payload, &event)
if err != nil {
log.Printf("Route: error in json.Unmarshal: %v", err)
return
}
if event.Repository == nil || event.Repository.FullName == "" {
log.Printf("Could not identify repository for event %v", event.Type)
return
}
repo := event.Repository.FullName
branch := event.Branch()
who := "<unknown>"
if event.Pusher != nil {
who = event.Pusher.Name
}
msgBytes, err := json.Marshal(map[string]string{
"Type": event.Type,
"Repo": repo,
"Branch": branch,
"SHA": event.After,
"Who": who,
})
if err != nil {
log.Printf("Failed to marshal Update: %v", err)
return
}
switch event.Type {
case "push":
topicFmt := "github.com/repo/%s/branch/%s"
// May fail
_ = publish(hookbot.Message{
Topic: fmt.Sprintf(topicFmt, repo, branch),
Body: msgBytes,
})
default:
log.Printf("Unhandled event type: %v", event.Type)
return
}
}
func init() {
hookbot.RegisterRouter(&Router{})
}