Compare commits

...

24 Commits

Author SHA1 Message Date
Dean Karn e4aa8cba6c Update README.md 2018-06-28 12:47:55 -07:00
Dean Karn 28d44801e9 Merge pull request #32 from c-Brooks/v4
Make Logger interface more flexible
2018-06-28 12:45:57 -07:00
Dean Karn 7e745505c0 Merge pull request #34 from go-playground/v3
Merge latest v3 changes
2018-06-28 12:34:55 -07:00
Dean Karn 8ffb2ffc32 Update README.md 2018-06-28 12:32:45 -07:00
Dean Karn 84dc839b94 Merge pull request #33 from kishorenc/fix_deployment_payload
Deployment Payload fields should be of type string.
2018-06-28 12:32:17 -07:00
Kishore Nallan 2c541a737f Deployment Payload fields should be string. 2018-06-28 16:33:41 +05:30
Corey Brooks d9e847f7ea Make Logger interface more flexible 2018-06-21 09:28:35 -07:00
Dean Karn be944ed461 update REAME + travis.yml 2018-05-08 07:55:49 -07:00
Dean Karn 16896cdb6b Merge pull request #29 from naiba/v3
Add gogs support
2018-05-08 07:51:34 -07:00
奶爸 3e1bb69b7d fix merge issue 2018-05-08 22:09:43 +08:00
奶爸 c9c9d981d7 godmt 2018-05-07 18:42:23 +08:00
奶爸 22713e3054 add gogs support 2018-05-07 18:41:12 +08:00
奶爸 0ebe5231e2 merge 2018-04-11 16:13:03 +08:00
奶爸 b26a00f48c remove space 2018-04-11 16:11:21 +08:00
Dean Karn 5580947e3e Update README.md 2018-04-08 09:15:57 -07:00
Dean Karn 8db8abb389 Merge pull request #28 from rtnpro/parse-github-pull-request-labels
Parse Github pull request labels from pull request payload.
2018-04-08 09:15:30 -07:00
Ratnadeep Debnath 9e4b7fa61b Parse Github pull request labels from pull request payload. 2018-04-02 12:14:20 +05:30
奶爸 a831111e26 Merge remote-tracking branch 'upstream/v3' into v3 2018-03-23 11:19:22 +08:00
奶爸 6d731433c9 Merge remote-tracking branch 'upstream/v3' into v3 2018-03-21 16:25:13 +08:00
奶爸 d4d9692af0 [fix]Gogs sign verify 2018-03-20 11:27:21 +08:00
奶爸 493e94de50 [fix] Gogs signature calc 2018-03-19 16:16:01 +08:00
奶爸 fc20b2a250 ignore IDEA 2018-03-19 14:26:32 +08:00
奶爸 1c3914ef16 edit import 2018-03-19 14:25:52 +08:00
奶爸 2aa5fdc243 add gogs support 2018-03-19 14:03:56 +08:00
16 changed files with 216 additions and 40 deletions
+1
View File
@@ -6,6 +6,7 @@
# Folders # Folders
_obj _obj
_test _test
.idea
# Architecture specific extensions/prefixes # Architecture specific extensions/prefixes
*.[568vq] *.[568vq]
+4 -4
View File
@@ -1,7 +1,6 @@
language: go language: go
go: go:
- 1.7.6 - 1.10.2
- 1.8.3
- tip - tip
matrix: matrix:
allow_failures: allow_failures:
@@ -22,6 +21,7 @@ before_install:
- mkdir -p $GOPATH/src/gopkg.in - 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.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.v3
- ln -s $GOPATH/src/github.com/$TRAVIS_REPO_SLUG $GOPATH/src/gopkg.in/webhooks.v4
before_script: before_script:
- go vet ./... - go vet ./...
@@ -34,6 +34,6 @@ script:
- go test -race - go test -race
after_success: | after_success: |
[ $TRAVIS_GO_VERSION = 1.8.3 ] && [ $TRAVIS_GO_VERSION = 1.10.2 ] &&
overalls -project="github.com/go-playground/webhooks" -covermode=count -ignore=.git,examples -debug && overalls -project="github.com/go-playground/webhooks" -covermode=count -ignore=.git,examples -debug &&
goveralls -coverprofile=overalls.coverprofile -service travis-ci -repotoken $COVERALLS_TOKEN goveralls -coverprofile=overalls.coverprofile -service travis-ci -repotoken $COVERALLS_TOKEN
+10 -10
View File
@@ -1,10 +1,10 @@
Library webhooks Library webhooks
================ ================
<img align="right" src="https://raw.githubusercontent.com/go-playground/webhooks/v3/logo.png">![Project status](https://img.shields.io/badge/version-3.10.0-green.svg) <img align="right" src="https://raw.githubusercontent.com/go-playground/webhooks/v3/logo.png">![Project status](https://img.shields.io/badge/version-4.0.0-green.svg)
[![Build Status](https://travis-ci.org/go-playground/webhooks.svg?branch=v3)](https://travis-ci.org/go-playground/webhooks) [![Build Status](https://travis-ci.org/go-playground/webhooks.svg?branch=v3)](https://travis-ci.org/go-playground/webhooks)
[![Coverage Status](https://coveralls.io/repos/go-playground/webhooks/badge.svg?branch=v3&service=github)](https://coveralls.io/github/go-playground/webhooks?branch=v3) [![Coverage Status](https://coveralls.io/repos/go-playground/webhooks/badge.svg?branch=v4&service=github)](https://coveralls.io/github/go-playground/webhooks?branch=v3)
[![Go Report Card](https://goreportcard.com/badge/go-playground/webhooks)](https://goreportcard.com/report/go-playground/webhooks) [![Go Report Card](https://goreportcard.com/badge/go-playground/webhooks)](https://goreportcard.com/report/go-playground/webhooks)
[![GoDoc](https://godoc.org/gopkg.in/go-playground/webhooks.v3?status.svg)](https://godoc.org/gopkg.in/go-playground/webhooks.v3) [![GoDoc](https://godoc.org/gopkg.in/go-playground/webhooks.v4?status.svg)](https://godoc.org/gopkg.in/go-playground/webhooks.v4)
![License](https://img.shields.io/dub/l/vibe-d.svg) ![License](https://img.shields.io/dub/l/vibe-d.svg)
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.v3 go get -u gopkg.in/go-playground/webhooks.v4
``` ```
Then import the package into your own code. Then import the package into your own code.
import "gopkg.in/go-playground/webhooks.v3" import "gopkg.in/go-playground/webhooks.v4"
Usage and Documentation Usage and Documentation
------ ------
Please see http://godoc.org/gopkg.in/go-playground/webhooks.v3 for detailed usage docs. Please see http://godoc.org/gopkg.in/go-playground/webhooks.v4 for detailed usage docs.
##### Examples: ##### Examples:
@@ -46,8 +46,8 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"gopkg.in/go-playground/webhooks.v3" "gopkg.in/go-playground/webhooks.v4"
"gopkg.in/go-playground/webhooks.v3/github" "gopkg.in/go-playground/webhooks.v4/github"
) )
const ( const (
@@ -103,8 +103,8 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"gopkg.in/go-playground/webhooks.v3" "gopkg.in/go-playground/webhooks.v4"
"gopkg.in/go-playground/webhooks.v3/github" "gopkg.in/go-playground/webhooks.v4/github"
) )
const ( const (
+1 -1
View File
@@ -6,7 +6,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"gopkg.in/go-playground/webhooks.v3" "gopkg.in/go-playground/webhooks.v4"
) )
// Webhook instance contains all methods needed to process events // Webhook instance contains all methods needed to process events
+1 -1
View File
@@ -9,7 +9,7 @@ import (
"time" "time"
. "gopkg.in/go-playground/assert.v1" . "gopkg.in/go-playground/assert.v1"
"gopkg.in/go-playground/webhooks.v3" "gopkg.in/go-playground/webhooks.v4"
) )
// NOTES: // NOTES:
+5 -5
View File
@@ -5,8 +5,8 @@ import (
"log" "log"
"strconv" "strconv"
"gopkg.in/go-playground/webhooks.v3" "gopkg.in/go-playground/webhooks.v4"
"gopkg.in/go-playground/webhooks.v3/github" "gopkg.in/go-playground/webhooks.v4/github"
) )
const ( const (
@@ -18,15 +18,15 @@ type myLogger struct {
PrintDebugs bool PrintDebugs bool
} }
func (l *myLogger) Info(msg string) { func (l *myLogger) Info(msg ...interface{}) {
log.Println(msg) log.Println(msg)
} }
func (l *myLogger) Error(msg string) { func (l *myLogger) Error(msg ...interface{}) {
log.Println(msg) log.Println(msg)
} }
func (l *myLogger) Debug(msg string) { func (l *myLogger) Debug(msg ...interface{}) {
if !l.PrintDebugs { if !l.PrintDebugs {
return return
} }
+2 -2
View File
@@ -4,8 +4,8 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"gopkg.in/go-playground/webhooks.v3" "gopkg.in/go-playground/webhooks.v4"
"gopkg.in/go-playground/webhooks.v3/github" "gopkg.in/go-playground/webhooks.v4/github"
) )
const ( const (
+2 -2
View File
@@ -4,8 +4,8 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"gopkg.in/go-playground/webhooks.v3" "gopkg.in/go-playground/webhooks.v4"
"gopkg.in/go-playground/webhooks.v3/github" "gopkg.in/go-playground/webhooks.v4/github"
) )
const ( const (
+1 -1
View File
@@ -9,7 +9,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"gopkg.in/go-playground/webhooks.v3" "gopkg.in/go-playground/webhooks.v4"
) )
// Webhook instance contains all methods needed to process events // Webhook instance contains all methods needed to process events
+1 -1
View File
@@ -9,7 +9,7 @@ import (
"time" "time"
. "gopkg.in/go-playground/assert.v1" . "gopkg.in/go-playground/assert.v1"
"gopkg.in/go-playground/webhooks.v3" "gopkg.in/go-playground/webhooks.v4"
) )
// NOTES: // NOTES:
+9 -4
View File
@@ -381,8 +381,7 @@ type DeploymentPayload struct {
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 struct { Payload string `json:"payload"`
} `json:"payload"`
Environment string `json:"environment"` Environment string `json:"environment"`
Description *string `json:"description"` Description *string `json:"description"`
Creator struct { Creator struct {
@@ -555,8 +554,7 @@ type DeploymentStatusPayload struct {
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 struct { Payload string `json:"payload"`
} `json:"payload"`
Environment string `json:"environment"` Environment string `json:"environment"`
Description *string `json:"description"` Description *string `json:"description"`
Creator struct { Creator struct {
@@ -2791,6 +2789,13 @@ type PullRequestPayload struct {
Type string `json:"type"` Type string `json:"type"`
SiteAdmin bool `json:"site_admin"` SiteAdmin bool `json:"site_admin"`
} `json:"requested_reviewers,omitempty"` } `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 { Head struct {
Label string `json:"label"` Label string `json:"label"`
Ref string `json:"ref"` Ref string `json:"ref"`
+1 -1
View File
@@ -6,7 +6,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"gopkg.in/go-playground/webhooks.v3" "gopkg.in/go-playground/webhooks.v4"
) )
// Webhook instance contains all methods needed to process events // Webhook instance contains all methods needed to process events
+2 -2
View File
@@ -9,7 +9,7 @@ import (
"time" "time"
. "gopkg.in/go-playground/assert.v1" . "gopkg.in/go-playground/assert.v1"
"gopkg.in/go-playground/webhooks.v3" "gopkg.in/go-playground/webhooks.v4"
) )
// NOTES: // NOTES:
@@ -945,7 +945,7 @@ func TestMergeRequestEvent(t *testing.T) {
}] }]
} }
} }
} }
` `
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)))
+167
View File
@@ -0,0 +1,167 @@
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)
}
+6 -6
View File
@@ -9,11 +9,11 @@ var DefaultLog Logger = new(logger)
// Logger allows for customizable logging // Logger allows for customizable logging
type Logger interface { type Logger interface {
// Info prints basic information. // Info prints basic information.
Info(string) Info(...interface{})
// Error prints error information. // Error prints error information.
Error(string) Error(...interface{})
// Debug prints information usefull for debugging. // Debug prints information usefull for debugging.
Debug(string) Debug(...interface{})
} }
// NewLogger returns a new logger for use. // NewLogger returns a new logger for use.
@@ -26,17 +26,17 @@ type logger struct {
} }
// Info prints basic information. // Info prints basic information.
func (l *logger) Info(msg string) { func (l *logger) Info(msg ...interface{}) {
log.Println("INFO:", msg) log.Println("INFO:", msg)
} }
// v prints error information. // v prints error information.
func (l *logger) Error(msg string) { func (l *logger) Error(msg ...interface{}) {
log.Println("ERROR:", msg) log.Println("ERROR:", msg)
} }
// Debug prints information usefull for debugging. // Debug prints information usefull for debugging.
func (l *logger) Debug(msg string) { func (l *logger) Debug(msg ...interface{}) {
if !l.PrintDebugs { if !l.PrintDebugs {
return return
} }
+3
View File
@@ -19,6 +19,8 @@ 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"
} }
@@ -29,6 +31,7 @@ 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