Refactor GitHub webhooks (#1240)

* Fix a typo.

* Extract similar code to generateEvent function.

* Remove functions to use generateEvent in the switch.

* Refactor tests.
This commit is contained in:
François de Metz 2016-05-23 12:21:34 +02:00 committed by Cameron Sparr
parent 4dcb82bf08
commit d6ceae7005
2 changed files with 52 additions and 375 deletions

View File

@ -91,193 +91,12 @@ func (gh *GithubWebhooks) eventHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
func newCommitComment(data []byte) (Event, error) { func generateEvent(data []byte, event Event) (Event, error) {
commitCommentStruct := CommitCommentEvent{} err := json.Unmarshal(data, event)
err := json.Unmarshal(data, &commitCommentStruct)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return commitCommentStruct, nil return event, nil
}
func newCreate(data []byte) (Event, error) {
createStruct := CreateEvent{}
err := json.Unmarshal(data, &createStruct)
if err != nil {
return nil, err
}
return createStruct, nil
}
func newDelete(data []byte) (Event, error) {
deleteStruct := DeleteEvent{}
err := json.Unmarshal(data, &deleteStruct)
if err != nil {
return nil, err
}
return deleteStruct, nil
}
func newDeployment(data []byte) (Event, error) {
deploymentStruct := DeploymentEvent{}
err := json.Unmarshal(data, &deploymentStruct)
if err != nil {
return nil, err
}
return deploymentStruct, nil
}
func newDeploymentStatus(data []byte) (Event, error) {
deploymentStatusStruct := DeploymentStatusEvent{}
err := json.Unmarshal(data, &deploymentStatusStruct)
if err != nil {
return nil, err
}
return deploymentStatusStruct, nil
}
func newFork(data []byte) (Event, error) {
forkStruct := ForkEvent{}
err := json.Unmarshal(data, &forkStruct)
if err != nil {
return nil, err
}
return forkStruct, nil
}
func newGollum(data []byte) (Event, error) {
gollumStruct := GollumEvent{}
err := json.Unmarshal(data, &gollumStruct)
if err != nil {
return nil, err
}
return gollumStruct, nil
}
func newIssueComment(data []byte) (Event, error) {
issueCommentStruct := IssueCommentEvent{}
err := json.Unmarshal(data, &issueCommentStruct)
if err != nil {
return nil, err
}
return issueCommentStruct, nil
}
func newIssues(data []byte) (Event, error) {
issuesStruct := IssuesEvent{}
err := json.Unmarshal(data, &issuesStruct)
if err != nil {
return nil, err
}
return issuesStruct, nil
}
func newMember(data []byte) (Event, error) {
memberStruct := MemberEvent{}
err := json.Unmarshal(data, &memberStruct)
if err != nil {
return nil, err
}
return memberStruct, nil
}
func newMembership(data []byte) (Event, error) {
membershipStruct := MembershipEvent{}
err := json.Unmarshal(data, &membershipStruct)
if err != nil {
return nil, err
}
return membershipStruct, nil
}
func newPageBuild(data []byte) (Event, error) {
pageBuildEvent := PageBuildEvent{}
err := json.Unmarshal(data, &pageBuildEvent)
if err != nil {
return nil, err
}
return pageBuildEvent, nil
}
func newPublic(data []byte) (Event, error) {
publicEvent := PublicEvent{}
err := json.Unmarshal(data, &publicEvent)
if err != nil {
return nil, err
}
return publicEvent, nil
}
func newPullRequest(data []byte) (Event, error) {
pullRequestStruct := PullRequestEvent{}
err := json.Unmarshal(data, &pullRequestStruct)
if err != nil {
return nil, err
}
return pullRequestStruct, nil
}
func newPullRequestReviewComment(data []byte) (Event, error) {
pullRequestReviewCommentStruct := PullRequestReviewCommentEvent{}
err := json.Unmarshal(data, &pullRequestReviewCommentStruct)
if err != nil {
return nil, err
}
return pullRequestReviewCommentStruct, nil
}
func newPush(data []byte) (Event, error) {
pushStruct := PushEvent{}
err := json.Unmarshal(data, &pushStruct)
if err != nil {
return nil, err
}
return pushStruct, nil
}
func newRelease(data []byte) (Event, error) {
releaseStruct := ReleaseEvent{}
err := json.Unmarshal(data, &releaseStruct)
if err != nil {
return nil, err
}
return releaseStruct, nil
}
func newRepository(data []byte) (Event, error) {
repositoryStruct := RepositoryEvent{}
err := json.Unmarshal(data, &repositoryStruct)
if err != nil {
return nil, err
}
return repositoryStruct, nil
}
func newStatus(data []byte) (Event, error) {
statusStruct := StatusEvent{}
err := json.Unmarshal(data, &statusStruct)
if err != nil {
return nil, err
}
return statusStruct, nil
}
func newTeamAdd(data []byte) (Event, error) {
teamAddStruct := TeamAddEvent{}
err := json.Unmarshal(data, &teamAddStruct)
if err != nil {
return nil, err
}
return teamAddStruct, nil
}
func newWatch(data []byte) (Event, error) {
watchStruct := WatchEvent{}
err := json.Unmarshal(data, &watchStruct)
if err != nil {
return nil, err
}
return watchStruct, nil
} }
type newEventError struct { type newEventError struct {
@ -288,51 +107,51 @@ func (e *newEventError) Error() string {
return e.s return e.s
} }
func NewEvent(r []byte, t string) (Event, error) { func NewEvent(data []byte, name string) (Event, error) {
log.Printf("New %v event recieved", t) log.Printf("New %v event received", name)
switch t { switch name {
case "commit_comment": case "commit_comment":
return newCommitComment(r) return generateEvent(data, &CommitCommentEvent{})
case "create": case "create":
return newCreate(r) return generateEvent(data, &CreateEvent{})
case "delete": case "delete":
return newDelete(r) return generateEvent(data, &DeleteEvent{})
case "deployment": case "deployment":
return newDeployment(r) return generateEvent(data, &DeploymentEvent{})
case "deployment_status": case "deployment_status":
return newDeploymentStatus(r) return generateEvent(data, &DeploymentStatusEvent{})
case "fork": case "fork":
return newFork(r) return generateEvent(data, &ForkEvent{})
case "gollum": case "gollum":
return newGollum(r) return generateEvent(data, &GollumEvent{})
case "issue_comment": case "issue_comment":
return newIssueComment(r) return generateEvent(data, &IssueCommentEvent{})
case "issues": case "issues":
return newIssues(r) return generateEvent(data, &IssuesEvent{})
case "member": case "member":
return newMember(r) return generateEvent(data, &MemberEvent{})
case "membership": case "membership":
return newMembership(r) return generateEvent(data, &MembershipEvent{})
case "page_build": case "page_build":
return newPageBuild(r) return generateEvent(data, &PageBuildEvent{})
case "public": case "public":
return newPublic(r) return generateEvent(data, &PublicEvent{})
case "pull_request": case "pull_request":
return newPullRequest(r) return generateEvent(data, &PullRequestEvent{})
case "pull_request_review_comment": case "pull_request_review_comment":
return newPullRequestReviewComment(r) return generateEvent(data, &PullRequestReviewCommentEvent{})
case "push": case "push":
return newPush(r) return generateEvent(data, &PushEvent{})
case "release": case "release":
return newRelease(r) return generateEvent(data, &ReleaseEvent{})
case "repository": case "repository":
return newRepository(r) return generateEvent(data, &RepositoryEvent{})
case "status": case "status":
return newStatus(r) return generateEvent(data, &StatusEvent{})
case "team_add": case "team_add":
return newTeamAdd(r) return generateEvent(data, &TeamAddEvent{})
case "watch": case "watch":
return newWatch(r) return generateEvent(data, &WatchEvent{})
} }
return nil, &newEventError{"Not a recognized event type"} return nil, &newEventError{"Not a recognized event type"}
} }

View File

@ -7,231 +7,89 @@ import (
"testing" "testing"
) )
func TestCommitCommentEvent(t *testing.T) { func GithubWebhookRequest(event string, jsonString string, t *testing.T) {
gh := NewGithubWebhooks() gh := NewGithubWebhooks()
jsonString := CommitCommentEventJSON()
req, _ := http.NewRequest("POST", "/", strings.NewReader(jsonString)) req, _ := http.NewRequest("POST", "/", strings.NewReader(jsonString))
req.Header.Add("X-Github-Event", "commit_comment") req.Header.Add("X-Github-Event", event)
w := httptest.NewRecorder() w := httptest.NewRecorder()
gh.eventHandler(w, req) gh.eventHandler(w, req)
if w.Code != http.StatusOK { if w.Code != http.StatusOK {
t.Errorf("POST commit_comment returned HTTP status code %v.\nExpected %v", w.Code, http.StatusOK) t.Errorf("POST "+event+" returned HTTP status code %v.\nExpected %v", w.Code, http.StatusOK)
} }
} }
func TestCommitCommentEvent(t *testing.T) {
GithubWebhookRequest("commit_comment", CommitCommentEventJSON(), t)
}
func TestDeleteEvent(t *testing.T) { func TestDeleteEvent(t *testing.T) {
gh := NewGithubWebhooks() GithubWebhookRequest("delete", DeleteEventJSON(), t)
jsonString := 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) { func TestDeploymentEvent(t *testing.T) {
gh := NewGithubWebhooks() GithubWebhookRequest("deployment", DeploymentEventJSON(), t)
jsonString := 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) { func TestDeploymentStatusEvent(t *testing.T) {
gh := NewGithubWebhooks() GithubWebhookRequest("deployment_status", DeploymentStatusEventJSON(), t)
jsonString := 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) { func TestForkEvent(t *testing.T) {
gh := NewGithubWebhooks() GithubWebhookRequest("fork", ForkEventJSON(), t)
jsonString := 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) { func TestGollumEvent(t *testing.T) {
gh := NewGithubWebhooks() GithubWebhookRequest("gollum", GollumEventJSON(), t)
jsonString := 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) { func TestIssueCommentEvent(t *testing.T) {
gh := NewGithubWebhooks() GithubWebhookRequest("issue_comment", IssueCommentEventJSON(), t)
jsonString := 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) { func TestIssuesEvent(t *testing.T) {
gh := NewGithubWebhooks() GithubWebhookRequest("issues", IssuesEventJSON(), t)
jsonString := 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) { func TestMemberEvent(t *testing.T) {
gh := NewGithubWebhooks() GithubWebhookRequest("member", MemberEventJSON(), t)
jsonString := 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) { func TestMembershipEvent(t *testing.T) {
gh := NewGithubWebhooks() GithubWebhookRequest("membership", MembershipEventJSON(), t)
jsonString := 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) { func TestPageBuildEvent(t *testing.T) {
gh := NewGithubWebhooks() GithubWebhookRequest("page_build", PageBuildEventJSON(), t)
jsonString := 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) { func TestPublicEvent(t *testing.T) {
gh := NewGithubWebhooks() GithubWebhookRequest("public", PublicEventJSON(), t)
jsonString := 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) { func TestPullRequestReviewCommentEvent(t *testing.T) {
gh := NewGithubWebhooks() GithubWebhookRequest("pull_request_review_comment", PullRequestReviewCommentEventJSON(), t)
jsonString := 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) { func TestPushEvent(t *testing.T) {
gh := NewGithubWebhooks() GithubWebhookRequest("push", PushEventJSON(), t)
jsonString := 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) { func TestReleaseEvent(t *testing.T) {
gh := NewGithubWebhooks() GithubWebhookRequest("release", ReleaseEventJSON(), t)
jsonString := 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) { func TestRepositoryEvent(t *testing.T) {
gh := NewGithubWebhooks() GithubWebhookRequest("repository", RepositoryEventJSON(), t)
jsonString := 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) { func TestStatusEvent(t *testing.T) {
gh := NewGithubWebhooks() GithubWebhookRequest("status", StatusEventJSON(), t)
jsonString := 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) { func TestTeamAddEvent(t *testing.T) {
gh := NewGithubWebhooks() GithubWebhookRequest("team_add", TeamAddEventJSON(), t)
jsonString := 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) { func TestWatchEvent(t *testing.T) {
gh := NewGithubWebhooks() GithubWebhookRequest("watch", WatchEventJSON(), t)
jsonString := 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)
}
} }