forked from whosonfirst/go-webhookd
/
github.go
95 lines (81 loc) · 2.09 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
package github
import (
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
gogithub "github.com/google/go-github/github"
)
func GenerateSignature(body string, secret string) (string, error) {
mac := hmac.New(sha1.New, []byte(secret))
mac.Write([]byte(body))
sum := mac.Sum(nil)
enc := hex.EncodeToString(sum)
sig := fmt.Sprintf("sha1=%s", enc)
return sig, nil
}
// this doesn't really work as I'd like it only returns an interface{}
// and not a typed thing that the compiler knows what to do with after
// the fact... (20161017/thisisaaronland)
func UnmarshalEvent(event_type string, body []byte) (interface{}, error) {
var event interface{}
ok := true
switch event_type {
case "commit_comment":
event = gogithub.CommitCommentEvent{}
case "create":
event = gogithub.CreateEvent{}
case "delete":
event = gogithub.DeleteEvent{}
case "deployment":
event = gogithub.DeploymentEvent{}
case "deployment_status":
event = gogithub.DeploymentStatusEvent{}
case "fork":
event = gogithub.ForkEvent{}
case "gollum":
event = gogithub.GollumEvent{}
case "issue_comment":
event = gogithub.IssueCommentEvent{}
case "issues":
event = gogithub.IssuesEvent{}
case "member":
event = gogithub.MemberEvent{}
case "membership":
event = gogithub.MembershipEvent{}
case "page_build":
event = gogithub.PageBuildEvent{}
case "public":
event = gogithub.PublicEvent{}
case "pull_request_review_comment":
event = gogithub.PullRequestReviewCommentEvent{}
// case "pull_request_review":
// event = gogithub.PullRequestReviewEvent{}
case "pull_request":
event = gogithub.PullRequestEvent{}
case "push":
event = gogithub.PushEvent{}
case "repository":
event = gogithub.RepositoryEvent{}
case "release":
event = gogithub.ReleaseEvent{}
case "status":
event = gogithub.StatusEvent{}
case "team_add":
event = gogithub.TeamAddEvent{}
case "watch":
event = gogithub.WatchEvent{}
default:
ok = false
}
if !ok {
return nil, errors.New("Unknown event type")
}
err := json.Unmarshal(body, &event)
if err != nil {
return nil, err
}
return event, nil
}