Compare commits
27 Commits
debug-issue8
...
v3.6.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 9494e434fc | |||
| 97dd8f3564 | |||
| 8f6fada23d | |||
| 94f9d6694d | |||
| ad5392160c | |||
| 19cab958b6 | |||
| 78ce03b046 | |||
| ced2e979bc | |||
| d60a03e52a | |||
| f553bfaa59 | |||
| 903279e458 | |||
| 5462959f1e | |||
| 4964805803 | |||
| 13e6611c00 | |||
| b9424ab72e | |||
| 203bf4218b | |||
| 5e4be82c0b | |||
| cd89a10b64 | |||
| e120e3b3ba | |||
| a5141d656b | |||
| 9a8b92d028 | |||
| 248dae5b83 | |||
| cc075dfe29 | |||
| c93876b3e9 | |||
| 0926003ddf | |||
| 58dd13d367 | |||
| 4fa39fdfab |
+39
@@ -0,0 +1,39 @@
|
|||||||
|
language: go
|
||||||
|
go:
|
||||||
|
- 1.7.6
|
||||||
|
- 1.8.3
|
||||||
|
- tip
|
||||||
|
matrix:
|
||||||
|
allow_failures:
|
||||||
|
- go: tip
|
||||||
|
|
||||||
|
notifications:
|
||||||
|
email:
|
||||||
|
recipients: dean.karn@gmail.com
|
||||||
|
on_success: change
|
||||||
|
on_failure: always
|
||||||
|
|
||||||
|
before_install:
|
||||||
|
- go get -u github.com/go-playground/overalls
|
||||||
|
- go get -u github.com/mattn/goveralls
|
||||||
|
- go get -u golang.org/x/tools/cmd/cover
|
||||||
|
- go get -u github.com/golang/lint/golint
|
||||||
|
- go get -u github.com/gordonklaus/ineffassign
|
||||||
|
- mkdir -p $GOPATH/src/gopkg.in
|
||||||
|
- ln -s $GOPATH/src/github.com/$TRAVIS_REPO_SLUG $GOPATH/src/gopkg.in/webhooks.v2
|
||||||
|
- ln -s $GOPATH/src/github.com/$TRAVIS_REPO_SLUG $GOPATH/src/gopkg.in/webhooks.v3
|
||||||
|
|
||||||
|
before_script:
|
||||||
|
- go vet ./...
|
||||||
|
|
||||||
|
script:
|
||||||
|
- gofmt -d -s .
|
||||||
|
- golint ./...
|
||||||
|
- ineffassign ./
|
||||||
|
- go test -v ./...
|
||||||
|
- go test -race
|
||||||
|
|
||||||
|
after_success: |
|
||||||
|
[ $TRAVIS_GO_VERSION = 1.8.3 ] &&
|
||||||
|
overalls -project="github.com/go-playground/webhooks" -covermode=count -ignore=.git,examples -debug &&
|
||||||
|
goveralls -coverprofile=overalls.coverprofile -service travis-ci -repotoken $COVERALLS_TOKEN
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
Library webhooks
|
Library webhooks
|
||||||
================
|
================
|
||||||
<img align="right" src="https://raw.githubusercontent.com/go-playground/webhooks/v3/logo.png">
|
<img align="right" src="https://raw.githubusercontent.com/go-playground/webhooks/v3/logo.png">
|
||||||
[](https://semaphoreci.com/joeybloggs/webhooks)
|
[](https://travis-ci.org/go-playground/webhooks)
|
||||||
[](https://coveralls.io/github/go-playground/webhooks?branch=v3)
|
[](https://coveralls.io/github/go-playground/webhooks?branch=v3)
|
||||||
[](https://goreportcard.com/report/go-playground/webhooks)
|
[](https://goreportcard.com/report/go-playground/webhooks)
|
||||||
[](https://godoc.org/gopkg.in/go-playground/webhooks.v3)
|
[](https://godoc.org/gopkg.in/go-playground/webhooks.v3)
|
||||||
|
|||||||
+12
-3
@@ -2,6 +2,7 @@ package bitbucket
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -69,38 +70,46 @@ 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 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 does not match configured uuid of %s", hook.uuid))
|
||||||
|
http.Error(w, "403 Forbidden - X-Hook-UUID does not match", http.StatusForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
http.Error(w, "Error reading Body", http.StatusInternalServerError)
|
webhooks.DefaultLog.Error("Issue reading Payload")
|
||||||
|
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 {
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"gopkg.in/go-playground/webhooks.v3"
|
||||||
|
"gopkg.in/go-playground/webhooks.v3/github"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
path = "/webhooks"
|
||||||
|
port = 3016
|
||||||
|
)
|
||||||
|
|
||||||
|
type myLogger struct {
|
||||||
|
PrintDebugs bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *myLogger) Info(msg string) {
|
||||||
|
log.Println(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *myLogger) Error(msg string) {
|
||||||
|
log.Println(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *myLogger) Debug(msg string) {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,7 +14,6 @@ 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)
|
||||||
@@ -27,7 +26,6 @@ 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)
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ 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
|
||||||
|
|
||||||
@@ -26,7 +25,6 @@ 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) {
|
||||||
|
|||||||
+23
-3
@@ -5,6 +5,7 @@ import (
|
|||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -35,6 +36,8 @@ 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"
|
||||||
@@ -44,6 +47,7 @@ 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"
|
||||||
@@ -96,36 +100,43 @@ 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-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)
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
payload, err := ioutil.ReadAll(r.Body)
|
payload, err := ioutil.ReadAll(r.Body)
|
||||||
if err != nil || len(payload) == 0 {
|
if err != nil || len(payload) == 0 {
|
||||||
http.Error(w, "Error reading Body", http.StatusInternalServerError)
|
webhooks.DefaultLog.Error("Issue reading Payload")
|
||||||
|
http.Error(w, "Issue 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-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)
|
||||||
@@ -133,6 +144,7 @@ func (hook Webhook) ParsePayload(w http.ResponseWriter, r *http.Request) {
|
|||||||
expectedMAC := hex.EncodeToString(mac.Sum(nil))
|
expectedMAC := hex.EncodeToString(mac.Sum(nil))
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
@@ -170,6 +182,10 @@ func (hook Webhook) ParsePayload(w http.ResponseWriter, r *http.Request) {
|
|||||||
var g GollumPayload
|
var g GollumPayload
|
||||||
json.Unmarshal([]byte(payload), &g)
|
json.Unmarshal([]byte(payload), &g)
|
||||||
hook.runProcessPayloadFunc(fn, g, hd)
|
hook.runProcessPayloadFunc(fn, g, hd)
|
||||||
|
case InstallationEvent, IntegrationInstallationEvent:
|
||||||
|
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)
|
json.Unmarshal([]byte(payload), &i)
|
||||||
@@ -206,6 +222,10 @@ func (hook Webhook) ParsePayload(w http.ResponseWriter, r *http.Request) {
|
|||||||
var p PageBuildPayload
|
var p PageBuildPayload
|
||||||
json.Unmarshal([]byte(payload), &p)
|
json.Unmarshal([]byte(payload), &p)
|
||||||
hook.runProcessPayloadFunc(fn, p, hd)
|
hook.runProcessPayloadFunc(fn, p, hd)
|
||||||
|
case PingEvent:
|
||||||
|
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)
|
json.Unmarshal([]byte(payload), &p)
|
||||||
|
|||||||
@@ -48,6 +48,8 @@ func TestMain(m *testing.M) {
|
|||||||
DeploymentStatusEvent,
|
DeploymentStatusEvent,
|
||||||
ForkEvent,
|
ForkEvent,
|
||||||
GollumEvent,
|
GollumEvent,
|
||||||
|
InstallationEvent,
|
||||||
|
IntegrationInstallationEvent,
|
||||||
IssueCommentEvent,
|
IssueCommentEvent,
|
||||||
IssuesEvent,
|
IssuesEvent,
|
||||||
LabelEvent,
|
LabelEvent,
|
||||||
@@ -57,6 +59,7 @@ func TestMain(m *testing.M) {
|
|||||||
OrganizationEvent,
|
OrganizationEvent,
|
||||||
OrgBlockEvent,
|
OrgBlockEvent,
|
||||||
PageBuildEvent,
|
PageBuildEvent,
|
||||||
|
PingEvent,
|
||||||
ProjectCardEvent,
|
ProjectCardEvent,
|
||||||
ProjectColumnEvent,
|
ProjectColumnEvent,
|
||||||
ProjectEvent,
|
ProjectEvent,
|
||||||
@@ -1309,6 +1312,186 @@ 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 := `{
|
||||||
@@ -2560,6 +2743,48 @@ 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 := `{
|
||||||
|
|||||||
+100
-2
@@ -2,8 +2,6 @@ package github
|
|||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
// PushPayload contains the information for GitHub's push hook event
|
|
||||||
|
|
||||||
// CommitCommentPayload contains the information for GitHub's commit_comment hook event
|
// CommitCommentPayload contains the information for GitHub's commit_comment hook event
|
||||||
type CommitCommentPayload struct {
|
type CommitCommentPayload struct {
|
||||||
Action string `json:"action"`
|
Action string `json:"action"`
|
||||||
@@ -37,6 +35,7 @@ type CommitCommentPayload struct {
|
|||||||
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"`
|
||||||
@@ -1008,6 +1007,74 @@ 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"`
|
||||||
@@ -1081,6 +1148,7 @@ type IssueCommentPayload struct {
|
|||||||
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"`
|
||||||
@@ -2094,6 +2162,27 @@ 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"`
|
||||||
@@ -2942,6 +3031,13 @@ 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"`
|
||||||
@@ -3527,6 +3623,7 @@ type PullRequestReviewCommentPayload struct {
|
|||||||
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"`
|
||||||
@@ -3954,6 +4051,7 @@ type PushPayload struct {
|
|||||||
BaseRef *string `json:"base_ref"`
|
BaseRef *string `json:"base_ref"`
|
||||||
Compare string `json:"compare"`
|
Compare string `json:"compare"`
|
||||||
Commits []struct {
|
Commits []struct {
|
||||||
|
Sha string `json:"sha"`
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
TreeID string `json:"tree_id"`
|
TreeID string `json:"tree_id"`
|
||||||
Distinct bool `json:"distinct"`
|
Distinct bool `json:"distinct"`
|
||||||
|
|||||||
+16
-3
@@ -2,6 +2,7 @@ package gitlab
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -28,6 +29,7 @@ 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"
|
||||||
@@ -59,33 +61,39 @@ 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 {
|
||||||
http.Error(w, "Error reading Body", http.StatusInternalServerError)
|
webhooks.DefaultLog.Error("Issue reading Payload")
|
||||||
|
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
|
||||||
}
|
}
|
||||||
@@ -105,6 +113,11 @@ 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)
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ func TestMain(m *testing.M) {
|
|||||||
PushEvents,
|
PushEvents,
|
||||||
TagEvents,
|
TagEvents,
|
||||||
IssuesEvents,
|
IssuesEvents,
|
||||||
|
ConfidentialIssuesEvents,
|
||||||
CommentEvents,
|
CommentEvents,
|
||||||
MergeRequestEvents,
|
MergeRequestEvents,
|
||||||
WikiPageEvents,
|
WikiPageEvents,
|
||||||
@@ -349,6 +350,78 @@ 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 := `{
|
||||||
|
|||||||
+35
-1
@@ -38,6 +38,14 @@ 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
|
||||||
@@ -45,6 +53,7 @@ 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"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// PushEventPayload contains the information for GitLab's push event
|
// PushEventPayload contains the information for GitLab's push event
|
||||||
@@ -312,7 +321,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 string `json:"source_project_id"`
|
SourceProjectID int64 `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"`
|
||||||
@@ -402,3 +411,28 @@ 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"`
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
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(string)
|
||||||
|
// Error prints error information.
|
||||||
|
Error(string)
|
||||||
|
// Debug prints information usefull for debugging.
|
||||||
|
Debug(string)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 string) {
|
||||||
|
log.Println("INFO:", msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// v prints error information.
|
||||||
|
func (l *logger) Error(msg string) {
|
||||||
|
log.Println("ERROR:", msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Debug prints information usefull for debugging.
|
||||||
|
func (l *logger) Debug(msg string) {
|
||||||
|
if !l.PrintDebugs {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Println("DEBUG:", msg)
|
||||||
|
}
|
||||||
+25
-4
@@ -1,6 +1,9 @@
|
|||||||
package webhooks
|
package webhooks
|
||||||
|
|
||||||
import "net/http"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
// Header provides http.Header to minimize imports
|
// Header provides http.Header to minimize imports
|
||||||
type Header http.Header
|
type Header http.Header
|
||||||
@@ -37,20 +40,29 @@ 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()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,10 +72,11 @@ 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()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,25 +89,33 @@ 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 r.Body.Close()
|
||||||
|
DefaultLog.Info("Webhook received")
|
||||||
|
|
||||||
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))
|
||||||
|
if s.includePathCheck {
|
||||||
if r.URL.Path != s.path {
|
if r.URL.Path != s.path {
|
||||||
|
DefaultLog.Error(fmt.Sprintf("404 Not found, POST made using path: %s, but expected %s", r.URL.Path, s.path))
|
||||||
http.Error(w, "404 Not found", http.StatusNotFound)
|
http.Error(w, "404 Not found", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
s.hook.ParsePayload(w, r)
|
s.hook.ParsePayload(w, r)
|
||||||
}
|
}
|
||||||
|
|||||||
+40
-1
@@ -8,6 +8,8 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"net/http/httptest"
|
||||||
|
|
||||||
. "gopkg.in/go-playground/assert.v1"
|
. "gopkg.in/go-playground/assert.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -47,6 +49,43 @@ func TestMain(m *testing.M) {
|
|||||||
// teardown
|
// teardown
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHandler(t *testing.T) {
|
||||||
|
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
mux.Handle("/webhooks", Handler(fakeHook))
|
||||||
|
|
||||||
|
s := httptest.NewServer(Handler(fakeHook))
|
||||||
|
defer s.Close()
|
||||||
|
|
||||||
|
payload := "{}"
|
||||||
|
|
||||||
|
req, err := http.NewRequest("POST", s.URL+"/webhooks", bytes.NewBuffer([]byte(payload)))
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
Equal(t, err, nil)
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
Equal(t, err, nil)
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
Equal(t, resp.StatusCode, http.StatusOK)
|
||||||
|
|
||||||
|
// Test BAD METHOD
|
||||||
|
req, err = http.NewRequest("GET", s.URL+"/webhooks", bytes.NewBuffer([]byte(payload)))
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
Equal(t, err, nil)
|
||||||
|
|
||||||
|
resp, err = client.Do(req)
|
||||||
|
Equal(t, err, nil)
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
Equal(t, resp.StatusCode, http.StatusMethodNotAllowed)
|
||||||
|
}
|
||||||
|
|
||||||
func TestRun(t *testing.T) {
|
func TestRun(t *testing.T) {
|
||||||
|
|
||||||
go Run(fakeHook, "127.0.0.1:3006", "/webhooks")
|
go Run(fakeHook, "127.0.0.1:3006", "/webhooks")
|
||||||
@@ -97,7 +136,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