2016-01-25 17:56:57 +00:00
|
|
|
package github_webhooks
|
2016-01-20 07:14:11 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2016-01-23 00:43:33 +00:00
|
|
|
"log"
|
2016-01-20 07:14:11 +00:00
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-01-25 17:56:57 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2016-01-20 07:14:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
inputs.Add("github_webhooks", func() telegraf.Input { return &GithubWebhooks{} })
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-25 17:56:57 +00:00
|
|
|
type GithubWebhooks struct {
|
2016-01-26 22:57:06 +00:00
|
|
|
ServiceAddress string
|
2016-01-23 00:43:33 +00:00
|
|
|
// Lock for the struct
|
2016-01-20 07:14:11 +00:00
|
|
|
sync.Mutex
|
2016-01-23 00:43:33 +00:00
|
|
|
// Events buffer to store events between Gather calls
|
2016-01-26 01:28:28 +00:00
|
|
|
events []Event
|
2016-01-23 00:43:33 +00:00
|
|
|
}
|
2016-01-20 07:14:11 +00:00
|
|
|
|
2016-01-25 17:56:57 +00:00
|
|
|
func NewGithubWebhooks() *GithubWebhooks {
|
|
|
|
return &GithubWebhooks{}
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-25 17:56:57 +00:00
|
|
|
func (gh *GithubWebhooks) SampleConfig() string {
|
2016-01-20 07:14:11 +00:00
|
|
|
return `
|
2016-02-18 21:26:51 +00:00
|
|
|
## Address and port to host Webhook listener on
|
2016-01-20 07:14:11 +00:00
|
|
|
service_address = ":1618"
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
2016-01-25 17:56:57 +00:00
|
|
|
func (gh *GithubWebhooks) Description() string {
|
2016-01-23 00:43:33 +00:00
|
|
|
return "A Github Webhook Event collector"
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Writes the points from <-gh.in to the Accumulator
|
2016-01-27 21:21:36 +00:00
|
|
|
func (gh *GithubWebhooks) Gather(acc telegraf.Accumulator) error {
|
2016-01-20 07:14:11 +00:00
|
|
|
gh.Lock()
|
|
|
|
defer gh.Unlock()
|
2016-01-23 00:43:33 +00:00
|
|
|
for _, event := range gh.events {
|
2016-01-27 23:15:14 +00:00
|
|
|
p := event.NewMetric()
|
2016-01-26 22:57:06 +00:00
|
|
|
acc.AddFields("github_webhooks", p.Fields(), p.Tags(), p.Time())
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
2016-01-26 01:28:28 +00:00
|
|
|
gh.events = make([]Event, 0)
|
2016-01-20 07:14:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-25 17:56:57 +00:00
|
|
|
func (gh *GithubWebhooks) Listen() {
|
2016-01-20 18:49:42 +00:00
|
|
|
r := mux.NewRouter()
|
2016-01-23 00:43:33 +00:00
|
|
|
r.HandleFunc("/", gh.eventHandler).Methods("POST")
|
|
|
|
err := http.ListenAndServe(fmt.Sprintf("%s", gh.ServiceAddress), r)
|
2016-01-20 20:19:03 +00:00
|
|
|
if err != nil {
|
2016-01-23 00:43:33 +00:00
|
|
|
log.Printf("Error starting server: %v", err)
|
2016-01-20 20:19:03 +00:00
|
|
|
}
|
2016-01-20 18:49:42 +00:00
|
|
|
}
|
|
|
|
|
2016-02-16 00:21:38 +00:00
|
|
|
func (gh *GithubWebhooks) Start(_ telegraf.Accumulator) error {
|
2016-01-23 00:43:33 +00:00
|
|
|
go gh.Listen()
|
2016-01-25 17:56:57 +00:00
|
|
|
log.Printf("Started the github_webhooks service on %s\n", gh.ServiceAddress)
|
2016-01-20 20:19:03 +00:00
|
|
|
return nil
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-25 17:56:57 +00:00
|
|
|
func (gh *GithubWebhooks) Stop() {
|
2016-01-20 07:14:11 +00:00
|
|
|
log.Println("Stopping the ghWebhooks service")
|
|
|
|
}
|
|
|
|
|
2016-01-23 00:43:33 +00:00
|
|
|
// Handles the / route
|
2016-01-25 17:56:57 +00:00
|
|
|
func (gh *GithubWebhooks) 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
|
|
|
}
|
|
|
|
gh.Lock()
|
|
|
|
gh.events = append(gh.events, e)
|
|
|
|
gh.Unlock()
|
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
|
|
|
}
|