Compare commits
42 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| adb918738a | |||
| 9cc34fc5d1 | |||
| 63866ac80d | |||
| 866b461aac | |||
| 0035246764 | |||
| 443e255708 | |||
| eb56c26534 | |||
| 2d55603413 | |||
| b9b0e19032 | |||
| e4aa8cba6c | |||
| 28d44801e9 | |||
| 7e745505c0 | |||
| 8ffb2ffc32 | |||
| 84dc839b94 | |||
| 2c541a737f | |||
| d9e847f7ea | |||
| 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 |
@@ -6,6 +6,7 @@
|
||||
# Folders
|
||||
_obj
|
||||
_test
|
||||
.idea
|
||||
|
||||
# Architecture specific extensions/prefixes
|
||||
*.[568vq]
|
||||
|
||||
+4
-4
@@ -1,7 +1,6 @@
|
||||
language: go
|
||||
go:
|
||||
- 1.7.6
|
||||
- 1.8.3
|
||||
- 1.10.2
|
||||
- tip
|
||||
matrix:
|
||||
allow_failures:
|
||||
@@ -22,6 +21,7 @@ before_install:
|
||||
- mkdir -p $GOPATH/src/gopkg.in
|
||||
- ln -s $GOPATH/src/github.com/$TRAVIS_REPO_SLUG $GOPATH/src/gopkg.in/webhooks.v2
|
||||
- ln -s $GOPATH/src/github.com/$TRAVIS_REPO_SLUG $GOPATH/src/gopkg.in/webhooks.v3
|
||||
- ln -s $GOPATH/src/github.com/$TRAVIS_REPO_SLUG $GOPATH/src/gopkg.in/webhooks.v4
|
||||
|
||||
before_script:
|
||||
- go vet ./...
|
||||
@@ -34,6 +34,6 @@ script:
|
||||
- go test -race
|
||||
|
||||
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 &&
|
||||
goveralls -coverprofile=overalls.coverprofile -service travis-ci -repotoken $COVERALLS_TOKEN
|
||||
goveralls -coverprofile=overalls.coverprofile -service travis-ci -repotoken $COVERALLS_TOKEN
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
Library webhooks
|
||||
================
|
||||
<img align="right" src="https://raw.githubusercontent.com/go-playground/webhooks/v3/logo.png">
|
||||
[](https://travis-ci.org/go-playground/webhooks)
|
||||
[](https://coveralls.io/github/go-playground/webhooks?branch=v3)
|
||||
<img align="right" src="https://raw.githubusercontent.com/go-playground/webhooks/v3/logo.png">
|
||||
[](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.v3)
|
||||
[](https://godoc.org/gopkg.in/go-playground/webhooks.v4)
|
||||

|
||||
|
||||
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.v3
|
||||
go get -u gopkg.in/go-playground/webhooks.v4
|
||||
```
|
||||
|
||||
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
|
||||
------
|
||||
|
||||
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:
|
||||
|
||||
@@ -46,8 +46,8 @@ import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"gopkg.in/go-playground/webhooks.v3"
|
||||
"gopkg.in/go-playground/webhooks.v3/github"
|
||||
"gopkg.in/go-playground/webhooks.v4"
|
||||
"gopkg.in/go-playground/webhooks.v4/github"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -103,8 +103,8 @@ import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"gopkg.in/go-playground/webhooks.v3"
|
||||
"gopkg.in/go-playground/webhooks.v3/github"
|
||||
"gopkg.in/go-playground/webhooks.v4"
|
||||
"gopkg.in/go-playground/webhooks.v4/github"
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
+10
-6
@@ -6,7 +6,7 @@ import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"gopkg.in/go-playground/webhooks.v3"
|
||||
"gopkg.in/go-playground/webhooks.v4"
|
||||
)
|
||||
|
||||
// Webhook instance contains all methods needed to process events
|
||||
@@ -43,7 +43,7 @@ const (
|
||||
PullRequestDeclinedEvent Event = "pullrequest:rejected"
|
||||
PullRequestCommentCreatedEvent Event = "pullrequest:comment_created"
|
||||
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
|
||||
@@ -80,10 +80,14 @@ func (hook Webhook) ParsePayload(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
webhooks.DefaultLog.Debug(fmt.Sprintf("X-Hook-UUID:%s", uuid))
|
||||
|
||||
if uuid != hook.uuid {
|
||||
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
|
||||
if len(hook.uuid) > 0 {
|
||||
if uuid != 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)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
webhooks.DefaultLog.Debug("hook uuid not defined - recommend setting for improved security")
|
||||
}
|
||||
|
||||
event := r.Header.Get("X-Event-Key")
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"time"
|
||||
|
||||
. "gopkg.in/go-playground/assert.v1"
|
||||
"gopkg.in/go-playground/webhooks.v3"
|
||||
"gopkg.in/go-playground/webhooks.v4"
|
||||
)
|
||||
|
||||
// NOTES:
|
||||
|
||||
@@ -5,8 +5,8 @@ import (
|
||||
"log"
|
||||
"strconv"
|
||||
|
||||
"gopkg.in/go-playground/webhooks.v3"
|
||||
"gopkg.in/go-playground/webhooks.v3/github"
|
||||
"gopkg.in/go-playground/webhooks.v4"
|
||||
"gopkg.in/go-playground/webhooks.v4/github"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -18,15 +18,15 @@ type myLogger struct {
|
||||
PrintDebugs bool
|
||||
}
|
||||
|
||||
func (l *myLogger) Info(msg string) {
|
||||
func (l *myLogger) Info(msg ...interface{}) {
|
||||
log.Println(msg)
|
||||
}
|
||||
|
||||
func (l *myLogger) Error(msg string) {
|
||||
func (l *myLogger) Error(msg ...interface{}) {
|
||||
log.Println(msg)
|
||||
}
|
||||
|
||||
func (l *myLogger) Debug(msg string) {
|
||||
func (l *myLogger) Debug(msg ...interface{}) {
|
||||
if !l.PrintDebugs {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -4,8 +4,8 @@ import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"gopkg.in/go-playground/webhooks.v3"
|
||||
"gopkg.in/go-playground/webhooks.v3/github"
|
||||
"gopkg.in/go-playground/webhooks.v4"
|
||||
"gopkg.in/go-playground/webhooks.v4/github"
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
@@ -4,8 +4,8 @@ import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"gopkg.in/go-playground/webhooks.v3"
|
||||
"gopkg.in/go-playground/webhooks.v3/github"
|
||||
"gopkg.in/go-playground/webhooks.v4"
|
||||
"gopkg.in/go-playground/webhooks.v4/github"
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@ import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"gopkg.in/go-playground/webhooks.v3"
|
||||
"gopkg.in/go-playground/webhooks.v4"
|
||||
)
|
||||
|
||||
// Webhook instance contains all methods needed to process events
|
||||
|
||||
+127
-104
@@ -9,7 +9,7 @@ import (
|
||||
"time"
|
||||
|
||||
. "gopkg.in/go-playground/assert.v1"
|
||||
"gopkg.in/go-playground/webhooks.v3"
|
||||
"gopkg.in/go-playground/webhooks.v4"
|
||||
)
|
||||
|
||||
// NOTES:
|
||||
@@ -181,7 +181,6 @@ func TestBadSignatureMatch(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCommitCommentEvent(t *testing.T) {
|
||||
|
||||
payload := `{
|
||||
"action": "created",
|
||||
"comment": {
|
||||
@@ -4763,137 +4762,160 @@ func TestPullRequestReviewCommentEvent(t *testing.T) {
|
||||
func TestPushEvent(t *testing.T) {
|
||||
|
||||
payload := `{
|
||||
"ref": "refs/heads/changes",
|
||||
"before": "9049f1265b7d61be4a8904a9a27120d2064dab3b",
|
||||
"after": "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c",
|
||||
"ref": "refs/heads/master",
|
||||
"before": "737d38c599c1b2991664dfc6155d6bf516fcce36",
|
||||
"after": "fd489864e7642b48eaad6e3f155c10e46810ec72",
|
||||
"created": false,
|
||||
"deleted": false,
|
||||
"forced": false,
|
||||
"base_ref": null,
|
||||
"compare": "https://github.com/baxterthehacker/public-repo/compare/9049f1265b7d...0d1a26e67d8f",
|
||||
"compare": "https://github.com/binkkatal/sample_app/compare/737d38c599c1...fd489864e764",
|
||||
"commits": [
|
||||
{
|
||||
"id": "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c",
|
||||
"tree_id": "f9d2a07e9488b91af2641b26b9407fe22a451433",
|
||||
"id": "fd489864e7642b48eaad6e3f155c10e46810ec72",
|
||||
"tree_id": "55e08136e14d5168b699038f88c73e175ddffd3b",
|
||||
"distinct": true,
|
||||
"message": "Update README.md",
|
||||
"timestamp": "2015-05-05T19:40:15-04:00",
|
||||
"url": "https://github.com/baxterthehacker/public-repo/commit/0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c",
|
||||
"message": "test a push event",
|
||||
"timestamp": "2018-06-29T19:34:13+05:30",
|
||||
"url": "https://github.com/binkkatal/sample_app/commit/fd489864e7642b48eaad6e3f155c10e46810ec72",
|
||||
"author": {
|
||||
"name": "baxterthehacker",
|
||||
"email": "baxterthehacker@users.noreply.github.com",
|
||||
"username": "baxterthehacker"
|
||||
"name": "binkkatal",
|
||||
"email": "binkkatal.r@gmail.com",
|
||||
"username": "binkkatal"
|
||||
},
|
||||
"committer": {
|
||||
"name": "baxterthehacker",
|
||||
"email": "baxterthehacker@users.noreply.github.com",
|
||||
"username": "baxterthehacker"
|
||||
"name": "binkkatal",
|
||||
"email": "binkkatal.r@gmail.com",
|
||||
"username": "binkkatal"
|
||||
},
|
||||
"added": [
|
||||
|
||||
".razorops.yaml"
|
||||
],
|
||||
"removed": [
|
||||
|
||||
],
|
||||
"modified": [
|
||||
"README.md"
|
||||
"app/controllers/application_controller.rb"
|
||||
]
|
||||
}
|
||||
],
|
||||
"head_commit": {
|
||||
"id": "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c",
|
||||
"tree_id": "f9d2a07e9488b91af2641b26b9407fe22a451433",
|
||||
"id": "fd489864e7642b48eaad6e3f155c10e46810ec72",
|
||||
"tree_id": "55e08136e14d5168b699038f88c73e175ddffd3b",
|
||||
"distinct": true,
|
||||
"message": "Update README.md",
|
||||
"timestamp": "2015-05-05T19:40:15-04:00",
|
||||
"url": "https://github.com/baxterthehacker/public-repo/commit/0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c",
|
||||
"message": "test a push event",
|
||||
"timestamp": "2018-06-29T19:34:13+05:30",
|
||||
"url": "https://github.com/binkkatal/sample_app/commit/fd489864e7642b48eaad6e3f155c10e46810ec72",
|
||||
"author": {
|
||||
"name": "baxterthehacker",
|
||||
"email": "baxterthehacker@users.noreply.github.com",
|
||||
"username": "baxterthehacker"
|
||||
"name": "binkkatal",
|
||||
"email": "binkkatal.r@gmail.com",
|
||||
"username": "binkkatal"
|
||||
},
|
||||
"committer": {
|
||||
"name": "baxterthehacker",
|
||||
"email": "baxterthehacker@users.noreply.github.com",
|
||||
"username": "baxterthehacker"
|
||||
"name": "binkkatal",
|
||||
"email": "binkkatal.r@gmail.com",
|
||||
"username": "binkkatal"
|
||||
},
|
||||
"added": [
|
||||
|
||||
".razorops.yaml"
|
||||
],
|
||||
"removed": [
|
||||
|
||||
],
|
||||
"modified": [
|
||||
"README.md"
|
||||
"app/controllers/application_controller.rb"
|
||||
]
|
||||
},
|
||||
"repository": {
|
||||
"id": 35129377,
|
||||
"name": "public-repo",
|
||||
"full_name": "baxterthehacker/public-repo",
|
||||
"id": 63933911,
|
||||
"node_id": "MDEwOlJlcG9zaXRvcnk2MzkzMzkxMQ==",
|
||||
"name": "sample_app",
|
||||
"full_name": "binkkatal/sample_app",
|
||||
"owner": {
|
||||
"name": "baxterthehacker",
|
||||
"email": "baxterthehacker@users.noreply.github.com"
|
||||
"name": "binkkatal",
|
||||
"email": "binkkatal.r@gmail.com",
|
||||
"login": "binkkatal",
|
||||
"id": 13351472,
|
||||
"node_id": "MDQ6VXNlcjEzMzUxNDcy",
|
||||
"avatar_url": "https://avatars3.githubusercontent.com/u/13351472?v=4",
|
||||
"gravatar_id": "",
|
||||
"url": "https://api.github.com/users/binkkatal",
|
||||
"html_url": "https://github.com/binkkatal",
|
||||
"followers_url": "https://api.github.com/users/binkkatal/followers",
|
||||
"following_url": "https://api.github.com/users/binkkatal/following{/other_user}",
|
||||
"gists_url": "https://api.github.com/users/binkkatal/gists{/gist_id}",
|
||||
"starred_url": "https://api.github.com/users/binkkatal/starred{/owner}{/repo}",
|
||||
"subscriptions_url": "https://api.github.com/users/binkkatal/subscriptions",
|
||||
"organizations_url": "https://api.github.com/users/binkkatal/orgs",
|
||||
"repos_url": "https://api.github.com/users/binkkatal/repos",
|
||||
"events_url": "https://api.github.com/users/binkkatal/events{/privacy}",
|
||||
"received_events_url": "https://api.github.com/users/binkkatal/received_events",
|
||||
"type": "User",
|
||||
"site_admin": false
|
||||
},
|
||||
"private": false,
|
||||
"html_url": "https://github.com/baxterthehacker/public-repo",
|
||||
"description": "",
|
||||
"html_url": "https://github.com/binkkatal/sample_app",
|
||||
"description": null,
|
||||
"fork": false,
|
||||
"url": "https://github.com/baxterthehacker/public-repo",
|
||||
"forks_url": "https://api.github.com/repos/baxterthehacker/public-repo/forks",
|
||||
"keys_url": "https://api.github.com/repos/baxterthehacker/public-repo/keys{/key_id}",
|
||||
"collaborators_url": "https://api.github.com/repos/baxterthehacker/public-repo/collaborators{/collaborator}",
|
||||
"teams_url": "https://api.github.com/repos/baxterthehacker/public-repo/teams",
|
||||
"hooks_url": "https://api.github.com/repos/baxterthehacker/public-repo/hooks",
|
||||
"issue_events_url": "https://api.github.com/repos/baxterthehacker/public-repo/issues/events{/number}",
|
||||
"events_url": "https://api.github.com/repos/baxterthehacker/public-repo/events",
|
||||
"assignees_url": "https://api.github.com/repos/baxterthehacker/public-repo/assignees{/user}",
|
||||
"branches_url": "https://api.github.com/repos/baxterthehacker/public-repo/branches{/branch}",
|
||||
"tags_url": "https://api.github.com/repos/baxterthehacker/public-repo/tags",
|
||||
"blobs_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/blobs{/sha}",
|
||||
"git_tags_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/tags{/sha}",
|
||||
"git_refs_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/refs{/sha}",
|
||||
"trees_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/trees{/sha}",
|
||||
"statuses_url": "https://api.github.com/repos/baxterthehacker/public-repo/statuses/{sha}",
|
||||
"languages_url": "https://api.github.com/repos/baxterthehacker/public-repo/languages",
|
||||
"stargazers_url": "https://api.github.com/repos/baxterthehacker/public-repo/stargazers",
|
||||
"contributors_url": "https://api.github.com/repos/baxterthehacker/public-repo/contributors",
|
||||
"subscribers_url": "https://api.github.com/repos/baxterthehacker/public-repo/subscribers",
|
||||
"subscription_url": "https://api.github.com/repos/baxterthehacker/public-repo/subscription",
|
||||
"commits_url": "https://api.github.com/repos/baxterthehacker/public-repo/commits{/sha}",
|
||||
"git_commits_url": "https://api.github.com/repos/baxterthehacker/public-repo/git/commits{/sha}",
|
||||
"comments_url": "https://api.github.com/repos/baxterthehacker/public-repo/comments{/number}",
|
||||
"issue_comment_url": "https://api.github.com/repos/baxterthehacker/public-repo/issues/comments{/number}",
|
||||
"contents_url": "https://api.github.com/repos/baxterthehacker/public-repo/contents/{+path}",
|
||||
"compare_url": "https://api.github.com/repos/baxterthehacker/public-repo/compare/{base}...{head}",
|
||||
"merges_url": "https://api.github.com/repos/baxterthehacker/public-repo/merges",
|
||||
"archive_url": "https://api.github.com/repos/baxterthehacker/public-repo/{archive_format}{/ref}",
|
||||
"downloads_url": "https://api.github.com/repos/baxterthehacker/public-repo/downloads",
|
||||
"issues_url": "https://api.github.com/repos/baxterthehacker/public-repo/issues{/number}",
|
||||
"pulls_url": "https://api.github.com/repos/baxterthehacker/public-repo/pulls{/number}",
|
||||
"milestones_url": "https://api.github.com/repos/baxterthehacker/public-repo/milestones{/number}",
|
||||
"notifications_url": "https://api.github.com/repos/baxterthehacker/public-repo/notifications{?since,all,participating}",
|
||||
"labels_url": "https://api.github.com/repos/baxterthehacker/public-repo/labels{/name}",
|
||||
"releases_url": "https://api.github.com/repos/baxterthehacker/public-repo/releases{/id}",
|
||||
"created_at": 1430869212,
|
||||
"updated_at": "2015-05-05T23:40:12Z",
|
||||
"pushed_at": 1430869217,
|
||||
"git_url": "git://github.com/baxterthehacker/public-repo.git",
|
||||
"ssh_url": "git@github.com:baxterthehacker/public-repo.git",
|
||||
"clone_url": "https://github.com/baxterthehacker/public-repo.git",
|
||||
"svn_url": "https://github.com/baxterthehacker/public-repo",
|
||||
"url": "https://github.com/binkkatal/sample_app",
|
||||
"forks_url": "https://api.github.com/repos/binkkatal/sample_app/forks",
|
||||
"keys_url": "https://api.github.com/repos/binkkatal/sample_app/keys{/key_id}",
|
||||
"collaborators_url": "https://api.github.com/repos/binkkatal/sample_app/collaborators{/collaborator}",
|
||||
"teams_url": "https://api.github.com/repos/binkkatal/sample_app/teams",
|
||||
"hooks_url": "https://api.github.com/repos/binkkatal/sample_app/hooks",
|
||||
"issue_events_url": "https://api.github.com/repos/binkkatal/sample_app/issues/events{/number}",
|
||||
"events_url": "https://api.github.com/repos/binkkatal/sample_app/events",
|
||||
"assignees_url": "https://api.github.com/repos/binkkatal/sample_app/assignees{/user}",
|
||||
"branches_url": "https://api.github.com/repos/binkkatal/sample_app/branches{/branch}",
|
||||
"tags_url": "https://api.github.com/repos/binkkatal/sample_app/tags",
|
||||
"blobs_url": "https://api.github.com/repos/binkkatal/sample_app/git/blobs{/sha}",
|
||||
"git_tags_url": "https://api.github.com/repos/binkkatal/sample_app/git/tags{/sha}",
|
||||
"git_refs_url": "https://api.github.com/repos/binkkatal/sample_app/git/refs{/sha}",
|
||||
"trees_url": "https://api.github.com/repos/binkkatal/sample_app/git/trees{/sha}",
|
||||
"statuses_url": "https://api.github.com/repos/binkkatal/sample_app/statuses/{sha}",
|
||||
"languages_url": "https://api.github.com/repos/binkkatal/sample_app/languages",
|
||||
"stargazers_url": "https://api.github.com/repos/binkkatal/sample_app/stargazers",
|
||||
"contributors_url": "https://api.github.com/repos/binkkatal/sample_app/contributors",
|
||||
"subscribers_url": "https://api.github.com/repos/binkkatal/sample_app/subscribers",
|
||||
"subscription_url": "https://api.github.com/repos/binkkatal/sample_app/subscription",
|
||||
"commits_url": "https://api.github.com/repos/binkkatal/sample_app/commits{/sha}",
|
||||
"git_commits_url": "https://api.github.com/repos/binkkatal/sample_app/git/commits{/sha}",
|
||||
"comments_url": "https://api.github.com/repos/binkkatal/sample_app/comments{/number}",
|
||||
"issue_comment_url": "https://api.github.com/repos/binkkatal/sample_app/issues/comments{/number}",
|
||||
"contents_url": "https://api.github.com/repos/binkkatal/sample_app/contents/{+path}",
|
||||
"compare_url": "https://api.github.com/repos/binkkatal/sample_app/compare/{base}...{head}",
|
||||
"merges_url": "https://api.github.com/repos/binkkatal/sample_app/merges",
|
||||
"archive_url": "https://api.github.com/repos/binkkatal/sample_app/{archive_format}{/ref}",
|
||||
"downloads_url": "https://api.github.com/repos/binkkatal/sample_app/downloads",
|
||||
"issues_url": "https://api.github.com/repos/binkkatal/sample_app/issues{/number}",
|
||||
"pulls_url": "https://api.github.com/repos/binkkatal/sample_app/pulls{/number}",
|
||||
"milestones_url": "https://api.github.com/repos/binkkatal/sample_app/milestones{/number}",
|
||||
"notifications_url": "https://api.github.com/repos/binkkatal/sample_app/notifications{?since,all,participating}",
|
||||
"labels_url": "https://api.github.com/repos/binkkatal/sample_app/labels{/name}",
|
||||
"releases_url": "https://api.github.com/repos/binkkatal/sample_app/releases{/id}",
|
||||
"deployments_url": "https://api.github.com/repos/binkkatal/sample_app/deployments",
|
||||
"created_at": 1469173225,
|
||||
"updated_at": "2016-07-22T07:48:39Z",
|
||||
"pushed_at": 1530281075,
|
||||
"git_url": "git://github.com/binkkatal/sample_app.git",
|
||||
"ssh_url": "git@github.com:binkkatal/sample_app.git",
|
||||
"clone_url": "https://github.com/binkkatal/sample_app.git",
|
||||
"svn_url": "https://github.com/binkkatal/sample_app",
|
||||
"homepage": null,
|
||||
"size": 0,
|
||||
"size": 23,
|
||||
"stargazers_count": 0,
|
||||
"watchers_count": 0,
|
||||
"language": null,
|
||||
"language": "Ruby",
|
||||
"has_issues": true,
|
||||
"has_projects": true,
|
||||
"has_downloads": true,
|
||||
"has_wiki": true,
|
||||
"has_pages": true,
|
||||
"has_pages": false,
|
||||
"forks_count": 0,
|
||||
"mirror_url": null,
|
||||
"archived": false,
|
||||
"open_issues_count": 0,
|
||||
"license": null,
|
||||
"forks": 0,
|
||||
"open_issues": 0,
|
||||
"watchers": 0,
|
||||
@@ -4902,35 +4924,35 @@ func TestPushEvent(t *testing.T) {
|
||||
"master_branch": "master"
|
||||
},
|
||||
"pusher": {
|
||||
"name": "baxterthehacker",
|
||||
"email": "baxterthehacker@users.noreply.github.com"
|
||||
"name": "binkkatal",
|
||||
"email": "binkkatal.r@gmail.com"
|
||||
},
|
||||
"sender": {
|
||||
"login": "baxterthehacker",
|
||||
"id": 6752317,
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/6752317?v=3",
|
||||
"login": "binkkatal",
|
||||
"id": 13351472,
|
||||
"node_id": "MDQ6VXNlcjEzMzUxNDcy",
|
||||
"avatar_url": "https://avatars3.githubusercontent.com/u/13351472?v=4",
|
||||
"gravatar_id": "",
|
||||
"url": "https://api.github.com/users/baxterthehacker",
|
||||
"html_url": "https://github.com/baxterthehacker",
|
||||
"followers_url": "https://api.github.com/users/baxterthehacker/followers",
|
||||
"following_url": "https://api.github.com/users/baxterthehacker/following{/other_user}",
|
||||
"gists_url": "https://api.github.com/users/baxterthehacker/gists{/gist_id}",
|
||||
"starred_url": "https://api.github.com/users/baxterthehacker/starred{/owner}{/repo}",
|
||||
"subscriptions_url": "https://api.github.com/users/baxterthehacker/subscriptions",
|
||||
"organizations_url": "https://api.github.com/users/baxterthehacker/orgs",
|
||||
"repos_url": "https://api.github.com/users/baxterthehacker/repos",
|
||||
"events_url": "https://api.github.com/users/baxterthehacker/events{/privacy}",
|
||||
"received_events_url": "https://api.github.com/users/baxterthehacker/received_events",
|
||||
"url": "https://api.github.com/users/binkkatal",
|
||||
"html_url": "https://github.com/binkkatal",
|
||||
"followers_url": "https://api.github.com/users/binkkatal/followers",
|
||||
"following_url": "https://api.github.com/users/binkkatal/following{/other_user}",
|
||||
"gists_url": "https://api.github.com/users/binkkatal/gists{/gist_id}",
|
||||
"starred_url": "https://api.github.com/users/binkkatal/starred{/owner}{/repo}",
|
||||
"subscriptions_url": "https://api.github.com/users/binkkatal/subscriptions",
|
||||
"organizations_url": "https://api.github.com/users/binkkatal/orgs",
|
||||
"repos_url": "https://api.github.com/users/binkkatal/repos",
|
||||
"events_url": "https://api.github.com/users/binkkatal/events{/privacy}",
|
||||
"received_events_url": "https://api.github.com/users/binkkatal/received_events",
|
||||
"type": "User",
|
||||
"site_admin": false
|
||||
}
|
||||
}
|
||||
`
|
||||
}`
|
||||
|
||||
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", "push")
|
||||
req.Header.Set("X-Hub-Signature", "sha1=c7097badc10db8a2ec615e340534956e75a483c6")
|
||||
req.Header.Set("X-Hub-Signature", "sha1=0534736f52c2fc5896ef1bd5a043127b20d233ba")
|
||||
|
||||
Equal(t, err, nil)
|
||||
|
||||
@@ -4941,6 +4963,7 @@ func TestPushEvent(t *testing.T) {
|
||||
defer resp.Body.Close()
|
||||
|
||||
Equal(t, resp.StatusCode, http.StatusOK)
|
||||
|
||||
}
|
||||
|
||||
func TestReleaseEvent(t *testing.T) {
|
||||
|
||||
+72
-30
@@ -376,13 +376,12 @@ type DeletePayload struct {
|
||||
// DeploymentPayload contains the information for GitHub's deployment hook
|
||||
type DeploymentPayload struct {
|
||||
Deployment struct {
|
||||
URL string `json:"url"`
|
||||
ID int64 `json:"id"`
|
||||
Sha string `json:"sha"`
|
||||
Ref string `json:"ref"`
|
||||
Task string `json:"task"`
|
||||
Payload struct {
|
||||
} `json:"payload"`
|
||||
URL string `json:"url"`
|
||||
ID int64 `json:"id"`
|
||||
Sha string `json:"sha"`
|
||||
Ref string `json:"ref"`
|
||||
Task string `json:"task"`
|
||||
Payload string `json:"payload"`
|
||||
Environment string `json:"environment"`
|
||||
Description *string `json:"description"`
|
||||
Creator struct {
|
||||
@@ -550,13 +549,12 @@ type DeploymentStatusPayload struct {
|
||||
RepositoryURL string `json:"repository_url"`
|
||||
} `json:"deployment_status"`
|
||||
Deployment struct {
|
||||
URL string `json:"url"`
|
||||
ID int64 `json:"id"`
|
||||
Sha string `json:"sha"`
|
||||
Ref string `json:"ref"`
|
||||
Task string `json:"task"`
|
||||
Payload struct {
|
||||
} `json:"payload"`
|
||||
URL string `json:"url"`
|
||||
ID int64 `json:"id"`
|
||||
Sha string `json:"sha"`
|
||||
Ref string `json:"ref"`
|
||||
Task string `json:"task"`
|
||||
Payload string `json:"payload"`
|
||||
Environment string `json:"environment"`
|
||||
Description *string `json:"description"`
|
||||
Creator struct {
|
||||
@@ -2759,20 +2757,46 @@ type PullRequestPayload struct {
|
||||
Type string `json:"type"`
|
||||
SiteAdmin bool `json:"site_admin"`
|
||||
} `json:"user"`
|
||||
Body string `json:"body"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
ClosedAt *time.Time `json:"closed_at"`
|
||||
MergedAt *time.Time `json:"merged_at"`
|
||||
MergeCommitSha *string `json:"merge_commit_sha"`
|
||||
Assignee *Assignee `json:"assignee"`
|
||||
Milestone *Milestone `json:"milestone"`
|
||||
CommitsURL string `json:"commits_url"`
|
||||
ReviewCommentsURL string `json:"review_comments_url"`
|
||||
ReviewCommentURL string `json:"review_comment_url"`
|
||||
CommentsURL string `json:"comments_url"`
|
||||
StatusesURL string `json:"statuses_url"`
|
||||
Head struct {
|
||||
Body string `json:"body"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
ClosedAt *time.Time `json:"closed_at"`
|
||||
MergedAt *time.Time `json:"merged_at"`
|
||||
MergeCommitSha *string `json:"merge_commit_sha"`
|
||||
Assignee *Assignee `json:"assignee"`
|
||||
Milestone *Milestone `json:"milestone"`
|
||||
CommitsURL string `json:"commits_url"`
|
||||
ReviewCommentsURL string `json:"review_comments_url"`
|
||||
ReviewCommentURL string `json:"review_comment_url"`
|
||||
CommentsURL string `json:"comments_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 {
|
||||
Label string `json:"label"`
|
||||
Ref string `json:"ref"`
|
||||
Sha string `json:"sha"`
|
||||
@@ -4098,8 +4122,23 @@ type PushPayload struct {
|
||||
Name string `json:"name"`
|
||||
FullName string `json:"full_name"`
|
||||
Owner struct {
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
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:"owner"`
|
||||
Private bool `json:"private"`
|
||||
HTMLURL string `json:"html_url"`
|
||||
@@ -4190,6 +4229,9 @@ type PushPayload struct {
|
||||
Type string `json:"type"`
|
||||
SiteAdmin bool `json:"site_admin"`
|
||||
} `json:"sender"`
|
||||
Installation struct {
|
||||
Id int `json:"id"`
|
||||
} `json:"installation"`
|
||||
}
|
||||
|
||||
// ReleasePayload contains the information for GitHub's release hook event
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@ import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"gopkg.in/go-playground/webhooks.v3"
|
||||
"gopkg.in/go-playground/webhooks.v4"
|
||||
)
|
||||
|
||||
// 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.v3"
|
||||
"gopkg.in/go-playground/webhooks.v4"
|
||||
)
|
||||
|
||||
// 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)))
|
||||
|
||||
+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.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)
|
||||
}
|
||||
@@ -9,11 +9,11 @@ var DefaultLog Logger = new(logger)
|
||||
// Logger allows for customizable logging
|
||||
type Logger interface {
|
||||
// Info prints basic information.
|
||||
Info(string)
|
||||
Info(...interface{})
|
||||
// Error prints error information.
|
||||
Error(string)
|
||||
Error(...interface{})
|
||||
// Debug prints information usefull for debugging.
|
||||
Debug(string)
|
||||
Debug(...interface{})
|
||||
}
|
||||
|
||||
// NewLogger returns a new logger for use.
|
||||
@@ -26,17 +26,17 @@ type logger struct {
|
||||
}
|
||||
|
||||
// Info prints basic information.
|
||||
func (l *logger) Info(msg string) {
|
||||
func (l *logger) Info(msg ...interface{}) {
|
||||
log.Println("INFO:", msg)
|
||||
}
|
||||
|
||||
// v prints error information.
|
||||
func (l *logger) Error(msg string) {
|
||||
func (l *logger) Error(msg ...interface{}) {
|
||||
log.Println("ERROR:", msg)
|
||||
}
|
||||
|
||||
// Debug prints information usefull for debugging.
|
||||
func (l *logger) Debug(msg string) {
|
||||
func (l *logger) Debug(msg ...interface{}) {
|
||||
if !l.PrintDebugs {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -19,6 +19,8 @@ func (p Provider) String() string {
|
||||
return "Bitbucket"
|
||||
case GitLab:
|
||||
return "GitLab"
|
||||
case Gogs:
|
||||
return "Gogs"
|
||||
default:
|
||||
return "Unknown"
|
||||
}
|
||||
@@ -29,6 +31,7 @@ const (
|
||||
GitHub Provider = iota
|
||||
Bitbucket
|
||||
GitLab
|
||||
Gogs
|
||||
)
|
||||
|
||||
// Webhook interface defines a webhook to receive events
|
||||
|
||||
Reference in New Issue
Block a user