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-01-26 01:28:28 +00:00
|
|
|
func newCommitComment(data []byte) (Event, error) {
|
|
|
|
commitCommentStruct := CommitCommentEvent{}
|
2016-01-20 07:14:11 +00:00
|
|
|
err := json.Unmarshal(data, &commitCommentStruct)
|
|
|
|
if err != nil {
|
2016-01-23 00:43:33 +00:00
|
|
|
return nil, err
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
2016-01-23 00:43:33 +00:00
|
|
|
return commitCommentStruct, nil
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 01:28:28 +00:00
|
|
|
func newCreate(data []byte) (Event, error) {
|
|
|
|
createStruct := CreateEvent{}
|
2016-01-20 07:14:11 +00:00
|
|
|
err := json.Unmarshal(data, &createStruct)
|
|
|
|
if err != nil {
|
2016-01-23 00:43:33 +00:00
|
|
|
return nil, err
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
2016-01-23 00:43:33 +00:00
|
|
|
return createStruct, nil
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 01:28:28 +00:00
|
|
|
func newDelete(data []byte) (Event, error) {
|
|
|
|
deleteStruct := DeleteEvent{}
|
2016-01-20 07:14:11 +00:00
|
|
|
err := json.Unmarshal(data, &deleteStruct)
|
|
|
|
if err != nil {
|
2016-01-23 00:43:33 +00:00
|
|
|
return nil, err
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
2016-01-23 00:43:33 +00:00
|
|
|
return deleteStruct, nil
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 01:28:28 +00:00
|
|
|
func newDeployment(data []byte) (Event, error) {
|
|
|
|
deploymentStruct := DeploymentEvent{}
|
2016-01-20 07:14:11 +00:00
|
|
|
err := json.Unmarshal(data, &deploymentStruct)
|
|
|
|
if err != nil {
|
2016-01-23 00:43:33 +00:00
|
|
|
return nil, err
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
2016-01-23 00:43:33 +00:00
|
|
|
return deploymentStruct, nil
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 01:28:28 +00:00
|
|
|
func newDeploymentStatus(data []byte) (Event, error) {
|
|
|
|
deploymentStatusStruct := DeploymentStatusEvent{}
|
2016-01-20 07:14:11 +00:00
|
|
|
err := json.Unmarshal(data, &deploymentStatusStruct)
|
|
|
|
if err != nil {
|
2016-01-23 00:43:33 +00:00
|
|
|
return nil, err
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
2016-01-23 00:43:33 +00:00
|
|
|
return deploymentStatusStruct, nil
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 01:28:28 +00:00
|
|
|
func newFork(data []byte) (Event, error) {
|
|
|
|
forkStruct := ForkEvent{}
|
2016-01-20 07:14:11 +00:00
|
|
|
err := json.Unmarshal(data, &forkStruct)
|
|
|
|
if err != nil {
|
2016-01-23 00:43:33 +00:00
|
|
|
return nil, err
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
2016-01-23 00:43:33 +00:00
|
|
|
return forkStruct, nil
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 01:28:28 +00:00
|
|
|
func newGollum(data []byte) (Event, error) {
|
|
|
|
gollumStruct := GollumEvent{}
|
2016-01-20 07:14:11 +00:00
|
|
|
err := json.Unmarshal(data, &gollumStruct)
|
|
|
|
if err != nil {
|
2016-01-23 00:43:33 +00:00
|
|
|
return nil, err
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
2016-01-23 00:43:33 +00:00
|
|
|
return gollumStruct, nil
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 01:28:28 +00:00
|
|
|
func newIssueComment(data []byte) (Event, error) {
|
|
|
|
issueCommentStruct := IssueCommentEvent{}
|
2016-01-20 07:14:11 +00:00
|
|
|
err := json.Unmarshal(data, &issueCommentStruct)
|
|
|
|
if err != nil {
|
2016-01-23 00:43:33 +00:00
|
|
|
return nil, err
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
2016-01-23 00:43:33 +00:00
|
|
|
return issueCommentStruct, nil
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 01:28:28 +00:00
|
|
|
func newIssues(data []byte) (Event, error) {
|
|
|
|
issuesStruct := IssuesEvent{}
|
2016-01-20 07:14:11 +00:00
|
|
|
err := json.Unmarshal(data, &issuesStruct)
|
|
|
|
if err != nil {
|
2016-01-23 00:43:33 +00:00
|
|
|
return nil, err
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
2016-01-23 00:43:33 +00:00
|
|
|
return issuesStruct, nil
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 01:28:28 +00:00
|
|
|
func newMember(data []byte) (Event, error) {
|
|
|
|
memberStruct := MemberEvent{}
|
2016-01-20 07:14:11 +00:00
|
|
|
err := json.Unmarshal(data, &memberStruct)
|
|
|
|
if err != nil {
|
2016-01-23 00:43:33 +00:00
|
|
|
return nil, err
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
2016-01-23 00:43:33 +00:00
|
|
|
return memberStruct, nil
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 01:28:28 +00:00
|
|
|
func newMembership(data []byte) (Event, error) {
|
|
|
|
membershipStruct := MembershipEvent{}
|
2016-01-20 07:14:11 +00:00
|
|
|
err := json.Unmarshal(data, &membershipStruct)
|
|
|
|
if err != nil {
|
2016-01-23 00:43:33 +00:00
|
|
|
return nil, err
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
2016-01-23 00:43:33 +00:00
|
|
|
return membershipStruct, nil
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 01:28:28 +00:00
|
|
|
func newPageBuild(data []byte) (Event, error) {
|
|
|
|
pageBuildEvent := PageBuildEvent{}
|
2016-01-20 07:14:11 +00:00
|
|
|
err := json.Unmarshal(data, &pageBuildEvent)
|
|
|
|
if err != nil {
|
2016-01-23 00:43:33 +00:00
|
|
|
return nil, err
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
2016-01-23 00:43:33 +00:00
|
|
|
return pageBuildEvent, nil
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 01:28:28 +00:00
|
|
|
func newPublic(data []byte) (Event, error) {
|
|
|
|
publicEvent := PublicEvent{}
|
2016-01-20 07:14:11 +00:00
|
|
|
err := json.Unmarshal(data, &publicEvent)
|
|
|
|
if err != nil {
|
2016-01-23 00:43:33 +00:00
|
|
|
return nil, err
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
2016-01-23 00:43:33 +00:00
|
|
|
return publicEvent, nil
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 01:28:28 +00:00
|
|
|
func newPullRequest(data []byte) (Event, error) {
|
|
|
|
pullRequestStruct := PullRequestEvent{}
|
2016-01-20 07:14:11 +00:00
|
|
|
err := json.Unmarshal(data, &pullRequestStruct)
|
|
|
|
if err != nil {
|
2016-01-23 00:43:33 +00:00
|
|
|
return nil, err
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
2016-01-23 00:43:33 +00:00
|
|
|
return pullRequestStruct, nil
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 01:28:28 +00:00
|
|
|
func newPullRequestReviewComment(data []byte) (Event, error) {
|
|
|
|
pullRequestReviewCommentStruct := PullRequestReviewCommentEvent{}
|
2016-01-20 07:14:11 +00:00
|
|
|
err := json.Unmarshal(data, &pullRequestReviewCommentStruct)
|
|
|
|
if err != nil {
|
2016-01-23 00:43:33 +00:00
|
|
|
return nil, err
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
2016-01-23 00:43:33 +00:00
|
|
|
return pullRequestReviewCommentStruct, nil
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 01:28:28 +00:00
|
|
|
func newPush(data []byte) (Event, error) {
|
|
|
|
pushStruct := PushEvent{}
|
2016-01-20 07:14:11 +00:00
|
|
|
err := json.Unmarshal(data, &pushStruct)
|
|
|
|
if err != nil {
|
2016-01-23 00:43:33 +00:00
|
|
|
return nil, err
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
2016-01-23 00:43:33 +00:00
|
|
|
return pushStruct, nil
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 01:28:28 +00:00
|
|
|
func newRelease(data []byte) (Event, error) {
|
|
|
|
releaseStruct := ReleaseEvent{}
|
2016-01-20 07:14:11 +00:00
|
|
|
err := json.Unmarshal(data, &releaseStruct)
|
|
|
|
if err != nil {
|
2016-01-23 00:43:33 +00:00
|
|
|
return nil, err
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
2016-01-23 00:43:33 +00:00
|
|
|
return releaseStruct, nil
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 01:28:28 +00:00
|
|
|
func newRepository(data []byte) (Event, error) {
|
|
|
|
repositoryStruct := RepositoryEvent{}
|
2016-01-20 07:14:11 +00:00
|
|
|
err := json.Unmarshal(data, &repositoryStruct)
|
|
|
|
if err != nil {
|
2016-01-23 00:43:33 +00:00
|
|
|
return nil, err
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
2016-01-23 00:43:33 +00:00
|
|
|
return repositoryStruct, nil
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 01:28:28 +00:00
|
|
|
func newStatus(data []byte) (Event, error) {
|
|
|
|
statusStruct := StatusEvent{}
|
2016-01-20 07:14:11 +00:00
|
|
|
err := json.Unmarshal(data, &statusStruct)
|
|
|
|
if err != nil {
|
2016-01-23 00:43:33 +00:00
|
|
|
return nil, err
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
2016-01-23 00:43:33 +00:00
|
|
|
return statusStruct, nil
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 01:28:28 +00:00
|
|
|
func newTeamAdd(data []byte) (Event, error) {
|
|
|
|
teamAddStruct := TeamAddEvent{}
|
2016-01-20 07:14:11 +00:00
|
|
|
err := json.Unmarshal(data, &teamAddStruct)
|
|
|
|
if err != nil {
|
2016-01-23 00:43:33 +00:00
|
|
|
return nil, err
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
2016-01-23 00:43:33 +00:00
|
|
|
return teamAddStruct, nil
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 01:28:28 +00:00
|
|
|
func newWatch(data []byte) (Event, error) {
|
|
|
|
watchStruct := WatchEvent{}
|
2016-01-20 07:14:11 +00:00
|
|
|
err := json.Unmarshal(data, &watchStruct)
|
|
|
|
if err != nil {
|
2016-01-23 00:43:33 +00:00
|
|
|
return nil, err
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|
2016-01-23 00:43:33 +00:00
|
|
|
return watchStruct, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-26 01:28:28 +00:00
|
|
|
func NewEvent(r []byte, t string) (Event, error) {
|
2016-01-23 00:43:33 +00:00
|
|
|
log.Printf("New %v event recieved", t)
|
2016-01-20 07:14:11 +00:00
|
|
|
switch t {
|
|
|
|
case "commit_comment":
|
|
|
|
return newCommitComment(r)
|
|
|
|
case "create":
|
|
|
|
return newCreate(r)
|
|
|
|
case "delete":
|
|
|
|
return newDelete(r)
|
|
|
|
case "deployment":
|
|
|
|
return newDeployment(r)
|
|
|
|
case "deployment_status":
|
|
|
|
return newDeploymentStatus(r)
|
|
|
|
case "fork":
|
|
|
|
return newFork(r)
|
|
|
|
case "gollum":
|
|
|
|
return newGollum(r)
|
|
|
|
case "issue_comment":
|
|
|
|
return newIssueComment(r)
|
|
|
|
case "issues":
|
|
|
|
return newIssues(r)
|
|
|
|
case "member":
|
|
|
|
return newMember(r)
|
|
|
|
case "membership":
|
|
|
|
return newMembership(r)
|
|
|
|
case "page_build":
|
|
|
|
return newPageBuild(r)
|
|
|
|
case "public":
|
|
|
|
return newPublic(r)
|
|
|
|
case "pull_request":
|
|
|
|
return newPullRequest(r)
|
|
|
|
case "pull_request_review_comment":
|
|
|
|
return newPullRequestReviewComment(r)
|
|
|
|
case "push":
|
|
|
|
return newPush(r)
|
|
|
|
case "release":
|
|
|
|
return newRelease(r)
|
|
|
|
case "repository":
|
|
|
|
return newRepository(r)
|
|
|
|
case "status":
|
|
|
|
return newStatus(r)
|
|
|
|
case "team_add":
|
|
|
|
return newTeamAdd(r)
|
|
|
|
case "watch":
|
|
|
|
return newWatch(r)
|
|
|
|
}
|
2016-01-23 00:43:33 +00:00
|
|
|
return nil, &newEventError{"Not a recgonized event type"}
|
2016-01-20 07:14:11 +00:00
|
|
|
}
|