/
consumer.go
108 lines (82 loc) · 2.3 KB
/
consumer.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
package main
import (
"errors"
"net/http"
"io/ioutil"
"encoding/json"
"fmt"
"github.com/google/go-github/github"
)
const (
githubEventHeader = "X-GitHub-Event"
githubSignatureHeader = "X-GitHub-Signature"
githubDeliveryIDHeader = "X-GitHub-Delivery"
)
// HookContext represents the http request from
// the webhook source
type HookContext struct {
Signature string
Payload []byte
DeliveryID string
Event string
PullRequestEvent github.PullRequestEvent
}
// HookResponse gets returned to the client
type HookResponse struct {
Message string `json:"message"`
Status int `json:"status"`
}
// HandleHook takes a request from the source and trys to process it
func HandleHook(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
e := json.NewEncoder(w)
hook, err := parseGitHook(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
e.Encode(HookResponse{"Invalid Request", 500})
return
}
if err := tryDeploy(hook); err != nil {
w.WriteHeader(http.StatusInternalServerError)
e.Encode(HookResponse{"Invalid Request", 500})
} else {
w.WriteHeader(http.StatusOK)
e.Encode(HookResponse{"Success", 200})
}
}
// Parses the request and builds a HookRequest struct to be used
// for deploying
func parseGitHook(r *http.Request) (*HookContext, error) {
hc := HookContext{}
if hc.Signature = r.Header.Get(githubSignatureHeader); len(hc.Signature) == 0 {
return nil, errors.New("no signature")
}
if hc.Event = r.Header.Get(githubEventHeader); len(hc.Event) == 0 {
return nil, errors.New("no event")
}
if hc.DeliveryID = r.Header.Get(githubDeliveryIDHeader); len(hc.DeliveryID) == 0 {
return nil, errors.New("no delivery id")
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
pr := github.PullRequestEvent{}
if err := json.Unmarshal(body, &pr); err != nil {
return nil, errors.New("invalid payload")
}
hc.PullRequestEvent = pr
if !verifySignature("") {
return nil, errors.New("invalid signature")
}
return &hc, nil
}
func verifySignature(s string) bool {
return true
}
func tryDeploy(h *HookContext) error {
fmt.Printf("Merged: %T", h.PullRequestEvent.PullRequest.Merged)
//pull out config items here and execute
fmt.Println(h.PullRequestEvent)
return nil
}