2016-05-27 15:27:54 +00:00
|
|
|
package github
|
2016-01-20 07:14:11 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"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:51:53 +00:00
|
|
|
type GithubWebhook struct {
|
2016-05-27 15:27:54 +00:00
|
|
|
Path string
|
|
|
|
acc telegraf.Accumulator
|
2016-01-23 00:43:33 +00:00
|
|
|
}
|
2016-01-20 07:14:11 +00:00
|
|
|
|
2016-05-27 15:51:53 +00:00
|
|
|
func (gh *GithubWebhook) Register(router *mux.Router, acc telegraf.Accumulator) {
|
2016-05-27 15:27:54 +00:00
|
|
|
router.HandleFunc(gh.Path, gh.eventHandler).Methods("POST")
|
|
|
|
log.Printf("Started the webhooks_github on %s\n", gh.Path)
|
|
|
|
gh.acc = acc
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 15:51:53 +00:00
|
|
|
func (gh *GithubWebhook) eventHandler(w http.ResponseWriter, r *http.Request) {
|
2016-03-14 10:29:43 +00:00
|
|
|
defer r.Body.Close()
|
2016-01-20 07:14:11 +00:00
|
|
|
eventType := r.Header["X-Github-Event"][0]
|
|
|
|
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
|
|
|
}
|
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
|
|
|
}
|
2016-05-27 15:27:54 +00:00
|
|
|
|
|
|
|
p := e.NewMetric()
|
|
|
|
gh.acc.AddFields("github_webhooks", p.Fields(), p.Tags(), p.Time())
|
|
|
|
|
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) {
|
|
|
|
log.Printf("New %v event received", name)
|
|
|
|
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{})
|
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
|
|
|
}
|