Compare commits
35 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 | |||
| 9494e434fc | |||
| 97dd8f3564 | |||
| 8f6fada23d | |||
| 94f9d6694d | |||
| ad5392160c | |||
| 19cab958b6 | |||
| 78ce03b046 |
@@ -6,6 +6,7 @@
|
||||
# Folders
|
||||
_obj
|
||||
_test
|
||||
.idea
|
||||
|
||||
# Architecture specific extensions/prefixes
|
||||
*.[568vq]
|
||||
|
||||
+2
-3
@@ -1,7 +1,6 @@
|
||||
language: go
|
||||
go:
|
||||
- 1.7.6
|
||||
- 1.8.3
|
||||
- 1.10.2
|
||||
- tip
|
||||
matrix:
|
||||
allow_failures:
|
||||
@@ -34,6 +33,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
|
||||
@@ -1,6 +1,6 @@
|
||||
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://coveralls.io/github/go-playground/webhooks?branch=v3)
|
||||
[](https://goreportcard.com/report/go-playground/webhooks)
|
||||
@@ -24,7 +24,7 @@ Installation
|
||||
Use go get.
|
||||
|
||||
```shell
|
||||
go get -u gopkg.in/go-playground/webhooks.v3
|
||||
go get -u gopkg.in/go-playground/webhooks.v3
|
||||
```
|
||||
|
||||
Then import the package into your own code.
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -36,6 +36,8 @@ const (
|
||||
DeploymentStatusEvent Event = "deployment_status"
|
||||
ForkEvent Event = "fork"
|
||||
GollumEvent Event = "gollum"
|
||||
InstallationEvent Event = "installation"
|
||||
IntegrationInstallationEvent Event = "integration_installation"
|
||||
IssueCommentEvent Event = "issue_comment"
|
||||
IssuesEvent Event = "issues"
|
||||
LabelEvent Event = "label"
|
||||
@@ -45,6 +47,7 @@ const (
|
||||
OrganizationEvent Event = "organization"
|
||||
OrgBlockEvent Event = "org_block"
|
||||
PageBuildEvent Event = "page_build"
|
||||
PingEvent Event = "ping"
|
||||
ProjectCardEvent Event = "project_card"
|
||||
ProjectColumnEvent Event = "project_column"
|
||||
ProjectEvent Event = "project"
|
||||
@@ -179,6 +182,10 @@ func (hook Webhook) ParsePayload(w http.ResponseWriter, r *http.Request) {
|
||||
var g GollumPayload
|
||||
json.Unmarshal([]byte(payload), &g)
|
||||
hook.runProcessPayloadFunc(fn, g, hd)
|
||||
case InstallationEvent, IntegrationInstallationEvent:
|
||||
var i InstallationPayload
|
||||
json.Unmarshal([]byte(payload), &i)
|
||||
hook.runProcessPayloadFunc(fn, i, hd)
|
||||
case IssueCommentEvent:
|
||||
var i IssueCommentPayload
|
||||
json.Unmarshal([]byte(payload), &i)
|
||||
@@ -215,6 +222,10 @@ func (hook Webhook) ParsePayload(w http.ResponseWriter, r *http.Request) {
|
||||
var p PageBuildPayload
|
||||
json.Unmarshal([]byte(payload), &p)
|
||||
hook.runProcessPayloadFunc(fn, p, hd)
|
||||
case PingEvent:
|
||||
var p PingPayload
|
||||
json.Unmarshal([]byte(payload), &p)
|
||||
hook.runProcessPayloadFunc(fn, p, hd)
|
||||
case ProjectCardEvent:
|
||||
var p ProjectCardPayload
|
||||
json.Unmarshal([]byte(payload), &p)
|
||||
|
||||
@@ -48,6 +48,8 @@ func TestMain(m *testing.M) {
|
||||
DeploymentStatusEvent,
|
||||
ForkEvent,
|
||||
GollumEvent,
|
||||
InstallationEvent,
|
||||
IntegrationInstallationEvent,
|
||||
IssueCommentEvent,
|
||||
IssuesEvent,
|
||||
LabelEvent,
|
||||
@@ -57,6 +59,7 @@ func TestMain(m *testing.M) {
|
||||
OrganizationEvent,
|
||||
OrgBlockEvent,
|
||||
PageBuildEvent,
|
||||
PingEvent,
|
||||
ProjectCardEvent,
|
||||
ProjectColumnEvent,
|
||||
ProjectEvent,
|
||||
@@ -1309,6 +1312,186 @@ func TestGollumEvent(t *testing.T) {
|
||||
Equal(t, resp.StatusCode, http.StatusOK)
|
||||
}
|
||||
|
||||
func TestInstallationEvent(t *testing.T) {
|
||||
|
||||
payload := `{
|
||||
"action": "created",
|
||||
"installation": {
|
||||
"id": 80429,
|
||||
"account": {
|
||||
"login": "PombeirP",
|
||||
"id": 138074,
|
||||
"avatar_url": "https://avatars1.githubusercontent.com/u/138074?v=4",
|
||||
"gravatar_id": "",
|
||||
"url": "https://api.github.com/users/PombeirP",
|
||||
"html_url": "https://github.com/PombeirP",
|
||||
"followers_url": "https://api.github.com/users/PombeirP/followers",
|
||||
"following_url": "https://api.github.com/users/PombeirP/following{/other_user}",
|
||||
"gists_url": "https://api.github.com/users/PombeirP/gists{/gist_id}",
|
||||
"starred_url": "https://api.github.com/users/PombeirP/starred{/owner}{/repo}",
|
||||
"subscriptions_url": "https://api.github.com/users/PombeirP/subscriptions",
|
||||
"organizations_url": "https://api.github.com/users/PombeirP/orgs",
|
||||
"repos_url": "https://api.github.com/users/PombeirP/repos",
|
||||
"events_url": "https://api.github.com/users/PombeirP/events{/privacy}",
|
||||
"received_events_url": "https://api.github.com/users/PombeirP/received_events",
|
||||
"type": "User",
|
||||
"site_admin": false
|
||||
},
|
||||
"repository_selection": "selected",
|
||||
"access_tokens_url": "https://api.github.com/installations/80429/access_tokens",
|
||||
"repositories_url": "https://api.github.com/installation/repositories",
|
||||
"html_url": "https://github.com/settings/installations/80429",
|
||||
"app_id": 8157,
|
||||
"target_id": 138074,
|
||||
"target_type": "User",
|
||||
"permissions": {
|
||||
"repository_projects": "write",
|
||||
"issues": "read",
|
||||
"metadata": "read",
|
||||
"pull_requests": "read"
|
||||
},
|
||||
"events": [
|
||||
"pull_request"
|
||||
],
|
||||
"created_at": 1516025475,
|
||||
"updated_at": 1516025475,
|
||||
"single_file_name": null
|
||||
},
|
||||
"repositories": [
|
||||
{
|
||||
"id": 117381220,
|
||||
"name": "status-github-bot",
|
||||
"full_name": "PombeirP/status-github-bot"
|
||||
}
|
||||
],
|
||||
"sender": {
|
||||
"login": "PombeirP",
|
||||
"id": 138074,
|
||||
"avatar_url": "https://avatars1.githubusercontent.com/u/138074?v=4",
|
||||
"gravatar_id": "",
|
||||
"url": "https://api.github.com/users/PombeirP",
|
||||
"html_url": "https://github.com/PombeirP",
|
||||
"followers_url": "https://api.github.com/users/PombeirP/followers",
|
||||
"following_url": "https://api.github.com/users/PombeirP/following{/other_user}",
|
||||
"gists_url": "https://api.github.com/users/PombeirP/gists{/gist_id}",
|
||||
"starred_url": "https://api.github.com/users/PombeirP/starred{/owner}{/repo}",
|
||||
"subscriptions_url": "https://api.github.com/users/PombeirP/subscriptions",
|
||||
"organizations_url": "https://api.github.com/users/PombeirP/orgs",
|
||||
"repos_url": "https://api.github.com/users/PombeirP/repos",
|
||||
"events_url": "https://api.github.com/users/PombeirP/events{/privacy}",
|
||||
"received_events_url": "https://api.github.com/users/PombeirP/received_events",
|
||||
"type": "User",
|
||||
"site_admin": false
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
req, err := http.NewRequest("POST", "http://127.0.0.1:3010/webhooks", bytes.NewBuffer([]byte(payload)))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-Github-Event", "installation")
|
||||
req.Header.Set("X-Hub-Signature", "sha1=987338c6e5c21794ab6c258abe51284f9b1df728")
|
||||
|
||||
Equal(t, err, nil)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
Equal(t, err, nil)
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
Equal(t, resp.StatusCode, http.StatusOK)
|
||||
}
|
||||
|
||||
func TestIntegrationInstallationEvent(t *testing.T) {
|
||||
|
||||
payload := `{
|
||||
"action": "created",
|
||||
"installation": {
|
||||
"id": 80429,
|
||||
"account": {
|
||||
"login": "PombeirP",
|
||||
"id": 138074,
|
||||
"avatar_url": "https://avatars1.githubusercontent.com/u/138074?v=4",
|
||||
"gravatar_id": "",
|
||||
"url": "https://api.github.com/users/PombeirP",
|
||||
"html_url": "https://github.com/PombeirP",
|
||||
"followers_url": "https://api.github.com/users/PombeirP/followers",
|
||||
"following_url": "https://api.github.com/users/PombeirP/following{/other_user}",
|
||||
"gists_url": "https://api.github.com/users/PombeirP/gists{/gist_id}",
|
||||
"starred_url": "https://api.github.com/users/PombeirP/starred{/owner}{/repo}",
|
||||
"subscriptions_url": "https://api.github.com/users/PombeirP/subscriptions",
|
||||
"organizations_url": "https://api.github.com/users/PombeirP/orgs",
|
||||
"repos_url": "https://api.github.com/users/PombeirP/repos",
|
||||
"events_url": "https://api.github.com/users/PombeirP/events{/privacy}",
|
||||
"received_events_url": "https://api.github.com/users/PombeirP/received_events",
|
||||
"type": "User",
|
||||
"site_admin": false
|
||||
},
|
||||
"repository_selection": "selected",
|
||||
"access_tokens_url": "https://api.github.com/installations/80429/access_tokens",
|
||||
"repositories_url": "https://api.github.com/installation/repositories",
|
||||
"html_url": "https://github.com/settings/installations/80429",
|
||||
"app_id": 8157,
|
||||
"target_id": 138074,
|
||||
"target_type": "User",
|
||||
"permissions": {
|
||||
"repository_projects": "write",
|
||||
"issues": "read",
|
||||
"metadata": "read",
|
||||
"pull_requests": "read"
|
||||
},
|
||||
"events": [
|
||||
"pull_request"
|
||||
],
|
||||
"created_at": 1516025475,
|
||||
"updated_at": 1516025475,
|
||||
"single_file_name": null
|
||||
},
|
||||
"repositories": [
|
||||
{
|
||||
"id": 117381220,
|
||||
"name": "status-github-bot",
|
||||
"full_name": "PombeirP/status-github-bot"
|
||||
}
|
||||
],
|
||||
"sender": {
|
||||
"login": "PombeirP",
|
||||
"id": 138074,
|
||||
"avatar_url": "https://avatars1.githubusercontent.com/u/138074?v=4",
|
||||
"gravatar_id": "",
|
||||
"url": "https://api.github.com/users/PombeirP",
|
||||
"html_url": "https://github.com/PombeirP",
|
||||
"followers_url": "https://api.github.com/users/PombeirP/followers",
|
||||
"following_url": "https://api.github.com/users/PombeirP/following{/other_user}",
|
||||
"gists_url": "https://api.github.com/users/PombeirP/gists{/gist_id}",
|
||||
"starred_url": "https://api.github.com/users/PombeirP/starred{/owner}{/repo}",
|
||||
"subscriptions_url": "https://api.github.com/users/PombeirP/subscriptions",
|
||||
"organizations_url": "https://api.github.com/users/PombeirP/orgs",
|
||||
"repos_url": "https://api.github.com/users/PombeirP/repos",
|
||||
"events_url": "https://api.github.com/users/PombeirP/events{/privacy}",
|
||||
"received_events_url": "https://api.github.com/users/PombeirP/received_events",
|
||||
"type": "User",
|
||||
"site_admin": false
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
req, err := http.NewRequest("POST", "http://127.0.0.1:3010/webhooks", bytes.NewBuffer([]byte(payload)))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-Github-Event", "integration_installation")
|
||||
req.Header.Set("X-Hub-Signature", "sha1=987338c6e5c21794ab6c258abe51284f9b1df728")
|
||||
|
||||
Equal(t, err, nil)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
Equal(t, err, nil)
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
Equal(t, resp.StatusCode, http.StatusOK)
|
||||
}
|
||||
|
||||
func TestIssueCommentEvent(t *testing.T) {
|
||||
|
||||
payload := `{
|
||||
@@ -2560,6 +2743,48 @@ func TestPageBuildEvent(t *testing.T) {
|
||||
Equal(t, resp.StatusCode, http.StatusOK)
|
||||
}
|
||||
|
||||
func TestPingEvent(t *testing.T) {
|
||||
|
||||
payload := `{
|
||||
"zen": "Keep it logically awesome.",
|
||||
"hook_id": 20081052,
|
||||
"hook": {
|
||||
"type": "App",
|
||||
"id": 20081052,
|
||||
"name": "web",
|
||||
"active": true,
|
||||
"events": [
|
||||
"pull_request"
|
||||
],
|
||||
"config": {
|
||||
"content_type": "json",
|
||||
"insecure_ssl": "0",
|
||||
"secret": "********",
|
||||
"url": "https://ngrok.io/webhook"
|
||||
},
|
||||
"updated_at": "2018-01-15T10:48:54Z",
|
||||
"created_at": "2018-01-15T10:48:54Z",
|
||||
"app_id": 8157
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
req, err := http.NewRequest("POST", "http://127.0.0.1:3010/webhooks", bytes.NewBuffer([]byte(payload)))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-Github-Event", "ping")
|
||||
req.Header.Set("X-Hub-Signature", "sha1=f82267eb5c6408d5986209da906747f57c11b33b")
|
||||
|
||||
Equal(t, err, nil)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
Equal(t, err, nil)
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
Equal(t, resp.StatusCode, http.StatusOK)
|
||||
}
|
||||
|
||||
func TestProjectCardEvent(t *testing.T) {
|
||||
|
||||
payload := `{
|
||||
|
||||
+129
-14
@@ -1007,6 +1007,74 @@ type GollumPayload struct {
|
||||
} `json:"sender"`
|
||||
}
|
||||
|
||||
// InstallationPayload contains the information for GitHub's installation and integration_installation hook events
|
||||
type InstallationPayload struct {
|
||||
Action string `json:"action"`
|
||||
Installation struct {
|
||||
ID int64 `json:"id"`
|
||||
Account struct {
|
||||
Login string `json:"login"`
|
||||
ID int64 `json:"id"`
|
||||
AvatarURL string `json:"avatar_url"`
|
||||
GravatarID string `json:"gravatar_id"`
|
||||
URL string `json:"url"`
|
||||
HTMLURL string `json:"html_url"`
|
||||
FollowersURL string `json:"followers_url"`
|
||||
FollowingURL string `json:"following_url"`
|
||||
GistsURL string `json:"gists_url"`
|
||||
StarredURL string `json:"starred_url"`
|
||||
SubscriptionsURL string `json:"subscriptions_url"`
|
||||
OrganizationsURL string `json:"organizations_url"`
|
||||
ReposURL string `json:"repos_url"`
|
||||
EventsURL string `json:"events_url"`
|
||||
ReceivedEventsURL string `json:"received_events_url"`
|
||||
Type string `json:"type"`
|
||||
SiteAdmin bool `json:"site_admin"`
|
||||
} `json:"account"`
|
||||
RepositorySelection string `json:"repository_selection"`
|
||||
AccessTokensURL string `json:"access_tokens_url"`
|
||||
RepositoriesURL string `json:"repositories_url"`
|
||||
HTMLURL string `json:"html_url"`
|
||||
AppID int `json:"app_id"`
|
||||
TargetID int `json:"target_id"`
|
||||
TargetType string `json:"target_type"`
|
||||
Permissions struct {
|
||||
Issues string `json:"issues"`
|
||||
Metadata string `json:"metadata"`
|
||||
PullRequests string `json:"pull_requests"`
|
||||
RepositoryProjects string `json:"repository_projects"`
|
||||
} `json:"permissions"`
|
||||
Events []string `json:"events"`
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
UpdatedAt int64 `json:"updated_at"`
|
||||
SingleFileName *string `json:"single_file_name"`
|
||||
} `json:"installation"`
|
||||
Repositories []struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
FullName string `json:"full_name"`
|
||||
} `json:"repositories"`
|
||||
Sender struct {
|
||||
Login string `json:"login"`
|
||||
ID int64 `json:"id"`
|
||||
AvatarURL string `json:"avatar_url"`
|
||||
GravatarID string `json:"gravatar_id"`
|
||||
URL string `json:"url"`
|
||||
HTMLURL string `json:"html_url"`
|
||||
FollowersURL string `json:"followers_url"`
|
||||
FollowingURL string `json:"following_url"`
|
||||
GistsURL string `json:"gists_url"`
|
||||
StarredURL string `json:"starred_url"`
|
||||
SubscriptionsURL string `json:"subscriptions_url"`
|
||||
OrganizationsURL string `json:"organizations_url"`
|
||||
ReposURL string `json:"repos_url"`
|
||||
EventsURL string `json:"events_url"`
|
||||
ReceivedEventsURL string `json:"received_events_url"`
|
||||
Type string `json:"type"`
|
||||
SiteAdmin bool `json:"site_admin"`
|
||||
} `json:"sender"`
|
||||
}
|
||||
|
||||
// IssueCommentPayload contains the information for GitHub's issue_comment hook event
|
||||
type IssueCommentPayload struct {
|
||||
Action string `json:"action"`
|
||||
@@ -2094,6 +2162,27 @@ type PageBuildPayload struct {
|
||||
} `json:"sender"`
|
||||
}
|
||||
|
||||
// PingPayload contains the information for GitHub's ping hook event
|
||||
type PingPayload struct {
|
||||
HookID int `json:"hook_id"`
|
||||
Hook struct {
|
||||
Type string `json:"type"`
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Active bool `json:"active"`
|
||||
Events []string `json:"events"`
|
||||
AppID int `json:"app_id"`
|
||||
Config struct {
|
||||
ContentType string `json:"content_type"`
|
||||
InsecureSSL int `json:"insecure_ssl"`
|
||||
Secret string `json:"secret"`
|
||||
URL string `json:"url"`
|
||||
} `json:"config"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
} `json:"hook"`
|
||||
}
|
||||
|
||||
// ProjectCardPayload contains the information for GitHub's project_payload hook event
|
||||
type ProjectCardPayload struct {
|
||||
Action string `json:"action"`
|
||||
@@ -2670,20 +2759,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"`
|
||||
|
||||
+129
-66
@@ -807,82 +807,145 @@ func TestCommentSunippetEvent(t *testing.T) {
|
||||
func TestMergeRequestEvent(t *testing.T) {
|
||||
|
||||
payload := `{
|
||||
"object_kind": "merge_request",
|
||||
"user": {
|
||||
"name": "Administrator",
|
||||
"username": "root",
|
||||
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
|
||||
},
|
||||
"object_attributes": {
|
||||
"id": 99,
|
||||
"target_branch": "master",
|
||||
"source_branch": "ms-viewport",
|
||||
"source_project_id": 14,
|
||||
"author_id": 51,
|
||||
"assignee_id": 6,
|
||||
"title": "MS-Viewport",
|
||||
"created_at": "2013-12-03T17:23:34Z",
|
||||
"updated_at": "2013-12-03T17:23:34Z",
|
||||
"st_commits": null,
|
||||
"st_diffs": null,
|
||||
"milestone_id": null,
|
||||
"state": "opened",
|
||||
"merge_status": "unchecked",
|
||||
"target_project_id": 14,
|
||||
"iid": 1,
|
||||
"description": "",
|
||||
"source":{
|
||||
"name":"Awesome Project",
|
||||
"description":"Aut reprehenderit ut est.",
|
||||
"web_url":"http://example.com/awesome_space/awesome_project",
|
||||
"avatar_url":null,
|
||||
"git_ssh_url":"git@example.com:awesome_space/awesome_project.git",
|
||||
"git_http_url":"http://example.com/awesome_space/awesome_project.git",
|
||||
"namespace":"Awesome Space",
|
||||
"visibility_level":20,
|
||||
"path_with_namespace":"awesome_space/awesome_project",
|
||||
"default_branch":"master",
|
||||
"homepage":"http://example.com/awesome_space/awesome_project",
|
||||
"url":"http://example.com/awesome_space/awesome_project.git",
|
||||
"ssh_url":"git@example.com:awesome_space/awesome_project.git",
|
||||
"http_url":"http://example.com/awesome_space/awesome_project.git"
|
||||
"object_kind": "merge_request",
|
||||
"user": {
|
||||
"name": "Administrator",
|
||||
"username": "root",
|
||||
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
|
||||
},
|
||||
"target": {
|
||||
"name":"Awesome Project",
|
||||
"project": {
|
||||
"id": 1,
|
||||
"name":"Gitlab Test",
|
||||
"description":"Aut reprehenderit ut est.",
|
||||
"web_url":"http://example.com/awesome_space/awesome_project",
|
||||
"web_url":"http://example.com/gitlabhq/gitlab-test",
|
||||
"avatar_url":null,
|
||||
"git_ssh_url":"git@example.com:awesome_space/awesome_project.git",
|
||||
"git_http_url":"http://example.com/awesome_space/awesome_project.git",
|
||||
"namespace":"Awesome Space",
|
||||
"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":"awesome_space/awesome_project",
|
||||
"path_with_namespace":"gitlabhq/gitlab-test",
|
||||
"default_branch":"master",
|
||||
"homepage":"http://example.com/awesome_space/awesome_project",
|
||||
"url":"http://example.com/awesome_space/awesome_project.git",
|
||||
"ssh_url":"git@example.com:awesome_space/awesome_project.git",
|
||||
"http_url":"http://example.com/awesome_space/awesome_project.git"
|
||||
"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"
|
||||
},
|
||||
"last_commit": {
|
||||
"id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
|
||||
"message": "fixed readme",
|
||||
"timestamp": "2012-01-03T23:36:29+02:00",
|
||||
"url": "http://example.com/awesome_space/awesome_project/commits/da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
|
||||
"author": {
|
||||
"name": "GitLab dev user",
|
||||
"email": "gitlabdev@dv6700.(none)"
|
||||
"repository": {
|
||||
"name": "Gitlab Test",
|
||||
"url": "http://example.com/gitlabhq/gitlab-test.git",
|
||||
"description": "Aut reprehenderit ut est.",
|
||||
"homepage": "http://example.com/gitlabhq/gitlab-test"
|
||||
},
|
||||
"object_attributes": {
|
||||
"id": 99,
|
||||
"target_branch": "master",
|
||||
"source_branch": "ms-viewport",
|
||||
"source_project_id": 14,
|
||||
"author_id": 51,
|
||||
"assignee_id": 6,
|
||||
"title": "MS-Viewport",
|
||||
"created_at": "2013-12-03T17:23:34Z",
|
||||
"updated_at": "2013-12-03T17:23:34Z",
|
||||
"milestone_id": null,
|
||||
"state": "opened",
|
||||
"merge_status": "unchecked",
|
||||
"target_project_id": 14,
|
||||
"iid": 1,
|
||||
"description": "",
|
||||
"source": {
|
||||
"name":"Awesome Project",
|
||||
"description":"Aut reprehenderit ut est.",
|
||||
"web_url":"http://example.com/awesome_space/awesome_project",
|
||||
"avatar_url":null,
|
||||
"git_ssh_url":"git@example.com:awesome_space/awesome_project.git",
|
||||
"git_http_url":"http://example.com/awesome_space/awesome_project.git",
|
||||
"namespace":"Awesome Space",
|
||||
"visibility_level":20,
|
||||
"path_with_namespace":"awesome_space/awesome_project",
|
||||
"default_branch":"master",
|
||||
"homepage":"http://example.com/awesome_space/awesome_project",
|
||||
"url":"http://example.com/awesome_space/awesome_project.git",
|
||||
"ssh_url":"git@example.com:awesome_space/awesome_project.git",
|
||||
"http_url":"http://example.com/awesome_space/awesome_project.git"
|
||||
},
|
||||
"target": {
|
||||
"name":"Awesome Project",
|
||||
"description":"Aut reprehenderit ut est.",
|
||||
"web_url":"http://example.com/awesome_space/awesome_project",
|
||||
"avatar_url":null,
|
||||
"git_ssh_url":"git@example.com:awesome_space/awesome_project.git",
|
||||
"git_http_url":"http://example.com/awesome_space/awesome_project.git",
|
||||
"namespace":"Awesome Space",
|
||||
"visibility_level":20,
|
||||
"path_with_namespace":"awesome_space/awesome_project",
|
||||
"default_branch":"master",
|
||||
"homepage":"http://example.com/awesome_space/awesome_project",
|
||||
"url":"http://example.com/awesome_space/awesome_project.git",
|
||||
"ssh_url":"git@example.com:awesome_space/awesome_project.git",
|
||||
"http_url":"http://example.com/awesome_space/awesome_project.git"
|
||||
},
|
||||
"last_commit": {
|
||||
"id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
|
||||
"message": "fixed readme",
|
||||
"timestamp": "2012-01-03T23:36:29+02:00",
|
||||
"url": "http://example.com/awesome_space/awesome_project/commits/da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
|
||||
"author": {
|
||||
"name": "GitLab dev user",
|
||||
"email": "gitlabdev@dv6700.(none)"
|
||||
}
|
||||
},
|
||||
"work_in_progress": false,
|
||||
"url": "http://example.com/diaspora/merge_requests/1",
|
||||
"action": "open",
|
||||
"assignee": {
|
||||
"name": "User1",
|
||||
"username": "user1",
|
||||
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
|
||||
}
|
||||
},
|
||||
"work_in_progress": false,
|
||||
"url": "http://example.com/diaspora/merge_requests/1",
|
||||
"action": "open",
|
||||
"assignee": {
|
||||
"name": "User1",
|
||||
"username": "user1",
|
||||
"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
|
||||
}]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
req, err := http.NewRequest("POST", "http://127.0.0.1:3011/webhooks", bytes.NewBuffer([]byte(payload)))
|
||||
|
||||
@@ -38,6 +38,7 @@ type IssueEventPayload struct {
|
||||
Repository Repository `json:"repository"`
|
||||
ObjectAttributes ObjectAttributes `json:"object_attributes"`
|
||||
Assignee Assignee `json:"assignee"`
|
||||
Changes Changes `json:"changes"`
|
||||
}
|
||||
|
||||
// ConfidentialIssueEventPayload contains the information for GitLab's confidential issue event
|
||||
@@ -52,6 +53,9 @@ type MergeRequestEventPayload struct {
|
||||
ObjectKind string `json:"object_kind"`
|
||||
User User `json:"user"`
|
||||
ObjectAttributes ObjectAttributes `json:"object_attributes"`
|
||||
Changes Changes `json:"changes"`
|
||||
Project Project `json:"project"`
|
||||
Repository Repository `json:"repository"`
|
||||
}
|
||||
|
||||
// PushEventPayload contains the information for GitLab's push event
|
||||
@@ -409,3 +413,28 @@ type Author struct {
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
// Changes contains all changes associated with a GitLab issue or MR
|
||||
type Changes struct {
|
||||
LabelChanges LabelChanges `json:"labels"`
|
||||
}
|
||||
|
||||
// LabelChanges contains changes in labels assocatiated with a GitLab issue or MR
|
||||
type LabelChanges struct {
|
||||
Previous []Label `json:"previous"`
|
||||
Current []Label `json:"current"`
|
||||
}
|
||||
|
||||
// Label contains all of the GitLab label information
|
||||
type Label struct {
|
||||
Id int64 `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Color string `json:"color"`
|
||||
ProjectId int64 `json:"project_id"`
|
||||
CreatedAt customTime `json:"created_at"`
|
||||
UpdatedAt customTime `json:"updated_at"`
|
||||
Template bool `json:"template"`
|
||||
Description string `json:"description"`
|
||||
Type string `json:"type"`
|
||||
GroupId int64 `json:"group_id"`
|
||||
}
|
||||
|
||||
+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"
|
||||
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