Add tests

This commit is contained in:
Jack Zampolin 2016-01-22 16:43:33 -08:00
parent ddcd99a1ce
commit 46b367e74b
4 changed files with 3946 additions and 216 deletions

View File

@ -4,11 +4,10 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"sync" "sync"
"time"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux" "github.com/gorilla/mux"
mod "github.com/influxdata/support-tools/ghWebhooks/models" mod "github.com/influxdata/support-tools/ghWebhooks/models"
"github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/inputs"
@ -21,12 +20,14 @@ func init() {
type GHWebhooks struct { type GHWebhooks struct {
ServiceAddress string ServiceAddress string
MeasurementName string MeasurementName string
// Lock for the struct
sync.Mutex sync.Mutex
// Events buffer to store events between Gather calls
events []mod.Event
}
// Channel for all incoming events from github func NewGHWebhooks() *GHWebhooks {
in chan mod.Event return &GHWebhooks{}
done chan struct{}
} }
func (gh *GHWebhooks) SampleConfig() string { func (gh *GHWebhooks) SampleConfig() string {
@ -34,290 +35,261 @@ func (gh *GHWebhooks) SampleConfig() string {
# Address and port to host Webhook listener on # Address and port to host Webhook listener on
service_address = ":1618" service_address = ":1618"
# Measurement name # Measurement name
measurement_name = "ghWebhooks" measurement_name = "ghwebhooks"
` `
} }
func (gh *GHWebhooks) Description() string { func (gh *GHWebhooks) Description() string {
return "Github Webhook Event collector" return "A Github Webhook Event collector"
} }
// Writes the points from <-gh.in to the Accumulator // Writes the points from <-gh.in to the Accumulator
func (gh *GHWebhooks) Gather(acc inputs.Accumulator) error { func (gh *GHWebhooks) Gather(acc inputs.Accumulator) error {
gh.Lock() gh.Lock()
defer gh.Unlock() defer gh.Unlock()
for { for _, event := range gh.events {
select { p := event.NewPoint()
case <-gh.done: acc.AddFields(gh.MeasurementName, p.Fields(), p.Tags(), p.Time())
return nil
case e := <-gh.in:
p := e.NewPoint()
acc.Add(gh.MeasurementName, p.Fields(), p.Tags(), p.Time())
}
} }
gh.events = make([]mod.Event, 0)
return nil return nil
} }
func (gh *GHWebhooks) listen() error { func (gh *GHWebhooks) Listen() {
fmt.Println("in listen!")
r := mux.NewRouter() r := mux.NewRouter()
r.HandleFunc("/", gh.webhookHandler).Methods("POST") r.HandleFunc("/", gh.eventHandler).Methods("POST")
err := http.ListenAndServe(fmt.Sprintf(":%s", gh.ServiceAddress), r) err := http.ListenAndServe(fmt.Sprintf("%s", gh.ServiceAddress), r)
if err != nil { if err != nil {
return err log.Printf("Error starting server: %v", err)
} }
fmt.Println("Exiting listen")
return nil
} }
func (gh *GHWebhooks) Start() error { func (gh *GHWebhooks) Start() error {
fmt.Println("In start function") go gh.Listen()
gh.done = make(chan struct{})
gh.in = make(chan mod.Event)
// Start the UDP listener
go gh.listen()
// Start the line parser
log.Printf("Started the ghwebhooks service on %s\n", gh.ServiceAddress) log.Printf("Started the ghwebhooks service on %s\n", gh.ServiceAddress)
return nil return nil
} }
func (gh *GHWebhooks) Stop() { func (gh *GHWebhooks) Stop() {
gh.Lock()
defer gh.Unlock()
log.Println("Stopping the ghWebhooks service") log.Println("Stopping the ghWebhooks service")
close(gh.done)
close(gh.in)
} }
// Handles the /webhooks route // Handles the / route
func (gh *GHWebhooks) webhookHandler(w http.ResponseWriter, r *http.Request) { func (gh *GHWebhooks) eventHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println("In webhook handler")
eventType := r.Header["X-Github-Event"][0] eventType := r.Header["X-Github-Event"][0]
data, err := ioutil.ReadAll(r.Body) data, err := ioutil.ReadAll(r.Body)
if err != nil { if err != nil {
fields := log.Fields{"time": time.Now(), "event": eventType, "error": err} w.WriteHeader(http.StatusBadRequest)
log.WithFields(fields).Fatal("Error reading Github payload")
} }
e, err := NewEvent(data, eventType)
// Send event down chan to GHWebhooks if err != nil {
e := NewEvent(data, eventType) w.WriteHeader(http.StatusBadRequest)
gh.in <- e }
fmt.Printf("%v\n", e.NewPoint()) gh.Lock()
gh.events = append(gh.events, e)
gh.Unlock()
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
func newCommitComment(data []byte) mod.Event { func newCommitComment(data []byte) (mod.Event, error) {
commitCommentStruct := mod.CommitCommentEvent{} commitCommentStruct := mod.CommitCommentEvent{}
err := json.Unmarshal(data, &commitCommentStruct) err := json.Unmarshal(data, &commitCommentStruct)
if err != nil { if err != nil {
fields := log.Fields{"time": time.Now(), "event": "CommitCommentEvent", "error": err} return nil, err
log.WithFields(fields).Fatalf("Error in unmarshaling JSON")
} }
return commitCommentStruct return commitCommentStruct, nil
} }
func newCreate(data []byte) mod.Event { func newCreate(data []byte) (mod.Event, error) {
createStruct := mod.CreateEvent{} createStruct := mod.CreateEvent{}
err := json.Unmarshal(data, &createStruct) err := json.Unmarshal(data, &createStruct)
if err != nil { if err != nil {
fields := log.Fields{"time": time.Now(), "event": "CreateEvent", "error": err} return nil, err
log.WithFields(fields).Fatalf("Error in unmarshaling JSON")
} }
return createStruct return createStruct, nil
} }
func newDelete(data []byte) mod.Event { func newDelete(data []byte) (mod.Event, error) {
deleteStruct := mod.DeleteEvent{} deleteStruct := mod.DeleteEvent{}
err := json.Unmarshal(data, &deleteStruct) err := json.Unmarshal(data, &deleteStruct)
if err != nil { if err != nil {
fields := log.Fields{"time": time.Now(), "event": "DeleteEvent", "error": err} return nil, err
log.WithFields(fields).Fatalf("Error in unmarshaling JSON")
} }
return deleteStruct return deleteStruct, nil
} }
func newDeployment(data []byte) mod.Event { func newDeployment(data []byte) (mod.Event, error) {
deploymentStruct := mod.DeploymentEvent{} deploymentStruct := mod.DeploymentEvent{}
err := json.Unmarshal(data, &deploymentStruct) err := json.Unmarshal(data, &deploymentStruct)
if err != nil { if err != nil {
fields := log.Fields{"time": time.Now(), "event": "DeploymentEvent", "error": err} return nil, err
log.WithFields(fields).Fatalf("Error in unmarshaling JSON")
} }
return deploymentStruct return deploymentStruct, nil
} }
func newDeploymentStatus(data []byte) mod.Event { func newDeploymentStatus(data []byte) (mod.Event, error) {
deploymentStatusStruct := mod.DeploymentStatusEvent{} deploymentStatusStruct := mod.DeploymentStatusEvent{}
err := json.Unmarshal(data, &deploymentStatusStruct) err := json.Unmarshal(data, &deploymentStatusStruct)
if err != nil { if err != nil {
fields := log.Fields{"time": time.Now(), "event": "DeploymentStatusEvent", "error": err} return nil, err
log.WithFields(fields).Fatalf("Error in unmarshaling JSON")
} }
return deploymentStatusStruct return deploymentStatusStruct, nil
} }
func newFork(data []byte) mod.Event { func newFork(data []byte) (mod.Event, error) {
forkStruct := mod.ForkEvent{} forkStruct := mod.ForkEvent{}
err := json.Unmarshal(data, &forkStruct) err := json.Unmarshal(data, &forkStruct)
if err != nil { if err != nil {
fields := log.Fields{"time": time.Now(), "event": "ForkEvent", "error": err} return nil, err
log.WithFields(fields).Fatalf("Error in unmarshaling JSON")
} }
return forkStruct return forkStruct, nil
} }
func newGollum(data []byte) mod.Event { func newGollum(data []byte) (mod.Event, error) {
gollumStruct := mod.GollumEvent{} gollumStruct := mod.GollumEvent{}
err := json.Unmarshal(data, &gollumStruct) err := json.Unmarshal(data, &gollumStruct)
if err != nil { if err != nil {
fields := log.Fields{"time": time.Now(), "event": "GollumEvent", "error": err} return nil, err
log.WithFields(fields).Fatalf("Error in unmarshaling JSON")
} }
return gollumStruct return gollumStruct, nil
} }
func newIssueComment(data []byte) mod.Event { func newIssueComment(data []byte) (mod.Event, error) {
issueCommentStruct := mod.IssueCommentEvent{} issueCommentStruct := mod.IssueCommentEvent{}
err := json.Unmarshal(data, &issueCommentStruct) err := json.Unmarshal(data, &issueCommentStruct)
if err != nil { if err != nil {
fields := log.Fields{"time": time.Now(), "event": "IssueCommentEvent", "error": err} return nil, err
log.WithFields(fields).Fatalf("Error in unmarshaling JSON")
} }
return issueCommentStruct return issueCommentStruct, nil
} }
func newIssues(data []byte) mod.Event { func newIssues(data []byte) (mod.Event, error) {
issuesStruct := mod.IssuesEvent{} issuesStruct := mod.IssuesEvent{}
err := json.Unmarshal(data, &issuesStruct) err := json.Unmarshal(data, &issuesStruct)
if err != nil { if err != nil {
fields := log.Fields{"time": time.Now(), "event": "IssuesEvent", "error": err} return nil, err
log.WithFields(fields).Fatalf("Error in unmarshaling JSON")
} }
return issuesStruct return issuesStruct, nil
} }
func newMember(data []byte) mod.Event { func newMember(data []byte) (mod.Event, error) {
memberStruct := mod.MemberEvent{} memberStruct := mod.MemberEvent{}
err := json.Unmarshal(data, &memberStruct) err := json.Unmarshal(data, &memberStruct)
if err != nil { if err != nil {
fields := log.Fields{"time": time.Now(), "event": "MemberEvent", "error": err} return nil, err
log.WithFields(fields).Fatalf("Error in unmarshaling JSON")
} }
return memberStruct return memberStruct, nil
} }
func newMembership(data []byte) mod.Event { func newMembership(data []byte) (mod.Event, error) {
membershipStruct := mod.MembershipEvent{} membershipStruct := mod.MembershipEvent{}
err := json.Unmarshal(data, &membershipStruct) err := json.Unmarshal(data, &membershipStruct)
if err != nil { if err != nil {
fields := log.Fields{"time": time.Now(), "event": "MembershipEvent", "error": err} return nil, err
log.WithFields(fields).Fatalf("Error in unmarshaling JSON")
} }
return membershipStruct return membershipStruct, nil
} }
func newPageBuild(data []byte) mod.Event { func newPageBuild(data []byte) (mod.Event, error) {
pageBuildEvent := mod.PageBuildEvent{} pageBuildEvent := mod.PageBuildEvent{}
err := json.Unmarshal(data, &pageBuildEvent) err := json.Unmarshal(data, &pageBuildEvent)
if err != nil { if err != nil {
fields := log.Fields{"time": time.Now(), "event": "PageBuildEvent", "error": err} return nil, err
log.WithFields(fields).Fatalf("Error in unmarshaling JSON")
} }
return pageBuildEvent return pageBuildEvent, nil
} }
func newPublic(data []byte) mod.Event { func newPublic(data []byte) (mod.Event, error) {
publicEvent := mod.PublicEvent{} publicEvent := mod.PublicEvent{}
err := json.Unmarshal(data, &publicEvent) err := json.Unmarshal(data, &publicEvent)
if err != nil { if err != nil {
fields := log.Fields{"time": time.Now(), "event": "PublicEvent", "error": err} return nil, err
log.WithFields(fields).Fatalf("Error in unmarshaling JSON")
} }
return publicEvent return publicEvent, nil
} }
func newPullRequest(data []byte) mod.Event { func newPullRequest(data []byte) (mod.Event, error) {
pullRequestStruct := mod.PullRequestEvent{} pullRequestStruct := mod.PullRequestEvent{}
err := json.Unmarshal(data, &pullRequestStruct) err := json.Unmarshal(data, &pullRequestStruct)
if err != nil { if err != nil {
fields := log.Fields{"time": time.Now(), "event": "PullRequestEvent", "error": err} return nil, err
log.WithFields(fields).Fatalf("Error in unmarshaling JSON")
} }
return pullRequestStruct return pullRequestStruct, nil
} }
func newPullRequestReviewComment(data []byte) mod.Event { func newPullRequestReviewComment(data []byte) (mod.Event, error) {
pullRequestReviewCommentStruct := mod.PullRequestReviewCommentEvent{} pullRequestReviewCommentStruct := mod.PullRequestReviewCommentEvent{}
err := json.Unmarshal(data, &pullRequestReviewCommentStruct) err := json.Unmarshal(data, &pullRequestReviewCommentStruct)
if err != nil { if err != nil {
fields := log.Fields{"time": time.Now(), "event": "PullRequestReviewCommentEvent", "error": err} return nil, err
log.WithFields(fields).Fatalf("Error in unmarshaling JSON")
} }
return pullRequestReviewCommentStruct return pullRequestReviewCommentStruct, nil
} }
func newPush(data []byte) mod.Event { func newPush(data []byte) (mod.Event, error) {
pushStruct := mod.PushEvent{} pushStruct := mod.PushEvent{}
err := json.Unmarshal(data, &pushStruct) err := json.Unmarshal(data, &pushStruct)
if err != nil { if err != nil {
fields := log.Fields{"time": time.Now(), "event": "PushEvent", "error": err} return nil, err
log.WithFields(fields).Fatalf("Error in unmarshaling JSON")
} }
return pushStruct return pushStruct, nil
} }
func newRelease(data []byte) mod.Event { func newRelease(data []byte) (mod.Event, error) {
releaseStruct := mod.ReleaseEvent{} releaseStruct := mod.ReleaseEvent{}
err := json.Unmarshal(data, &releaseStruct) err := json.Unmarshal(data, &releaseStruct)
if err != nil { if err != nil {
fields := log.Fields{"time": time.Now(), "event": "ReleaseEvent", "error": err} return nil, err
log.WithFields(fields).Fatalf("Error in unmarshaling JSON")
} }
return releaseStruct return releaseStruct, nil
} }
func newRepository(data []byte) mod.Event { func newRepository(data []byte) (mod.Event, error) {
repositoryStruct := mod.RepositoryEvent{} repositoryStruct := mod.RepositoryEvent{}
err := json.Unmarshal(data, &repositoryStruct) err := json.Unmarshal(data, &repositoryStruct)
if err != nil { if err != nil {
fields := log.Fields{"time": time.Now(), "event": "RepositoryEvent", "error": err} return nil, err
log.WithFields(fields).Fatalf("Error in unmarshaling JSON")
} }
return repositoryStruct return repositoryStruct, nil
} }
func newStatus(data []byte) mod.Event { func newStatus(data []byte) (mod.Event, error) {
statusStruct := mod.StatusEvent{} statusStruct := mod.StatusEvent{}
err := json.Unmarshal(data, &statusStruct) err := json.Unmarshal(data, &statusStruct)
if err != nil { if err != nil {
fields := log.Fields{"time": time.Now(), "event": "StatusEvent", "error": err} return nil, err
log.WithFields(fields).Fatalf("Error in unmarshaling JSON")
} }
return statusStruct return statusStruct, nil
} }
func newTeamAdd(data []byte) mod.Event { func newTeamAdd(data []byte) (mod.Event, error) {
teamAddStruct := mod.TeamAddEvent{} teamAddStruct := mod.TeamAddEvent{}
err := json.Unmarshal(data, &teamAddStruct) err := json.Unmarshal(data, &teamAddStruct)
if err != nil { if err != nil {
fields := log.Fields{"time": time.Now(), "event": "TeamAddEvent", "error": err} return nil, err
log.WithFields(fields).Fatalf("Error in unmarshaling JSON")
} }
return teamAddStruct return teamAddStruct, nil
} }
func newWatch(data []byte) mod.Event { func newWatch(data []byte) (mod.Event, error) {
watchStruct := mod.WatchEvent{} watchStruct := mod.WatchEvent{}
err := json.Unmarshal(data, &watchStruct) err := json.Unmarshal(data, &watchStruct)
if err != nil { if err != nil {
fields := log.Fields{"time": time.Now(), "event": "WatchEvent", "error": err} return nil, err
log.WithFields(fields).Fatalf("Error in unmarshaling JSON")
} }
return watchStruct return watchStruct, nil
} }
func NewEvent(r []byte, t string) mod.Event { type newEventError struct {
log.WithFields(log.Fields{"event": t, "time": time.Now()}).Info("Event Recieved") s string
}
func (e *newEventError) Error() string {
return e.s
}
func NewEvent(r []byte, t string) (mod.Event, error) {
log.Printf("New %v event recieved", t)
switch t { switch t {
case "commit_comment": case "commit_comment":
return newCommitComment(r) return newCommitComment(r)
@ -362,5 +334,5 @@ func NewEvent(r []byte, t string) mod.Event {
case "watch": case "watch":
return newWatch(r) return newWatch(r)
} }
return nil return nil, &newEventError{"Not a recgonized event type"}
} }

View File

@ -0,0 +1,238 @@
package ghwebhooks
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
mod "github.com/influxdata/telegraf/plugins/inputs/ghwebhooks/models"
)
func TestCommitCommentEvent(t *testing.T) {
gh := NewGHWebhooks()
jsonString := mod.Mock{}.CommitCommentEventJSON()
req, _ := http.NewRequest("POST", "/", strings.NewReader(jsonString))
req.Header.Add("X-Github-Event", "commit_comment")
w := httptest.NewRecorder()
gh.eventHandler(w, req)
if w.Code != http.StatusOK {
t.Errorf("POST commit_comment returned HTTP status code %v.\nExpected %v", w.Code, http.StatusOK)
}
}
func TestDeleteEvent(t *testing.T) {
gh := NewGHWebhooks()
jsonString := mod.Mock{}.DeleteEventJSON()
req, _ := http.NewRequest("POST", "/", strings.NewReader(jsonString))
req.Header.Add("X-Github-Event", "delete")
w := httptest.NewRecorder()
gh.eventHandler(w, req)
if w.Code != http.StatusOK {
t.Errorf("POST commit_comment returned HTTP status code %v.\nExpected %v", w.Code, http.StatusOK)
}
}
func TestDeploymentEvent(t *testing.T) {
gh := NewGHWebhooks()
jsonString := mod.Mock{}.DeploymentEventJSON()
req, _ := http.NewRequest("POST", "/", strings.NewReader(jsonString))
req.Header.Add("X-Github-Event", "deployment")
w := httptest.NewRecorder()
gh.eventHandler(w, req)
if w.Code != http.StatusOK {
t.Errorf("POST commit_comment returned HTTP status code %v.\nExpected %v", w.Code, http.StatusOK)
}
}
func TestDeploymentStatusEvent(t *testing.T) {
gh := NewGHWebhooks()
jsonString := mod.Mock{}.DeploymentStatusEventJSON()
req, _ := http.NewRequest("POST", "/", strings.NewReader(jsonString))
req.Header.Add("X-Github-Event", "deployment_status")
w := httptest.NewRecorder()
gh.eventHandler(w, req)
if w.Code != http.StatusOK {
t.Errorf("POST commit_comment returned HTTP status code %v.\nExpected %v", w.Code, http.StatusOK)
}
}
func TestForkEvent(t *testing.T) {
gh := NewGHWebhooks()
jsonString := mod.Mock{}.ForkEventJSON()
req, _ := http.NewRequest("POST", "/", strings.NewReader(jsonString))
req.Header.Add("X-Github-Event", "fork")
w := httptest.NewRecorder()
gh.eventHandler(w, req)
if w.Code != http.StatusOK {
t.Errorf("POST commit_comment returned HTTP status code %v.\nExpected %v", w.Code, http.StatusOK)
}
}
func TestGollumEvent(t *testing.T) {
gh := NewGHWebhooks()
jsonString := mod.Mock{}.GollumEventJSON()
req, _ := http.NewRequest("POST", "/", strings.NewReader(jsonString))
req.Header.Add("X-Github-Event", "gollum")
w := httptest.NewRecorder()
gh.eventHandler(w, req)
if w.Code != http.StatusOK {
t.Errorf("POST commit_comment returned HTTP status code %v.\nExpected %v", w.Code, http.StatusOK)
}
}
func TestIssueCommentEvent(t *testing.T) {
gh := NewGHWebhooks()
jsonString := mod.Mock{}.IssueCommentEventJSON()
req, _ := http.NewRequest("POST", "/", strings.NewReader(jsonString))
req.Header.Add("X-Github-Event", "issue_comment")
w := httptest.NewRecorder()
gh.eventHandler(w, req)
if w.Code != http.StatusOK {
t.Errorf("POST commit_comment returned HTTP status code %v.\nExpected %v", w.Code, http.StatusOK)
}
}
func TestIssuesEvent(t *testing.T) {
gh := NewGHWebhooks()
jsonString := mod.Mock{}.IssuesEventJSON()
req, _ := http.NewRequest("POST", "/", strings.NewReader(jsonString))
req.Header.Add("X-Github-Event", "issues")
w := httptest.NewRecorder()
gh.eventHandler(w, req)
if w.Code != http.StatusOK {
t.Errorf("POST commit_comment returned HTTP status code %v.\nExpected %v", w.Code, http.StatusOK)
}
}
func TestMemberEvent(t *testing.T) {
gh := NewGHWebhooks()
jsonString := mod.Mock{}.MemberEventJSON()
req, _ := http.NewRequest("POST", "/", strings.NewReader(jsonString))
req.Header.Add("X-Github-Event", "member")
w := httptest.NewRecorder()
gh.eventHandler(w, req)
if w.Code != http.StatusOK {
t.Errorf("POST commit_comment returned HTTP status code %v.\nExpected %v", w.Code, http.StatusOK)
}
}
func TestMembershipEvent(t *testing.T) {
gh := NewGHWebhooks()
jsonString := mod.Mock{}.MembershipEventJSON()
req, _ := http.NewRequest("POST", "/", strings.NewReader(jsonString))
req.Header.Add("X-Github-Event", "membership")
w := httptest.NewRecorder()
gh.eventHandler(w, req)
if w.Code != http.StatusOK {
t.Errorf("POST commit_comment returned HTTP status code %v.\nExpected %v", w.Code, http.StatusOK)
}
}
func TestPageBuildEvent(t *testing.T) {
gh := NewGHWebhooks()
jsonString := mod.Mock{}.PageBuildEventJSON()
req, _ := http.NewRequest("POST", "/", strings.NewReader(jsonString))
req.Header.Add("X-Github-Event", "page_build")
w := httptest.NewRecorder()
gh.eventHandler(w, req)
if w.Code != http.StatusOK {
t.Errorf("POST commit_comment returned HTTP status code %v.\nExpected %v", w.Code, http.StatusOK)
}
}
func TestPublicEvent(t *testing.T) {
gh := NewGHWebhooks()
jsonString := mod.Mock{}.PublicEventJSON()
req, _ := http.NewRequest("POST", "/", strings.NewReader(jsonString))
req.Header.Add("X-Github-Event", "public")
w := httptest.NewRecorder()
gh.eventHandler(w, req)
if w.Code != http.StatusOK {
t.Errorf("POST commit_comment returned HTTP status code %v.\nExpected %v", w.Code, http.StatusOK)
}
}
func TestPullRequestReviewCommentEvent(t *testing.T) {
gh := NewGHWebhooks()
jsonString := mod.Mock{}.PullRequestReviewCommentEventJSON()
req, _ := http.NewRequest("POST", "/", strings.NewReader(jsonString))
req.Header.Add("X-Github-Event", "pull_request_review_comment")
w := httptest.NewRecorder()
gh.eventHandler(w, req)
if w.Code != http.StatusOK {
t.Errorf("POST commit_comment returned HTTP status code %v.\nExpected %v", w.Code, http.StatusOK)
}
}
func TestPushEvent(t *testing.T) {
gh := NewGHWebhooks()
jsonString := mod.Mock{}.PushEventJSON()
req, _ := http.NewRequest("POST", "/", strings.NewReader(jsonString))
req.Header.Add("X-Github-Event", "push")
w := httptest.NewRecorder()
gh.eventHandler(w, req)
if w.Code != http.StatusOK {
t.Errorf("POST commit_comment returned HTTP status code %v.\nExpected %v", w.Code, http.StatusOK)
}
}
func TestReleaseEvent(t *testing.T) {
gh := NewGHWebhooks()
jsonString := mod.Mock{}.ReleaseEventJSON()
req, _ := http.NewRequest("POST", "/", strings.NewReader(jsonString))
req.Header.Add("X-Github-Event", "release")
w := httptest.NewRecorder()
gh.eventHandler(w, req)
if w.Code != http.StatusOK {
t.Errorf("POST commit_comment returned HTTP status code %v.\nExpected %v", w.Code, http.StatusOK)
}
}
func TestRepositoryEvent(t *testing.T) {
gh := NewGHWebhooks()
jsonString := mod.Mock{}.RepositoryEventJSON()
req, _ := http.NewRequest("POST", "/", strings.NewReader(jsonString))
req.Header.Add("X-Github-Event", "repository")
w := httptest.NewRecorder()
gh.eventHandler(w, req)
if w.Code != http.StatusOK {
t.Errorf("POST commit_comment returned HTTP status code %v.\nExpected %v", w.Code, http.StatusOK)
}
}
func TestStatusEvent(t *testing.T) {
gh := NewGHWebhooks()
jsonString := mod.Mock{}.StatusEventJSON()
req, _ := http.NewRequest("POST", "/", strings.NewReader(jsonString))
req.Header.Add("X-Github-Event", "status")
w := httptest.NewRecorder()
gh.eventHandler(w, req)
if w.Code != http.StatusOK {
t.Errorf("POST commit_comment returned HTTP status code %v.\nExpected %v", w.Code, http.StatusOK)
}
}
func TestTeamAddEvent(t *testing.T) {
gh := NewGHWebhooks()
jsonString := mod.Mock{}.TeamAddEventJSON()
req, _ := http.NewRequest("POST", "/", strings.NewReader(jsonString))
req.Header.Add("X-Github-Event", "team_add")
w := httptest.NewRecorder()
gh.eventHandler(w, req)
if w.Code != http.StatusOK {
t.Errorf("POST commit_comment returned HTTP status code %v.\nExpected %v", w.Code, http.StatusOK)
}
}
func TestWatchEvent(t *testing.T) {
gh := NewGHWebhooks()
jsonString := mod.Mock{}.WatchEventJSON()
req, _ := http.NewRequest("POST", "/", strings.NewReader(jsonString))
req.Header.Add("X-Github-Event", "watch")
w := httptest.NewRecorder()
gh.eventHandler(w, req)
if w.Code != http.StatusOK {
t.Errorf("POST commit_comment returned HTTP status code %v.\nExpected %v", w.Code, http.StatusOK)
}
}

File diff suppressed because it is too large Load Diff

View File

@ -2,16 +2,17 @@ package models
import ( import (
"fmt" "fmt"
"log"
"time" "time"
log "github.com/Sirupsen/logrus"
"github.com/influxdata/influxdb/client/v2" "github.com/influxdata/influxdb/client/v2"
) )
const Measurement = "ghWebhooks" const meas = "ghWebhooks"
type Event interface { type Event interface {
NewPoint() *client.Point NewPoint() *client.Point
JSON() string
} }
type Repository struct { type Repository struct {
@ -106,11 +107,9 @@ func (s CommitCommentEvent) NewPoint() *client.Point {
"commit": s.Comment.Commit, "commit": s.Comment.Commit,
"comment": s.Comment.Body, "comment": s.Comment.Body,
} }
time := time.Now() p, err := client.NewPoint(meas, t, f, time.Now())
p, err := client.NewPoint(Measurement, t, f, time)
if err != nil { if err != nil {
i := log.Fields{"event": event, "error": err} log.Fatalf("Failed to create %v event", event)
log.WithFields(i).Fatal("Error creating new point...")
} }
return p return p
} }
@ -138,11 +137,9 @@ func (s CreateEvent) NewPoint() *client.Point {
"ref": s.Ref, "ref": s.Ref,
"refType": s.RefType, "refType": s.RefType,
} }
time := time.Now() p, err := client.NewPoint(meas, t, f, time.Now())
p, err := client.NewPoint(Measurement, t, f, time)
if err != nil { if err != nil {
i := log.Fields{"event": event, "error": err} log.Fatalf("Failed to create %v event", event)
log.WithFields(i).Fatal("Error creating new point...")
} }
return p return p
} }
@ -170,11 +167,9 @@ func (s DeleteEvent) NewPoint() *client.Point {
"ref": s.Ref, "ref": s.Ref,
"refType": s.RefType, "refType": s.RefType,
} }
time := time.Now() p, err := client.NewPoint(meas, t, f, time.Now())
p, err := client.NewPoint(Measurement, t, f, time)
if err != nil { if err != nil {
i := log.Fields{"event": event, "error": err} log.Fatalf("Failed to create %v event", event)
log.WithFields(i).Fatal("Error creating new point...")
} }
return p return p
} }
@ -203,11 +198,9 @@ func (s DeploymentEvent) NewPoint() *client.Point {
"environment": s.Deployment.Environment, "environment": s.Deployment.Environment,
"description": s.Deployment.Description, "description": s.Deployment.Description,
} }
time := time.Now() p, err := client.NewPoint(meas, t, f, time.Now())
p, err := client.NewPoint(Measurement, t, f, time)
if err != nil { if err != nil {
i := log.Fields{"event": event, "error": err} log.Fatalf("Failed to create %v event", event)
log.WithFields(i).Fatal("Error creating new point...")
} }
return p return p
} }
@ -239,11 +232,9 @@ func (s DeploymentStatusEvent) NewPoint() *client.Point {
"depState": s.DeploymentStatus.State, "depState": s.DeploymentStatus.State,
"depDescription": s.DeploymentStatus.Description, "depDescription": s.DeploymentStatus.Description,
} }
time := time.Now() p, err := client.NewPoint(meas, t, f, time.Now())
p, err := client.NewPoint(Measurement, t, f, time)
if err != nil { if err != nil {
i := log.Fields{"event": event, "error": err} log.Fatalf("Failed to create %v event", event)
log.WithFields(i).Fatal("Error creating new point...")
} }
return p return p
} }
@ -269,11 +260,9 @@ func (s ForkEvent) NewPoint() *client.Point {
"issues": s.Repository.Issues, "issues": s.Repository.Issues,
"fork": s.Forkee.Repository, "fork": s.Forkee.Repository,
} }
time := time.Now() p, err := client.NewPoint(meas, t, f, time.Now())
p, err := client.NewPoint(Measurement, t, f, time)
if err != nil { if err != nil {
i := log.Fields{"event": event, "error": err} log.Fatalf("Failed to create %v event", event)
log.WithFields(i).Fatal("Error creating new point...")
} }
return p return p
} }
@ -299,11 +288,9 @@ func (s GollumEvent) NewPoint() *client.Point {
"forks": s.Repository.Forks, "forks": s.Repository.Forks,
"issues": s.Repository.Issues, "issues": s.Repository.Issues,
} }
time := time.Now() p, err := client.NewPoint(meas, t, f, time.Now())
p, err := client.NewPoint(Measurement, t, f, time)
if err != nil { if err != nil {
i := log.Fields{"event": event, "error": err} log.Fatalf("Failed to create %v event", event)
log.WithFields(i).Fatal("Error creating new point...")
} }
return p return p
} }
@ -333,11 +320,9 @@ func (s IssueCommentEvent) NewPoint() *client.Point {
"comments": s.Issue.Comments, "comments": s.Issue.Comments,
"body": s.Comment.Body, "body": s.Comment.Body,
} }
time := time.Now() p, err := client.NewPoint(meas, t, f, time.Now())
p, err := client.NewPoint(Measurement, t, f, time)
if err != nil { if err != nil {
i := log.Fields{"event": event, "error": err} log.Fatalf("Failed to create %v event", event)
log.WithFields(i).Fatal("Error creating new point...")
} }
return p return p
} }
@ -367,11 +352,9 @@ func (s IssuesEvent) NewPoint() *client.Point {
"title": s.Issue.Title, "title": s.Issue.Title,
"comments": s.Issue.Comments, "comments": s.Issue.Comments,
} }
time := time.Now() p, err := client.NewPoint(meas, t, f, time.Now())
p, err := client.NewPoint(Measurement, t, f, time)
if err != nil { if err != nil {
i := log.Fields{"event": event, "error": err} log.Fatalf("Failed to create %v event", event)
log.WithFields(i).Fatal("Error creating new point...")
} }
return p return p
} }
@ -398,11 +381,9 @@ func (s MemberEvent) NewPoint() *client.Point {
"newMember": s.Member.User, "newMember": s.Member.User,
"newMemberStatus": s.Member.Admin, "newMemberStatus": s.Member.Admin,
} }
time := time.Now() p, err := client.NewPoint(meas, t, f, time.Now())
p, err := client.NewPoint(Measurement, t, f, time)
if err != nil { if err != nil {
i := log.Fields{"event": event, "error": err} log.Fatalf("Failed to create %v event", event)
log.WithFields(i).Fatal("Error creating new point...")
} }
return p return p
} }
@ -426,11 +407,9 @@ func (s MembershipEvent) NewPoint() *client.Point {
"newMember": s.Member.User, "newMember": s.Member.User,
"newMemberStatus": s.Member.Admin, "newMemberStatus": s.Member.Admin,
} }
time := time.Now() p, err := client.NewPoint(meas, t, f, time.Now())
p, err := client.NewPoint(Measurement, t, f, time)
if err != nil { if err != nil {
i := log.Fields{"event": event, "error": err} log.Fatalf("Failed to create %v event", event)
log.WithFields(i).Fatal("Error creating new point...")
} }
return p return p
} }
@ -454,11 +433,9 @@ func (s PageBuildEvent) NewPoint() *client.Point {
"forks": s.Repository.Forks, "forks": s.Repository.Forks,
"issues": s.Repository.Issues, "issues": s.Repository.Issues,
} }
time := time.Now() p, err := client.NewPoint(meas, t, f, time.Now())
p, err := client.NewPoint(Measurement, t, f, time)
if err != nil { if err != nil {
i := log.Fields{"event": event, "error": err} log.Fatalf("Failed to create %v event", event)
log.WithFields(i).Fatal("Error creating new point...")
} }
return p return p
} }
@ -482,11 +459,9 @@ func (s PublicEvent) NewPoint() *client.Point {
"forks": s.Repository.Forks, "forks": s.Repository.Forks,
"issues": s.Repository.Issues, "issues": s.Repository.Issues,
} }
time := time.Now() p, err := client.NewPoint(meas, t, f, time.Now())
p, err := client.NewPoint(Measurement, t, f, time)
if err != nil { if err != nil {
i := log.Fields{"event": event, "error": err} log.Fatalf("Failed to create %v event", event)
log.WithFields(i).Fatal("Error creating new point...")
} }
return p return p
} }
@ -521,11 +496,9 @@ func (s PullRequestEvent) NewPoint() *client.Point {
"deletions": s.PullRequest.Deletions, "deletions": s.PullRequest.Deletions,
"changedFiles": s.PullRequest.ChangedFiles, "changedFiles": s.PullRequest.ChangedFiles,
} }
time := time.Now() p, err := client.NewPoint(meas, t, f, time.Now())
p, err := client.NewPoint(Measurement, t, f, time)
if err != nil { if err != nil {
i := log.Fields{"event": event, "error": err} log.Fatalf("Failed to create %v event", event)
log.WithFields(i).Fatal("Error creating new point...")
} }
return p return p
} }
@ -561,11 +534,9 @@ func (s PullRequestReviewCommentEvent) NewPoint() *client.Point {
"commentFile": s.Comment.File, "commentFile": s.Comment.File,
"comment": s.Comment.Comment, "comment": s.Comment.Comment,
} }
time := time.Now() p, err := client.NewPoint(meas, t, f, time.Now())
p, err := client.NewPoint(Measurement, t, f, time)
if err != nil { if err != nil {
i := log.Fields{"event": event, "error": err} log.Fatalf("Failed to create %v event", event)
log.WithFields(i).Fatal("Error creating new point...")
} }
return p return p
} }
@ -595,11 +566,9 @@ func (s PushEvent) NewPoint() *client.Point {
"Before": s.Before, "Before": s.Before,
"After": s.After, "After": s.After,
} }
time := time.Now() p, err := client.NewPoint(meas, t, f, time.Now())
p, err := client.NewPoint(Measurement, t, f, time)
if err != nil { if err != nil {
i := log.Fields{"event": event, "error": err} log.Fatalf("Failed to create %v event", event)
log.WithFields(i).Fatal("Error creating new point...")
} }
return p return p
} }
@ -625,11 +594,9 @@ func (s ReleaseEvent) NewPoint() *client.Point {
"issues": s.Repository.Issues, "issues": s.Repository.Issues,
"tagName": s.Release.TagName, "tagName": s.Release.TagName,
} }
time := time.Now() p, err := client.NewPoint(meas, t, f, time.Now())
p, err := client.NewPoint(Measurement, t, f, time)
if err != nil { if err != nil {
i := log.Fields{"event": event, "error": err} log.Fatalf("Failed to create %v event", event)
log.WithFields(i).Fatal("Error creating new point...")
} }
return p return p
} }
@ -653,11 +620,9 @@ func (s RepositoryEvent) NewPoint() *client.Point {
"forks": s.Repository.Forks, "forks": s.Repository.Forks,
"issues": s.Repository.Issues, "issues": s.Repository.Issues,
} }
time := time.Now() p, err := client.NewPoint(meas, t, f, time.Now())
p, err := client.NewPoint(Measurement, t, f, time)
if err != nil { if err != nil {
i := log.Fields{"event": event, "error": err} log.Fatalf("Failed to create %v event", event)
log.WithFields(i).Fatal("Error creating new point...")
} }
return p return p
} }
@ -685,11 +650,9 @@ func (s StatusEvent) NewPoint() *client.Point {
"commit": s.Commit, "commit": s.Commit,
"state": s.State, "state": s.State,
} }
time := time.Now() p, err := client.NewPoint(meas, t, f, time.Now())
p, err := client.NewPoint(Measurement, t, f, time)
if err != nil { if err != nil {
i := log.Fields{"event": event, "error": err} log.Fatalf("Failed to create %v event", event)
log.WithFields(i).Fatal("Error creating new point...")
} }
return p return p
} }
@ -715,11 +678,9 @@ func (s TeamAddEvent) NewPoint() *client.Point {
"issues": s.Repository.Issues, "issues": s.Repository.Issues,
"teamName": s.Team.Name, "teamName": s.Team.Name,
} }
time := time.Now() p, err := client.NewPoint(meas, t, f, time.Now())
p, err := client.NewPoint(Measurement, t, f, time)
if err != nil { if err != nil {
i := log.Fields{"event": event, "error": err} log.Fatalf("Failed to create %v event", event)
log.WithFields(i).Fatal("Error creating new point...")
} }
return p return p
} }
@ -743,11 +704,9 @@ func (s WatchEvent) NewPoint() *client.Point {
"forks": s.Repository.Forks, "forks": s.Repository.Forks,
"issues": s.Repository.Issues, "issues": s.Repository.Issues,
} }
time := time.Now() p, err := client.NewPoint(meas, t, f, time.Now())
p, err := client.NewPoint(Measurement, t, f, time)
if err != nil { if err != nil {
i := log.Fields{"event": event, "error": err} log.Fatalf("Failed to create %v event", event)
log.WithFields(i).Fatal("Error creating new point...")
} }
return p return p
} }