-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.go
135 lines (114 loc) · 3.08 KB
/
parser.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
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"net/http"
"time"
)
const (
// sha1Prefix is the prefix used by GitHub before the HMAC hexdigest.
sha1Prefix = "sha1"
// signatureHeader is the GitHub header key used to pass the HMAC hexdigest.
signatureHeader = "X-Hub-Signature"
// eventTypeHeader is the GitHub header key used to pass the event type.
eventTypeHeader = "X-Github-Event"
// deliveryIDHeader is the GitHub header key used to pass the unique ID for the webhook event.
deliveryIDHeader = "X-Github-Delivery"
)
func parseGitHubWebHook(req *http.Request) (*SimpleEvent, error) {
var sig string
if sig = req.Header.Get(signatureHeader); sig == "" {
return nil, errors.New("No signature")
}
log.Printf("Sig: %s", sig)
eventType := req.Header.Get(eventTypeHeader)
if eventType == "" {
return nil, errors.New("No event")
}
log.Printf("Type: %s", eventType)
eventID := req.Header.Get(deliveryIDHeader)
if eventID == "" {
return nil, errors.New("No event ID")
}
log.Printf("ID: %s", eventID)
body, err := ioutil.ReadAll(req.Body)
if err != nil {
return nil, err
}
if !checkContentSignature([]byte(webHookSecret), sig, body) {
return nil, errors.New("Invalid signature")
}
return parseSimpleEvent(body, eventID, eventType)
}
func parseSimpleEvent(body []byte, eventID, eventType string) (*SimpleEvent, error) {
// placeholder for returned struct
se := &SimpleEvent{
Type: eventType,
ID: eventID,
Countable: true,
}
// TODO: Could this be cast into some kind of countable type?
switch se.Type {
case "issue_comment":
fallthrough
case "commit_comment":
ev := &CommentEvent{}
err := json.Unmarshal(body, &ev)
if err != nil {
log.Printf("Error parsing %s: %v", se.Type, err)
return nil, err
}
se.Actor = ev.Comment.User.Name
se.EventAt = ev.Comment.CreatedAt
se.Repo = ev.Repo.Name
case "issues":
ev := &IssuesEvent{}
err := json.Unmarshal(body, &ev)
if err != nil {
log.Printf("Error parsing issues: %v", err)
return nil, err
}
se.Actor = ev.Issue.User.Name
se.EventAt = ev.Issue.CreatedAt
se.Repo = ev.Repo.Name
case "pull_request":
fallthrough
case "pull_request_review_comment":
ev := &PullRequestEvent{}
err := json.Unmarshal(body, &ev)
if err != nil {
log.Printf("Error parsing %s: %v", se.Type, err)
return nil, err
}
se.Actor = ev.PR.User.Name
se.EventAt = ev.PR.CreatedAt
se.Repo = ev.Repo.Name
case "pull_request_review":
ev := &ReviewEvent{}
err := json.Unmarshal(body, &ev)
if err != nil {
log.Printf("Error parsing %s: %v", se.Type, err)
return nil, err
}
se.Actor = ev.Review.User.Name
se.EventAt = ev.Review.CreatedAt
se.Repo = ev.Repo.Name
case "push":
ev := &SimplePushEvent{}
err := json.Unmarshal(body, &ev)
if err != nil {
log.Printf("Error parsing %s: %v", se.Type, err)
return nil, err
}
se.Actor = ev.User.Name
// There is no push time, using WebHook execution time
se.EventAt = time.Now()
se.Repo = ev.Repo.Name
default:
se.Countable = false
log.Printf("Uncountable type: %s", se.Type)
}
return se, nil
}