Skip to content

Commit

Permalink
daemon: return ok if webhook type is ping (#532)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobheadxi committed Feb 7, 2019
1 parent e600916 commit 51c177b
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 18 deletions.
4 changes: 4 additions & 0 deletions daemon/inertiad/daemon/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func (s *Server) webhookHandler(w http.ResponseWriter, r *http.Request) {

// process event
switch event := payload.GetEventType(); event {
case webhook.PingEvent:
fmt.Fprint(w, api.MsgDaemonOK)
println("ping webhook received")
return
case webhook.PushEvent:
fmt.Fprint(w, api.MsgDaemonOK)
processPushEvent(s, payload, os.Stdout)
Expand Down
4 changes: 2 additions & 2 deletions daemon/inertiad/webhook/bitbucket_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
// Implements Payload interface
// See bitbucket_test.go for an example request body
type bitbucketPushEvent struct {
eventType string
eventType EventType
branchName string
fullName string
}
Expand Down Expand Up @@ -37,7 +37,7 @@ func (b bitbucketPushEvent) GetSource() string {
}

// GetEventType returns the event type of the webhook
func (b bitbucketPushEvent) GetEventType() string {
func (b bitbucketPushEvent) GetEventType() EventType {
return b.eventType
}

Expand Down
6 changes: 5 additions & 1 deletion daemon/inertiad/webhook/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@ package webhook

import (
"errors"
"fmt"
"net/url"
)

// x-github-event header values
var (
GithubPingHeader = "ping"
GithubPushHeader = "push"
// GithubPullHeader = "pull"
)

func parseGithubEvent(rawJSON map[string]interface{}, event string) (Payload, error) {
switch event {
case GithubPingHeader:
return githubPushEvent{eventType: PingEvent}, nil
case GithubPushHeader:
return parseGithubPushEvent(rawJSON), nil
default:
return nil, errors.New("unsupported Github event")
return nil, fmt.Errorf("unsupported Github event %s", event)
}
}

Expand Down
4 changes: 2 additions & 2 deletions daemon/inertiad/webhook/github_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package webhook
// Implements Payload interface
// See github_test.go for an example request body
type githubPushEvent struct {
eventType string
eventType EventType
ref string
name string
gitURL string
Expand Down Expand Up @@ -36,7 +36,7 @@ func (g githubPushEvent) GetSource() string {
}

// GetEventType returns the event type of the webhook
func (g githubPushEvent) GetEventType() string {
func (g githubPushEvent) GetEventType() EventType {
return g.eventType
}

Expand Down
4 changes: 2 additions & 2 deletions daemon/inertiad/webhook/gitlab_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package webhook
// Implements Payload interface
// See gitlab_test.go for an example request body
type gitlabPushEvent struct {
eventType string
eventType EventType
ref string
name string
gitURL string
Expand Down Expand Up @@ -34,7 +34,7 @@ func (g gitlabPushEvent) GetSource() string {
}

// GetEventType returns the event type of the webhook
func (g gitlabPushEvent) GetEventType() string {
func (g gitlabPushEvent) GetEventType() EventType {
return g.eventType
}

Expand Down
11 changes: 7 additions & 4 deletions daemon/inertiad/webhook/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import (
"strings"
)

// EventType denotes types of webhook events
type EventType string

// Constants for the generic webhook interface
const (
// Events
PushEvent = "push"
PullEvent = "pull"
PushEvent EventType = "push"
PullEvent EventType = "pull"
PingEvent EventType = "ping"

// Hosts
GitHub = "github"
Expand All @@ -22,7 +25,7 @@ const (
// Payload represents a generic webhook payload
type Payload interface {
GetSource() string
GetEventType() string
GetEventType() EventType
GetRepoName() string
GetRef() string
GetGitURL() string
Expand Down
19 changes: 12 additions & 7 deletions daemon/inertiad/webhook/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ func TestTypeAndParse(t *testing.T) {
reqBody []byte
eventHeader string
eventValue string
wantType EventType
}{
{GitHub, "application/x-www-form-urlencoded", githubPushFormEncoded, "x-github-event", GithubPushHeader},
{GitHub, "application/json", githubPushRawJSON, "x-github-event", GithubPushHeader},
{GitLab, "application/json", gitlabPushRawJSON, "x-gitlab-event", GitlabPushHeader},
{BitBucket, "application/json", bitbucketPushRawJSON, "x-event-key", BitbucketPushHeader},
{GitHub, "application/x-www-form-urlencoded", githubPushFormEncoded, "x-github-event", GithubPushHeader, PushEvent},
{GitHub, "application/json", githubPushRawJSON, "x-github-event", GithubPushHeader, PushEvent},
{GitHub, "application/json", githubPushRawJSON, "x-github-event", GithubPingHeader, PingEvent},
{GitLab, "application/json", gitlabPushRawJSON, "x-gitlab-event", GitlabPushHeader, PushEvent},
{BitBucket, "application/json", bitbucketPushRawJSON, "x-event-key", BitbucketPushHeader, PushEvent},
}
for _, tc := range testCases {
req := getMockRequest("/webhook", tc.contentType, tc.reqBody)
Expand All @@ -51,9 +53,12 @@ func TestTypeAndParse(t *testing.T) {
assert.Nil(t, err)

assert.Equal(t, tc.source, payload.GetSource())
assert.Equal(t, "push", payload.GetEventType())
assert.Equal(t, "inertia-deploy-test", payload.GetRepoName())
assert.Equal(t, "refs/heads/master", payload.GetRef())
assert.Equal(t, tc.wantType, payload.GetEventType())
switch tc.wantType {
case PushEvent:
assert.Equal(t, "inertia-deploy-test", payload.GetRepoName())
assert.Equal(t, "refs/heads/master", payload.GetRef())
}
}
}

Expand Down

0 comments on commit 51c177b

Please sign in to comment.