Converted GitHub
This commit is contained in:
@@ -4,7 +4,7 @@ Library webhooks
|
||||
[](https://travis-ci.org/go-playground/webhooks)
|
||||
[](https://coveralls.io/github/go-playground/webhooks?branch=v3)
|
||||
[](https://goreportcard.com/report/go-playground/webhooks)
|
||||
[](https://godoc.org/gopkg.in/go-playground/webhooks.v4)
|
||||
[](https://godoc.org/gopkg.in/go-playground/webhooks.v5)
|
||||

|
||||
|
||||
Library webhooks allows for easy receiving and parsing of GitHub, Bitbucket and GitLab Webhook Events
|
||||
@@ -24,17 +24,17 @@ Installation
|
||||
Use go get.
|
||||
|
||||
```shell
|
||||
go get -u gopkg.in/go-playground/webhooks.v4
|
||||
go get -u gopkg.in/go-playground/webhooks.v5
|
||||
```
|
||||
|
||||
Then import the package into your own code.
|
||||
|
||||
import "gopkg.in/go-playground/webhooks.v4"
|
||||
import "gopkg.in/go-playground/webhooks.v5"
|
||||
|
||||
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.v5 for detailed usage docs.
|
||||
|
||||
##### Examples:
|
||||
|
||||
@@ -46,8 +46,8 @@ import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"gopkg.in/go-playground/webhooks.v4"
|
||||
"gopkg.in/go-playground/webhooks.v4/github"
|
||||
"gopkg.in/go-playground/webhooks.v5"
|
||||
"gopkg.in/go-playground/webhooks.v5/github"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -103,8 +103,8 @@ import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"gopkg.in/go-playground/webhooks.v4"
|
||||
"gopkg.in/go-playground/webhooks.v4/github"
|
||||
"gopkg.in/go-playground/webhooks.v5"
|
||||
"gopkg.in/go-playground/webhooks.v5/github"
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"gopkg.in/go-playground/webhooks.v4"
|
||||
"gopkg.in/go-playground/webhooks.v5"
|
||||
)
|
||||
|
||||
// Webhook instance contains all methods needed to process events
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"time"
|
||||
|
||||
. "gopkg.in/go-playground/assert.v1"
|
||||
"gopkg.in/go-playground/webhooks.v4"
|
||||
"gopkg.in/go-playground/webhooks.v5"
|
||||
)
|
||||
|
||||
// NOTES:
|
||||
|
||||
@@ -5,8 +5,8 @@ import (
|
||||
"log"
|
||||
"strconv"
|
||||
|
||||
"gopkg.in/go-playground/webhooks.v4"
|
||||
"gopkg.in/go-playground/webhooks.v4/github"
|
||||
"gopkg.in/go-playground/webhooks.v5"
|
||||
"gopkg.in/go-playground/webhooks.v5/github"
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
@@ -4,8 +4,8 @@ import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"gopkg.in/go-playground/webhooks.v4"
|
||||
"gopkg.in/go-playground/webhooks.v4/github"
|
||||
"gopkg.in/go-playground/webhooks.v5"
|
||||
"gopkg.in/go-playground/webhooks.v5/github"
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
@@ -4,8 +4,8 @@ import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"gopkg.in/go-playground/webhooks.v4"
|
||||
"gopkg.in/go-playground/webhooks.v4/github"
|
||||
"gopkg.in/go-playground/webhooks.v5"
|
||||
"gopkg.in/go-playground/webhooks.v5/github"
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
+163
-160
@@ -5,24 +5,23 @@ import (
|
||||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"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 GitHub Webhook instance
|
||||
type Config struct {
|
||||
Secret string
|
||||
}
|
||||
// parse errros
|
||||
var (
|
||||
ErrEventNotSpecifiedToParse = errors.New("No Event specified to parse")
|
||||
ErrInvalidHTTPMethod = errors.New("Invalid HTTP Method")
|
||||
ErrMissingGithubEventHeader = errors.New("Missing X-GitHub-Event Header")
|
||||
ErrMissingHubSignatureHeader = errors.New("Missing X-Hub-Signature")
|
||||
ErrEventNotFound = errors.New("Event not defined to be parsed")
|
||||
ErrParsingPayload = errors.New("Error Reading Payload")
|
||||
ErrHMACVerificationFailed = errors.New("HMAC verification failed")
|
||||
)
|
||||
|
||||
// Event defines a GitHub hook event type
|
||||
type Event string
|
||||
@@ -76,217 +75,221 @@ const (
|
||||
IssueSubtype EventSubtype = "issues"
|
||||
)
|
||||
|
||||
// Option is a configuration option for the webhook
|
||||
type Option func(*Webhook) error
|
||||
|
||||
// Options is a namespace var for configuration options
|
||||
var Options = WebhookOptions{}
|
||||
|
||||
// WebhookOptions is a namespace for configuration option methods
|
||||
type WebhookOptions struct{}
|
||||
|
||||
// Secret registers the GitHub secret
|
||||
func (WebhookOptions) Secret(secret string) Option {
|
||||
return func(hook *Webhook) error {
|
||||
hook.secret = secret
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Webhook instance contains all methods needed to process events
|
||||
type Webhook struct {
|
||||
secret string
|
||||
}
|
||||
|
||||
// New creates and returns a WebHook instance denoted by the Provider type
|
||||
func New(config *Config) *Webhook {
|
||||
return &Webhook{
|
||||
provider: webhooks.GitHub,
|
||||
secret: config.Secret,
|
||||
eventFuncs: map[Event]webhooks.ProcessPayloadFunc{},
|
||||
func New(options ...Option) (*Webhook, error) {
|
||||
hook := new(Webhook)
|
||||
for _, opt := range options {
|
||||
if err := opt(hook); err != nil {
|
||||
return nil, errors.New("Error applying Option")
|
||||
}
|
||||
}
|
||||
return hook, nil
|
||||
}
|
||||
|
||||
// Provider returns the current hooks provider ID
|
||||
func (hook Webhook) Provider() webhooks.Provider {
|
||||
return hook.provider
|
||||
}
|
||||
// Parse verifies and parses the events specified and returns the payload object or an error
|
||||
func (hook Webhook) Parse(r *http.Request, events ...Event) (interface{}, error) {
|
||||
defer func() {
|
||||
_, _ = io.Copy(ioutil.Discard, r.Body)
|
||||
_ = r.Body.Close()
|
||||
}()
|
||||
|
||||
// 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
|
||||
if len(events) == 0 {
|
||||
return nil, ErrEventNotSpecifiedToParse
|
||||
}
|
||||
if r.Method != http.MethodPost {
|
||||
return nil, ErrInvalidHTTPMethod
|
||||
}
|
||||
|
||||
// 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-GitHub-Event")
|
||||
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)
|
||||
return
|
||||
return nil, ErrMissingGithubEventHeader
|
||||
}
|
||||
webhooks.DefaultLog.Debug(fmt.Sprintf("X-GitHub-Event:%s", event))
|
||||
|
||||
gitHubEvent := Event(event)
|
||||
|
||||
fn, ok := hook.eventFuncs[gitHubEvent]
|
||||
// if no event registered
|
||||
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
|
||||
var found bool
|
||||
for _, evt := range events {
|
||||
if evt == gitHubEvent {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
// event not defined to be parsed
|
||||
if !found {
|
||||
return nil, ErrEventNotFound
|
||||
}
|
||||
|
||||
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
|
||||
return nil, ErrParsingPayload
|
||||
}
|
||||
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-Hub-Signature")
|
||||
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)
|
||||
return
|
||||
return nil, ErrMissingHubSignatureHeader
|
||||
}
|
||||
webhooks.DefaultLog.Debug(fmt.Sprintf("X-Hub-Signature:%s", signature))
|
||||
|
||||
mac := hmac.New(sha1.New, []byte(hook.secret))
|
||||
mac.Write(payload)
|
||||
|
||||
expectedMAC := hex.EncodeToString(mac.Sum(nil))
|
||||
|
||||
if !hmac.Equal([]byte(signature[5:]), []byte(expectedMAC)) {
|
||||
webhooks.DefaultLog.Error("HMAC verification failed")
|
||||
http.Error(w, "403 Forbidden - HMAC verification failed", http.StatusForbidden)
|
||||
return
|
||||
return nil, ErrHMACVerificationFailed
|
||||
}
|
||||
}
|
||||
|
||||
// Make headers available to ProcessPayloadFunc as a webhooks type
|
||||
hd := webhooks.Header(r.Header)
|
||||
|
||||
switch gitHubEvent {
|
||||
case CommitCommentEvent:
|
||||
var cc CommitCommentPayload
|
||||
json.Unmarshal([]byte(payload), &cc)
|
||||
hook.runProcessPayloadFunc(fn, cc, hd)
|
||||
var pl CommitCommentPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case CreateEvent:
|
||||
var c CreatePayload
|
||||
json.Unmarshal([]byte(payload), &c)
|
||||
hook.runProcessPayloadFunc(fn, c, hd)
|
||||
var pl CreatePayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case DeleteEvent:
|
||||
var d DeletePayload
|
||||
json.Unmarshal([]byte(payload), &d)
|
||||
hook.runProcessPayloadFunc(fn, d, hd)
|
||||
var pl DeletePayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case DeploymentEvent:
|
||||
var d DeploymentPayload
|
||||
json.Unmarshal([]byte(payload), &d)
|
||||
hook.runProcessPayloadFunc(fn, d, hd)
|
||||
var pl DeploymentPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case DeploymentStatusEvent:
|
||||
var d DeploymentStatusPayload
|
||||
json.Unmarshal([]byte(payload), &d)
|
||||
hook.runProcessPayloadFunc(fn, d, hd)
|
||||
var pl DeploymentStatusPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case ForkEvent:
|
||||
var f ForkPayload
|
||||
json.Unmarshal([]byte(payload), &f)
|
||||
hook.runProcessPayloadFunc(fn, f, hd)
|
||||
var pl ForkPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case GollumEvent:
|
||||
var g GollumPayload
|
||||
json.Unmarshal([]byte(payload), &g)
|
||||
hook.runProcessPayloadFunc(fn, g, hd)
|
||||
var pl GollumPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case InstallationEvent, IntegrationInstallationEvent:
|
||||
var i InstallationPayload
|
||||
json.Unmarshal([]byte(payload), &i)
|
||||
hook.runProcessPayloadFunc(fn, i, hd)
|
||||
var pl InstallationPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case IssueCommentEvent:
|
||||
var i IssueCommentPayload
|
||||
json.Unmarshal([]byte(payload), &i)
|
||||
hook.runProcessPayloadFunc(fn, i, hd)
|
||||
var pl IssueCommentPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case IssuesEvent:
|
||||
var i IssuesPayload
|
||||
json.Unmarshal([]byte(payload), &i)
|
||||
hook.runProcessPayloadFunc(fn, i, hd)
|
||||
var pl IssuesPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case LabelEvent:
|
||||
var l LabelPayload
|
||||
json.Unmarshal([]byte(payload), &l)
|
||||
hook.runProcessPayloadFunc(fn, l, hd)
|
||||
var pl LabelPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case MemberEvent:
|
||||
var m MemberPayload
|
||||
json.Unmarshal([]byte(payload), &m)
|
||||
hook.runProcessPayloadFunc(fn, m, hd)
|
||||
var pl MemberPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case MembershipEvent:
|
||||
var m MembershipPayload
|
||||
json.Unmarshal([]byte(payload), &m)
|
||||
hook.runProcessPayloadFunc(fn, m, hd)
|
||||
var pl MembershipPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case MilestoneEvent:
|
||||
var m MilestonePayload
|
||||
json.Unmarshal([]byte(payload), &m)
|
||||
hook.runProcessPayloadFunc(fn, m, hd)
|
||||
var pl MilestonePayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case OrganizationEvent:
|
||||
var o OrganizationPayload
|
||||
json.Unmarshal([]byte(payload), &o)
|
||||
hook.runProcessPayloadFunc(fn, o, hd)
|
||||
var pl OrganizationPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case OrgBlockEvent:
|
||||
var o OrgBlockPayload
|
||||
json.Unmarshal([]byte(payload), &o)
|
||||
hook.runProcessPayloadFunc(fn, o, hd)
|
||||
var pl OrgBlockPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case PageBuildEvent:
|
||||
var p PageBuildPayload
|
||||
json.Unmarshal([]byte(payload), &p)
|
||||
hook.runProcessPayloadFunc(fn, p, hd)
|
||||
var pl PageBuildPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case PingEvent:
|
||||
var p PingPayload
|
||||
json.Unmarshal([]byte(payload), &p)
|
||||
hook.runProcessPayloadFunc(fn, p, hd)
|
||||
var pl PingPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case ProjectCardEvent:
|
||||
var p ProjectCardPayload
|
||||
json.Unmarshal([]byte(payload), &p)
|
||||
hook.runProcessPayloadFunc(fn, p, hd)
|
||||
var pl ProjectCardPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case ProjectColumnEvent:
|
||||
var p ProjectColumnPayload
|
||||
json.Unmarshal([]byte(payload), &p)
|
||||
hook.runProcessPayloadFunc(fn, p, hd)
|
||||
var pl ProjectColumnPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case ProjectEvent:
|
||||
var p ProjectPayload
|
||||
json.Unmarshal([]byte(payload), &p)
|
||||
hook.runProcessPayloadFunc(fn, p, hd)
|
||||
var pl ProjectPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case PublicEvent:
|
||||
var p PublicPayload
|
||||
json.Unmarshal([]byte(payload), &p)
|
||||
hook.runProcessPayloadFunc(fn, p, hd)
|
||||
var pl PublicPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case PullRequestEvent:
|
||||
var p PullRequestPayload
|
||||
json.Unmarshal([]byte(payload), &p)
|
||||
hook.runProcessPayloadFunc(fn, p, hd)
|
||||
var pl PullRequestPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case PullRequestReviewEvent:
|
||||
var p PullRequestReviewPayload
|
||||
json.Unmarshal([]byte(payload), &p)
|
||||
hook.runProcessPayloadFunc(fn, p, hd)
|
||||
var pl PullRequestReviewPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case PullRequestReviewCommentEvent:
|
||||
var p PullRequestReviewCommentPayload
|
||||
json.Unmarshal([]byte(payload), &p)
|
||||
hook.runProcessPayloadFunc(fn, p, hd)
|
||||
var pl PullRequestReviewCommentPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case PushEvent:
|
||||
var p PushPayload
|
||||
json.Unmarshal([]byte(payload), &p)
|
||||
hook.runProcessPayloadFunc(fn, p, hd)
|
||||
var pl PushPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case ReleaseEvent:
|
||||
var r ReleasePayload
|
||||
json.Unmarshal([]byte(payload), &r)
|
||||
hook.runProcessPayloadFunc(fn, r, hd)
|
||||
var pl ReleasePayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case RepositoryEvent:
|
||||
var r RepositoryPayload
|
||||
json.Unmarshal([]byte(payload), &r)
|
||||
hook.runProcessPayloadFunc(fn, r, hd)
|
||||
var pl RepositoryPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case StatusEvent:
|
||||
var s StatusPayload
|
||||
json.Unmarshal([]byte(payload), &s)
|
||||
hook.runProcessPayloadFunc(fn, s, hd)
|
||||
var pl StatusPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case TeamEvent:
|
||||
var t TeamPayload
|
||||
json.Unmarshal([]byte(payload), &t)
|
||||
hook.runProcessPayloadFunc(fn, t, hd)
|
||||
var pl TeamPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case TeamAddEvent:
|
||||
var t TeamAddPayload
|
||||
json.Unmarshal([]byte(payload), &t)
|
||||
hook.runProcessPayloadFunc(fn, t, hd)
|
||||
var pl TeamAddPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case WatchEvent:
|
||||
var w WatchPayload
|
||||
json.Unmarshal([]byte(payload), &w)
|
||||
hook.runProcessPayloadFunc(fn, w, hd)
|
||||
var pl WatchPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown event %s", gitHubEvent)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
+449
-222
File diff suppressed because it is too large
Load Diff
+4
-4
@@ -381,7 +381,7 @@ type DeploymentPayload struct {
|
||||
Sha string `json:"sha"`
|
||||
Ref string `json:"ref"`
|
||||
Task string `json:"task"`
|
||||
Payload string `json:"payload"`
|
||||
Payload struct{} `json:"payload"`
|
||||
Environment string `json:"environment"`
|
||||
Description *string `json:"description"`
|
||||
Creator struct {
|
||||
@@ -554,7 +554,7 @@ type DeploymentStatusPayload struct {
|
||||
Sha string `json:"sha"`
|
||||
Ref string `json:"ref"`
|
||||
Task string `json:"task"`
|
||||
Payload string `json:"payload"`
|
||||
Payload struct{} `json:"payload"`
|
||||
Environment string `json:"environment"`
|
||||
Description *string `json:"description"`
|
||||
Creator struct {
|
||||
@@ -2172,7 +2172,7 @@ type PingPayload struct {
|
||||
AppID int `json:"app_id"`
|
||||
Config struct {
|
||||
ContentType string `json:"content_type"`
|
||||
InsecureSSL int `json:"insecure_ssl"`
|
||||
InsecureSSL string `json:"insecure_ssl"`
|
||||
Secret string `json:"secret"`
|
||||
URL string `json:"url"`
|
||||
} `json:"config"`
|
||||
@@ -4230,7 +4230,7 @@ type PushPayload struct {
|
||||
SiteAdmin bool `json:"site_admin"`
|
||||
} `json:"sender"`
|
||||
Installation struct {
|
||||
Id int `json:"id"`
|
||||
ID int `json:"id"`
|
||||
} `json:"installation"`
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@ import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"gopkg.in/go-playground/webhooks.v4"
|
||||
"gopkg.in/go-playground/webhooks.v5"
|
||||
)
|
||||
|
||||
// Webhook instance contains all methods needed to process events
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"time"
|
||||
|
||||
. "gopkg.in/go-playground/assert.v1"
|
||||
"gopkg.in/go-playground/webhooks.v4"
|
||||
"gopkg.in/go-playground/webhooks.v5"
|
||||
)
|
||||
|
||||
// NOTES:
|
||||
|
||||
+2
-1
@@ -9,8 +9,9 @@ import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
|
||||
client "github.com/gogits/go-gogs-client"
|
||||
"gopkg.in/go-playground/webhooks.v4"
|
||||
"gopkg.in/go-playground/webhooks.v5"
|
||||
)
|
||||
|
||||
// Webhook instance contains all methods needed to process events
|
||||
|
||||
+95
-96
@@ -1,124 +1,123 @@
|
||||
package webhooks
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Header provides http.Header to minimize imports
|
||||
type Header http.Header
|
||||
|
||||
// Provider defines the type of webhook
|
||||
type Provider int
|
||||
// // Provider defines the type of webhook
|
||||
// type Provider int
|
||||
|
||||
func (p Provider) String() string {
|
||||
switch p {
|
||||
case GitHub:
|
||||
return "GitHub"
|
||||
case Bitbucket:
|
||||
return "Bitbucket"
|
||||
case GitLab:
|
||||
return "GitLab"
|
||||
case Gogs:
|
||||
return "Gogs"
|
||||
default:
|
||||
return "Unknown"
|
||||
}
|
||||
}
|
||||
// func (p Provider) String() string {
|
||||
// switch p {
|
||||
// case GitHub:
|
||||
// return "GitHub"
|
||||
// case Bitbucket:
|
||||
// return "Bitbucket"
|
||||
// case GitLab:
|
||||
// return "GitLab"
|
||||
// case Gogs:
|
||||
// return "Gogs"
|
||||
// default:
|
||||
// return "Unknown"
|
||||
// }
|
||||
// }
|
||||
|
||||
// webhooks available providers
|
||||
const (
|
||||
GitHub Provider = iota
|
||||
Bitbucket
|
||||
GitLab
|
||||
Gogs
|
||||
)
|
||||
// // webhooks available providers
|
||||
// const (
|
||||
// GitHub Provider = iota
|
||||
// Bitbucket
|
||||
// GitLab
|
||||
// Gogs
|
||||
// )
|
||||
|
||||
// Webhook interface defines a webhook to receive events
|
||||
type Webhook interface {
|
||||
Provider() Provider
|
||||
ParsePayload(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
// // Webhook interface defines a webhook to receive events
|
||||
// type Webhook interface {
|
||||
// Provider() Provider
|
||||
// ParsePayload(w http.ResponseWriter, r *http.Request)
|
||||
// }
|
||||
|
||||
type server struct {
|
||||
hook Webhook
|
||||
path string
|
||||
includePathCheck bool
|
||||
}
|
||||
// type server struct {
|
||||
// hook Webhook
|
||||
// path string
|
||||
// includePathCheck bool
|
||||
// }
|
||||
|
||||
// ProcessPayloadFunc is a common function for payload return values
|
||||
type ProcessPayloadFunc func(payload interface{}, header Header)
|
||||
type ProcessPayloadFunc func(payload interface{}, header Header) error
|
||||
|
||||
// Handler returns the webhook http.Handler for use in your own Mux implementation
|
||||
func Handler(hook Webhook) http.Handler {
|
||||
return &server{
|
||||
hook: hook,
|
||||
}
|
||||
}
|
||||
// // 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
|
||||
func Run(hook Webhook, addr string, path string) error {
|
||||
srv := &server{
|
||||
hook: hook,
|
||||
path: path,
|
||||
includePathCheck: true,
|
||||
}
|
||||
s := &http.Server{Addr: addr, Handler: srv}
|
||||
// // Run runs a server
|
||||
// func Run(hook Webhook, addr string, path string) error {
|
||||
// srv := &server{
|
||||
// hook: hook,
|
||||
// path: path,
|
||||
// includePathCheck: true,
|
||||
// }
|
||||
// s := &http.Server{Addr: addr, Handler: srv}
|
||||
|
||||
DefaultLog.Info(fmt.Sprintf("Listening on addr: %s path: %s", addr, path))
|
||||
return s.ListenAndServe()
|
||||
}
|
||||
// DefaultLog.Info(fmt.Sprintf("Listening on addr: %s path: %s", addr, path))
|
||||
// return s.ListenAndServe()
|
||||
// }
|
||||
|
||||
// RunServer runs a custom server.
|
||||
func RunServer(s *http.Server, hook Webhook, path string) error {
|
||||
// // RunServer runs a custom server.
|
||||
// func RunServer(s *http.Server, hook Webhook, path string) error {
|
||||
|
||||
srv := &server{
|
||||
hook: hook,
|
||||
path: path,
|
||||
includePathCheck: true,
|
||||
}
|
||||
// srv := &server{
|
||||
// hook: hook,
|
||||
// path: path,
|
||||
// includePathCheck: true,
|
||||
// }
|
||||
|
||||
s.Handler = srv
|
||||
DefaultLog.Info(fmt.Sprintf("Listening on addr: %s path: %s", s.Addr, path))
|
||||
return s.ListenAndServe()
|
||||
}
|
||||
// s.Handler = srv
|
||||
// DefaultLog.Info(fmt.Sprintf("Listening on addr: %s path: %s", s.Addr, path))
|
||||
// return s.ListenAndServe()
|
||||
// }
|
||||
|
||||
// RunTLSServer runs a custom server with TLS configuration.
|
||||
// NOTE: http.Server Handler will be overridden by this library, just set it to nil.
|
||||
// Setting the Certificates can be done in the http.Server.TLSConfig.Certificates
|
||||
// see example here: https://github.com/go-playground/webhooks/blob/v2/webhooks_test.go#L178
|
||||
func RunTLSServer(s *http.Server, hook Webhook, path string) error {
|
||||
// // RunTLSServer runs a custom server with TLS configuration.
|
||||
// // NOTE: http.Server Handler will be overridden by this library, just set it to nil.
|
||||
// // Setting the Certificates can be done in the http.Server.TLSConfig.Certificates
|
||||
// // see example here: https://github.com/go-playground/webhooks/blob/v2/webhooks_test.go#L178
|
||||
// func RunTLSServer(s *http.Server, hook Webhook, path string) error {
|
||||
|
||||
srv := &server{
|
||||
hook: hook,
|
||||
path: path,
|
||||
includePathCheck: true,
|
||||
}
|
||||
// srv := &server{
|
||||
// hook: hook,
|
||||
// path: path,
|
||||
// includePathCheck: true,
|
||||
// }
|
||||
|
||||
s.Handler = srv
|
||||
DefaultLog.Info(fmt.Sprintf("Listening on addr: %s path: %s", s.Addr, path))
|
||||
return s.ListenAndServeTLS("", "")
|
||||
}
|
||||
// s.Handler = srv
|
||||
// DefaultLog.Info(fmt.Sprintf("Listening on addr: %s path: %s", s.Addr, path))
|
||||
// return s.ListenAndServeTLS("", "")
|
||||
// }
|
||||
|
||||
// ServeHTTP is the Handler for every posted WebHook Event
|
||||
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
DefaultLog.Info("Webhook received")
|
||||
// // ServeHTTP is the Handler for every posted WebHook Event
|
||||
// func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
// defer r.Body.Close()
|
||||
// DefaultLog.Info("Webhook received")
|
||||
|
||||
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)
|
||||
return
|
||||
}
|
||||
// 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)
|
||||
// return
|
||||
// }
|
||||
|
||||
DefaultLog.Debug(fmt.Sprintf("Include path check: %t", s.includePathCheck))
|
||||
if s.includePathCheck {
|
||||
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)
|
||||
return
|
||||
}
|
||||
}
|
||||
// DefaultLog.Debug(fmt.Sprintf("Include path check: %t", s.includePathCheck))
|
||||
// if s.includePathCheck {
|
||||
// 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)
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
|
||||
s.hook.ParsePayload(w, r)
|
||||
}
|
||||
// s.hook.ParsePayload(w, r)
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user