Skip to content

Commit

Permalink
Merge 3bca676 into 0c4136e
Browse files Browse the repository at this point in the history
  • Loading branch information
brian-nguyen committed Jul 7, 2018
2 parents 0c4136e + 3bca676 commit 696108c
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
67 changes: 67 additions & 0 deletions daemon/inertiad/webhook/github.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package webhook

import (
"encoding/json"
"errors"
"fmt"
"net/http"
)

// GithubPushEvent represents a push to a Github respository
// see https://developer.github.com/v3/activity/events/types/#pushevent
type GithubPushEvent struct {
eventType string
Ref string `json:"ref"`
Repo GithubPushEventRepository `json:"repository"`
}

// GithubPushEventRepository represents the repository object in a Github PushEvent
// see https://developer.github.com/v3/activity/events/types/#pushevent
type GithubPushEventRepository struct {
FullName string `json:"full_name"`
GitURL string `json:"clone_url"`
SSHURL string `json:"ssh_url"`
}

func parseGithubEvent(r *http.Request, event string) (Payload, error) {
var payload Payload
switch event {
case "push":
fmt.Println("Push event")
payload = GithubPushEvent{eventType: event}
default:
return nil, errors.New("Unsupported Github event")
}

err := json.NewDecoder(r.Body).Decode(&payload)
if err != nil {
return nil, errors.New("Error decoding body")
}

return payload, nil
}

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

// GetRepoName returns the full repo name
func (g GithubPushEvent) GetRepoName() string {
return g.Repo.FullName
}

// GetRef returns the full ref
func (g GithubPushEvent) GetRef() string {
return g.Ref
}

// GetGitURL returns the git clone URL
func (g GithubPushEvent) GetGitURL() string {
return g.Repo.GitURL
}

// GetSSHURL returns the ssh URL
func (g GithubPushEvent) GetSSHURL() string {
return g.Repo.SSHURL
}
49 changes: 49 additions & 0 deletions daemon/inertiad/webhook/webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package webhook

import (
"errors"
"fmt"
"net/http"
"strings"
)

// Payload represents a generic webhook payload
type Payload interface {
GetEventType() string
GetRepoName() string
GetRef() string
GetGitURL() string
GetSSHURL() string
}

// Parse takes in a webhook request and parses it into one of the supported types
func Parse(r *http.Request) (Payload, error) {
fmt.Println("Parsing webhook...")

if r.Header.Get("content-type") != "application/json" {
return nil, errors.New("Content-Type must be JSON")
}

// Try Github
githubEventHeader := r.Header.Get("x-github-event")
if len(githubEventHeader) > 0 {
fmt.Println("Github webhook received")
return parseGithubEvent(r, githubEventHeader)
}

// Try Gitlab
gitlabEventHeader := r.Header.Get("x-gitlab-event")
if len(gitlabEventHeader) > 0 {
fmt.Println("Gitlab webhook received")
return nil, errors.New("Unsupported webhook received")
}

// Try Bitbucket
userAgent := r.Header.Get("user-agent")
if strings.Contains(userAgent, "Bitbucket") {
fmt.Println("Bitbucket webhook received")
return nil, errors.New("Unsupported webhook received")
}

return nil, errors.New("Unsupported webhook received")
}

0 comments on commit 696108c

Please sign in to comment.