Skip to content

Commit

Permalink
Export the main webhook parse method
Browse files Browse the repository at this point in the history
  • Loading branch information
brian-nguyen committed Jul 7, 2018
1 parent 544168d commit 3bca676
Showing 1 changed file with 4 additions and 17 deletions.
21 changes: 4 additions & 17 deletions daemon/inertiad/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"fmt"
"net/http"
"strings"

"github.com/ubclaunchpad/inertia/common"
)

// Payload represents a generic webhook payload
Expand All @@ -18,7 +16,8 @@ type Payload interface {
GetSSHURL() string
}

func parse(r *http.Request) (Payload, error) {
// 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" {
Expand All @@ -36,27 +35,15 @@ func parse(r *http.Request) (Payload, error) {
gitlabEventHeader := r.Header.Get("x-gitlab-event")
if len(gitlabEventHeader) > 0 {
fmt.Println("Gitlab webhook received")
return nil, nil
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, nil
return nil, errors.New("Unsupported webhook received")
}

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

// Handler receives a webhook and parses it into one of the supported types
func Handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, common.MsgDaemonOK)
payload, err := parse(r)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(payload)
}

0 comments on commit 3bca676

Please sign in to comment.