telegraf/plugins/inputs/github_webhooks/github_webhooks.go

336 lines
7.2 KiB
Go
Raw Normal View History

package github_webhooks
import (
"encoding/json"
"fmt"
"io/ioutil"
2016-01-23 00:43:33 +00:00
"log"
"net/http"
"sync"
"github.com/gorilla/mux"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
)
func init() {
inputs.Add("github_webhooks", func() telegraf.Input { return &GithubWebhooks{} })
}
type GithubWebhooks struct {
ServiceAddress string
2016-01-23 00:43:33 +00:00
// Lock for the struct
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
}
func NewGithubWebhooks() *GithubWebhooks {
return &GithubWebhooks{}
}
func (gh *GithubWebhooks) SampleConfig() string {
return `
# Address and port to host Webhook listener on
service_address = ":1618"
`
}
func (gh *GithubWebhooks) Description() string {
2016-01-23 00:43:33 +00:00
return "A Github Webhook Event collector"
}
// Writes the points from <-gh.in to the Accumulator
func (gh *GithubWebhooks) Gather(acc telegraf.Accumulator) error {
gh.Lock()
defer gh.Unlock()
2016-01-23 00:43:33 +00:00
for _, event := range gh.events {
p := event.NewPoint()
acc.AddFields("github_webhooks", p.Fields(), p.Tags(), p.Time())
}
2016-01-26 01:28:28 +00:00
gh.events = make([]Event, 0)
return nil
}
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
}
func (gh *GithubWebhooks) Start() error {
2016-01-23 00:43:33 +00:00
go gh.Listen()
log.Printf("Started the github_webhooks service on %s\n", gh.ServiceAddress)
2016-01-20 20:19:03 +00:00
return nil
}
func (gh *GithubWebhooks) Stop() {
log.Println("Stopping the ghWebhooks service")
}
2016-01-23 00:43:33 +00:00
// Handles the / route
func (gh *GithubWebhooks) eventHandler(w http.ResponseWriter, r *http.Request) {
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-01-23 00:43:33 +00:00
e, err := NewEvent(data, eventType)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
}
gh.Lock()
gh.events = append(gh.events, e)
gh.Unlock()
w.WriteHeader(http.StatusOK)
}
2016-01-26 01:28:28 +00:00
func newCommitComment(data []byte) (Event, error) {
commitCommentStruct := CommitCommentEvent{}
err := json.Unmarshal(data, &commitCommentStruct)
if err != nil {
2016-01-23 00:43:33 +00:00
return nil, err
}
2016-01-23 00:43:33 +00:00
return commitCommentStruct, nil
}
2016-01-26 01:28:28 +00:00
func newCreate(data []byte) (Event, error) {
createStruct := CreateEvent{}
err := json.Unmarshal(data, &createStruct)
if err != nil {
2016-01-23 00:43:33 +00:00
return nil, err
}
2016-01-23 00:43:33 +00:00
return createStruct, nil
}
2016-01-26 01:28:28 +00:00
func newDelete(data []byte) (Event, error) {
deleteStruct := DeleteEvent{}
err := json.Unmarshal(data, &deleteStruct)
if err != nil {
2016-01-23 00:43:33 +00:00
return nil, err
}
2016-01-23 00:43:33 +00:00
return deleteStruct, nil
}
2016-01-26 01:28:28 +00:00
func newDeployment(data []byte) (Event, error) {
deploymentStruct := DeploymentEvent{}
err := json.Unmarshal(data, &deploymentStruct)
if err != nil {
2016-01-23 00:43:33 +00:00
return nil, err
}
2016-01-23 00:43:33 +00:00
return deploymentStruct, nil
}
2016-01-26 01:28:28 +00:00
func newDeploymentStatus(data []byte) (Event, error) {
deploymentStatusStruct := DeploymentStatusEvent{}
err := json.Unmarshal(data, &deploymentStatusStruct)
if err != nil {
2016-01-23 00:43:33 +00:00
return nil, err
}
2016-01-23 00:43:33 +00:00
return deploymentStatusStruct, nil
}
2016-01-26 01:28:28 +00:00
func newFork(data []byte) (Event, error) {
forkStruct := ForkEvent{}
err := json.Unmarshal(data, &forkStruct)
if err != nil {
2016-01-23 00:43:33 +00:00
return nil, err
}
2016-01-23 00:43:33 +00:00
return forkStruct, nil
}
2016-01-26 01:28:28 +00:00
func newGollum(data []byte) (Event, error) {
gollumStruct := GollumEvent{}
err := json.Unmarshal(data, &gollumStruct)
if err != nil {
2016-01-23 00:43:33 +00:00
return nil, err
}
2016-01-23 00:43:33 +00:00
return gollumStruct, nil
}
2016-01-26 01:28:28 +00:00
func newIssueComment(data []byte) (Event, error) {
issueCommentStruct := IssueCommentEvent{}
err := json.Unmarshal(data, &issueCommentStruct)
if err != nil {
2016-01-23 00:43:33 +00:00
return nil, err
}
2016-01-23 00:43:33 +00:00
return issueCommentStruct, nil
}
2016-01-26 01:28:28 +00:00
func newIssues(data []byte) (Event, error) {
issuesStruct := IssuesEvent{}
err := json.Unmarshal(data, &issuesStruct)
if err != nil {
2016-01-23 00:43:33 +00:00
return nil, err
}
2016-01-23 00:43:33 +00:00
return issuesStruct, nil
}
2016-01-26 01:28:28 +00:00
func newMember(data []byte) (Event, error) {
memberStruct := MemberEvent{}
err := json.Unmarshal(data, &memberStruct)
if err != nil {
2016-01-23 00:43:33 +00:00
return nil, err
}
2016-01-23 00:43:33 +00:00
return memberStruct, nil
}
2016-01-26 01:28:28 +00:00
func newMembership(data []byte) (Event, error) {
membershipStruct := MembershipEvent{}
err := json.Unmarshal(data, &membershipStruct)
if err != nil {
2016-01-23 00:43:33 +00:00
return nil, err
}
2016-01-23 00:43:33 +00:00
return membershipStruct, nil
}
2016-01-26 01:28:28 +00:00
func newPageBuild(data []byte) (Event, error) {
pageBuildEvent := PageBuildEvent{}
err := json.Unmarshal(data, &pageBuildEvent)
if err != nil {
2016-01-23 00:43:33 +00:00
return nil, err
}
2016-01-23 00:43:33 +00:00
return pageBuildEvent, nil
}
2016-01-26 01:28:28 +00:00
func newPublic(data []byte) (Event, error) {
publicEvent := PublicEvent{}
err := json.Unmarshal(data, &publicEvent)
if err != nil {
2016-01-23 00:43:33 +00:00
return nil, err
}
2016-01-23 00:43:33 +00:00
return publicEvent, nil
}
2016-01-26 01:28:28 +00:00
func newPullRequest(data []byte) (Event, error) {
pullRequestStruct := PullRequestEvent{}
err := json.Unmarshal(data, &pullRequestStruct)
if err != nil {
2016-01-23 00:43:33 +00:00
return nil, err
}
2016-01-23 00:43:33 +00:00
return pullRequestStruct, nil
}
2016-01-26 01:28:28 +00:00
func newPullRequestReviewComment(data []byte) (Event, error) {
pullRequestReviewCommentStruct := PullRequestReviewCommentEvent{}
err := json.Unmarshal(data, &pullRequestReviewCommentStruct)
if err != nil {
2016-01-23 00:43:33 +00:00
return nil, err
}
2016-01-23 00:43:33 +00:00
return pullRequestReviewCommentStruct, nil
}
2016-01-26 01:28:28 +00:00
func newPush(data []byte) (Event, error) {
pushStruct := PushEvent{}
err := json.Unmarshal(data, &pushStruct)
if err != nil {
2016-01-23 00:43:33 +00:00
return nil, err
}
2016-01-23 00:43:33 +00:00
return pushStruct, nil
}
2016-01-26 01:28:28 +00:00
func newRelease(data []byte) (Event, error) {
releaseStruct := ReleaseEvent{}
err := json.Unmarshal(data, &releaseStruct)
if err != nil {
2016-01-23 00:43:33 +00:00
return nil, err
}
2016-01-23 00:43:33 +00:00
return releaseStruct, nil
}
2016-01-26 01:28:28 +00:00
func newRepository(data []byte) (Event, error) {
repositoryStruct := RepositoryEvent{}
err := json.Unmarshal(data, &repositoryStruct)
if err != nil {
2016-01-23 00:43:33 +00:00
return nil, err
}
2016-01-23 00:43:33 +00:00
return repositoryStruct, nil
}
2016-01-26 01:28:28 +00:00
func newStatus(data []byte) (Event, error) {
statusStruct := StatusEvent{}
err := json.Unmarshal(data, &statusStruct)
if err != nil {
2016-01-23 00:43:33 +00:00
return nil, err
}
2016-01-23 00:43:33 +00:00
return statusStruct, nil
}
2016-01-26 01:28:28 +00:00
func newTeamAdd(data []byte) (Event, error) {
teamAddStruct := TeamAddEvent{}
err := json.Unmarshal(data, &teamAddStruct)
if err != nil {
2016-01-23 00:43:33 +00:00
return nil, err
}
2016-01-23 00:43:33 +00:00
return teamAddStruct, nil
}
2016-01-26 01:28:28 +00:00
func newWatch(data []byte) (Event, error) {
watchStruct := WatchEvent{}
err := json.Unmarshal(data, &watchStruct)
if err != nil {
2016-01-23 00:43:33 +00:00
return nil, err
}
2016-01-23 00:43:33 +00:00
return watchStruct, nil
}
type newEventError struct {
s string
}
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)
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"}
}