2016-05-27 15:27:54 +00:00
|
|
|
package github
|
2016-01-20 07:14:11 +00:00
|
|
|
|
|
|
|
import (
|
2017-04-17 18:42:03 +00:00
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/hex"
|
2016-01-20 07:14:11 +00:00
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
2016-01-23 00:43:33 +00:00
|
|
|
"log"
|
2016-01-20 07:14:11 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-01-20 07:14:11 +00:00
|
|
|
)
|
|
|
|
|
2016-05-27 15:27:54 +00:00
|
|
|
type GithubWebhook struct {
|
2017-04-17 18:42:03 +00:00
|
|
|
Path string
|
|
|
|
Secret string
|
|
|
|
acc telegraf.Accumulator
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 15:27:54 +00:00
|
|
|
func (gh *GithubWebhook) Register(router *mux.Router, acc telegraf.Accumulator) {
|
|
|
|
router.HandleFunc(gh.Path, gh.eventHandler).Methods("POST")
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("I! Started the webhooks_github on %s\n", gh.Path)
|
2016-05-27 15:27:54 +00:00
|
|
|
gh.acc = acc
|
2016-01-23 00:43:33 +00:00
|
|
|
}
|
2016-01-20 07:14:11 +00:00
|
|
|
|
2016-05-27 15:27:54 +00:00
|
|
|
func (gh *GithubWebhook) eventHandler(w http.ResponseWriter, r *http.Request) {
|
2016-03-14 10:29:43 +00:00
|
|
|
defer r.Body.Close()
|
2017-04-17 18:42:03 +00:00
|
|
|
eventType := r.Header.Get("X-Github-Event")
|
2016-01-20 07:14:11 +00:00
|
|
|
data, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2016-01-23 00:43:33 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2016-03-14 10:29:43 +00:00
|
|
|
return
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
2017-04-17 18:42:03 +00:00
|
|
|
|
|
|
|
if gh.Secret != "" && !checkSignature(gh.Secret, data, r.Header.Get("X-Hub-Signature")) {
|
|
|
|
log.Printf("E! Fail to check the github webhook signature\n")
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-01-23 00:43:33 +00:00
|
|
|
e, err := NewEvent(data, eventType)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2016-03-14 10:29:43 +00:00
|
|
|
return
|
2016-01-23 00:43:33 +00:00
|
|
|
}
|
2017-03-06 12:34:41 +00:00
|
|
|
if e != nil {
|
|
|
|
p := e.NewMetric()
|
|
|
|
gh.acc.AddFields("github_webhooks", p.Fields(), p.Tags(), p.Time())
|
|
|
|
}
|
2016-05-27 15:27:54 +00:00
|
|
|
|
2016-01-20 07:14:11 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2016-05-23 10:21:34 +00:00
|
|
|
func generateEvent(data []byte, event Event) (Event, error) {
|
|
|
|
err := json.Unmarshal(data, event)
|
2016-01-20 07:14:11 +00:00
|
|
|
if err != nil {
|
2016-01-23 00:43:33 +00:00
|
|
|
return nil, err
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
2016-05-23 10:21:34 +00:00
|
|
|
return event, nil
|
2016-01-23 00:43:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type newEventError struct {
|
|
|
|
s string
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 00:43:33 +00:00
|
|
|
func (e *newEventError) Error() string {
|
|
|
|
return e.s
|
|
|
|
}
|
|
|
|
|
2016-05-23 10:21:34 +00:00
|
|
|
func NewEvent(data []byte, name string) (Event, error) {
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("D! New %v event received", name)
|
2016-05-23 10:21:34 +00:00
|
|
|
switch name {
|
2016-01-20 07:14:11 +00:00
|
|
|
case "commit_comment":
|
2016-05-23 10:21:34 +00:00
|
|
|
return generateEvent(data, &CommitCommentEvent{})
|
2016-01-20 07:14:11 +00:00
|
|
|
case "create":
|
2016-05-23 10:21:34 +00:00
|
|
|
return generateEvent(data, &CreateEvent{})
|
2016-01-20 07:14:11 +00:00
|
|
|
case "delete":
|
2016-05-23 10:21:34 +00:00
|
|
|
return generateEvent(data, &DeleteEvent{})
|
2016-01-20 07:14:11 +00:00
|
|
|
case "deployment":
|
2016-05-23 10:21:34 +00:00
|
|
|
return generateEvent(data, &DeploymentEvent{})
|
2016-01-20 07:14:11 +00:00
|
|
|
case "deployment_status":
|
2016-05-23 10:21:34 +00:00
|
|
|
return generateEvent(data, &DeploymentStatusEvent{})
|
2016-01-20 07:14:11 +00:00
|
|
|
case "fork":
|
2016-05-23 10:21:34 +00:00
|
|
|
return generateEvent(data, &ForkEvent{})
|
2016-01-20 07:14:11 +00:00
|
|
|
case "gollum":
|
2016-05-23 10:21:34 +00:00
|
|
|
return generateEvent(data, &GollumEvent{})
|
2016-01-20 07:14:11 +00:00
|
|
|
case "issue_comment":
|
2016-05-23 10:21:34 +00:00
|
|
|
return generateEvent(data, &IssueCommentEvent{})
|
2016-01-20 07:14:11 +00:00
|
|
|
case "issues":
|
2016-05-23 10:21:34 +00:00
|
|
|
return generateEvent(data, &IssuesEvent{})
|
2016-01-20 07:14:11 +00:00
|
|
|
case "member":
|
2016-05-23 10:21:34 +00:00
|
|
|
return generateEvent(data, &MemberEvent{})
|
2016-01-20 07:14:11 +00:00
|
|
|
case "membership":
|
2016-05-23 10:21:34 +00:00
|
|
|
return generateEvent(data, &MembershipEvent{})
|
2016-01-20 07:14:11 +00:00
|
|
|
case "page_build":
|
2016-05-23 10:21:34 +00:00
|
|
|
return generateEvent(data, &PageBuildEvent{})
|
2017-03-06 12:34:41 +00:00
|
|
|
case "ping":
|
|
|
|
return nil, nil
|
2016-01-20 07:14:11 +00:00
|
|
|
case "public":
|
2016-05-23 10:21:34 +00:00
|
|
|
return generateEvent(data, &PublicEvent{})
|
2016-01-20 07:14:11 +00:00
|
|
|
case "pull_request":
|
2016-05-23 10:21:34 +00:00
|
|
|
return generateEvent(data, &PullRequestEvent{})
|
2016-01-20 07:14:11 +00:00
|
|
|
case "pull_request_review_comment":
|
2016-05-23 10:21:34 +00:00
|
|
|
return generateEvent(data, &PullRequestReviewCommentEvent{})
|
2016-01-20 07:14:11 +00:00
|
|
|
case "push":
|
2016-05-23 10:21:34 +00:00
|
|
|
return generateEvent(data, &PushEvent{})
|
2016-01-20 07:14:11 +00:00
|
|
|
case "release":
|
2016-05-23 10:21:34 +00:00
|
|
|
return generateEvent(data, &ReleaseEvent{})
|
2016-01-20 07:14:11 +00:00
|
|
|
case "repository":
|
2016-05-23 10:21:34 +00:00
|
|
|
return generateEvent(data, &RepositoryEvent{})
|
2016-01-20 07:14:11 +00:00
|
|
|
case "status":
|
2016-05-23 10:21:34 +00:00
|
|
|
return generateEvent(data, &StatusEvent{})
|
2016-01-20 07:14:11 +00:00
|
|
|
case "team_add":
|
2016-05-23 10:21:34 +00:00
|
|
|
return generateEvent(data, &TeamAddEvent{})
|
2016-01-20 07:14:11 +00:00
|
|
|
case "watch":
|
2016-05-23 10:21:34 +00:00
|
|
|
return generateEvent(data, &WatchEvent{})
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
2016-05-18 13:14:50 +00:00
|
|
|
return nil, &newEventError{"Not a recognized event type"}
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
2017-04-17 18:42:03 +00:00
|
|
|
|
|
|
|
func checkSignature(secret string, data []byte, signature string) bool {
|
|
|
|
return hmac.Equal([]byte(signature), []byte(generateSignature(secret, data)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateSignature(secret string, data []byte) string {
|
|
|
|
mac := hmac.New(sha1.New, []byte(secret))
|
|
|
|
mac.Write(data)
|
|
|
|
result := mac.Sum(nil)
|
|
|
|
return "sha1=" + hex.EncodeToString(result)
|
|
|
|
}
|