Compare commits
28 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| be944ed461 | |||
| 16896cdb6b | |||
| 3e1bb69b7d | |||
| c9c9d981d7 | |||
| 22713e3054 | |||
| 0ebe5231e2 | |||
| b26a00f48c | |||
| 5580947e3e | |||
| 8db8abb389 | |||
| 9e4b7fa61b | |||
| a831111e26 | |||
| f2acbcde40 | |||
| 3ad83a882d | |||
| 32331e68eb | |||
| 6d731433c9 | |||
| 1253715fd8 | |||
| d4d9692af0 | |||
| 1b6492ce45 | |||
| 493e94de50 | |||
| fc20b2a250 | |||
| 1c3914ef16 | |||
| 2aa5fdc243 | |||
| ea44f6921c | |||
| b6e930d373 | |||
| c271ec3e32 | |||
| 0c4911f7f5 | |||
| 3667088d60 | |||
| 9cafa895ff |
@@ -6,6 +6,7 @@
|
|||||||
# Folders
|
# Folders
|
||||||
_obj
|
_obj
|
||||||
_test
|
_test
|
||||||
|
.idea
|
||||||
|
|
||||||
# Architecture specific extensions/prefixes
|
# Architecture specific extensions/prefixes
|
||||||
*.[568vq]
|
*.[568vq]
|
||||||
|
|||||||
+2
-3
@@ -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:
|
||||||
@@ -34,6 +33,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
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
Library webhooks
|
Library webhooks
|
||||||
================
|
================
|
||||||
<img align="right" src="https://raw.githubusercontent.com/go-playground/webhooks/v3/logo.png">
|
<img align="right" src="https://raw.githubusercontent.com/go-playground/webhooks/v3/logo.png">
|
||||||
[](https://travis-ci.org/go-playground/webhooks)
|
[](https://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)
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ const (
|
|||||||
PullRequestDeclinedEvent Event = "pullrequest:rejected"
|
PullRequestDeclinedEvent Event = "pullrequest:rejected"
|
||||||
PullRequestCommentCreatedEvent Event = "pullrequest:comment_created"
|
PullRequestCommentCreatedEvent Event = "pullrequest:comment_created"
|
||||||
PullRequestCommentUpdatedEvent Event = "pullrequest:comment_updated"
|
PullRequestCommentUpdatedEvent Event = "pullrequest:comment_updated"
|
||||||
PullRequestCommentDeletedEvent Event = "pull_request:comment_deleted"
|
PullRequestCommentDeletedEvent Event = "pullrequest:comment_deleted"
|
||||||
)
|
)
|
||||||
|
|
||||||
// New creates and returns a WebHook instance denoted by the Provider type
|
// New creates and returns a WebHook instance denoted by the Provider type
|
||||||
@@ -80,11 +80,15 @@ func (hook Webhook) ParsePayload(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
webhooks.DefaultLog.Debug(fmt.Sprintf("X-Hook-UUID:%s", uuid))
|
webhooks.DefaultLog.Debug(fmt.Sprintf("X-Hook-UUID:%s", uuid))
|
||||||
|
|
||||||
|
if len(hook.uuid) > 0 {
|
||||||
if uuid != hook.uuid {
|
if uuid != hook.uuid {
|
||||||
webhooks.DefaultLog.Error(fmt.Sprintf("X-Hook-UUID does not match configured uuid of %s", hook.uuid))
|
webhooks.DefaultLog.Error(fmt.Sprintf("X-Hook-UUID %s does not match configured uuid of %s", uuid, hook.uuid))
|
||||||
http.Error(w, "403 Forbidden - X-Hook-UUID does not match", http.StatusForbidden)
|
http.Error(w, "403 Forbidden - X-Hook-UUID does not match", http.StatusForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
webhooks.DefaultLog.Debug("hook uuid not defined - recommend setting for improved security")
|
||||||
|
}
|
||||||
|
|
||||||
event := r.Header.Get("X-Event-Key")
|
event := r.Header.Get("X-Event-Key")
|
||||||
if event == "" {
|
if event == "" {
|
||||||
|
|||||||
@@ -2772,6 +2772,32 @@ type PullRequestPayload struct {
|
|||||||
ReviewCommentURL string `json:"review_comment_url"`
|
ReviewCommentURL string `json:"review_comment_url"`
|
||||||
CommentsURL string `json:"comments_url"`
|
CommentsURL string `json:"comments_url"`
|
||||||
StatusesURL string `json:"statuses_url"`
|
StatusesURL string `json:"statuses_url"`
|
||||||
|
RequestedReviewers []struct {
|
||||||
|
Login string `json:"login"`
|
||||||
|
ID int `json:"id"`
|
||||||
|
AvatarURL string `json:"avatar_url"`
|
||||||
|
GravatarID string `json:"gravatar_id"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
HTMLURL string `json:"html_url"`
|
||||||
|
FollowersURL string `json:"followers_url"`
|
||||||
|
FollowingURL string `json:"following_url"`
|
||||||
|
GistsURL string `json:"gists_url"`
|
||||||
|
StarredURL string `json:"starred_url"`
|
||||||
|
SubscriptionsURL string `json:"subscriptions_url"`
|
||||||
|
OrganizationsURL string `json:"organizations_url"`
|
||||||
|
ReposURL string `json:"repos_url"`
|
||||||
|
EventsURL string `json:"events_url"`
|
||||||
|
ReceivedEventsURL string `json:"received_events_url"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
SiteAdmin bool `json:"site_admin"`
|
||||||
|
} `json:"requested_reviewers,omitempty"`
|
||||||
|
Labels []struct {
|
||||||
|
ID int64 `json:"id"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Color string `json:"color"`
|
||||||
|
Default bool `json:"default"`
|
||||||
|
} `json:"labels"`
|
||||||
Head struct {
|
Head struct {
|
||||||
Label string `json:"label"`
|
Label string `json:"label"`
|
||||||
Ref string `json:"ref"`
|
Ref string `json:"ref"`
|
||||||
|
|||||||
+65
-2
@@ -813,6 +813,29 @@ func TestMergeRequestEvent(t *testing.T) {
|
|||||||
"username": "root",
|
"username": "root",
|
||||||
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
|
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
|
||||||
},
|
},
|
||||||
|
"project": {
|
||||||
|
"id": 1,
|
||||||
|
"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": {
|
"object_attributes": {
|
||||||
"id": 99,
|
"id": 99,
|
||||||
"target_branch": "master",
|
"target_branch": "master",
|
||||||
@@ -823,8 +846,6 @@ func TestMergeRequestEvent(t *testing.T) {
|
|||||||
"title": "MS-Viewport",
|
"title": "MS-Viewport",
|
||||||
"created_at": "2013-12-03T17:23:34Z",
|
"created_at": "2013-12-03T17:23:34Z",
|
||||||
"updated_at": "2013-12-03T17:23:34Z",
|
"updated_at": "2013-12-03T17:23:34Z",
|
||||||
"st_commits": null,
|
|
||||||
"st_diffs": null,
|
|
||||||
"milestone_id": null,
|
"milestone_id": null,
|
||||||
"state": "opened",
|
"state": "opened",
|
||||||
"merge_status": "unchecked",
|
"merge_status": "unchecked",
|
||||||
@@ -881,6 +902,48 @@ func TestMergeRequestEvent(t *testing.T) {
|
|||||||
"username": "user1",
|
"username": "user1",
|
||||||
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
|
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"labels": [{
|
||||||
|
"id": 206,
|
||||||
|
"title": "API",
|
||||||
|
"color": "#ffffff",
|
||||||
|
"project_id": 14,
|
||||||
|
"created_at": "2013-12-03T17:15:43Z",
|
||||||
|
"updated_at": "2013-12-03T17:15:43Z",
|
||||||
|
"template": false,
|
||||||
|
"description": "API related issues",
|
||||||
|
"type": "ProjectLabel",
|
||||||
|
"group_id": 41
|
||||||
|
}],
|
||||||
|
"changes": {
|
||||||
|
"updated_by_id": [null, 1],
|
||||||
|
"updated_at": ["2017-09-15 16:50:55 UTC", "2017-09-15 16:52:00 UTC"],
|
||||||
|
"labels": {
|
||||||
|
"previous": [{
|
||||||
|
"id": 206,
|
||||||
|
"title": "API",
|
||||||
|
"color": "#ffffff",
|
||||||
|
"project_id": 14,
|
||||||
|
"created_at": "2013-12-03T17:15:43Z",
|
||||||
|
"updated_at": "2013-12-03T17:15:43Z",
|
||||||
|
"template": false,
|
||||||
|
"description": "API related issues",
|
||||||
|
"type": "ProjectLabel",
|
||||||
|
"group_id": 41
|
||||||
|
}],
|
||||||
|
"current": [{
|
||||||
|
"id": 205,
|
||||||
|
"title": "Platform",
|
||||||
|
"color": "#123123",
|
||||||
|
"project_id": 14,
|
||||||
|
"created_at": "2013-12-03T17:15:43Z",
|
||||||
|
"updated_at": "2013-12-03T17:15:43Z",
|
||||||
|
"template": false,
|
||||||
|
"description": "Platform related issues",
|
||||||
|
"type": "ProjectLabel",
|
||||||
|
"group_id": 41
|
||||||
|
}]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|||||||
@@ -54,6 +54,8 @@ type MergeRequestEventPayload struct {
|
|||||||
User User `json:"user"`
|
User User `json:"user"`
|
||||||
ObjectAttributes ObjectAttributes `json:"object_attributes"`
|
ObjectAttributes ObjectAttributes `json:"object_attributes"`
|
||||||
Changes Changes `json:"changes"`
|
Changes Changes `json:"changes"`
|
||||||
|
Project Project `json:"project"`
|
||||||
|
Repository Repository `json:"repository"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// PushEventPayload contains the information for GitLab's push event
|
// PushEventPayload contains the information for GitLab's push event
|
||||||
|
|||||||
+167
@@ -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.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user