Skip to content

Commit 8f6fada

Browse files
author
Pedro Pombeiro
committed
Add support for ping events
1 parent 94f9d66 commit 8f6fada

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

github/github.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const (
4747
OrganizationEvent Event = "organization"
4848
OrgBlockEvent Event = "org_block"
4949
PageBuildEvent Event = "page_build"
50+
PingEvent Event = "ping"
5051
ProjectCardEvent Event = "project_card"
5152
ProjectColumnEvent Event = "project_column"
5253
ProjectEvent Event = "project"
@@ -221,6 +222,10 @@ func (hook Webhook) ParsePayload(w http.ResponseWriter, r *http.Request) {
221222
var p PageBuildPayload
222223
json.Unmarshal([]byte(payload), &p)
223224
hook.runProcessPayloadFunc(fn, p, hd)
225+
case PingEvent:
226+
var p PingPayload
227+
json.Unmarshal([]byte(payload), &p)
228+
hook.runProcessPayloadFunc(fn, p, hd)
224229
case ProjectCardEvent:
225230
var p ProjectCardPayload
226231
json.Unmarshal([]byte(payload), &p)

github/github_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func TestMain(m *testing.M) {
5959
OrganizationEvent,
6060
OrgBlockEvent,
6161
PageBuildEvent,
62+
PingEvent,
6263
ProjectCardEvent,
6364
ProjectColumnEvent,
6465
ProjectEvent,
@@ -2742,6 +2743,48 @@ func TestPageBuildEvent(t *testing.T) {
27422743
Equal(t, resp.StatusCode, http.StatusOK)
27432744
}
27442745

2746+
func TestPingEvent(t *testing.T) {
2747+
2748+
payload := `{
2749+
"zen": "Keep it logically awesome.",
2750+
"hook_id": 20081052,
2751+
"hook": {
2752+
"type": "App",
2753+
"id": 20081052,
2754+
"name": "web",
2755+
"active": true,
2756+
"events": [
2757+
"pull_request"
2758+
],
2759+
"config": {
2760+
"content_type": "json",
2761+
"insecure_ssl": "0",
2762+
"secret": "********",
2763+
"url": "https://ngrok.io/webhook"
2764+
},
2765+
"updated_at": "2018-01-15T10:48:54Z",
2766+
"created_at": "2018-01-15T10:48:54Z",
2767+
"app_id": 8157
2768+
}
2769+
}
2770+
`
2771+
2772+
req, err := http.NewRequest("POST", "http://127.0.0.1:3010/webhooks", bytes.NewBuffer([]byte(payload)))
2773+
req.Header.Set("Content-Type", "application/json")
2774+
req.Header.Set("X-Github-Event", "ping")
2775+
req.Header.Set("X-Hub-Signature", "sha1=f82267eb5c6408d5986209da906747f57c11b33b")
2776+
2777+
Equal(t, err, nil)
2778+
2779+
client := &http.Client{}
2780+
resp, err := client.Do(req)
2781+
Equal(t, err, nil)
2782+
2783+
defer resp.Body.Close()
2784+
2785+
Equal(t, resp.StatusCode, http.StatusOK)
2786+
}
2787+
27452788
func TestProjectCardEvent(t *testing.T) {
27462789

27472790
payload := `{

github/payload.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,6 +2162,27 @@ type PageBuildPayload struct {
21622162
} `json:"sender"`
21632163
}
21642164

2165+
// PingPayload contains the information for GitHub's ping hook event
2166+
type PingPayload struct {
2167+
HookID int `json:"hook_id"`
2168+
Hook struct {
2169+
Type string `json:"type"`
2170+
ID int64 `json:"id"`
2171+
Name string `json:"name"`
2172+
Active bool `json:"active"`
2173+
Events []string `json:"events"`
2174+
AppID int `json:"app_id"`
2175+
Config struct {
2176+
ContentType string `json:"content_type"`
2177+
InsecureSSL int `json:"insecure_ssl"`
2178+
Secret string `json:"secret"`
2179+
URL string `json:"url"`
2180+
} `json:"config"`
2181+
CreatedAt time.Time `json:"created_at"`
2182+
UpdatedAt time.Time `json:"updated_at"`
2183+
} `json:"hook"`
2184+
}
2185+
21652186
// ProjectCardPayload contains the information for GitHub's project_payload hook event
21662187
type ProjectCardPayload struct {
21672188
Action string `json:"action"`

0 commit comments

Comments
 (0)