Compare commits
1 Commits
v4
..
debug-issue8
| Author | SHA1 | Date | |
|---|---|---|---|
| 19138534b5 |
@@ -6,7 +6,6 @@
|
|||||||
# Folders
|
# Folders
|
||||||
_obj
|
_obj
|
||||||
_test
|
_test
|
||||||
.idea
|
|
||||||
|
|
||||||
# Architecture specific extensions/prefixes
|
# Architecture specific extensions/prefixes
|
||||||
*.[568vq]
|
*.[568vq]
|
||||||
|
|||||||
-39
@@ -1,39 +0,0 @@
|
|||||||
language: go
|
|
||||||
go:
|
|
||||||
- 1.10.2
|
|
||||||
- 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
|
|
||||||
- ln -s $GOPATH/src/github.com/$TRAVIS_REPO_SLUG $GOPATH/src/gopkg.in/webhooks.v4
|
|
||||||
|
|
||||||
before_script:
|
|
||||||
- go vet ./...
|
|
||||||
|
|
||||||
script:
|
|
||||||
- gofmt -d -s .
|
|
||||||
- golint ./...
|
|
||||||
- ineffassign ./
|
|
||||||
- go test -v ./...
|
|
||||||
- go test -race
|
|
||||||
|
|
||||||
after_success: |
|
|
||||||
[ $TRAVIS_GO_VERSION = 1.10.2 ] &&
|
|
||||||
overalls -project="github.com/go-playground/webhooks" -covermode=count -ignore=.git,examples -debug &&
|
|
||||||
goveralls -coverprofile=overalls.coverprofile -service travis-ci -repotoken $COVERALLS_TOKEN
|
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
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://travis-ci.org/go-playground/webhooks)
|
[](https://semaphoreci.com/joeybloggs/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.v4)
|
[](https://godoc.org/gopkg.in/go-playground/webhooks.v3)
|
||||||

|

|
||||||
|
|
||||||
Library webhooks allows for easy receiving and parsing of GitHub, Bitbucket and GitLab Webhook Events
|
Library webhooks allows for easy receiving and parsing of GitHub, Bitbucket and GitLab Webhook Events
|
||||||
@@ -24,17 +24,17 @@ Installation
|
|||||||
Use go get.
|
Use go get.
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
go get -u gopkg.in/go-playground/webhooks.v4
|
go get -u gopkg.in/go-playground/webhooks.v3
|
||||||
```
|
```
|
||||||
|
|
||||||
Then import the package into your own code.
|
Then import the package into your own code.
|
||||||
|
|
||||||
import "gopkg.in/go-playground/webhooks.v4"
|
import "gopkg.in/go-playground/webhooks.v3"
|
||||||
|
|
||||||
Usage and Documentation
|
Usage and Documentation
|
||||||
------
|
------
|
||||||
|
|
||||||
Please see http://godoc.org/gopkg.in/go-playground/webhooks.v4 for detailed usage docs.
|
Please see http://godoc.org/gopkg.in/go-playground/webhooks.v3 for detailed usage docs.
|
||||||
|
|
||||||
##### Examples:
|
##### Examples:
|
||||||
|
|
||||||
@@ -46,8 +46,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"gopkg.in/go-playground/webhooks.v4"
|
"gopkg.in/go-playground/webhooks.v3"
|
||||||
"gopkg.in/go-playground/webhooks.v4/github"
|
"gopkg.in/go-playground/webhooks.v3/github"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -103,8 +103,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"gopkg.in/go-playground/webhooks.v4"
|
"gopkg.in/go-playground/webhooks.v3"
|
||||||
"gopkg.in/go-playground/webhooks.v4/github"
|
"gopkg.in/go-playground/webhooks.v3/github"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|||||||
+7
-20
@@ -2,11 +2,10 @@ package bitbucket
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"gopkg.in/go-playground/webhooks.v4"
|
"gopkg.in/go-playground/webhooks.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Webhook instance contains all methods needed to process events
|
// Webhook instance contains all methods needed to process events
|
||||||
@@ -43,7 +42,7 @@ const (
|
|||||||
PullRequestDeclinedEvent Event = "pullrequest:rejected"
|
PullRequestDeclinedEvent Event = "pullrequest:rejected"
|
||||||
PullRequestCommentCreatedEvent Event = "pullrequest:comment_created"
|
PullRequestCommentCreatedEvent Event = "pullrequest:comment_created"
|
||||||
PullRequestCommentUpdatedEvent Event = "pullrequest:comment_updated"
|
PullRequestCommentUpdatedEvent Event = "pullrequest:comment_updated"
|
||||||
PullRequestCommentDeletedEvent Event = "pullrequest:comment_deleted"
|
PullRequestCommentDeletedEvent Event = "pull_request:comment_deleted"
|
||||||
)
|
)
|
||||||
|
|
||||||
// New creates and returns a WebHook instance denoted by the Provider type
|
// New creates and returns a WebHook instance denoted by the Provider type
|
||||||
@@ -70,50 +69,38 @@ func (hook Webhook) RegisterEvents(fn webhooks.ProcessPayloadFunc, events ...Eve
|
|||||||
|
|
||||||
// ParsePayload parses and verifies the payload and fires off the mapped function, if it exists.
|
// ParsePayload parses and verifies the payload and fires off the mapped function, if it exists.
|
||||||
func (hook Webhook) ParsePayload(w http.ResponseWriter, r *http.Request) {
|
func (hook Webhook) ParsePayload(w http.ResponseWriter, r *http.Request) {
|
||||||
webhooks.DefaultLog.Info("Parsing Payload...")
|
|
||||||
|
|
||||||
uuid := r.Header.Get("X-Hook-UUID")
|
uuid := r.Header.Get("X-Hook-UUID")
|
||||||
if uuid == "" {
|
if uuid == "" {
|
||||||
webhooks.DefaultLog.Error("Missing X-Hook-UUID Header")
|
|
||||||
http.Error(w, "400 Bad Request - Missing X-Hook-UUID Header", http.StatusBadRequest)
|
http.Error(w, "400 Bad Request - Missing X-Hook-UUID Header", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
webhooks.DefaultLog.Debug(fmt.Sprintf("X-Hook-UUID:%s", uuid))
|
|
||||||
|
|
||||||
if len(hook.uuid) > 0 {
|
if uuid != hook.uuid {
|
||||||
if uuid != hook.uuid {
|
http.Error(w, "403 Forbidden - Missing X-Hook-UUID does not match", http.StatusForbidden)
|
||||||
webhooks.DefaultLog.Error(fmt.Sprintf("X-Hook-UUID %s does not match configured uuid of %s", uuid, hook.uuid))
|
return
|
||||||
http.Error(w, "403 Forbidden - X-Hook-UUID does not match", http.StatusForbidden)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
webhooks.DefaultLog.Debug("hook uuid not defined - recommend setting for improved security")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
event := r.Header.Get("X-Event-Key")
|
event := r.Header.Get("X-Event-Key")
|
||||||
if event == "" {
|
if event == "" {
|
||||||
webhooks.DefaultLog.Error("Missing X-Event-Key Header")
|
|
||||||
http.Error(w, "400 Bad Request - Missing X-Event-Key Header", http.StatusBadRequest)
|
http.Error(w, "400 Bad Request - Missing X-Event-Key Header", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
webhooks.DefaultLog.Debug(fmt.Sprintf("X-Event-Key:%s", event))
|
|
||||||
|
|
||||||
bitbucketEvent := Event(event)
|
bitbucketEvent := Event(event)
|
||||||
|
|
||||||
fn, ok := hook.eventFuncs[bitbucketEvent]
|
fn, ok := hook.eventFuncs[bitbucketEvent]
|
||||||
// if no event registered
|
// if no event registered
|
||||||
if !ok {
|
if !ok {
|
||||||
webhooks.DefaultLog.Info(fmt.Sprintf("Webhook Event %s not registered, it is recommended to setup only events in bitbucket that will be registered in the webhook to avoid unnecessary traffic and reduce potential attack vectors.", event))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
payload, err := ioutil.ReadAll(r.Body)
|
payload, err := ioutil.ReadAll(r.Body)
|
||||||
if err != nil || len(payload) == 0 {
|
if err != nil || len(payload) == 0 {
|
||||||
webhooks.DefaultLog.Error("Issue reading Payload")
|
http.Error(w, "Error reading Body", http.StatusInternalServerError)
|
||||||
http.Error(w, "Issue reading Payload", http.StatusInternalServerError)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
webhooks.DefaultLog.Debug(fmt.Sprintf("Payload:%s", string(payload)))
|
|
||||||
hd := webhooks.Header(r.Header)
|
hd := webhooks.Header(r.Header)
|
||||||
|
|
||||||
switch bitbucketEvent {
|
switch bitbucketEvent {
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
. "gopkg.in/go-playground/assert.v1"
|
. "gopkg.in/go-playground/assert.v1"
|
||||||
"gopkg.in/go-playground/webhooks.v4"
|
"gopkg.in/go-playground/webhooks.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NOTES:
|
// NOTES:
|
||||||
|
|||||||
@@ -1,67 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"gopkg.in/go-playground/webhooks.v4"
|
|
||||||
"gopkg.in/go-playground/webhooks.v4/github"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
path = "/webhooks"
|
|
||||||
port = 3016
|
|
||||||
)
|
|
||||||
|
|
||||||
type myLogger struct {
|
|
||||||
PrintDebugs bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *myLogger) Info(msg ...interface{}) {
|
|
||||||
log.Println(msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *myLogger) Error(msg ...interface{}) {
|
|
||||||
log.Println(msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *myLogger) Debug(msg ...interface{}) {
|
|
||||||
if !l.PrintDebugs {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log.Println(msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
// webhooks.DefaultLog=webhooks.NewLogger(true)
|
|
||||||
//
|
|
||||||
// or override with your own
|
|
||||||
webhooks.DefaultLog = &myLogger{PrintDebugs: true}
|
|
||||||
|
|
||||||
hook := github.New(&github.Config{Secret: "MyGitHubSuperSecretSecrect...?"})
|
|
||||||
hook.RegisterEvents(HandleMultiple, github.ReleaseEvent, github.PullRequestEvent) // Add as many as you want
|
|
||||||
|
|
||||||
err := webhooks.Run(hook, ":"+strconv.Itoa(port), path)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// HandleMultiple handles multiple GitHub events
|
|
||||||
func HandleMultiple(payload interface{}, header webhooks.Header) {
|
|
||||||
fmt.Println("Handling Payload..")
|
|
||||||
|
|
||||||
switch payload.(type) {
|
|
||||||
|
|
||||||
case github.ReleasePayload:
|
|
||||||
release := payload.(github.ReleasePayload)
|
|
||||||
// Do whatever you want from here...
|
|
||||||
fmt.Printf("%+v", release)
|
|
||||||
|
|
||||||
case github.PullRequestPayload:
|
|
||||||
pullRequest := payload.(github.PullRequestPayload)
|
|
||||||
// Do whatever you want from here...
|
|
||||||
fmt.Printf("%+v", pullRequest)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -4,8 +4,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"gopkg.in/go-playground/webhooks.v4"
|
"gopkg.in/go-playground/webhooks.v3"
|
||||||
"gopkg.in/go-playground/webhooks.v4/github"
|
"gopkg.in/go-playground/webhooks.v3/github"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -14,6 +14,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
hook := github.New(&github.Config{Secret: "MyGitHubSuperSecretSecrect...?"})
|
hook := github.New(&github.Config{Secret: "MyGitHubSuperSecretSecrect...?"})
|
||||||
hook.RegisterEvents(HandleRelease, github.ReleaseEvent)
|
hook.RegisterEvents(HandleRelease, github.ReleaseEvent)
|
||||||
hook.RegisterEvents(HandlePullRequest, github.PullRequestEvent)
|
hook.RegisterEvents(HandlePullRequest, github.PullRequestEvent)
|
||||||
@@ -26,6 +27,7 @@ func main() {
|
|||||||
|
|
||||||
// HandleRelease handles GitHub release events
|
// HandleRelease handles GitHub release events
|
||||||
func HandleRelease(payload interface{}, header webhooks.Header) {
|
func HandleRelease(payload interface{}, header webhooks.Header) {
|
||||||
|
|
||||||
fmt.Println("Handling Release")
|
fmt.Println("Handling Release")
|
||||||
|
|
||||||
pl := payload.(github.ReleasePayload)
|
pl := payload.(github.ReleasePayload)
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"gopkg.in/go-playground/webhooks.v4"
|
"gopkg.in/go-playground/webhooks.v3"
|
||||||
"gopkg.in/go-playground/webhooks.v4/github"
|
"gopkg.in/go-playground/webhooks.v3/github"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -14,6 +14,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
hook := github.New(&github.Config{Secret: "MyGitHubSuperSecretSecrect...?"})
|
hook := github.New(&github.Config{Secret: "MyGitHubSuperSecretSecrect...?"})
|
||||||
hook.RegisterEvents(HandleMultiple, github.ReleaseEvent, github.PullRequestEvent) // Add as many as you want
|
hook.RegisterEvents(HandleMultiple, github.ReleaseEvent, github.PullRequestEvent) // Add as many as you want
|
||||||
|
|
||||||
@@ -25,6 +26,7 @@ func main() {
|
|||||||
|
|
||||||
// HandleMultiple handles multiple GitHub events
|
// HandleMultiple handles multiple GitHub events
|
||||||
func HandleMultiple(payload interface{}, header webhooks.Header) {
|
func HandleMultiple(payload interface{}, header webhooks.Header) {
|
||||||
|
|
||||||
fmt.Println("Handling Payload..")
|
fmt.Println("Handling Payload..")
|
||||||
|
|
||||||
switch payload.(type) {
|
switch payload.(type) {
|
||||||
|
|||||||
+174
-82
@@ -5,11 +5,11 @@ import (
|
|||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"gopkg.in/go-playground/webhooks.v4"
|
"gopkg.in/go-playground/webhooks.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Webhook instance contains all methods needed to process events
|
// Webhook instance contains all methods needed to process events
|
||||||
@@ -36,8 +36,6 @@ const (
|
|||||||
DeploymentStatusEvent Event = "deployment_status"
|
DeploymentStatusEvent Event = "deployment_status"
|
||||||
ForkEvent Event = "fork"
|
ForkEvent Event = "fork"
|
||||||
GollumEvent Event = "gollum"
|
GollumEvent Event = "gollum"
|
||||||
InstallationEvent Event = "installation"
|
|
||||||
IntegrationInstallationEvent Event = "integration_installation"
|
|
||||||
IssueCommentEvent Event = "issue_comment"
|
IssueCommentEvent Event = "issue_comment"
|
||||||
IssuesEvent Event = "issues"
|
IssuesEvent Event = "issues"
|
||||||
LabelEvent Event = "label"
|
LabelEvent Event = "label"
|
||||||
@@ -47,7 +45,6 @@ const (
|
|||||||
OrganizationEvent Event = "organization"
|
OrganizationEvent Event = "organization"
|
||||||
OrgBlockEvent Event = "org_block"
|
OrgBlockEvent Event = "org_block"
|
||||||
PageBuildEvent Event = "page_build"
|
PageBuildEvent Event = "page_build"
|
||||||
PingEvent Event = "ping"
|
|
||||||
ProjectCardEvent Event = "project_card"
|
ProjectCardEvent Event = "project_card"
|
||||||
ProjectColumnEvent Event = "project_column"
|
ProjectColumnEvent Event = "project_column"
|
||||||
ProjectEvent Event = "project"
|
ProjectEvent Event = "project"
|
||||||
@@ -100,193 +97,288 @@ func (hook Webhook) RegisterEvents(fn webhooks.ProcessPayloadFunc, events ...Eve
|
|||||||
|
|
||||||
// ParsePayload parses and verifies the payload and fires off the mapped function, if it exists.
|
// ParsePayload parses and verifies the payload and fires off the mapped function, if it exists.
|
||||||
func (hook Webhook) ParsePayload(w http.ResponseWriter, r *http.Request) {
|
func (hook Webhook) ParsePayload(w http.ResponseWriter, r *http.Request) {
|
||||||
webhooks.DefaultLog.Info("Parsing Payload...")
|
|
||||||
|
|
||||||
|
log.Println("Gettting X-GitHub-Event")
|
||||||
event := r.Header.Get("X-GitHub-Event")
|
event := r.Header.Get("X-GitHub-Event")
|
||||||
if len(event) == 0 {
|
if len(event) == 0 {
|
||||||
webhooks.DefaultLog.Error("Missing X-GitHub-Event Header")
|
|
||||||
http.Error(w, "400 Bad Request - Missing X-GitHub-Event Header", http.StatusBadRequest)
|
http.Error(w, "400 Bad Request - Missing X-GitHub-Event Header", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
webhooks.DefaultLog.Debug(fmt.Sprintf("X-GitHub-Event:%s", event))
|
|
||||||
|
|
||||||
gitHubEvent := Event(event)
|
gitHubEvent := Event(event)
|
||||||
|
|
||||||
|
log.Println("Looking for Hook:", gitHubEvent)
|
||||||
fn, ok := hook.eventFuncs[gitHubEvent]
|
fn, ok := hook.eventFuncs[gitHubEvent]
|
||||||
// if no event registered
|
// if no event registered
|
||||||
if !ok {
|
if !ok {
|
||||||
webhooks.DefaultLog.Info(fmt.Sprintf("Webhook Event %s not registered, it is recommended to setup only events in github that will be registered in the webhook to avoid unnecessary traffic and reduce potential attack vectors.", event))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Println("READING PAYLOAD FROM BODY")
|
||||||
|
|
||||||
payload, err := ioutil.ReadAll(r.Body)
|
payload, err := ioutil.ReadAll(r.Body)
|
||||||
if err != nil || len(payload) == 0 {
|
if err != nil || len(payload) == 0 {
|
||||||
webhooks.DefaultLog.Error("Issue reading Payload")
|
http.Error(w, "Error reading Body", http.StatusInternalServerError)
|
||||||
http.Error(w, "Issue reading Payload", http.StatusInternalServerError)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
webhooks.DefaultLog.Debug(fmt.Sprintf("Payload:%s", string(payload)))
|
|
||||||
|
log.Println("Checking GitHub secret")
|
||||||
|
|
||||||
// If we have a Secret set, we should check the MAC
|
// If we have a Secret set, we should check the MAC
|
||||||
if len(hook.secret) > 0 {
|
if len(hook.secret) > 0 {
|
||||||
webhooks.DefaultLog.Info("Checking secret")
|
|
||||||
|
log.Println("Get GitHub signature")
|
||||||
signature := r.Header.Get("X-Hub-Signature")
|
signature := r.Header.Get("X-Hub-Signature")
|
||||||
|
|
||||||
if len(signature) == 0 {
|
if len(signature) == 0 {
|
||||||
webhooks.DefaultLog.Error("Missing X-Hub-Signature required for HMAC verification")
|
|
||||||
http.Error(w, "403 Forbidden - Missing X-Hub-Signature required for HMAC verification", http.StatusForbidden)
|
http.Error(w, "403 Forbidden - Missing X-Hub-Signature required for HMAC verification", http.StatusForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
webhooks.DefaultLog.Debug(fmt.Sprintf("X-Hub-Signature:%s", signature))
|
|
||||||
|
|
||||||
mac := hmac.New(sha1.New, []byte(hook.secret))
|
mac := hmac.New(sha1.New, []byte(hook.secret))
|
||||||
mac.Write(payload)
|
mac.Write(payload)
|
||||||
|
|
||||||
expectedMAC := hex.EncodeToString(mac.Sum(nil))
|
expectedMAC := hex.EncodeToString(mac.Sum(nil))
|
||||||
|
|
||||||
|
log.Println("Checking HMAC Equality")
|
||||||
|
|
||||||
if !hmac.Equal([]byte(signature[5:]), []byte(expectedMAC)) {
|
if !hmac.Equal([]byte(signature[5:]), []byte(expectedMAC)) {
|
||||||
webhooks.DefaultLog.Error("HMAC verification failed")
|
|
||||||
http.Error(w, "403 Forbidden - HMAC verification failed", http.StatusForbidden)
|
http.Error(w, "403 Forbidden - HMAC verification failed", http.StatusForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Println("HMAC Equal")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make headers available to ProcessPayloadFunc as a webhooks type
|
// Make headers available to ProcessPayloadFunc as a webhooks type
|
||||||
hd := webhooks.Header(r.Header)
|
hd := webhooks.Header(r.Header)
|
||||||
|
var pl interface{}
|
||||||
|
|
||||||
|
log.Println("Unmarshal based on GitHub event:", gitHubEvent)
|
||||||
|
|
||||||
switch gitHubEvent {
|
switch gitHubEvent {
|
||||||
case CommitCommentEvent:
|
case CommitCommentEvent:
|
||||||
|
|
||||||
var cc CommitCommentPayload
|
var cc CommitCommentPayload
|
||||||
json.Unmarshal([]byte(payload), &cc)
|
|
||||||
hook.runProcessPayloadFunc(fn, cc, hd)
|
err = json.Unmarshal([]byte(payload), &cc)
|
||||||
|
pl = cc
|
||||||
|
|
||||||
case CreateEvent:
|
case CreateEvent:
|
||||||
|
|
||||||
var c CreatePayload
|
var c CreatePayload
|
||||||
json.Unmarshal([]byte(payload), &c)
|
|
||||||
hook.runProcessPayloadFunc(fn, c, hd)
|
err = json.Unmarshal([]byte(payload), &c)
|
||||||
|
pl = c
|
||||||
|
|
||||||
case DeleteEvent:
|
case DeleteEvent:
|
||||||
var d DeletePayload
|
var d DeletePayload
|
||||||
json.Unmarshal([]byte(payload), &d)
|
json.Unmarshal([]byte(payload), &d)
|
||||||
hook.runProcessPayloadFunc(fn, d, hd)
|
hook.runProcessPayloadFunc(fn, d, hd)
|
||||||
|
|
||||||
case DeploymentEvent:
|
case DeploymentEvent:
|
||||||
|
|
||||||
var d DeploymentPayload
|
var d DeploymentPayload
|
||||||
json.Unmarshal([]byte(payload), &d)
|
|
||||||
hook.runProcessPayloadFunc(fn, d, hd)
|
err = json.Unmarshal([]byte(payload), &d)
|
||||||
|
pl = d
|
||||||
|
|
||||||
case DeploymentStatusEvent:
|
case DeploymentStatusEvent:
|
||||||
|
|
||||||
var d DeploymentStatusPayload
|
var d DeploymentStatusPayload
|
||||||
json.Unmarshal([]byte(payload), &d)
|
|
||||||
hook.runProcessPayloadFunc(fn, d, hd)
|
err = json.Unmarshal([]byte(payload), &d)
|
||||||
|
pl = d
|
||||||
|
|
||||||
case ForkEvent:
|
case ForkEvent:
|
||||||
|
|
||||||
var f ForkPayload
|
var f ForkPayload
|
||||||
json.Unmarshal([]byte(payload), &f)
|
|
||||||
hook.runProcessPayloadFunc(fn, f, hd)
|
err = json.Unmarshal([]byte(payload), &f)
|
||||||
|
pl = f
|
||||||
|
|
||||||
case GollumEvent:
|
case GollumEvent:
|
||||||
|
|
||||||
var g GollumPayload
|
var g GollumPayload
|
||||||
json.Unmarshal([]byte(payload), &g)
|
|
||||||
hook.runProcessPayloadFunc(fn, g, hd)
|
err = json.Unmarshal([]byte(payload), &g)
|
||||||
case InstallationEvent, IntegrationInstallationEvent:
|
pl = g
|
||||||
var i InstallationPayload
|
|
||||||
json.Unmarshal([]byte(payload), &i)
|
|
||||||
hook.runProcessPayloadFunc(fn, i, hd)
|
|
||||||
case IssueCommentEvent:
|
case IssueCommentEvent:
|
||||||
|
|
||||||
var i IssueCommentPayload
|
var i IssueCommentPayload
|
||||||
json.Unmarshal([]byte(payload), &i)
|
|
||||||
hook.runProcessPayloadFunc(fn, i, hd)
|
err = json.Unmarshal([]byte(payload), &i)
|
||||||
|
pl = i
|
||||||
|
|
||||||
case IssuesEvent:
|
case IssuesEvent:
|
||||||
|
|
||||||
var i IssuesPayload
|
var i IssuesPayload
|
||||||
json.Unmarshal([]byte(payload), &i)
|
|
||||||
hook.runProcessPayloadFunc(fn, i, hd)
|
err = json.Unmarshal([]byte(payload), &i)
|
||||||
|
pl = i
|
||||||
|
|
||||||
case LabelEvent:
|
case LabelEvent:
|
||||||
|
|
||||||
var l LabelPayload
|
var l LabelPayload
|
||||||
json.Unmarshal([]byte(payload), &l)
|
|
||||||
hook.runProcessPayloadFunc(fn, l, hd)
|
err = json.Unmarshal([]byte(payload), &l)
|
||||||
|
pl = l
|
||||||
|
|
||||||
case MemberEvent:
|
case MemberEvent:
|
||||||
|
|
||||||
var m MemberPayload
|
var m MemberPayload
|
||||||
json.Unmarshal([]byte(payload), &m)
|
|
||||||
hook.runProcessPayloadFunc(fn, m, hd)
|
err = json.Unmarshal([]byte(payload), &m)
|
||||||
|
pl = m
|
||||||
|
|
||||||
case MembershipEvent:
|
case MembershipEvent:
|
||||||
|
|
||||||
var m MembershipPayload
|
var m MembershipPayload
|
||||||
json.Unmarshal([]byte(payload), &m)
|
|
||||||
hook.runProcessPayloadFunc(fn, m, hd)
|
err = json.Unmarshal([]byte(payload), &m)
|
||||||
|
pl = m
|
||||||
|
|
||||||
case MilestoneEvent:
|
case MilestoneEvent:
|
||||||
|
|
||||||
var m MilestonePayload
|
var m MilestonePayload
|
||||||
json.Unmarshal([]byte(payload), &m)
|
|
||||||
hook.runProcessPayloadFunc(fn, m, hd)
|
err = json.Unmarshal([]byte(payload), &m)
|
||||||
|
pl = m
|
||||||
|
|
||||||
case OrganizationEvent:
|
case OrganizationEvent:
|
||||||
|
|
||||||
var o OrganizationPayload
|
var o OrganizationPayload
|
||||||
json.Unmarshal([]byte(payload), &o)
|
|
||||||
hook.runProcessPayloadFunc(fn, o, hd)
|
err = json.Unmarshal([]byte(payload), &o)
|
||||||
|
pl = o
|
||||||
|
|
||||||
case OrgBlockEvent:
|
case OrgBlockEvent:
|
||||||
|
|
||||||
var o OrgBlockPayload
|
var o OrgBlockPayload
|
||||||
json.Unmarshal([]byte(payload), &o)
|
|
||||||
hook.runProcessPayloadFunc(fn, o, hd)
|
err = json.Unmarshal([]byte(payload), &o)
|
||||||
|
pl = o
|
||||||
|
|
||||||
case PageBuildEvent:
|
case PageBuildEvent:
|
||||||
|
|
||||||
var p PageBuildPayload
|
var p PageBuildPayload
|
||||||
json.Unmarshal([]byte(payload), &p)
|
|
||||||
hook.runProcessPayloadFunc(fn, p, hd)
|
err = json.Unmarshal([]byte(payload), &p)
|
||||||
case PingEvent:
|
pl = p
|
||||||
var p PingPayload
|
|
||||||
json.Unmarshal([]byte(payload), &p)
|
|
||||||
hook.runProcessPayloadFunc(fn, p, hd)
|
|
||||||
case ProjectCardEvent:
|
case ProjectCardEvent:
|
||||||
|
|
||||||
var p ProjectCardPayload
|
var p ProjectCardPayload
|
||||||
json.Unmarshal([]byte(payload), &p)
|
|
||||||
hook.runProcessPayloadFunc(fn, p, hd)
|
err = json.Unmarshal([]byte(payload), &p)
|
||||||
|
pl = p
|
||||||
|
|
||||||
case ProjectColumnEvent:
|
case ProjectColumnEvent:
|
||||||
|
|
||||||
var p ProjectColumnPayload
|
var p ProjectColumnPayload
|
||||||
json.Unmarshal([]byte(payload), &p)
|
|
||||||
hook.runProcessPayloadFunc(fn, p, hd)
|
err = json.Unmarshal([]byte(payload), &p)
|
||||||
|
pl = p
|
||||||
|
|
||||||
case ProjectEvent:
|
case ProjectEvent:
|
||||||
|
|
||||||
var p ProjectPayload
|
var p ProjectPayload
|
||||||
json.Unmarshal([]byte(payload), &p)
|
|
||||||
hook.runProcessPayloadFunc(fn, p, hd)
|
err = json.Unmarshal([]byte(payload), &p)
|
||||||
|
pl = p
|
||||||
|
|
||||||
case PublicEvent:
|
case PublicEvent:
|
||||||
|
|
||||||
var p PublicPayload
|
var p PublicPayload
|
||||||
json.Unmarshal([]byte(payload), &p)
|
|
||||||
hook.runProcessPayloadFunc(fn, p, hd)
|
err = json.Unmarshal([]byte(payload), &p)
|
||||||
|
pl = p
|
||||||
|
|
||||||
case PullRequestEvent:
|
case PullRequestEvent:
|
||||||
|
|
||||||
var p PullRequestPayload
|
var p PullRequestPayload
|
||||||
json.Unmarshal([]byte(payload), &p)
|
|
||||||
hook.runProcessPayloadFunc(fn, p, hd)
|
err = json.Unmarshal([]byte(payload), &p)
|
||||||
|
pl = p
|
||||||
|
|
||||||
case PullRequestReviewEvent:
|
case PullRequestReviewEvent:
|
||||||
|
|
||||||
var p PullRequestReviewPayload
|
var p PullRequestReviewPayload
|
||||||
json.Unmarshal([]byte(payload), &p)
|
|
||||||
hook.runProcessPayloadFunc(fn, p, hd)
|
err = json.Unmarshal([]byte(payload), &p)
|
||||||
|
pl = p
|
||||||
|
|
||||||
case PullRequestReviewCommentEvent:
|
case PullRequestReviewCommentEvent:
|
||||||
|
|
||||||
var p PullRequestReviewCommentPayload
|
var p PullRequestReviewCommentPayload
|
||||||
json.Unmarshal([]byte(payload), &p)
|
|
||||||
hook.runProcessPayloadFunc(fn, p, hd)
|
err = json.Unmarshal([]byte(payload), &p)
|
||||||
|
pl = p
|
||||||
|
|
||||||
case PushEvent:
|
case PushEvent:
|
||||||
|
|
||||||
var p PushPayload
|
var p PushPayload
|
||||||
json.Unmarshal([]byte(payload), &p)
|
|
||||||
hook.runProcessPayloadFunc(fn, p, hd)
|
err = json.Unmarshal([]byte(payload), &p)
|
||||||
|
pl = p
|
||||||
|
|
||||||
case ReleaseEvent:
|
case ReleaseEvent:
|
||||||
|
|
||||||
var r ReleasePayload
|
var r ReleasePayload
|
||||||
json.Unmarshal([]byte(payload), &r)
|
|
||||||
hook.runProcessPayloadFunc(fn, r, hd)
|
err = json.Unmarshal([]byte(payload), &r)
|
||||||
|
pl = r
|
||||||
|
|
||||||
case RepositoryEvent:
|
case RepositoryEvent:
|
||||||
|
|
||||||
var r RepositoryPayload
|
var r RepositoryPayload
|
||||||
json.Unmarshal([]byte(payload), &r)
|
|
||||||
hook.runProcessPayloadFunc(fn, r, hd)
|
err = json.Unmarshal([]byte(payload), &r)
|
||||||
|
pl = r
|
||||||
|
|
||||||
case StatusEvent:
|
case StatusEvent:
|
||||||
|
|
||||||
var s StatusPayload
|
var s StatusPayload
|
||||||
json.Unmarshal([]byte(payload), &s)
|
|
||||||
hook.runProcessPayloadFunc(fn, s, hd)
|
err = json.Unmarshal([]byte(payload), &s)
|
||||||
|
pl = s
|
||||||
|
|
||||||
case TeamEvent:
|
case TeamEvent:
|
||||||
|
|
||||||
var t TeamPayload
|
var t TeamPayload
|
||||||
json.Unmarshal([]byte(payload), &t)
|
|
||||||
hook.runProcessPayloadFunc(fn, t, hd)
|
err = json.Unmarshal([]byte(payload), &t)
|
||||||
|
pl = t
|
||||||
|
|
||||||
case TeamAddEvent:
|
case TeamAddEvent:
|
||||||
|
|
||||||
var t TeamAddPayload
|
var t TeamAddPayload
|
||||||
json.Unmarshal([]byte(payload), &t)
|
|
||||||
hook.runProcessPayloadFunc(fn, t, hd)
|
err = json.Unmarshal([]byte(payload), &t)
|
||||||
|
pl = t
|
||||||
|
|
||||||
case WatchEvent:
|
case WatchEvent:
|
||||||
|
|
||||||
var w WatchPayload
|
var w WatchPayload
|
||||||
json.Unmarshal([]byte(payload), &w)
|
|
||||||
hook.runProcessPayloadFunc(fn, w, hd)
|
err = json.Unmarshal([]byte(payload), &w)
|
||||||
|
pl = w
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Println("There was an erro parsing JSON:", err)
|
||||||
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("Running runProcessPayloadFunc")
|
||||||
|
hook.runProcessPayloadFunc(fn, pl, hd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hook Webhook) runProcessPayloadFunc(fn webhooks.ProcessPayloadFunc, results interface{}, header webhooks.Header) {
|
func (hook Webhook) runProcessPayloadFunc(fn webhooks.ProcessPayloadFunc, results interface{}, header webhooks.Header) {
|
||||||
go func(fn webhooks.ProcessPayloadFunc, results interface{}, header webhooks.Header) {
|
go func(fn webhooks.ProcessPayloadFunc, results interface{}, header webhooks.Header) {
|
||||||
|
log.Println("Calling hook function")
|
||||||
fn(results, header)
|
fn(results, header)
|
||||||
}(fn, results, header)
|
}(fn, results, header)
|
||||||
}
|
}
|
||||||
|
|||||||
+104
-352
@@ -9,7 +9,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
. "gopkg.in/go-playground/assert.v1"
|
. "gopkg.in/go-playground/assert.v1"
|
||||||
"gopkg.in/go-playground/webhooks.v4"
|
"gopkg.in/go-playground/webhooks.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NOTES:
|
// NOTES:
|
||||||
@@ -48,8 +48,6 @@ func TestMain(m *testing.M) {
|
|||||||
DeploymentStatusEvent,
|
DeploymentStatusEvent,
|
||||||
ForkEvent,
|
ForkEvent,
|
||||||
GollumEvent,
|
GollumEvent,
|
||||||
InstallationEvent,
|
|
||||||
IntegrationInstallationEvent,
|
|
||||||
IssueCommentEvent,
|
IssueCommentEvent,
|
||||||
IssuesEvent,
|
IssuesEvent,
|
||||||
LabelEvent,
|
LabelEvent,
|
||||||
@@ -59,7 +57,6 @@ func TestMain(m *testing.M) {
|
|||||||
OrganizationEvent,
|
OrganizationEvent,
|
||||||
OrgBlockEvent,
|
OrgBlockEvent,
|
||||||
PageBuildEvent,
|
PageBuildEvent,
|
||||||
PingEvent,
|
|
||||||
ProjectCardEvent,
|
ProjectCardEvent,
|
||||||
ProjectColumnEvent,
|
ProjectColumnEvent,
|
||||||
ProjectEvent,
|
ProjectEvent,
|
||||||
@@ -181,6 +178,7 @@ func TestBadSignatureMatch(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCommitCommentEvent(t *testing.T) {
|
func TestCommitCommentEvent(t *testing.T) {
|
||||||
|
|
||||||
payload := `{
|
payload := `{
|
||||||
"action": "created",
|
"action": "created",
|
||||||
"comment": {
|
"comment": {
|
||||||
@@ -1311,186 +1309,6 @@ func TestGollumEvent(t *testing.T) {
|
|||||||
Equal(t, resp.StatusCode, http.StatusOK)
|
Equal(t, resp.StatusCode, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInstallationEvent(t *testing.T) {
|
|
||||||
|
|
||||||
payload := `{
|
|
||||||
"action": "created",
|
|
||||||
"installation": {
|
|
||||||
"id": 80429,
|
|
||||||
"account": {
|
|
||||||
"login": "PombeirP",
|
|
||||||
"id": 138074,
|
|
||||||
"avatar_url": "https://avatars1.githubusercontent.com/u/138074?v=4",
|
|
||||||
"gravatar_id": "",
|
|
||||||
"url": "https://api.github.com/users/PombeirP",
|
|
||||||
"html_url": "https://github.com/PombeirP",
|
|
||||||
"followers_url": "https://api.github.com/users/PombeirP/followers",
|
|
||||||
"following_url": "https://api.github.com/users/PombeirP/following{/other_user}",
|
|
||||||
"gists_url": "https://api.github.com/users/PombeirP/gists{/gist_id}",
|
|
||||||
"starred_url": "https://api.github.com/users/PombeirP/starred{/owner}{/repo}",
|
|
||||||
"subscriptions_url": "https://api.github.com/users/PombeirP/subscriptions",
|
|
||||||
"organizations_url": "https://api.github.com/users/PombeirP/orgs",
|
|
||||||
"repos_url": "https://api.github.com/users/PombeirP/repos",
|
|
||||||
"events_url": "https://api.github.com/users/PombeirP/events{/privacy}",
|
|
||||||
"received_events_url": "https://api.github.com/users/PombeirP/received_events",
|
|
||||||
"type": "User",
|
|
||||||
"site_admin": false
|
|
||||||
},
|
|
||||||
"repository_selection": "selected",
|
|
||||||
"access_tokens_url": "https://api.github.com/installations/80429/access_tokens",
|
|
||||||
"repositories_url": "https://api.github.com/installation/repositories",
|
|
||||||
"html_url": "https://github.com/settings/installations/80429",
|
|
||||||
"app_id": 8157,
|
|
||||||
"target_id": 138074,
|
|
||||||
"target_type": "User",
|
|
||||||
"permissions": {
|
|
||||||
"repository_projects": "write",
|
|
||||||
"issues": "read",
|
|
||||||
"metadata": "read",
|
|
||||||
"pull_requests": "read"
|
|
||||||
},
|
|
||||||
"events": [
|
|
||||||
"pull_request"
|
|
||||||
],
|
|
||||||
"created_at": 1516025475,
|
|
||||||
"updated_at": 1516025475,
|
|
||||||
"single_file_name": null
|
|
||||||
},
|
|
||||||
"repositories": [
|
|
||||||
{
|
|
||||||
"id": 117381220,
|
|
||||||
"name": "status-github-bot",
|
|
||||||
"full_name": "PombeirP/status-github-bot"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"sender": {
|
|
||||||
"login": "PombeirP",
|
|
||||||
"id": 138074,
|
|
||||||
"avatar_url": "https://avatars1.githubusercontent.com/u/138074?v=4",
|
|
||||||
"gravatar_id": "",
|
|
||||||
"url": "https://api.github.com/users/PombeirP",
|
|
||||||
"html_url": "https://github.com/PombeirP",
|
|
||||||
"followers_url": "https://api.github.com/users/PombeirP/followers",
|
|
||||||
"following_url": "https://api.github.com/users/PombeirP/following{/other_user}",
|
|
||||||
"gists_url": "https://api.github.com/users/PombeirP/gists{/gist_id}",
|
|
||||||
"starred_url": "https://api.github.com/users/PombeirP/starred{/owner}{/repo}",
|
|
||||||
"subscriptions_url": "https://api.github.com/users/PombeirP/subscriptions",
|
|
||||||
"organizations_url": "https://api.github.com/users/PombeirP/orgs",
|
|
||||||
"repos_url": "https://api.github.com/users/PombeirP/repos",
|
|
||||||
"events_url": "https://api.github.com/users/PombeirP/events{/privacy}",
|
|
||||||
"received_events_url": "https://api.github.com/users/PombeirP/received_events",
|
|
||||||
"type": "User",
|
|
||||||
"site_admin": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`
|
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", "http://127.0.0.1:3010/webhooks", bytes.NewBuffer([]byte(payload)))
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
req.Header.Set("X-Github-Event", "installation")
|
|
||||||
req.Header.Set("X-Hub-Signature", "sha1=987338c6e5c21794ab6c258abe51284f9b1df728")
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIntegrationInstallationEvent(t *testing.T) {
|
|
||||||
|
|
||||||
payload := `{
|
|
||||||
"action": "created",
|
|
||||||
"installation": {
|
|
||||||
"id": 80429,
|
|
||||||
"account": {
|
|
||||||
"login": "PombeirP",
|
|
||||||
"id": 138074,
|
|
||||||
"avatar_url": "https://avatars1.githubusercontent.com/u/138074?v=4",
|
|
||||||
"gravatar_id": "",
|
|
||||||
"url": "https://api.github.com/users/PombeirP",
|
|
||||||
"html_url": "https://github.com/PombeirP",
|
|
||||||
"followers_url": "https://api.github.com/users/PombeirP/followers",
|
|
||||||
"following_url": "https://api.github.com/users/PombeirP/following{/other_user}",
|
|
||||||
"gists_url": "https://api.github.com/users/PombeirP/gists{/gist_id}",
|
|
||||||
"starred_url": "https://api.github.com/users/PombeirP/starred{/owner}{/repo}",
|
|
||||||
"subscriptions_url": "https://api.github.com/users/PombeirP/subscriptions",
|
|
||||||
"organizations_url": "https://api.github.com/users/PombeirP/orgs",
|
|
||||||
"repos_url": "https://api.github.com/users/PombeirP/repos",
|
|
||||||
"events_url": "https://api.github.com/users/PombeirP/events{/privacy}",
|
|
||||||
"received_events_url": "https://api.github.com/users/PombeirP/received_events",
|
|
||||||
"type": "User",
|
|
||||||
"site_admin": false
|
|
||||||
},
|
|
||||||
"repository_selection": "selected",
|
|
||||||
"access_tokens_url": "https://api.github.com/installations/80429/access_tokens",
|
|
||||||
"repositories_url": "https://api.github.com/installation/repositories",
|
|
||||||
"html_url": "https://github.com/settings/installations/80429",
|
|
||||||
"app_id": 8157,
|
|
||||||
"target_id": 138074,
|
|
||||||
"target_type": "User",
|
|
||||||
"permissions": {
|
|
||||||
"repository_projects": "write",
|
|
||||||
"issues": "read",
|
|
||||||
"metadata": "read",
|
|
||||||
"pull_requests": "read"
|
|
||||||
},
|
|
||||||
"events": [
|
|
||||||
"pull_request"
|
|
||||||
],
|
|
||||||
"created_at": 1516025475,
|
|
||||||
"updated_at": 1516025475,
|
|
||||||
"single_file_name": null
|
|
||||||
},
|
|
||||||
"repositories": [
|
|
||||||
{
|
|
||||||
"id": 117381220,
|
|
||||||
"name": "status-github-bot",
|
|
||||||
"full_name": "PombeirP/status-github-bot"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"sender": {
|
|
||||||
"login": "PombeirP",
|
|
||||||
"id": 138074,
|
|
||||||
"avatar_url": "https://avatars1.githubusercontent.com/u/138074?v=4",
|
|
||||||
"gravatar_id": "",
|
|
||||||
"url": "https://api.github.com/users/PombeirP",
|
|
||||||
"html_url": "https://github.com/PombeirP",
|
|
||||||
"followers_url": "https://api.github.com/users/PombeirP/followers",
|
|
||||||
"following_url": "https://api.github.com/users/PombeirP/following{/other_user}",
|
|
||||||
"gists_url": "https://api.github.com/users/PombeirP/gists{/gist_id}",
|
|
||||||
"starred_url": "https://api.github.com/users/PombeirP/starred{/owner}{/repo}",
|
|
||||||
"subscriptions_url": "https://api.github.com/users/PombeirP/subscriptions",
|
|
||||||
"organizations_url": "https://api.github.com/users/PombeirP/orgs",
|
|
||||||
"repos_url": "https://api.github.com/users/PombeirP/repos",
|
|
||||||
"events_url": "https://api.github.com/users/PombeirP/events{/privacy}",
|
|
||||||
"received_events_url": "https://api.github.com/users/PombeirP/received_events",
|
|
||||||
"type": "User",
|
|
||||||
"site_admin": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`
|
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", "http://127.0.0.1:3010/webhooks", bytes.NewBuffer([]byte(payload)))
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
req.Header.Set("X-Github-Event", "integration_installation")
|
|
||||||
req.Header.Set("X-Hub-Signature", "sha1=987338c6e5c21794ab6c258abe51284f9b1df728")
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIssueCommentEvent(t *testing.T) {
|
func TestIssueCommentEvent(t *testing.T) {
|
||||||
|
|
||||||
payload := `{
|
payload := `{
|
||||||
@@ -2742,48 +2560,6 @@ func TestPageBuildEvent(t *testing.T) {
|
|||||||
Equal(t, resp.StatusCode, http.StatusOK)
|
Equal(t, resp.StatusCode, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPingEvent(t *testing.T) {
|
|
||||||
|
|
||||||
payload := `{
|
|
||||||
"zen": "Keep it logically awesome.",
|
|
||||||
"hook_id": 20081052,
|
|
||||||
"hook": {
|
|
||||||
"type": "App",
|
|
||||||
"id": 20081052,
|
|
||||||
"name": "web",
|
|
||||||
"active": true,
|
|
||||||
"events": [
|
|
||||||
"pull_request"
|
|
||||||
],
|
|
||||||
"config": {
|
|
||||||
"content_type": "json",
|
|
||||||
"insecure_ssl": "0",
|
|
||||||
"secret": "********",
|
|
||||||
"url": "https://ngrok.io/webhook"
|
|
||||||
},
|
|
||||||
"updated_at": "2018-01-15T10:48:54Z",
|
|
||||||
"created_at": "2018-01-15T10:48:54Z",
|
|
||||||
"app_id": 8157
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`
|
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", "http://127.0.0.1:3010/webhooks", bytes.NewBuffer([]byte(payload)))
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
req.Header.Set("X-Github-Event", "ping")
|
|
||||||
req.Header.Set("X-Hub-Signature", "sha1=f82267eb5c6408d5986209da906747f57c11b33b")
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestProjectCardEvent(t *testing.T) {
|
func TestProjectCardEvent(t *testing.T) {
|
||||||
|
|
||||||
payload := `{
|
payload := `{
|
||||||
@@ -4762,160 +4538,137 @@ func TestPullRequestReviewCommentEvent(t *testing.T) {
|
|||||||
func TestPushEvent(t *testing.T) {
|
func TestPushEvent(t *testing.T) {
|
||||||
|
|
||||||
payload := `{
|
payload := `{
|
||||||
"ref": "refs/heads/master",
|
"ref": "refs/heads/changes",
|
||||||
"before": "737d38c599c1b2991664dfc6155d6bf516fcce36",
|
"before": "9049f1265b7d61be4a8904a9a27120d2064dab3b",
|
||||||
"after": "fd489864e7642b48eaad6e3f155c10e46810ec72",
|
"after": "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c",
|
||||||
"created": false,
|
"created": false,
|
||||||
"deleted": false,
|
"deleted": false,
|
||||||
"forced": false,
|
"forced": false,
|
||||||
"base_ref": null,
|
"base_ref": null,
|
||||||
"compare": "https://github.com/binkkatal/sample_app/compare/737d38c599c1...fd489864e764",
|
"compare": "https://github.com/baxterthehacker/public-repo/compare/9049f1265b7d...0d1a26e67d8f",
|
||||||
"commits": [
|
"commits": [
|
||||||
{
|
{
|
||||||
"id": "fd489864e7642b48eaad6e3f155c10e46810ec72",
|
"id": "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c",
|
||||||
"tree_id": "55e08136e14d5168b699038f88c73e175ddffd3b",
|
"tree_id": "f9d2a07e9488b91af2641b26b9407fe22a451433",
|
||||||
"distinct": true,
|
"distinct": true,
|
||||||
"message": "test a push event",
|
"message": "Update README.md",
|
||||||
"timestamp": "2018-06-29T19:34:13+05:30",
|
"timestamp": "2015-05-05T19:40:15-04:00",
|
||||||
"url": "https://github.com/binkkatal/sample_app/commit/fd489864e7642b48eaad6e3f155c10e46810ec72",
|
"url": "https://github.com/baxterthehacker/public-repo/commit/0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c",
|
||||||
"author": {
|
"author": {
|
||||||
"name": "binkkatal",
|
"name": "baxterthehacker",
|
||||||
"email": "binkkatal.r@gmail.com",
|
"email": "baxterthehacker@users.noreply.github.com",
|
||||||
"username": "binkkatal"
|
"username": "baxterthehacker"
|
||||||
},
|
},
|
||||||
"committer": {
|
"committer": {
|
||||||
"name": "binkkatal",
|
"name": "baxterthehacker",
|
||||||
"email": "binkkatal.r@gmail.com",
|
"email": "baxterthehacker@users.noreply.github.com",
|
||||||
"username": "binkkatal"
|
"username": "baxterthehacker"
|
||||||
},
|
},
|
||||||
"added": [
|
"added": [
|
||||||
".razorops.yaml"
|
|
||||||
],
|
],
|
||||||
"removed": [
|
"removed": [
|
||||||
|
|
||||||
],
|
],
|
||||||
"modified": [
|
"modified": [
|
||||||
"app/controllers/application_controller.rb"
|
"README.md"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"head_commit": {
|
"head_commit": {
|
||||||
"id": "fd489864e7642b48eaad6e3f155c10e46810ec72",
|
"id": "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c",
|
||||||
"tree_id": "55e08136e14d5168b699038f88c73e175ddffd3b",
|
"tree_id": "f9d2a07e9488b91af2641b26b9407fe22a451433",
|
||||||
"distinct": true,
|
"distinct": true,
|
||||||
"message": "test a push event",
|
"message": "Update README.md",
|
||||||
"timestamp": "2018-06-29T19:34:13+05:30",
|
"timestamp": "2015-05-05T19:40:15-04:00",
|
||||||
"url": "https://github.com/binkkatal/sample_app/commit/fd489864e7642b48eaad6e3f155c10e46810ec72",
|
"url": "https://github.com/baxterthehacker/public-repo/commit/0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c",
|
||||||
"author": {
|
"author": {
|
||||||
"name": "binkkatal",
|
"name": "baxterthehacker",
|
||||||
"email": "binkkatal.r@gmail.com",
|
"email": "baxterthehacker@users.noreply.github.com",
|
||||||
"username": "binkkatal"
|
"username": "baxterthehacker"
|
||||||
},
|
},
|
||||||
"committer": {
|
"committer": {
|
||||||
"name": "binkkatal",
|
"name": "baxterthehacker",
|
||||||
"email": "binkkatal.r@gmail.com",
|
"email": "baxterthehacker@users.noreply.github.com",
|
||||||
"username": "binkkatal"
|
"username": "baxterthehacker"
|
||||||
},
|
},
|
||||||
"added": [
|
"added": [
|
||||||
".razorops.yaml"
|
|
||||||
],
|
],
|
||||||
"removed": [
|
"removed": [
|
||||||
|
|
||||||
],
|
],
|
||||||
"modified": [
|
"modified": [
|
||||||
"app/controllers/application_controller.rb"
|
"README.md"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"id": 63933911,
|
"id": 35129377,
|
||||||
"node_id": "MDEwOlJlcG9zaXRvcnk2MzkzMzkxMQ==",
|
"name": "public-repo",
|
||||||
"name": "sample_app",
|
"full_name": "baxterthehacker/public-repo",
|
||||||
"full_name": "binkkatal/sample_app",
|
|
||||||
"owner": {
|
"owner": {
|
||||||
"name": "binkkatal",
|
"name": "baxterthehacker",
|
||||||
"email": "binkkatal.r@gmail.com",
|
"email": "baxterthehacker@users.noreply.github.com"
|
||||||
"login": "binkkatal",
|
|
||||||
"id": 13351472,
|
|
||||||
"node_id": "MDQ6VXNlcjEzMzUxNDcy",
|
|
||||||
"avatar_url": "https://avatars3.githubusercontent.com/u/13351472?v=4",
|
|
||||||
"gravatar_id": "",
|
|
||||||
"url": "https://api.github.com/users/binkkatal",
|
|
||||||
"html_url": "https://github.com/binkkatal",
|
|
||||||
"followers_url": "https://api.github.com/users/binkkatal/followers",
|
|
||||||
"following_url": "https://api.github.com/users/binkkatal/following{/other_user}",
|
|
||||||
"gists_url": "https://api.github.com/users/binkkatal/gists{/gist_id}",
|
|
||||||
"starred_url": "https://api.github.com/users/binkkatal/starred{/owner}{/repo}",
|
|
||||||
"subscriptions_url": "https://api.github.com/users/binkkatal/subscriptions",
|
|
||||||
"organizations_url": "https://api.github.com/users/binkkatal/orgs",
|
|
||||||
"repos_url": "https://api.github.com/users/binkkatal/repos",
|
|
||||||
"events_url": "https://api.github.com/users/binkkatal/events{/privacy}",
|
|
||||||
"received_events_url": "https://api.github.com/users/binkkatal/received_events",
|
|
||||||
"type": "User",
|
|
||||||
"site_admin": false
|
|
||||||
},
|
},
|
||||||
"private": false,
|
"private": false,
|
||||||
"html_url": "https://github.com/binkkatal/sample_app",
|
"html_url": "https://github.com/baxterthehacker/public-repo",
|
||||||
"description": null,
|
"description": "",
|
||||||
"fork": false,
|
"fork": false,
|
||||||
"url": "https://github.com/binkkatal/sample_app",
|
"url": "https://github.com/baxterthehacker/public-repo",
|
||||||
"forks_url": "https://api.github.com/repos/binkkatal/sample_app/forks",
|
"forks_url": "https://api.github.com/repos/baxterthehacker/public-repo/forks",
|
||||||
"keys_url": "https://api.github.com/repos/binkkatal/sample_app/keys{/key_id}",
|
"keys_url": "https://api.github.com/repos/baxterthehacker/public-repo/keys{/key_id}",
|
||||||
"collaborators_url": "https://api.github.com/repos/binkkatal/sample_app/collaborators{/collaborator}",
|
"collaborators_url": "https://api.github.com/repos/baxterthehacker/public-repo/collaborators{/collaborator}",
|
||||||
"teams_url": "https://api.github.com/repos/binkkatal/sample_app/teams",
|
"teams_url": "https://api.github.com/repos/baxterthehacker/public-repo/teams",
|
||||||
"hooks_url": "https://api.github.com/repos/binkkatal/sample_app/hooks",
|
"hooks_url": "https://api.github.com/repos/baxterthehacker/public-repo/hooks",
|
||||||
"issue_events_url": "https://api.github.com/repos/binkkatal/sample_app/issues/events{/number}",
|
"issue_events_url": "https://api.github.com/repos/baxterthehacker/public-repo/issues/events{/number}",
|
||||||
"events_url": "https://api.github.com/repos/binkkatal/sample_app/events",
|
"events_url": "https://api.github.com/repos/baxterthehacker/public-repo/events",
|
||||||
"assignees_url": "https://api.github.com/repos/binkkatal/sample_app/assignees{/user}",
|
"assignees_url": "https://api.github.com/repos/baxterthehacker/public-repo/assignees{/user}",
|
||||||
"branches_url": "https://api.github.com/repos/binkkatal/sample_app/branches{/branch}",
|
"branches_url": "https://api.github.com/repos/baxterthehacker/public-repo/branches{/branch}",
|
||||||
"tags_url": "https://api.github.com/repos/binkkatal/sample_app/tags",
|
"tags_url": "https://api.github.com/repos/baxterthehacker/public-repo/tags",
|
||||||
"blobs_url": "https://api.github.com/repos/binkkatal/sample_app/git/blobs{/sha}",
|
"blobs_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/blobs{/sha}",
|
||||||
"git_tags_url": "https://api.github.com/repos/binkkatal/sample_app/git/tags{/sha}",
|
"git_tags_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/tags{/sha}",
|
||||||
"git_refs_url": "https://api.github.com/repos/binkkatal/sample_app/git/refs{/sha}",
|
"git_refs_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/refs{/sha}",
|
||||||
"trees_url": "https://api.github.com/repos/binkkatal/sample_app/git/trees{/sha}",
|
"trees_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/trees{/sha}",
|
||||||
"statuses_url": "https://api.github.com/repos/binkkatal/sample_app/statuses/{sha}",
|
"statuses_url": "https://api.github.com/repos/baxterthehacker/public-repo/statuses/{sha}",
|
||||||
"languages_url": "https://api.github.com/repos/binkkatal/sample_app/languages",
|
"languages_url": "https://api.github.com/repos/baxterthehacker/public-repo/languages",
|
||||||
"stargazers_url": "https://api.github.com/repos/binkkatal/sample_app/stargazers",
|
"stargazers_url": "https://api.github.com/repos/baxterthehacker/public-repo/stargazers",
|
||||||
"contributors_url": "https://api.github.com/repos/binkkatal/sample_app/contributors",
|
"contributors_url": "https://api.github.com/repos/baxterthehacker/public-repo/contributors",
|
||||||
"subscribers_url": "https://api.github.com/repos/binkkatal/sample_app/subscribers",
|
"subscribers_url": "https://api.github.com/repos/baxterthehacker/public-repo/subscribers",
|
||||||
"subscription_url": "https://api.github.com/repos/binkkatal/sample_app/subscription",
|
"subscription_url": "https://api.github.com/repos/baxterthehacker/public-repo/subscription",
|
||||||
"commits_url": "https://api.github.com/repos/binkkatal/sample_app/commits{/sha}",
|
"commits_url": "https://api.github.com/repos/baxterthehacker/public-repo/commits{/sha}",
|
||||||
"git_commits_url": "https://api.github.com/repos/binkkatal/sample_app/git/commits{/sha}",
|
"git_commits_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/commits{/sha}",
|
||||||
"comments_url": "https://api.github.com/repos/binkkatal/sample_app/comments{/number}",
|
"comments_url": "https://api.github.com/repos/baxterthehacker/public-repo/comments{/number}",
|
||||||
"issue_comment_url": "https://api.github.com/repos/binkkatal/sample_app/issues/comments{/number}",
|
"issue_comment_url": "https://api.github.com/repos/baxterthehacker/public-repo/issues/comments{/number}",
|
||||||
"contents_url": "https://api.github.com/repos/binkkatal/sample_app/contents/{+path}",
|
"contents_url": "https://api.github.com/repos/baxterthehacker/public-repo/contents/{+path}",
|
||||||
"compare_url": "https://api.github.com/repos/binkkatal/sample_app/compare/{base}...{head}",
|
"compare_url": "https://api.github.com/repos/baxterthehacker/public-repo/compare/{base}...{head}",
|
||||||
"merges_url": "https://api.github.com/repos/binkkatal/sample_app/merges",
|
"merges_url": "https://api.github.com/repos/baxterthehacker/public-repo/merges",
|
||||||
"archive_url": "https://api.github.com/repos/binkkatal/sample_app/{archive_format}{/ref}",
|
"archive_url": "https://api.github.com/repos/baxterthehacker/public-repo/{archive_format}{/ref}",
|
||||||
"downloads_url": "https://api.github.com/repos/binkkatal/sample_app/downloads",
|
"downloads_url": "https://api.github.com/repos/baxterthehacker/public-repo/downloads",
|
||||||
"issues_url": "https://api.github.com/repos/binkkatal/sample_app/issues{/number}",
|
"issues_url": "https://api.github.com/repos/baxterthehacker/public-repo/issues{/number}",
|
||||||
"pulls_url": "https://api.github.com/repos/binkkatal/sample_app/pulls{/number}",
|
"pulls_url": "https://api.github.com/repos/baxterthehacker/public-repo/pulls{/number}",
|
||||||
"milestones_url": "https://api.github.com/repos/binkkatal/sample_app/milestones{/number}",
|
"milestones_url": "https://api.github.com/repos/baxterthehacker/public-repo/milestones{/number}",
|
||||||
"notifications_url": "https://api.github.com/repos/binkkatal/sample_app/notifications{?since,all,participating}",
|
"notifications_url": "https://api.github.com/repos/baxterthehacker/public-repo/notifications{?since,all,participating}",
|
||||||
"labels_url": "https://api.github.com/repos/binkkatal/sample_app/labels{/name}",
|
"labels_url": "https://api.github.com/repos/baxterthehacker/public-repo/labels{/name}",
|
||||||
"releases_url": "https://api.github.com/repos/binkkatal/sample_app/releases{/id}",
|
"releases_url": "https://api.github.com/repos/baxterthehacker/public-repo/releases{/id}",
|
||||||
"deployments_url": "https://api.github.com/repos/binkkatal/sample_app/deployments",
|
"created_at": 1430869212,
|
||||||
"created_at": 1469173225,
|
"updated_at": "2015-05-05T23:40:12Z",
|
||||||
"updated_at": "2016-07-22T07:48:39Z",
|
"pushed_at": 1430869217,
|
||||||
"pushed_at": 1530281075,
|
"git_url": "git://github.com/baxterthehacker/public-repo.git",
|
||||||
"git_url": "git://github.com/binkkatal/sample_app.git",
|
"ssh_url": "git@github.com:baxterthehacker/public-repo.git",
|
||||||
"ssh_url": "git@github.com:binkkatal/sample_app.git",
|
"clone_url": "https://github.com/baxterthehacker/public-repo.git",
|
||||||
"clone_url": "https://github.com/binkkatal/sample_app.git",
|
"svn_url": "https://github.com/baxterthehacker/public-repo",
|
||||||
"svn_url": "https://github.com/binkkatal/sample_app",
|
|
||||||
"homepage": null,
|
"homepage": null,
|
||||||
"size": 23,
|
"size": 0,
|
||||||
"stargazers_count": 0,
|
"stargazers_count": 0,
|
||||||
"watchers_count": 0,
|
"watchers_count": 0,
|
||||||
"language": "Ruby",
|
"language": null,
|
||||||
"has_issues": true,
|
"has_issues": true,
|
||||||
"has_projects": true,
|
|
||||||
"has_downloads": true,
|
"has_downloads": true,
|
||||||
"has_wiki": true,
|
"has_wiki": true,
|
||||||
"has_pages": false,
|
"has_pages": true,
|
||||||
"forks_count": 0,
|
"forks_count": 0,
|
||||||
"mirror_url": null,
|
"mirror_url": null,
|
||||||
"archived": false,
|
|
||||||
"open_issues_count": 0,
|
"open_issues_count": 0,
|
||||||
"license": null,
|
|
||||||
"forks": 0,
|
"forks": 0,
|
||||||
"open_issues": 0,
|
"open_issues": 0,
|
||||||
"watchers": 0,
|
"watchers": 0,
|
||||||
@@ -4924,35 +4677,35 @@ func TestPushEvent(t *testing.T) {
|
|||||||
"master_branch": "master"
|
"master_branch": "master"
|
||||||
},
|
},
|
||||||
"pusher": {
|
"pusher": {
|
||||||
"name": "binkkatal",
|
"name": "baxterthehacker",
|
||||||
"email": "binkkatal.r@gmail.com"
|
"email": "baxterthehacker@users.noreply.github.com"
|
||||||
},
|
},
|
||||||
"sender": {
|
"sender": {
|
||||||
"login": "binkkatal",
|
"login": "baxterthehacker",
|
||||||
"id": 13351472,
|
"id": 6752317,
|
||||||
"node_id": "MDQ6VXNlcjEzMzUxNDcy",
|
"avatar_url": "https://avatars.githubusercontent.com/u/6752317?v=3",
|
||||||
"avatar_url": "https://avatars3.githubusercontent.com/u/13351472?v=4",
|
|
||||||
"gravatar_id": "",
|
"gravatar_id": "",
|
||||||
"url": "https://api.github.com/users/binkkatal",
|
"url": "https://api.github.com/users/baxterthehacker",
|
||||||
"html_url": "https://github.com/binkkatal",
|
"html_url": "https://github.com/baxterthehacker",
|
||||||
"followers_url": "https://api.github.com/users/binkkatal/followers",
|
"followers_url": "https://api.github.com/users/baxterthehacker/followers",
|
||||||
"following_url": "https://api.github.com/users/binkkatal/following{/other_user}",
|
"following_url": "https://api.github.com/users/baxterthehacker/following{/other_user}",
|
||||||
"gists_url": "https://api.github.com/users/binkkatal/gists{/gist_id}",
|
"gists_url": "https://api.github.com/users/baxterthehacker/gists{/gist_id}",
|
||||||
"starred_url": "https://api.github.com/users/binkkatal/starred{/owner}{/repo}",
|
"starred_url": "https://api.github.com/users/baxterthehacker/starred{/owner}{/repo}",
|
||||||
"subscriptions_url": "https://api.github.com/users/binkkatal/subscriptions",
|
"subscriptions_url": "https://api.github.com/users/baxterthehacker/subscriptions",
|
||||||
"organizations_url": "https://api.github.com/users/binkkatal/orgs",
|
"organizations_url": "https://api.github.com/users/baxterthehacker/orgs",
|
||||||
"repos_url": "https://api.github.com/users/binkkatal/repos",
|
"repos_url": "https://api.github.com/users/baxterthehacker/repos",
|
||||||
"events_url": "https://api.github.com/users/binkkatal/events{/privacy}",
|
"events_url": "https://api.github.com/users/baxterthehacker/events{/privacy}",
|
||||||
"received_events_url": "https://api.github.com/users/binkkatal/received_events",
|
"received_events_url": "https://api.github.com/users/baxterthehacker/received_events",
|
||||||
"type": "User",
|
"type": "User",
|
||||||
"site_admin": false
|
"site_admin": false
|
||||||
}
|
}
|
||||||
}`
|
}
|
||||||
|
`
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", "http://127.0.0.1:3010/webhooks", bytes.NewBuffer([]byte(payload)))
|
req, err := http.NewRequest("POST", "http://127.0.0.1:3010/webhooks", bytes.NewBuffer([]byte(payload)))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
req.Header.Set("X-Github-Event", "push")
|
req.Header.Set("X-Github-Event", "push")
|
||||||
req.Header.Set("X-Hub-Signature", "sha1=0534736f52c2fc5896ef1bd5a043127b20d233ba")
|
req.Header.Set("X-Hub-Signature", "sha1=c7097badc10db8a2ec615e340534956e75a483c6")
|
||||||
|
|
||||||
Equal(t, err, nil)
|
Equal(t, err, nil)
|
||||||
|
|
||||||
@@ -4963,7 +4716,6 @@ func TestPushEvent(t *testing.T) {
|
|||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
Equal(t, resp.StatusCode, http.StatusOK)
|
Equal(t, resp.StatusCode, http.StatusOK)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReleaseEvent(t *testing.T) {
|
func TestReleaseEvent(t *testing.T) {
|
||||||
|
|||||||
+48
-188
@@ -2,6 +2,8 @@ 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"`
|
||||||
@@ -28,14 +30,13 @@ type CommitCommentPayload struct {
|
|||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
SiteAdmin bool `json:"site_admin"`
|
SiteAdmin bool `json:"site_admin"`
|
||||||
} `json:"user"`
|
} `json:"user"`
|
||||||
Position *int64 `json:"position"`
|
Position *int64 `json:"position"`
|
||||||
Line *int64 `json:"line"`
|
Line *int64 `json:"line"`
|
||||||
Path *string `json:"path"`
|
Path *string `json:"path"`
|
||||||
CommitID string `json:"commit_id"`
|
CommitID string `json:"commit_id"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
Body string `json:"body"`
|
Body string `json:"body"`
|
||||||
AuthorAssociation string `json:"author_association"`
|
|
||||||
} `json:"comment"`
|
} `json:"comment"`
|
||||||
Repository struct {
|
Repository struct {
|
||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
@@ -376,12 +377,13 @@ type DeletePayload struct {
|
|||||||
// DeploymentPayload contains the information for GitHub's deployment hook
|
// DeploymentPayload contains the information for GitHub's deployment hook
|
||||||
type DeploymentPayload struct {
|
type DeploymentPayload struct {
|
||||||
Deployment struct {
|
Deployment struct {
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
Sha string `json:"sha"`
|
Sha string `json:"sha"`
|
||||||
Ref string `json:"ref"`
|
Ref string `json:"ref"`
|
||||||
Task string `json:"task"`
|
Task string `json:"task"`
|
||||||
Payload string `json:"payload"`
|
Payload struct {
|
||||||
|
} `json:"payload"`
|
||||||
Environment string `json:"environment"`
|
Environment string `json:"environment"`
|
||||||
Description *string `json:"description"`
|
Description *string `json:"description"`
|
||||||
Creator struct {
|
Creator struct {
|
||||||
@@ -549,12 +551,13 @@ type DeploymentStatusPayload struct {
|
|||||||
RepositoryURL string `json:"repository_url"`
|
RepositoryURL string `json:"repository_url"`
|
||||||
} `json:"deployment_status"`
|
} `json:"deployment_status"`
|
||||||
Deployment struct {
|
Deployment struct {
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
Sha string `json:"sha"`
|
Sha string `json:"sha"`
|
||||||
Ref string `json:"ref"`
|
Ref string `json:"ref"`
|
||||||
Task string `json:"task"`
|
Task string `json:"task"`
|
||||||
Payload string `json:"payload"`
|
Payload struct {
|
||||||
|
} `json:"payload"`
|
||||||
Environment string `json:"environment"`
|
Environment string `json:"environment"`
|
||||||
Description *string `json:"description"`
|
Description *string `json:"description"`
|
||||||
Creator struct {
|
Creator struct {
|
||||||
@@ -1005,74 +1008,6 @@ type GollumPayload struct {
|
|||||||
} `json:"sender"`
|
} `json:"sender"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// InstallationPayload contains the information for GitHub's installation and integration_installation hook events
|
|
||||||
type InstallationPayload struct {
|
|
||||||
Action string `json:"action"`
|
|
||||||
Installation struct {
|
|
||||||
ID int64 `json:"id"`
|
|
||||||
Account struct {
|
|
||||||
Login string `json:"login"`
|
|
||||||
ID int64 `json:"id"`
|
|
||||||
AvatarURL string `json:"avatar_url"`
|
|
||||||
GravatarID string `json:"gravatar_id"`
|
|
||||||
URL string `json:"url"`
|
|
||||||
HTMLURL string `json:"html_url"`
|
|
||||||
FollowersURL string `json:"followers_url"`
|
|
||||||
FollowingURL string `json:"following_url"`
|
|
||||||
GistsURL string `json:"gists_url"`
|
|
||||||
StarredURL string `json:"starred_url"`
|
|
||||||
SubscriptionsURL string `json:"subscriptions_url"`
|
|
||||||
OrganizationsURL string `json:"organizations_url"`
|
|
||||||
ReposURL string `json:"repos_url"`
|
|
||||||
EventsURL string `json:"events_url"`
|
|
||||||
ReceivedEventsURL string `json:"received_events_url"`
|
|
||||||
Type string `json:"type"`
|
|
||||||
SiteAdmin bool `json:"site_admin"`
|
|
||||||
} `json:"account"`
|
|
||||||
RepositorySelection string `json:"repository_selection"`
|
|
||||||
AccessTokensURL string `json:"access_tokens_url"`
|
|
||||||
RepositoriesURL string `json:"repositories_url"`
|
|
||||||
HTMLURL string `json:"html_url"`
|
|
||||||
AppID int `json:"app_id"`
|
|
||||||
TargetID int `json:"target_id"`
|
|
||||||
TargetType string `json:"target_type"`
|
|
||||||
Permissions struct {
|
|
||||||
Issues string `json:"issues"`
|
|
||||||
Metadata string `json:"metadata"`
|
|
||||||
PullRequests string `json:"pull_requests"`
|
|
||||||
RepositoryProjects string `json:"repository_projects"`
|
|
||||||
} `json:"permissions"`
|
|
||||||
Events []string `json:"events"`
|
|
||||||
CreatedAt int64 `json:"created_at"`
|
|
||||||
UpdatedAt int64 `json:"updated_at"`
|
|
||||||
SingleFileName *string `json:"single_file_name"`
|
|
||||||
} `json:"installation"`
|
|
||||||
Repositories []struct {
|
|
||||||
ID int64 `json:"id"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
FullName string `json:"full_name"`
|
|
||||||
} `json:"repositories"`
|
|
||||||
Sender struct {
|
|
||||||
Login string `json:"login"`
|
|
||||||
ID int64 `json:"id"`
|
|
||||||
AvatarURL string `json:"avatar_url"`
|
|
||||||
GravatarID string `json:"gravatar_id"`
|
|
||||||
URL string `json:"url"`
|
|
||||||
HTMLURL string `json:"html_url"`
|
|
||||||
FollowersURL string `json:"followers_url"`
|
|
||||||
FollowingURL string `json:"following_url"`
|
|
||||||
GistsURL string `json:"gists_url"`
|
|
||||||
StarredURL string `json:"starred_url"`
|
|
||||||
SubscriptionsURL string `json:"subscriptions_url"`
|
|
||||||
OrganizationsURL string `json:"organizations_url"`
|
|
||||||
ReposURL string `json:"repos_url"`
|
|
||||||
EventsURL string `json:"events_url"`
|
|
||||||
ReceivedEventsURL string `json:"received_events_url"`
|
|
||||||
Type string `json:"type"`
|
|
||||||
SiteAdmin bool `json:"site_admin"`
|
|
||||||
} `json:"sender"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// IssueCommentPayload contains the information for GitHub's issue_comment hook event
|
// IssueCommentPayload contains the information for GitHub's issue_comment hook event
|
||||||
type IssueCommentPayload struct {
|
type IssueCommentPayload struct {
|
||||||
Action string `json:"action"`
|
Action string `json:"action"`
|
||||||
@@ -1143,10 +1078,9 @@ type IssueCommentPayload struct {
|
|||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
SiteAdmin bool `json:"site_admin"`
|
SiteAdmin bool `json:"site_admin"`
|
||||||
} `json:"user"`
|
} `json:"user"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
Body string `json:"body"`
|
Body string `json:"body"`
|
||||||
AuthorAssociation string `json:"author_association"`
|
|
||||||
} `json:"comment"`
|
} `json:"comment"`
|
||||||
Repository struct {
|
Repository struct {
|
||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
@@ -2160,27 +2094,6 @@ type PageBuildPayload struct {
|
|||||||
} `json:"sender"`
|
} `json:"sender"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// PingPayload contains the information for GitHub's ping hook event
|
|
||||||
type PingPayload struct {
|
|
||||||
HookID int `json:"hook_id"`
|
|
||||||
Hook struct {
|
|
||||||
Type string `json:"type"`
|
|
||||||
ID int64 `json:"id"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
Active bool `json:"active"`
|
|
||||||
Events []string `json:"events"`
|
|
||||||
AppID int `json:"app_id"`
|
|
||||||
Config struct {
|
|
||||||
ContentType string `json:"content_type"`
|
|
||||||
InsecureSSL int `json:"insecure_ssl"`
|
|
||||||
Secret string `json:"secret"`
|
|
||||||
URL string `json:"url"`
|
|
||||||
} `json:"config"`
|
|
||||||
CreatedAt time.Time `json:"created_at"`
|
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
|
||||||
} `json:"hook"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// ProjectCardPayload contains the information for GitHub's project_payload hook event
|
// ProjectCardPayload contains the information for GitHub's project_payload hook event
|
||||||
type ProjectCardPayload struct {
|
type ProjectCardPayload struct {
|
||||||
Action string `json:"action"`
|
Action string `json:"action"`
|
||||||
@@ -2757,46 +2670,20 @@ type PullRequestPayload struct {
|
|||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
SiteAdmin bool `json:"site_admin"`
|
SiteAdmin bool `json:"site_admin"`
|
||||||
} `json:"user"`
|
} `json:"user"`
|
||||||
Body string `json:"body"`
|
Body string `json:"body"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
ClosedAt *time.Time `json:"closed_at"`
|
ClosedAt *time.Time `json:"closed_at"`
|
||||||
MergedAt *time.Time `json:"merged_at"`
|
MergedAt *time.Time `json:"merged_at"`
|
||||||
MergeCommitSha *string `json:"merge_commit_sha"`
|
MergeCommitSha *string `json:"merge_commit_sha"`
|
||||||
Assignee *Assignee `json:"assignee"`
|
Assignee *Assignee `json:"assignee"`
|
||||||
Milestone *Milestone `json:"milestone"`
|
Milestone *Milestone `json:"milestone"`
|
||||||
CommitsURL string `json:"commits_url"`
|
CommitsURL string `json:"commits_url"`
|
||||||
ReviewCommentsURL string `json:"review_comments_url"`
|
ReviewCommentsURL string `json:"review_comments_url"`
|
||||||
ReviewCommentURL string `json:"review_comment_url"`
|
ReviewCommentURL string `json:"review_comment_url"`
|
||||||
CommentsURL string `json:"comments_url"`
|
CommentsURL string `json:"comments_url"`
|
||||||
StatusesURL string `json:"statuses_url"`
|
StatusesURL string `json:"statuses_url"`
|
||||||
RequestedReviewers []struct {
|
Head struct {
|
||||||
Login string `json:"login"`
|
|
||||||
ID int `json:"id"`
|
|
||||||
AvatarURL string `json:"avatar_url"`
|
|
||||||
GravatarID string `json:"gravatar_id"`
|
|
||||||
URL string `json:"url"`
|
|
||||||
HTMLURL string `json:"html_url"`
|
|
||||||
FollowersURL string `json:"followers_url"`
|
|
||||||
FollowingURL string `json:"following_url"`
|
|
||||||
GistsURL string `json:"gists_url"`
|
|
||||||
StarredURL string `json:"starred_url"`
|
|
||||||
SubscriptionsURL string `json:"subscriptions_url"`
|
|
||||||
OrganizationsURL string `json:"organizations_url"`
|
|
||||||
ReposURL string `json:"repos_url"`
|
|
||||||
EventsURL string `json:"events_url"`
|
|
||||||
ReceivedEventsURL string `json:"received_events_url"`
|
|
||||||
Type string `json:"type"`
|
|
||||||
SiteAdmin bool `json:"site_admin"`
|
|
||||||
} `json:"requested_reviewers,omitempty"`
|
|
||||||
Labels []struct {
|
|
||||||
ID int64 `json:"id"`
|
|
||||||
URL string `json:"url"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
Color string `json:"color"`
|
|
||||||
Default bool `json:"default"`
|
|
||||||
} `json:"labels"`
|
|
||||||
Head struct {
|
|
||||||
Label string `json:"label"`
|
Label string `json:"label"`
|
||||||
Ref string `json:"ref"`
|
Ref string `json:"ref"`
|
||||||
Sha string `json:"sha"`
|
Sha string `json:"sha"`
|
||||||
@@ -3055,13 +2942,6 @@ type PullRequestPayload struct {
|
|||||||
Deletions int64 `json:"deletions"`
|
Deletions int64 `json:"deletions"`
|
||||||
ChangedFiles int64 `json:"changed_files"`
|
ChangedFiles int64 `json:"changed_files"`
|
||||||
} `json:"pull_request"`
|
} `json:"pull_request"`
|
||||||
Label struct {
|
|
||||||
ID int64 `json:"id"`
|
|
||||||
URL string `json:"url"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
Color string `json:"color"`
|
|
||||||
Default bool `json:"default"`
|
|
||||||
} `json:"label"`
|
|
||||||
Repository struct {
|
Repository struct {
|
||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
@@ -3646,13 +3526,12 @@ type PullRequestReviewCommentPayload struct {
|
|||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
SiteAdmin bool `json:"site_admin"`
|
SiteAdmin bool `json:"site_admin"`
|
||||||
} `json:"user"`
|
} `json:"user"`
|
||||||
Body string `json:"body"`
|
Body string `json:"body"`
|
||||||
AuthorAssociation string `json:"author_association"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
HTMLURL string `json:"html_url"`
|
||||||
HTMLURL string `json:"html_url"`
|
PullRequestURL string `json:"pull_request_url"`
|
||||||
PullRequestURL string `json:"pull_request_url"`
|
Links struct {
|
||||||
Links struct {
|
|
||||||
Self struct {
|
Self struct {
|
||||||
Href string `json:"href"`
|
Href string `json:"href"`
|
||||||
} `json:"self"`
|
} `json:"self"`
|
||||||
@@ -4075,7 +3954,6 @@ 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"`
|
||||||
@@ -4122,23 +4000,8 @@ type PushPayload struct {
|
|||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
FullName string `json:"full_name"`
|
FullName string `json:"full_name"`
|
||||||
Owner struct {
|
Owner struct {
|
||||||
Login string `json:"login"`
|
Name string `json:"name"`
|
||||||
ID int64 `json:"id"`
|
Email string `json:"email"`
|
||||||
AvatarURL string `json:"avatar_url"`
|
|
||||||
GravatarID string `json:"gravatar_id"`
|
|
||||||
URL string `json:"url"`
|
|
||||||
HTMLURL string `json:"html_url"`
|
|
||||||
FollowersURL string `json:"followers_url"`
|
|
||||||
FollowingURL string `json:"following_url"`
|
|
||||||
GistsURL string `json:"gists_url"`
|
|
||||||
StarredURL string `json:"starred_url"`
|
|
||||||
SubscriptionsURL string `json:"subscriptions_url"`
|
|
||||||
OrganizationsURL string `json:"organizations_url"`
|
|
||||||
ReposURL string `json:"repos_url"`
|
|
||||||
EventsURL string `json:"events_url"`
|
|
||||||
ReceivedEventsURL string `json:"received_events_url"`
|
|
||||||
Type string `json:"type"`
|
|
||||||
SiteAdmin bool `json:"site_admin"`
|
|
||||||
} `json:"owner"`
|
} `json:"owner"`
|
||||||
Private bool `json:"private"`
|
Private bool `json:"private"`
|
||||||
HTMLURL string `json:"html_url"`
|
HTMLURL string `json:"html_url"`
|
||||||
@@ -4229,9 +4092,6 @@ type PushPayload struct {
|
|||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
SiteAdmin bool `json:"site_admin"`
|
SiteAdmin bool `json:"site_admin"`
|
||||||
} `json:"sender"`
|
} `json:"sender"`
|
||||||
Installation struct {
|
|
||||||
Id int `json:"id"`
|
|
||||||
} `json:"installation"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReleasePayload contains the information for GitHub's release hook event
|
// ReleasePayload contains the information for GitHub's release hook event
|
||||||
|
|||||||
+12
-25
@@ -2,11 +2,10 @@ package gitlab
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"gopkg.in/go-playground/webhooks.v4"
|
"gopkg.in/go-playground/webhooks.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Webhook instance contains all methods needed to process events
|
// Webhook instance contains all methods needed to process events
|
||||||
@@ -26,15 +25,14 @@ type Event string
|
|||||||
|
|
||||||
// GitLab hook types
|
// GitLab hook types
|
||||||
const (
|
const (
|
||||||
PushEvents Event = "Push Hook"
|
PushEvents Event = "Push Hook"
|
||||||
TagEvents Event = "Tag Push Hook"
|
TagEvents Event = "Tag Push Hook"
|
||||||
IssuesEvents Event = "Issue Hook"
|
IssuesEvents Event = "Issue Hook"
|
||||||
ConfidentialIssuesEvents Event = "Confidential Issue Hook"
|
CommentEvents Event = "Note Hook"
|
||||||
CommentEvents Event = "Note Hook"
|
MergeRequestEvents Event = "Merge Request Hook"
|
||||||
MergeRequestEvents Event = "Merge Request Hook"
|
WikiPageEvents Event = "Wiki Page Hook"
|
||||||
WikiPageEvents Event = "Wiki Page Hook"
|
PipelineEvents Event = "Pipeline Hook"
|
||||||
PipelineEvents Event = "Pipeline Hook"
|
BuildEvents Event = "Build Hook"
|
||||||
BuildEvents Event = "Build Hook"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// New creates and returns a WebHook instance denoted by the Provider type
|
// New creates and returns a WebHook instance denoted by the Provider type
|
||||||
@@ -61,39 +59,33 @@ func (hook Webhook) RegisterEvents(fn webhooks.ProcessPayloadFunc, events ...Eve
|
|||||||
|
|
||||||
// ParsePayload parses and verifies the payload and fires off the mapped function, if it exists.
|
// ParsePayload parses and verifies the payload and fires off the mapped function, if it exists.
|
||||||
func (hook Webhook) ParsePayload(w http.ResponseWriter, r *http.Request) {
|
func (hook Webhook) ParsePayload(w http.ResponseWriter, r *http.Request) {
|
||||||
webhooks.DefaultLog.Info("Parsing Payload...")
|
|
||||||
|
|
||||||
event := r.Header.Get("X-Gitlab-Event")
|
event := r.Header.Get("X-Gitlab-Event")
|
||||||
if len(event) == 0 {
|
if len(event) == 0 {
|
||||||
webhooks.DefaultLog.Error("Missing X-Gitlab-Event Header")
|
|
||||||
http.Error(w, "400 Bad Request - Missing X-Gitlab-Event Header", http.StatusBadRequest)
|
http.Error(w, "400 Bad Request - Missing X-Gitlab-Event Header", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
webhooks.DefaultLog.Debug(fmt.Sprintf("X-Gitlab-Event:%s", event))
|
|
||||||
|
|
||||||
gitLabEvent := Event(event)
|
gitLabEvent := Event(event)
|
||||||
|
|
||||||
fn, ok := hook.eventFuncs[gitLabEvent]
|
fn, ok := hook.eventFuncs[gitLabEvent]
|
||||||
// if no event registered
|
// if no event registered
|
||||||
if !ok {
|
if !ok {
|
||||||
webhooks.DefaultLog.Info(fmt.Sprintf("Webhook Event %s not registered, it is recommended to setup only events in gitlab that will be registered in the webhook to avoid unnecessary traffic and reduce potential attack vectors.", event))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
payload, err := ioutil.ReadAll(r.Body)
|
payload, err := ioutil.ReadAll(r.Body)
|
||||||
if err != nil || len(payload) == 0 {
|
if err != nil || len(payload) == 0 {
|
||||||
webhooks.DefaultLog.Error("Issue reading Payload")
|
http.Error(w, "Error reading Body", http.StatusInternalServerError)
|
||||||
http.Error(w, "Error reading Payload", http.StatusInternalServerError)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
webhooks.DefaultLog.Debug(fmt.Sprintf("Payload:%s", string(payload)))
|
|
||||||
|
|
||||||
// If we have a Secret set, we should check the MAC
|
// If we have a Secret set, we should check the MAC
|
||||||
if len(hook.secret) > 0 {
|
if len(hook.secret) > 0 {
|
||||||
webhooks.DefaultLog.Info("Checking secret")
|
|
||||||
signature := r.Header.Get("X-Gitlab-Token")
|
signature := r.Header.Get("X-Gitlab-Token")
|
||||||
|
|
||||||
if signature != hook.secret {
|
if signature != hook.secret {
|
||||||
webhooks.DefaultLog.Error(fmt.Sprintf("Invalid X-Gitlab-Token of '%s'", signature))
|
|
||||||
http.Error(w, "403 Forbidden - Token missmatch", http.StatusForbidden)
|
http.Error(w, "403 Forbidden - Token missmatch", http.StatusForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -113,11 +105,6 @@ func (hook Webhook) ParsePayload(w http.ResponseWriter, r *http.Request) {
|
|||||||
json.Unmarshal([]byte(payload), &te)
|
json.Unmarshal([]byte(payload), &te)
|
||||||
hook.runProcessPayloadFunc(fn, te, hd)
|
hook.runProcessPayloadFunc(fn, te, hd)
|
||||||
|
|
||||||
case ConfidentialIssuesEvents:
|
|
||||||
var cie ConfidentialIssueEventPayload
|
|
||||||
json.Unmarshal([]byte(payload), &cie)
|
|
||||||
hook.runProcessPayloadFunc(fn, cie, hd)
|
|
||||||
|
|
||||||
case IssuesEvents:
|
case IssuesEvents:
|
||||||
var ie IssueEventPayload
|
var ie IssueEventPayload
|
||||||
json.Unmarshal([]byte(payload), &ie)
|
json.Unmarshal([]byte(payload), &ie)
|
||||||
|
|||||||
+67
-203
@@ -9,7 +9,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
. "gopkg.in/go-playground/assert.v1"
|
. "gopkg.in/go-playground/assert.v1"
|
||||||
"gopkg.in/go-playground/webhooks.v4"
|
"gopkg.in/go-playground/webhooks.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NOTES:
|
// NOTES:
|
||||||
@@ -43,7 +43,6 @@ func TestMain(m *testing.M) {
|
|||||||
PushEvents,
|
PushEvents,
|
||||||
TagEvents,
|
TagEvents,
|
||||||
IssuesEvents,
|
IssuesEvents,
|
||||||
ConfidentialIssuesEvents,
|
|
||||||
CommentEvents,
|
CommentEvents,
|
||||||
MergeRequestEvents,
|
MergeRequestEvents,
|
||||||
WikiPageEvents,
|
WikiPageEvents,
|
||||||
@@ -350,78 +349,6 @@ func TestIssueEvent(t *testing.T) {
|
|||||||
Equal(t, resp.StatusCode, http.StatusOK)
|
Equal(t, resp.StatusCode, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConfidentialIssueEvent(t *testing.T) {
|
|
||||||
|
|
||||||
payload := `{
|
|
||||||
"object_kind": "issue",
|
|
||||||
"user": {
|
|
||||||
"name": "Administrator",
|
|
||||||
"username": "root",
|
|
||||||
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
|
|
||||||
},
|
|
||||||
"project":{
|
|
||||||
"name":"Gitlab Test",
|
|
||||||
"description":"Aut reprehenderit ut est.",
|
|
||||||
"web_url":"http://example.com/gitlabhq/gitlab-test",
|
|
||||||
"avatar_url":null,
|
|
||||||
"git_ssh_url":"git@example.com:gitlabhq/gitlab-test.git",
|
|
||||||
"git_http_url":"http://example.com/gitlabhq/gitlab-test.git",
|
|
||||||
"namespace":"GitlabHQ",
|
|
||||||
"visibility_level":20,
|
|
||||||
"path_with_namespace":"gitlabhq/gitlab-test",
|
|
||||||
"default_branch":"master",
|
|
||||||
"homepage":"http://example.com/gitlabhq/gitlab-test",
|
|
||||||
"url":"http://example.com/gitlabhq/gitlab-test.git",
|
|
||||||
"ssh_url":"git@example.com:gitlabhq/gitlab-test.git",
|
|
||||||
"http_url":"http://example.com/gitlabhq/gitlab-test.git"
|
|
||||||
},
|
|
||||||
"repository":{
|
|
||||||
"name": "Gitlab Test",
|
|
||||||
"url": "http://example.com/gitlabhq/gitlab-test.git",
|
|
||||||
"description": "Aut reprehenderit ut est.",
|
|
||||||
"homepage": "http://example.com/gitlabhq/gitlab-test"
|
|
||||||
},
|
|
||||||
"object_attributes": {
|
|
||||||
"id": 301,
|
|
||||||
"title": "New API: create/update/delete file",
|
|
||||||
"assignee_id": 51,
|
|
||||||
"author_id": 51,
|
|
||||||
"project_id": 14,
|
|
||||||
"created_at": "2013-12-03T17:15:43Z",
|
|
||||||
"updated_at": "2013-12-03T17:15:43Z",
|
|
||||||
"position": 0,
|
|
||||||
"branch_name": null,
|
|
||||||
"description": "Create new API for manipulations with repository",
|
|
||||||
"milestone_id": null,
|
|
||||||
"state": "opened",
|
|
||||||
"iid": 23,
|
|
||||||
"url": "http://example.com/diaspora/issues/23",
|
|
||||||
"action": "open"
|
|
||||||
},
|
|
||||||
"assignee": {
|
|
||||||
"name": "User1",
|
|
||||||
"username": "user1",
|
|
||||||
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`
|
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", "http://127.0.0.1:3011/webhooks", bytes.NewBuffer([]byte(payload)))
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
req.Header.Set("X-Gitlab-Event", "Confidential Issue Hook")
|
|
||||||
req.Header.Set("X-Gitlab-Token", "sampleToken!")
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCommentCommitEvent(t *testing.T) {
|
func TestCommentCommitEvent(t *testing.T) {
|
||||||
|
|
||||||
payload := `{
|
payload := `{
|
||||||
@@ -807,145 +734,82 @@ func TestCommentSunippetEvent(t *testing.T) {
|
|||||||
func TestMergeRequestEvent(t *testing.T) {
|
func TestMergeRequestEvent(t *testing.T) {
|
||||||
|
|
||||||
payload := `{
|
payload := `{
|
||||||
"object_kind": "merge_request",
|
"object_kind": "merge_request",
|
||||||
"user": {
|
"user": {
|
||||||
"name": "Administrator",
|
"name": "Administrator",
|
||||||
"username": "root",
|
"username": "root",
|
||||||
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
|
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
|
||||||
},
|
},
|
||||||
"project": {
|
"object_attributes": {
|
||||||
"id": 1,
|
"id": 99,
|
||||||
"name":"Gitlab Test",
|
"target_branch": "master",
|
||||||
|
"source_branch": "ms-viewport",
|
||||||
|
"source_project_id": 14,
|
||||||
|
"author_id": 51,
|
||||||
|
"assignee_id": 6,
|
||||||
|
"title": "MS-Viewport",
|
||||||
|
"created_at": "2013-12-03T17:23:34Z",
|
||||||
|
"updated_at": "2013-12-03T17:23:34Z",
|
||||||
|
"st_commits": null,
|
||||||
|
"st_diffs": null,
|
||||||
|
"milestone_id": null,
|
||||||
|
"state": "opened",
|
||||||
|
"merge_status": "unchecked",
|
||||||
|
"target_project_id": 14,
|
||||||
|
"iid": 1,
|
||||||
|
"description": "",
|
||||||
|
"source":{
|
||||||
|
"name":"Awesome Project",
|
||||||
"description":"Aut reprehenderit ut est.",
|
"description":"Aut reprehenderit ut est.",
|
||||||
"web_url":"http://example.com/gitlabhq/gitlab-test",
|
"web_url":"http://example.com/awesome_space/awesome_project",
|
||||||
"avatar_url":null,
|
"avatar_url":null,
|
||||||
"git_ssh_url":"git@example.com:gitlabhq/gitlab-test.git",
|
"git_ssh_url":"git@example.com:awesome_space/awesome_project.git",
|
||||||
"git_http_url":"http://example.com/gitlabhq/gitlab-test.git",
|
"git_http_url":"http://example.com/awesome_space/awesome_project.git",
|
||||||
"namespace":"GitlabHQ",
|
"namespace":"Awesome Space",
|
||||||
"visibility_level":20,
|
"visibility_level":20,
|
||||||
"path_with_namespace":"gitlabhq/gitlab-test",
|
"path_with_namespace":"awesome_space/awesome_project",
|
||||||
"default_branch":"master",
|
"default_branch":"master",
|
||||||
"homepage":"http://example.com/gitlabhq/gitlab-test",
|
"homepage":"http://example.com/awesome_space/awesome_project",
|
||||||
"url":"http://example.com/gitlabhq/gitlab-test.git",
|
"url":"http://example.com/awesome_space/awesome_project.git",
|
||||||
"ssh_url":"git@example.com:gitlabhq/gitlab-test.git",
|
"ssh_url":"git@example.com:awesome_space/awesome_project.git",
|
||||||
"http_url":"http://example.com/gitlabhq/gitlab-test.git"
|
"http_url":"http://example.com/awesome_space/awesome_project.git"
|
||||||
},
|
},
|
||||||
"repository": {
|
"target": {
|
||||||
"name": "Gitlab Test",
|
"name":"Awesome Project",
|
||||||
"url": "http://example.com/gitlabhq/gitlab-test.git",
|
"description":"Aut reprehenderit ut est.",
|
||||||
"description": "Aut reprehenderit ut est.",
|
"web_url":"http://example.com/awesome_space/awesome_project",
|
||||||
"homepage": "http://example.com/gitlabhq/gitlab-test"
|
"avatar_url":null,
|
||||||
|
"git_ssh_url":"git@example.com:awesome_space/awesome_project.git",
|
||||||
|
"git_http_url":"http://example.com/awesome_space/awesome_project.git",
|
||||||
|
"namespace":"Awesome Space",
|
||||||
|
"visibility_level":20,
|
||||||
|
"path_with_namespace":"awesome_space/awesome_project",
|
||||||
|
"default_branch":"master",
|
||||||
|
"homepage":"http://example.com/awesome_space/awesome_project",
|
||||||
|
"url":"http://example.com/awesome_space/awesome_project.git",
|
||||||
|
"ssh_url":"git@example.com:awesome_space/awesome_project.git",
|
||||||
|
"http_url":"http://example.com/awesome_space/awesome_project.git"
|
||||||
},
|
},
|
||||||
"object_attributes": {
|
"last_commit": {
|
||||||
"id": 99,
|
"id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
|
||||||
"target_branch": "master",
|
"message": "fixed readme",
|
||||||
"source_branch": "ms-viewport",
|
"timestamp": "2012-01-03T23:36:29+02:00",
|
||||||
"source_project_id": 14,
|
"url": "http://example.com/awesome_space/awesome_project/commits/da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
|
||||||
"author_id": 51,
|
"author": {
|
||||||
"assignee_id": 6,
|
"name": "GitLab dev user",
|
||||||
"title": "MS-Viewport",
|
"email": "gitlabdev@dv6700.(none)"
|
||||||
"created_at": "2013-12-03T17:23:34Z",
|
|
||||||
"updated_at": "2013-12-03T17:23:34Z",
|
|
||||||
"milestone_id": null,
|
|
||||||
"state": "opened",
|
|
||||||
"merge_status": "unchecked",
|
|
||||||
"target_project_id": 14,
|
|
||||||
"iid": 1,
|
|
||||||
"description": "",
|
|
||||||
"source": {
|
|
||||||
"name":"Awesome Project",
|
|
||||||
"description":"Aut reprehenderit ut est.",
|
|
||||||
"web_url":"http://example.com/awesome_space/awesome_project",
|
|
||||||
"avatar_url":null,
|
|
||||||
"git_ssh_url":"git@example.com:awesome_space/awesome_project.git",
|
|
||||||
"git_http_url":"http://example.com/awesome_space/awesome_project.git",
|
|
||||||
"namespace":"Awesome Space",
|
|
||||||
"visibility_level":20,
|
|
||||||
"path_with_namespace":"awesome_space/awesome_project",
|
|
||||||
"default_branch":"master",
|
|
||||||
"homepage":"http://example.com/awesome_space/awesome_project",
|
|
||||||
"url":"http://example.com/awesome_space/awesome_project.git",
|
|
||||||
"ssh_url":"git@example.com:awesome_space/awesome_project.git",
|
|
||||||
"http_url":"http://example.com/awesome_space/awesome_project.git"
|
|
||||||
},
|
|
||||||
"target": {
|
|
||||||
"name":"Awesome Project",
|
|
||||||
"description":"Aut reprehenderit ut est.",
|
|
||||||
"web_url":"http://example.com/awesome_space/awesome_project",
|
|
||||||
"avatar_url":null,
|
|
||||||
"git_ssh_url":"git@example.com:awesome_space/awesome_project.git",
|
|
||||||
"git_http_url":"http://example.com/awesome_space/awesome_project.git",
|
|
||||||
"namespace":"Awesome Space",
|
|
||||||
"visibility_level":20,
|
|
||||||
"path_with_namespace":"awesome_space/awesome_project",
|
|
||||||
"default_branch":"master",
|
|
||||||
"homepage":"http://example.com/awesome_space/awesome_project",
|
|
||||||
"url":"http://example.com/awesome_space/awesome_project.git",
|
|
||||||
"ssh_url":"git@example.com:awesome_space/awesome_project.git",
|
|
||||||
"http_url":"http://example.com/awesome_space/awesome_project.git"
|
|
||||||
},
|
|
||||||
"last_commit": {
|
|
||||||
"id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
|
|
||||||
"message": "fixed readme",
|
|
||||||
"timestamp": "2012-01-03T23:36:29+02:00",
|
|
||||||
"url": "http://example.com/awesome_space/awesome_project/commits/da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
|
|
||||||
"author": {
|
|
||||||
"name": "GitLab dev user",
|
|
||||||
"email": "gitlabdev@dv6700.(none)"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"work_in_progress": false,
|
|
||||||
"url": "http://example.com/diaspora/merge_requests/1",
|
|
||||||
"action": "open",
|
|
||||||
"assignee": {
|
|
||||||
"name": "User1",
|
|
||||||
"username": "user1",
|
|
||||||
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"labels": [{
|
"work_in_progress": false,
|
||||||
"id": 206,
|
"url": "http://example.com/diaspora/merge_requests/1",
|
||||||
"title": "API",
|
"action": "open",
|
||||||
"color": "#ffffff",
|
"assignee": {
|
||||||
"project_id": 14,
|
"name": "User1",
|
||||||
"created_at": "2013-12-03T17:15:43Z",
|
"username": "user1",
|
||||||
"updated_at": "2013-12-03T17:15:43Z",
|
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
|
||||||
"template": false,
|
|
||||||
"description": "API related issues",
|
|
||||||
"type": "ProjectLabel",
|
|
||||||
"group_id": 41
|
|
||||||
}],
|
|
||||||
"changes": {
|
|
||||||
"updated_by_id": [null, 1],
|
|
||||||
"updated_at": ["2017-09-15 16:50:55 UTC", "2017-09-15 16:52:00 UTC"],
|
|
||||||
"labels": {
|
|
||||||
"previous": [{
|
|
||||||
"id": 206,
|
|
||||||
"title": "API",
|
|
||||||
"color": "#ffffff",
|
|
||||||
"project_id": 14,
|
|
||||||
"created_at": "2013-12-03T17:15:43Z",
|
|
||||||
"updated_at": "2013-12-03T17:15:43Z",
|
|
||||||
"template": false,
|
|
||||||
"description": "API related issues",
|
|
||||||
"type": "ProjectLabel",
|
|
||||||
"group_id": 41
|
|
||||||
}],
|
|
||||||
"current": [{
|
|
||||||
"id": 205,
|
|
||||||
"title": "Platform",
|
|
||||||
"color": "#123123",
|
|
||||||
"project_id": 14,
|
|
||||||
"created_at": "2013-12-03T17:15:43Z",
|
|
||||||
"updated_at": "2013-12-03T17:15:43Z",
|
|
||||||
"template": false,
|
|
||||||
"description": "Platform related issues",
|
|
||||||
"type": "ProjectLabel",
|
|
||||||
"group_id": 41
|
|
||||||
}]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", "http://127.0.0.1:3011/webhooks", bytes.NewBuffer([]byte(payload)))
|
req, err := http.NewRequest("POST", "http://127.0.0.1:3011/webhooks", bytes.NewBuffer([]byte(payload)))
|
||||||
|
|||||||
+1
-37
@@ -38,14 +38,6 @@ type IssueEventPayload struct {
|
|||||||
Repository Repository `json:"repository"`
|
Repository Repository `json:"repository"`
|
||||||
ObjectAttributes ObjectAttributes `json:"object_attributes"`
|
ObjectAttributes ObjectAttributes `json:"object_attributes"`
|
||||||
Assignee Assignee `json:"assignee"`
|
Assignee Assignee `json:"assignee"`
|
||||||
Changes Changes `json:"changes"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// ConfidentialIssueEventPayload contains the information for GitLab's confidential issue event
|
|
||||||
type ConfidentialIssueEventPayload struct {
|
|
||||||
// The data for confidential issues is currently the same as normal issues,
|
|
||||||
// so we can just embed the normal issue payload type here.
|
|
||||||
IssueEventPayload
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MergeRequestEventPayload contains the information for GitLab's merge request event
|
// MergeRequestEventPayload contains the information for GitLab's merge request event
|
||||||
@@ -53,9 +45,6 @@ type MergeRequestEventPayload struct {
|
|||||||
ObjectKind string `json:"object_kind"`
|
ObjectKind string `json:"object_kind"`
|
||||||
User User `json:"user"`
|
User User `json:"user"`
|
||||||
ObjectAttributes ObjectAttributes `json:"object_attributes"`
|
ObjectAttributes ObjectAttributes `json:"object_attributes"`
|
||||||
Changes Changes `json:"changes"`
|
|
||||||
Project Project `json:"project"`
|
|
||||||
Repository Repository `json:"repository"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// PushEventPayload contains the information for GitLab's push event
|
// PushEventPayload contains the information for GitLab's push event
|
||||||
@@ -323,7 +312,7 @@ type MergeRequest struct {
|
|||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
TargetBranch string `json:"target_branch"`
|
TargetBranch string `json:"target_branch"`
|
||||||
SourceBranch string `json:"source_branch"`
|
SourceBranch string `json:"source_branch"`
|
||||||
SourceProjectID int64 `json:"source_project_id"`
|
SourceProjectID string `json:"source_project_id"`
|
||||||
AssigneeID int64 `json:"assignee_id"`
|
AssigneeID int64 `json:"assignee_id"`
|
||||||
AuthorID int64 `json:"author_id"`
|
AuthorID int64 `json:"author_id"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
@@ -413,28 +402,3 @@ type Author struct {
|
|||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Changes contains all changes associated with a GitLab issue or MR
|
|
||||||
type Changes struct {
|
|
||||||
LabelChanges LabelChanges `json:"labels"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// LabelChanges contains changes in labels assocatiated with a GitLab issue or MR
|
|
||||||
type LabelChanges struct {
|
|
||||||
Previous []Label `json:"previous"`
|
|
||||||
Current []Label `json:"current"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Label contains all of the GitLab label information
|
|
||||||
type Label struct {
|
|
||||||
Id int64 `json:"id"`
|
|
||||||
Title string `json:"title"`
|
|
||||||
Color string `json:"color"`
|
|
||||||
ProjectId int64 `json:"project_id"`
|
|
||||||
CreatedAt customTime `json:"created_at"`
|
|
||||||
UpdatedAt customTime `json:"updated_at"`
|
|
||||||
Template bool `json:"template"`
|
|
||||||
Description string `json:"description"`
|
|
||||||
Type string `json:"type"`
|
|
||||||
GroupId int64 `json:"group_id"`
|
|
||||||
}
|
|
||||||
|
|||||||
-167
@@ -1,167 +0,0 @@
|
|||||||
package gogs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"crypto/hmac"
|
|
||||||
"crypto/sha256"
|
|
||||||
"encoding/hex"
|
|
||||||
client "github.com/gogits/go-gogs-client"
|
|
||||||
"gopkg.in/go-playground/webhooks.v4"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Webhook instance contains all methods needed to process events
|
|
||||||
type Webhook struct {
|
|
||||||
provider webhooks.Provider
|
|
||||||
secret string
|
|
||||||
eventFuncs map[Event]webhooks.ProcessPayloadFunc
|
|
||||||
}
|
|
||||||
|
|
||||||
// Config defines the configuration to create a new Gogs Webhook instance
|
|
||||||
type Config struct {
|
|
||||||
Secret string
|
|
||||||
}
|
|
||||||
|
|
||||||
// Event defines a Gogs hook event type
|
|
||||||
type Event string
|
|
||||||
|
|
||||||
// Gogs hook types
|
|
||||||
const (
|
|
||||||
CreateEvent Event = "create"
|
|
||||||
DeleteEvent Event = "delete"
|
|
||||||
ForkEvent Event = "fork"
|
|
||||||
PushEvent Event = "push"
|
|
||||||
IssuesEvent Event = "issues"
|
|
||||||
IssueCommentEvent Event = "issue_comment"
|
|
||||||
PullRequestEvent Event = "pull_request"
|
|
||||||
ReleaseEvent Event = "release"
|
|
||||||
)
|
|
||||||
|
|
||||||
// New creates and returns a WebHook instance denoted by the Provider type
|
|
||||||
func New(config *Config) *Webhook {
|
|
||||||
return &Webhook{
|
|
||||||
provider: webhooks.Gogs,
|
|
||||||
secret: config.Secret,
|
|
||||||
eventFuncs: map[Event]webhooks.ProcessPayloadFunc{},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Provider returns the current hooks provider ID
|
|
||||||
func (hook Webhook) Provider() webhooks.Provider {
|
|
||||||
return hook.provider
|
|
||||||
}
|
|
||||||
|
|
||||||
// RegisterEvents registers the function to call when the specified event(s) are encountered
|
|
||||||
func (hook Webhook) RegisterEvents(fn webhooks.ProcessPayloadFunc, events ...Event) {
|
|
||||||
|
|
||||||
for _, event := range events {
|
|
||||||
hook.eventFuncs[event] = fn
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ParsePayload parses and verifies the payload and fires off the mapped function, if it exists.
|
|
||||||
func (hook Webhook) ParsePayload(w http.ResponseWriter, r *http.Request) {
|
|
||||||
webhooks.DefaultLog.Info("Parsing Payload...")
|
|
||||||
|
|
||||||
event := r.Header.Get("X-Gogs-Event")
|
|
||||||
if len(event) == 0 {
|
|
||||||
webhooks.DefaultLog.Error("Missing X-Gogs-Event Header")
|
|
||||||
http.Error(w, "400 Bad Request - Missing X-Gogs-Event Header", http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
webhooks.DefaultLog.Debug(fmt.Sprintf("X-Gogs-Event:%s", event))
|
|
||||||
|
|
||||||
gogsEvent := Event(event)
|
|
||||||
|
|
||||||
fn, ok := hook.eventFuncs[gogsEvent]
|
|
||||||
// if no event registered
|
|
||||||
if !ok {
|
|
||||||
webhooks.DefaultLog.Info(fmt.Sprintf("Webhook Event %s not registered, it is recommended to setup only events in gogs that will be registered in the webhook to avoid unnecessary traffic and reduce potential attack vectors.", event))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
payload, err := ioutil.ReadAll(r.Body)
|
|
||||||
if err != nil || len(payload) == 0 {
|
|
||||||
webhooks.DefaultLog.Error("Issue reading Payload")
|
|
||||||
http.Error(w, "Issue reading Payload", http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
webhooks.DefaultLog.Debug(fmt.Sprintf("Payload:%s", string(payload)))
|
|
||||||
|
|
||||||
// If we have a Secret set, we should check the MAC
|
|
||||||
if len(hook.secret) > 0 {
|
|
||||||
webhooks.DefaultLog.Info("Checking secret")
|
|
||||||
signature := r.Header.Get("X-Gogs-Signature")
|
|
||||||
if len(signature) == 0 {
|
|
||||||
webhooks.DefaultLog.Error("Missing X-Gogs-Signature required for HMAC verification")
|
|
||||||
http.Error(w, "403 Forbidden - Missing X-Gogs-Signature required for HMAC verification", http.StatusForbidden)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
webhooks.DefaultLog.Debug(fmt.Sprintf("X-Gogs-Signature:%s", signature))
|
|
||||||
|
|
||||||
mac := hmac.New(sha256.New, []byte(hook.secret))
|
|
||||||
mac.Write(payload)
|
|
||||||
|
|
||||||
expectedMAC := hex.EncodeToString(mac.Sum(nil))
|
|
||||||
|
|
||||||
if !hmac.Equal([]byte(signature), []byte(expectedMAC)) {
|
|
||||||
webhooks.DefaultLog.Debug(string(payload))
|
|
||||||
http.Error(w, "403 Forbidden - HMAC verification failed", http.StatusForbidden)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make headers available to ProcessPayloadFunc as a webhooks type
|
|
||||||
hd := webhooks.Header(r.Header)
|
|
||||||
|
|
||||||
switch gogsEvent {
|
|
||||||
case CreateEvent:
|
|
||||||
var pe client.CreatePayload
|
|
||||||
json.Unmarshal([]byte(payload), &pe)
|
|
||||||
hook.runProcessPayloadFunc(fn, pe, hd)
|
|
||||||
|
|
||||||
case ReleaseEvent:
|
|
||||||
var re client.ReleasePayload
|
|
||||||
json.Unmarshal([]byte(payload), &re)
|
|
||||||
hook.runProcessPayloadFunc(fn, re, hd)
|
|
||||||
|
|
||||||
case PushEvent:
|
|
||||||
var pe client.PushPayload
|
|
||||||
json.Unmarshal([]byte(payload), &pe)
|
|
||||||
hook.runProcessPayloadFunc(fn, pe, hd)
|
|
||||||
|
|
||||||
case DeleteEvent:
|
|
||||||
var de client.DeletePayload
|
|
||||||
json.Unmarshal([]byte(payload), &de)
|
|
||||||
hook.runProcessPayloadFunc(fn, de, hd)
|
|
||||||
|
|
||||||
case ForkEvent:
|
|
||||||
var fe client.ForkPayload
|
|
||||||
json.Unmarshal([]byte(payload), &fe)
|
|
||||||
hook.runProcessPayloadFunc(fn, fe, hd)
|
|
||||||
|
|
||||||
case IssuesEvent:
|
|
||||||
var ie client.IssuesPayload
|
|
||||||
json.Unmarshal([]byte(payload), &ie)
|
|
||||||
hook.runProcessPayloadFunc(fn, ie, hd)
|
|
||||||
|
|
||||||
case IssueCommentEvent:
|
|
||||||
var ice client.IssueCommentPayload
|
|
||||||
json.Unmarshal([]byte(payload), &ice)
|
|
||||||
hook.runProcessPayloadFunc(fn, ice, hd)
|
|
||||||
|
|
||||||
case PullRequestEvent:
|
|
||||||
var pre client.PullRequestPayload
|
|
||||||
json.Unmarshal([]byte(payload), &pre)
|
|
||||||
hook.runProcessPayloadFunc(fn, pre, hd)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (hook Webhook) runProcessPayloadFunc(fn webhooks.ProcessPayloadFunc, results interface{}, header webhooks.Header) {
|
|
||||||
go func(fn webhooks.ProcessPayloadFunc, results interface{}, header webhooks.Header) {
|
|
||||||
fn(results, header)
|
|
||||||
}(fn, results, header)
|
|
||||||
}
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
package webhooks
|
|
||||||
|
|
||||||
import "log"
|
|
||||||
|
|
||||||
// DefaultLog contains the default logger for webhooks, and prints only info and error messages by default
|
|
||||||
// for debugs override DefaultLog or see NewLogger for creating one without debugs.
|
|
||||||
var DefaultLog Logger = new(logger)
|
|
||||||
|
|
||||||
// Logger allows for customizable logging
|
|
||||||
type Logger interface {
|
|
||||||
// Info prints basic information.
|
|
||||||
Info(...interface{})
|
|
||||||
// Error prints error information.
|
|
||||||
Error(...interface{})
|
|
||||||
// Debug prints information usefull for debugging.
|
|
||||||
Debug(...interface{})
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewLogger returns a new logger for use.
|
|
||||||
func NewLogger(debug bool) Logger {
|
|
||||||
return &logger{PrintDebugs: debug}
|
|
||||||
}
|
|
||||||
|
|
||||||
type logger struct {
|
|
||||||
PrintDebugs bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// Info prints basic information.
|
|
||||||
func (l *logger) Info(msg ...interface{}) {
|
|
||||||
log.Println("INFO:", msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
// v prints error information.
|
|
||||||
func (l *logger) Error(msg ...interface{}) {
|
|
||||||
log.Println("ERROR:", msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Debug prints information usefull for debugging.
|
|
||||||
func (l *logger) Debug(msg ...interface{}) {
|
|
||||||
if !l.PrintDebugs {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log.Println("DEBUG:", msg)
|
|
||||||
}
|
|
||||||
+22
-36
@@ -1,7 +1,7 @@
|
|||||||
package webhooks
|
package webhooks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -19,8 +19,6 @@ func (p Provider) String() string {
|
|||||||
return "Bitbucket"
|
return "Bitbucket"
|
||||||
case GitLab:
|
case GitLab:
|
||||||
return "GitLab"
|
return "GitLab"
|
||||||
case Gogs:
|
|
||||||
return "Gogs"
|
|
||||||
default:
|
default:
|
||||||
return "Unknown"
|
return "Unknown"
|
||||||
}
|
}
|
||||||
@@ -31,7 +29,6 @@ const (
|
|||||||
GitHub Provider = iota
|
GitHub Provider = iota
|
||||||
Bitbucket
|
Bitbucket
|
||||||
GitLab
|
GitLab
|
||||||
Gogs
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Webhook interface defines a webhook to receive events
|
// Webhook interface defines a webhook to receive events
|
||||||
@@ -41,31 +38,22 @@ 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}
|
||||||
|
|
||||||
DefaultLog.Info(fmt.Sprintf("Listening on addr: %s path: %s", addr, path))
|
|
||||||
return s.ListenAndServe()
|
return s.ListenAndServe()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,13 +61,12 @@ func Run(hook Webhook, addr string, path string) error {
|
|||||||
func RunServer(s *http.Server, hook Webhook, path string) error {
|
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
|
||||||
DefaultLog.Info(fmt.Sprintf("Listening on addr: %s path: %s", s.Addr, path))
|
|
||||||
return s.ListenAndServe()
|
return s.ListenAndServe()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,35 +77,34 @@ func RunServer(s *http.Server, hook Webhook, path string) error {
|
|||||||
func RunTLSServer(s *http.Server, hook Webhook, path string) error {
|
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
|
||||||
DefaultLog.Info(fmt.Sprintf("Listening on addr: %s path: %s", s.Addr, path))
|
|
||||||
return s.ListenAndServeTLS("", "")
|
return s.ListenAndServeTLS("", "")
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeHTTP is the Handler for every posted WebHook Event
|
// ServeHTTP is the Handler for every posted WebHook Event
|
||||||
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
defer r.Body.Close()
|
defer func() {
|
||||||
DefaultLog.Info("Webhook received")
|
log.Println("Closing Request Body")
|
||||||
|
r.Body.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
log.Println("HTTP METHOD:", r.Method)
|
||||||
if r.Method != "POST" {
|
if r.Method != "POST" {
|
||||||
DefaultLog.Error(fmt.Sprintf("405 Method not allowed, attempt made using Method: %s", r.Method))
|
|
||||||
http.Error(w, "405 Method not allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "405 Method not allowed", http.StatusMethodNotAllowed)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
DefaultLog.Debug(fmt.Sprintf("Include path check: %t", s.includePathCheck))
|
log.Println("Chking that paths match:", r.URL.Path == s.path)
|
||||||
if s.includePathCheck {
|
if r.URL.Path != s.path {
|
||||||
if r.URL.Path != s.path {
|
http.Error(w, "404 Not found", http.StatusNotFound)
|
||||||
DefaultLog.Error(fmt.Sprintf("404 Not found, POST made using path: %s, but expected %s", r.URL.Path, s.path))
|
return
|
||||||
http.Error(w, "404 Not found", http.StatusNotFound)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Println("Parsing Payload")
|
||||||
s.hook.ParsePayload(w, r)
|
s.hook.ParsePayload(w, r)
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-40
@@ -8,8 +8,6 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"net/http/httptest"
|
|
||||||
|
|
||||||
. "gopkg.in/go-playground/assert.v1"
|
. "gopkg.in/go-playground/assert.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -49,43 +47,6 @@ 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")
|
||||||
@@ -136,7 +97,7 @@ func TestRun(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRunServer(t *testing.T) {
|
func TestRunServer(t *testing.T) {
|
||||||
DefaultLog = NewLogger(true)
|
|
||||||
server := &http.Server{Addr: "127.0.0.1:3007", Handler: nil}
|
server := &http.Server{Addr: "127.0.0.1:3007", Handler: nil}
|
||||||
go RunServer(server, fakeHook, "/webhooks")
|
go RunServer(server, fakeHook, "/webhooks")
|
||||||
time.Sleep(5000)
|
time.Sleep(5000)
|
||||||
|
|||||||
Reference in New Issue
Block a user