Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9a8b92d028 | |||
| 248dae5b83 | |||
| cc075dfe29 | |||
| c93876b3e9 | |||
| 0926003ddf | |||
| 58dd13d367 | |||
| 4fa39fdfab | |||
| fccbba5986 |
+39
@@ -0,0 +1,39 @@
|
|||||||
|
language: go
|
||||||
|
go:
|
||||||
|
- 1.7.5
|
||||||
|
- 1.8.1
|
||||||
|
- tip
|
||||||
|
matrix:
|
||||||
|
allow_failures:
|
||||||
|
- go: tip
|
||||||
|
|
||||||
|
notifications:
|
||||||
|
email:
|
||||||
|
recipients: dean.karn@gmail.com
|
||||||
|
on_success: change
|
||||||
|
on_failure: always
|
||||||
|
|
||||||
|
before_install:
|
||||||
|
- go get -u github.com/go-playground/overalls
|
||||||
|
- go get -u github.com/mattn/goveralls
|
||||||
|
- go get -u golang.org/x/tools/cmd/cover
|
||||||
|
- go get -u github.com/golang/lint/golint
|
||||||
|
- go get -u github.com/gordonklaus/ineffassign
|
||||||
|
- mkdir -p $GOPATH/src/gopkg.in
|
||||||
|
- ln -s $GOPATH/src/github.com/$TRAVIS_REPO_SLUG $GOPATH/src/gopkg.in/webhooks.v2
|
||||||
|
- ln -s $GOPATH/src/github.com/$TRAVIS_REPO_SLUG $GOPATH/src/gopkg.in/webhooks.v3
|
||||||
|
|
||||||
|
before_script:
|
||||||
|
- go vet ./...
|
||||||
|
|
||||||
|
script:
|
||||||
|
- gofmt -d -s .
|
||||||
|
- golint ./...
|
||||||
|
- ineffassign ./
|
||||||
|
- go test -v ./...
|
||||||
|
- go test -race
|
||||||
|
|
||||||
|
after_success: |
|
||||||
|
[ $TRAVIS_GO_VERSION = 1.8.1 ] &&
|
||||||
|
overalls -project="github.com/go-playground/webhooks" -covermode=count -ignore=.git,examples -debug &&
|
||||||
|
goveralls -coverprofile=overalls.coverprofile -service travis-ci -repotoken $COVERALLS_TOKEN
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
Library webhooks
|
Library webhooks
|
||||||
================
|
================
|
||||||
<img align="right" src="https://raw.githubusercontent.com/go-playground/webhooks/v3/logo.png">
|
<img align="right" src="https://raw.githubusercontent.com/go-playground/webhooks/v3/logo.png">
|
||||||
[](https://semaphoreci.com/joeybloggs/webhooks)
|
[](https://travis-ci.org/go-playground/webhooks)
|
||||||
[](https://coveralls.io/github/go-playground/webhooks?branch=v3)
|
[](https://coveralls.io/github/go-playground/webhooks?branch=v3)
|
||||||
[](https://goreportcard.com/report/go-playground/webhooks)
|
[](https://goreportcard.com/report/go-playground/webhooks)
|
||||||
[](https://godoc.org/gopkg.in/go-playground/webhooks.v3)
|
[](https://godoc.org/gopkg.in/go-playground/webhooks.v3)
|
||||||
@@ -75,7 +75,7 @@ func HandleRelease(payload interface{}, header webhooks.Header) {
|
|||||||
pl := payload.(github.ReleasePayload)
|
pl := payload.(github.ReleasePayload)
|
||||||
|
|
||||||
// only want to compile on full releases
|
// only want to compile on full releases
|
||||||
if pl.Release.Draft || pl.Release.Prelelease || pl.Release.TargetCommitish != "master" {
|
if pl.Release.Draft || pl.Release.Prerelease || pl.Release.TargetCommitish != "master" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,7 +93,6 @@ func HandlePullRequest(payload interface{}, header webhooks.Header) {
|
|||||||
// Do whatever you want from here...
|
// Do whatever you want from here...
|
||||||
fmt.Printf("%+v", pl)
|
fmt.Printf("%+v", pl)
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Single receiver for events you subscribe to
|
Single receiver for events you subscribe to
|
||||||
|
|||||||
+1
-2
@@ -2,8 +2,6 @@ package github
|
|||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
// PushPayload contains the information for GitHub's push hook event
|
|
||||||
|
|
||||||
// CommitCommentPayload contains the information for GitHub's commit_comment hook event
|
// CommitCommentPayload contains the information for GitHub's commit_comment hook event
|
||||||
type CommitCommentPayload struct {
|
type CommitCommentPayload struct {
|
||||||
Action string `json:"action"`
|
Action string `json:"action"`
|
||||||
@@ -3954,6 +3952,7 @@ type PushPayload struct {
|
|||||||
BaseRef *string `json:"base_ref"`
|
BaseRef *string `json:"base_ref"`
|
||||||
Compare string `json:"compare"`
|
Compare string `json:"compare"`
|
||||||
Commits []struct {
|
Commits []struct {
|
||||||
|
Sha string `json:"sha"`
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
TreeID string `json:"tree_id"`
|
TreeID string `json:"tree_id"`
|
||||||
Distinct bool `json:"distinct"`
|
Distinct bool `json:"distinct"`
|
||||||
|
|||||||
+14
@@ -37,16 +37,25 @@ type Webhook interface {
|
|||||||
type server struct {
|
type server struct {
|
||||||
hook Webhook
|
hook Webhook
|
||||||
path string
|
path string
|
||||||
|
includePathCheck bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProcessPayloadFunc is a common function for payload return values
|
// ProcessPayloadFunc is a common function for payload return values
|
||||||
type ProcessPayloadFunc func(payload interface{}, header Header)
|
type ProcessPayloadFunc func(payload interface{}, header Header)
|
||||||
|
|
||||||
|
// Handler returns the webhook http.Handler for use in your own Mux implementation
|
||||||
|
func Handler(hook Webhook) http.Handler {
|
||||||
|
return &server{
|
||||||
|
hook: hook,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Run runs a server
|
// Run runs a server
|
||||||
func Run(hook Webhook, addr string, path string) error {
|
func Run(hook Webhook, addr string, path string) error {
|
||||||
srv := &server{
|
srv := &server{
|
||||||
hook: hook,
|
hook: hook,
|
||||||
path: path,
|
path: path,
|
||||||
|
includePathCheck: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
s := &http.Server{Addr: addr, Handler: srv}
|
s := &http.Server{Addr: addr, Handler: srv}
|
||||||
@@ -60,6 +69,7 @@ func RunServer(s *http.Server, hook Webhook, path string) error {
|
|||||||
srv := &server{
|
srv := &server{
|
||||||
hook: hook,
|
hook: hook,
|
||||||
path: path,
|
path: path,
|
||||||
|
includePathCheck: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
s.Handler = srv
|
s.Handler = srv
|
||||||
@@ -76,6 +86,7 @@ func RunTLSServer(s *http.Server, hook Webhook, path string) error {
|
|||||||
srv := &server{
|
srv := &server{
|
||||||
hook: hook,
|
hook: hook,
|
||||||
path: path,
|
path: path,
|
||||||
|
includePathCheck: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
s.Handler = srv
|
s.Handler = srv
|
||||||
@@ -91,10 +102,13 @@ func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, "405 Method not allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "405 Method not allowed", http.StatusMethodNotAllowed)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if s.includePathCheck {
|
||||||
if r.URL.Path != s.path {
|
if r.URL.Path != s.path {
|
||||||
http.Error(w, "404 Not found", http.StatusNotFound)
|
http.Error(w, "404 Not found", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
s.hook.ParsePayload(w, r)
|
s.hook.ParsePayload(w, r)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"net/http/httptest"
|
||||||
|
|
||||||
. "gopkg.in/go-playground/assert.v1"
|
. "gopkg.in/go-playground/assert.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -47,6 +49,43 @@ func TestMain(m *testing.M) {
|
|||||||
// teardown
|
// teardown
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHandler(t *testing.T) {
|
||||||
|
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
mux.Handle("/webhooks", Handler(fakeHook))
|
||||||
|
|
||||||
|
s := httptest.NewServer(Handler(fakeHook))
|
||||||
|
defer s.Close()
|
||||||
|
|
||||||
|
payload := "{}"
|
||||||
|
|
||||||
|
req, err := http.NewRequest("POST", s.URL+"/webhooks", bytes.NewBuffer([]byte(payload)))
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
Equal(t, err, nil)
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
Equal(t, err, nil)
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
Equal(t, resp.StatusCode, http.StatusOK)
|
||||||
|
|
||||||
|
// Test BAD METHOD
|
||||||
|
req, err = http.NewRequest("GET", s.URL+"/webhooks", bytes.NewBuffer([]byte(payload)))
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
Equal(t, err, nil)
|
||||||
|
|
||||||
|
resp, err = client.Do(req)
|
||||||
|
Equal(t, err, nil)
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
Equal(t, resp.StatusCode, http.StatusMethodNotAllowed)
|
||||||
|
}
|
||||||
|
|
||||||
func TestRun(t *testing.T) {
|
func TestRun(t *testing.T) {
|
||||||
|
|
||||||
go Run(fakeHook, "127.0.0.1:3006", "/webhooks")
|
go Run(fakeHook, "127.0.0.1:3006", "/webhooks")
|
||||||
|
|||||||
Reference in New Issue
Block a user