Skip to content

Commit 97dd8f3

Browse files
author
Dean Karn
authored
Merge pull request #23 from PombeirP/add-installation-and-ping
Add installation and ping
2 parents ad53921 + 8f6fada commit 97dd8f3

File tree

3 files changed

+325
-0
lines changed

3 files changed

+325
-0
lines changed

github/github.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ const (
3636
DeploymentStatusEvent Event = "deployment_status"
3737
ForkEvent Event = "fork"
3838
GollumEvent Event = "gollum"
39+
InstallationEvent Event = "installation"
40+
IntegrationInstallationEvent Event = "integration_installation"
3941
IssueCommentEvent Event = "issue_comment"
4042
IssuesEvent Event = "issues"
4143
LabelEvent Event = "label"
@@ -45,6 +47,7 @@ const (
4547
OrganizationEvent Event = "organization"
4648
OrgBlockEvent Event = "org_block"
4749
PageBuildEvent Event = "page_build"
50+
PingEvent Event = "ping"
4851
ProjectCardEvent Event = "project_card"
4952
ProjectColumnEvent Event = "project_column"
5053
ProjectEvent Event = "project"
@@ -179,6 +182,10 @@ func (hook Webhook) ParsePayload(w http.ResponseWriter, r *http.Request) {
179182
var g GollumPayload
180183
json.Unmarshal([]byte(payload), &g)
181184
hook.runProcessPayloadFunc(fn, g, hd)
185+
case InstallationEvent, IntegrationInstallationEvent:
186+
var i InstallationPayload
187+
json.Unmarshal([]byte(payload), &i)
188+
hook.runProcessPayloadFunc(fn, i, hd)
182189
case IssueCommentEvent:
183190
var i IssueCommentPayload
184191
json.Unmarshal([]byte(payload), &i)
@@ -215,6 +222,10 @@ func (hook Webhook) ParsePayload(w http.ResponseWriter, r *http.Request) {
215222
var p PageBuildPayload
216223
json.Unmarshal([]byte(payload), &p)
217224
hook.runProcessPayloadFunc(fn, p, hd)
225+
case PingEvent:
226+
var p PingPayload
227+
json.Unmarshal([]byte(payload), &p)
228+
hook.runProcessPayloadFunc(fn, p, hd)
218229
case ProjectCardEvent:
219230
var p ProjectCardPayload
220231
json.Unmarshal([]byte(payload), &p)

github/github_test.go

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ func TestMain(m *testing.M) {
4848
DeploymentStatusEvent,
4949
ForkEvent,
5050
GollumEvent,
51+
InstallationEvent,
52+
IntegrationInstallationEvent,
5153
IssueCommentEvent,
5254
IssuesEvent,
5355
LabelEvent,
@@ -57,6 +59,7 @@ func TestMain(m *testing.M) {
5759
OrganizationEvent,
5860
OrgBlockEvent,
5961
PageBuildEvent,
62+
PingEvent,
6063
ProjectCardEvent,
6164
ProjectColumnEvent,
6265
ProjectEvent,
@@ -1309,6 +1312,186 @@ func TestGollumEvent(t *testing.T) {
13091312
Equal(t, resp.StatusCode, http.StatusOK)
13101313
}
13111314

1315+
func TestInstallationEvent(t *testing.T) {
1316+
1317+
payload := `{
1318+
"action": "created",
1319+
"installation": {
1320+
"id": 80429,
1321+
"account": {
1322+
"login": "PombeirP",
1323+
"id": 138074,
1324+
"avatar_url": "https://avatars1.githubusercontent.com/u/138074?v=4",
1325+
"gravatar_id": "",
1326+
"url": "https://api.github.com/users/PombeirP",
1327+
"html_url": "https://github.com/PombeirP",
1328+
"followers_url": "https://api.github.com/users/PombeirP/followers",
1329+
"following_url": "https://api.github.com/users/PombeirP/following{/other_user}",
1330+
"gists_url": "https://api.github.com/users/PombeirP/gists{/gist_id}",
1331+
"starred_url": "https://api.github.com/users/PombeirP/starred{/owner}{/repo}",
1332+
"subscriptions_url": "https://api.github.com/users/PombeirP/subscriptions",
1333+
"organizations_url": "https://api.github.com/users/PombeirP/orgs",
1334+
"repos_url": "https://api.github.com/users/PombeirP/repos",
1335+
"events_url": "https://api.github.com/users/PombeirP/events{/privacy}",
1336+
"received_events_url": "https://api.github.com/users/PombeirP/received_events",
1337+
"type": "User",
1338+
"site_admin": false
1339+
},
1340+
"repository_selection": "selected",
1341+
"access_tokens_url": "https://api.github.com/installations/80429/access_tokens",
1342+
"repositories_url": "https://api.github.com/installation/repositories",
1343+
"html_url": "https://github.com/settings/installations/80429",
1344+
"app_id": 8157,
1345+
"target_id": 138074,
1346+
"target_type": "User",
1347+
"permissions": {
1348+
"repository_projects": "write",
1349+
"issues": "read",
1350+
"metadata": "read",
1351+
"pull_requests": "read"
1352+
},
1353+
"events": [
1354+
"pull_request"
1355+
],
1356+
"created_at": 1516025475,
1357+
"updated_at": 1516025475,
1358+
"single_file_name": null
1359+
},
1360+
"repositories": [
1361+
{
1362+
"id": 117381220,
1363+
"name": "status-github-bot",
1364+
"full_name": "PombeirP/status-github-bot"
1365+
}
1366+
],
1367+
"sender": {
1368+
"login": "PombeirP",
1369+
"id": 138074,
1370+
"avatar_url": "https://avatars1.githubusercontent.com/u/138074?v=4",
1371+
"gravatar_id": "",
1372+
"url": "https://api.github.com/users/PombeirP",
1373+
"html_url": "https://github.com/PombeirP",
1374+
"followers_url": "https://api.github.com/users/PombeirP/followers",
1375+
"following_url": "https://api.github.com/users/PombeirP/following{/other_user}",
1376+
"gists_url": "https://api.github.com/users/PombeirP/gists{/gist_id}",
1377+
"starred_url": "https://api.github.com/users/PombeirP/starred{/owner}{/repo}",
1378+
"subscriptions_url": "https://api.github.com/users/PombeirP/subscriptions",
1379+
"organizations_url": "https://api.github.com/users/PombeirP/orgs",
1380+
"repos_url": "https://api.github.com/users/PombeirP/repos",
1381+
"events_url": "https://api.github.com/users/PombeirP/events{/privacy}",
1382+
"received_events_url": "https://api.github.com/users/PombeirP/received_events",
1383+
"type": "User",
1384+
"site_admin": false
1385+
}
1386+
}
1387+
`
1388+
1389+
req, err := http.NewRequest("POST", "http://127.0.0.1:3010/webhooks", bytes.NewBuffer([]byte(payload)))
1390+
req.Header.Set("Content-Type", "application/json")
1391+
req.Header.Set("X-Github-Event", "installation")
1392+
req.Header.Set("X-Hub-Signature", "sha1=987338c6e5c21794ab6c258abe51284f9b1df728")
1393+
1394+
Equal(t, err, nil)
1395+
1396+
client := &http.Client{}
1397+
resp, err := client.Do(req)
1398+
Equal(t, err, nil)
1399+
1400+
defer resp.Body.Close()
1401+
1402+
Equal(t, resp.StatusCode, http.StatusOK)
1403+
}
1404+
1405+
func TestIntegrationInstallationEvent(t *testing.T) {
1406+
1407+
payload := `{
1408+
"action": "created",
1409+
"installation": {
1410+
"id": 80429,
1411+
"account": {
1412+
"login": "PombeirP",
1413+
"id": 138074,
1414+
"avatar_url": "https://avatars1.githubusercontent.com/u/138074?v=4",
1415+
"gravatar_id": "",
1416+
"url": "https://api.github.com/users/PombeirP",
1417+
"html_url": "https://github.com/PombeirP",
1418+
"followers_url": "https://api.github.com/users/PombeirP/followers",
1419+
"following_url": "https://api.github.com/users/PombeirP/following{/other_user}",
1420+
"gists_url": "https://api.github.com/users/PombeirP/gists{/gist_id}",
1421+
"starred_url": "https://api.github.com/users/PombeirP/starred{/owner}{/repo}",
1422+
"subscriptions_url": "https://api.github.com/users/PombeirP/subscriptions",
1423+
"organizations_url": "https://api.github.com/users/PombeirP/orgs",
1424+
"repos_url": "https://api.github.com/users/PombeirP/repos",
1425+
"events_url": "https://api.github.com/users/PombeirP/events{/privacy}",
1426+
"received_events_url": "https://api.github.com/users/PombeirP/received_events",
1427+
"type": "User",
1428+
"site_admin": false
1429+
},
1430+
"repository_selection": "selected",
1431+
"access_tokens_url": "https://api.github.com/installations/80429/access_tokens",
1432+
"repositories_url": "https://api.github.com/installation/repositories",
1433+
"html_url": "https://github.com/settings/installations/80429",
1434+
"app_id": 8157,
1435+
"target_id": 138074,
1436+
"target_type": "User",
1437+
"permissions": {
1438+
"repository_projects": "write",
1439+
"issues": "read",
1440+
"metadata": "read",
1441+
"pull_requests": "read"
1442+
},
1443+
"events": [
1444+
"pull_request"
1445+
],
1446+
"created_at": 1516025475,
1447+
"updated_at": 1516025475,
1448+
"single_file_name": null
1449+
},
1450+
"repositories": [
1451+
{
1452+
"id": 117381220,
1453+
"name": "status-github-bot",
1454+
"full_name": "PombeirP/status-github-bot"
1455+
}
1456+
],
1457+
"sender": {
1458+
"login": "PombeirP",
1459+
"id": 138074,
1460+
"avatar_url": "https://avatars1.githubusercontent.com/u/138074?v=4",
1461+
"gravatar_id": "",
1462+
"url": "https://api.github.com/users/PombeirP",
1463+
"html_url": "https://github.com/PombeirP",
1464+
"followers_url": "https://api.github.com/users/PombeirP/followers",
1465+
"following_url": "https://api.github.com/users/PombeirP/following{/other_user}",
1466+
"gists_url": "https://api.github.com/users/PombeirP/gists{/gist_id}",
1467+
"starred_url": "https://api.github.com/users/PombeirP/starred{/owner}{/repo}",
1468+
"subscriptions_url": "https://api.github.com/users/PombeirP/subscriptions",
1469+
"organizations_url": "https://api.github.com/users/PombeirP/orgs",
1470+
"repos_url": "https://api.github.com/users/PombeirP/repos",
1471+
"events_url": "https://api.github.com/users/PombeirP/events{/privacy}",
1472+
"received_events_url": "https://api.github.com/users/PombeirP/received_events",
1473+
"type": "User",
1474+
"site_admin": false
1475+
}
1476+
}
1477+
`
1478+
1479+
req, err := http.NewRequest("POST", "http://127.0.0.1:3010/webhooks", bytes.NewBuffer([]byte(payload)))
1480+
req.Header.Set("Content-Type", "application/json")
1481+
req.Header.Set("X-Github-Event", "integration_installation")
1482+
req.Header.Set("X-Hub-Signature", "sha1=987338c6e5c21794ab6c258abe51284f9b1df728")
1483+
1484+
Equal(t, err, nil)
1485+
1486+
client := &http.Client{}
1487+
resp, err := client.Do(req)
1488+
Equal(t, err, nil)
1489+
1490+
defer resp.Body.Close()
1491+
1492+
Equal(t, resp.StatusCode, http.StatusOK)
1493+
}
1494+
13121495
func TestIssueCommentEvent(t *testing.T) {
13131496

13141497
payload := `{
@@ -2560,6 +2743,48 @@ func TestPageBuildEvent(t *testing.T) {
25602743
Equal(t, resp.StatusCode, http.StatusOK)
25612744
}
25622745

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+
25632788
func TestProjectCardEvent(t *testing.T) {
25642789

25652790
payload := `{

github/payload.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,74 @@ type GollumPayload struct {
10071007
} `json:"sender"`
10081008
}
10091009

1010+
// InstallationPayload contains the information for GitHub's installation and integration_installation hook events
1011+
type InstallationPayload struct {
1012+
Action string `json:"action"`
1013+
Installation struct {
1014+
ID int64 `json:"id"`
1015+
Account struct {
1016+
Login string `json:"login"`
1017+
ID int64 `json:"id"`
1018+
AvatarURL string `json:"avatar_url"`
1019+
GravatarID string `json:"gravatar_id"`
1020+
URL string `json:"url"`
1021+
HTMLURL string `json:"html_url"`
1022+
FollowersURL string `json:"followers_url"`
1023+
FollowingURL string `json:"following_url"`
1024+
GistsURL string `json:"gists_url"`
1025+
StarredURL string `json:"starred_url"`
1026+
SubscriptionsURL string `json:"subscriptions_url"`
1027+
OrganizationsURL string `json:"organizations_url"`
1028+
ReposURL string `json:"repos_url"`
1029+
EventsURL string `json:"events_url"`
1030+
ReceivedEventsURL string `json:"received_events_url"`
1031+
Type string `json:"type"`
1032+
SiteAdmin bool `json:"site_admin"`
1033+
} `json:"account"`
1034+
RepositorySelection string `json:"repository_selection"`
1035+
AccessTokensURL string `json:"access_tokens_url"`
1036+
RepositoriesURL string `json:"repositories_url"`
1037+
HTMLURL string `json:"html_url"`
1038+
AppID int `json:"app_id"`
1039+
TargetID int `json:"target_id"`
1040+
TargetType string `json:"target_type"`
1041+
Permissions struct {
1042+
Issues string `json:"issues"`
1043+
Metadata string `json:"metadata"`
1044+
PullRequests string `json:"pull_requests"`
1045+
RepositoryProjects string `json:"repository_projects"`
1046+
} `json:"permissions"`
1047+
Events []string `json:"events"`
1048+
CreatedAt int64 `json:"created_at"`
1049+
UpdatedAt int64 `json:"updated_at"`
1050+
SingleFileName *string `json:"single_file_name"`
1051+
} `json:"installation"`
1052+
Repositories []struct {
1053+
ID int64 `json:"id"`
1054+
Name string `json:"name"`
1055+
FullName string `json:"full_name"`
1056+
} `json:"repositories"`
1057+
Sender struct {
1058+
Login string `json:"login"`
1059+
ID int64 `json:"id"`
1060+
AvatarURL string `json:"avatar_url"`
1061+
GravatarID string `json:"gravatar_id"`
1062+
URL string `json:"url"`
1063+
HTMLURL string `json:"html_url"`
1064+
FollowersURL string `json:"followers_url"`
1065+
FollowingURL string `json:"following_url"`
1066+
GistsURL string `json:"gists_url"`
1067+
StarredURL string `json:"starred_url"`
1068+
SubscriptionsURL string `json:"subscriptions_url"`
1069+
OrganizationsURL string `json:"organizations_url"`
1070+
ReposURL string `json:"repos_url"`
1071+
EventsURL string `json:"events_url"`
1072+
ReceivedEventsURL string `json:"received_events_url"`
1073+
Type string `json:"type"`
1074+
SiteAdmin bool `json:"site_admin"`
1075+
} `json:"sender"`
1076+
}
1077+
10101078
// IssueCommentPayload contains the information for GitHub's issue_comment hook event
10111079
type IssueCommentPayload struct {
10121080
Action string `json:"action"`
@@ -2094,6 +2162,27 @@ type PageBuildPayload struct {
20942162
} `json:"sender"`
20952163
}
20962164

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+
20972186
// ProjectCardPayload contains the information for GitHub's project_payload hook event
20982187
type ProjectCardPayload struct {
20992188
Action string `json:"action"`

0 commit comments

Comments
 (0)