Skip to content

Commit 642df98

Browse files
committed
feat: add ping payload for organization
1 parent cfa5365 commit 642df98

File tree

4 files changed

+148
-1
lines changed

4 files changed

+148
-1
lines changed

Diff for: github/github.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ const (
9090
IssueSubtype EventSubtype = "issues"
9191
)
9292

93+
// InstallationTargetType defines a GitHub Hook HTTP Header type
94+
type InstallationTargetType string
95+
96+
// GitHub hook HTTP header installation target types
97+
const (
98+
InstallationTargetTypeRepository InstallationTargetType = "repository"
99+
InstallationTargetTypeOrganization InstallationTargetType = "organization"
100+
)
101+
93102
// Option is a configuration option for the webhook
94103
type Option func(*Webhook) error
95104

@@ -178,6 +187,12 @@ func (hook Webhook) Parse(r *http.Request, events ...Event) (interface{}, error)
178187
}
179188
}
180189

190+
targetType := InstallationTargetTypeRepository
191+
instTargetType := InstallationTargetType(r.Header.Get("X-GitHub-Hook-Installation-Target-Type"))
192+
if instTargetType == InstallationTargetTypeOrganization {
193+
targetType = InstallationTargetTypeOrganization
194+
}
195+
181196
switch gitHubEvent {
182197
case CheckRunEvent:
183198
var pl CheckRunPayload
@@ -272,7 +287,12 @@ func (hook Webhook) Parse(r *http.Request, events ...Event) (interface{}, error)
272287
err = json.Unmarshal([]byte(payload), &pl)
273288
return pl, err
274289
case PingEvent:
275-
var pl PingPayload
290+
if targetType == InstallationTargetTypeRepository {
291+
var pl PingPayload
292+
err = json.Unmarshal([]byte(payload), &pl)
293+
return pl, err
294+
}
295+
var pl PingOrganizationPayload
276296
err = json.Unmarshal([]byte(payload), &pl)
277297
return pl, err
278298
case ProjectCardEvent:

Diff for: github/github_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,16 @@ func TestWebhooks(t *testing.T) {
366366
"X-Github-Event": []string{"ping"},
367367
},
368368
},
369+
{
370+
name: "PingEvent",
371+
event: PingEvent,
372+
typ: PingOrganizationPayload{},
373+
filename: "../testdata/github/ping-organization.json",
374+
headers: http.Header{
375+
"X-Github-Event": []string{"ping"},
376+
"X-GitHub-Hook-Installation-Target-Type": []string{"organization"},
377+
},
378+
},
369379
{
370380
name: "ProjectCardEvent",
371381
event: ProjectCardEvent,

Diff for: github/payload.go

+58
Original file line numberDiff line numberDiff line change
@@ -3336,6 +3336,64 @@ type PingPayload struct {
33363336
} `json:"sender"`
33373337
}
33383338

3339+
// PingOrganizationPayload contains the information for GitHub's organization ping hook event
3340+
type PingOrganizationPayload struct {
3341+
Zen string `json:"zen"`
3342+
HookID int `json:"hook_id"`
3343+
Hook struct {
3344+
Type string `json:"type"`
3345+
ID int64 `json:"id"`
3346+
Name string `json:"name"`
3347+
Active bool `json:"active"`
3348+
Events []string `json:"events"`
3349+
Config struct {
3350+
ContentType string `json:"content_type"`
3351+
InsecureSSL string `json:"insecure_ssl"`
3352+
Secret string `json:"secret"`
3353+
URL string `json:"url"`
3354+
} `json:"config"`
3355+
UpdatedAt time.Time `json:"updated_at"`
3356+
CreatedAt time.Time `json:"created_at"`
3357+
URL string `json:"url"`
3358+
PingURL string `json:"ping_url"`
3359+
DeliveriesURL string `json:"deliveries_url"`
3360+
} `json:"hook"`
3361+
Organization struct {
3362+
Login string `json:"login"`
3363+
ID int64 `json:"id"`
3364+
NodeID string `json:"node_id"`
3365+
URL string `json:"url"`
3366+
ReposURL string `json:"repos_url"`
3367+
EventsURL string `json:"events_url"`
3368+
HooksURL string `json:"hooks_url"`
3369+
IssuesURL string `json:"issues_url"`
3370+
MembersURL string `json:"members_url"`
3371+
PublicMembersURL string `json:"public_members_url"`
3372+
AvatarURL string `json:"avatar_url"`
3373+
Description string `json:"description"`
3374+
} `json:"organization"`
3375+
Sender struct {
3376+
Login string `json:"login"`
3377+
ID int64 `json:"id"`
3378+
NodeID string `json:"node_id"`
3379+
AvatarURL string `json:"avatar_url"`
3380+
GravatarID string `json:"gravatar_id"`
3381+
URL string `json:"url"`
3382+
HTMLURL string `json:"html_url"`
3383+
FollowersURL string `json:"followers_url"`
3384+
FollowingURL string `json:"following_url"`
3385+
GistsURL string `json:"gists_url"`
3386+
StarredURL string `json:"starred_url"`
3387+
SubscriptionsURL string `json:"subscriptions_url"`
3388+
OrganizationsURL string `json:"organizations_url"`
3389+
ReposURL string `json:"repos_url"`
3390+
EventsURL string `json:"events_url"`
3391+
ReceivedEventsURL string `json:"received_events_url"`
3392+
Type string `json:"type"`
3393+
SiteAdmin bool `json:"site_admin"`
3394+
} `json:"sender"`
3395+
}
3396+
33393397
// ProjectCardPayload contains the information for GitHub's project_payload hook event
33403398
type ProjectCardPayload struct {
33413399
Action string `json:"action"`

Diff for: testdata/github/ping-organization.json

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"zen": "Avoid administrative distraction.",
3+
"hook_id": 523968606,
4+
"hook": {
5+
"type": "Organization",
6+
"id": 523968606,
7+
"name": "web",
8+
"active": true,
9+
"events": [
10+
"*"
11+
],
12+
"config": {
13+
"content_type": "json",
14+
"insecure_ssl": "0",
15+
"secret": "********",
16+
"url": "https://github.devchain.network/v1/webhook/github"
17+
},
18+
"updated_at": "2025-01-13T07:08:59Z",
19+
"created_at": "2025-01-13T07:08:59Z",
20+
"url": "https://api.github.com/orgs/vbyazilim/hooks/523968606",
21+
"ping_url": "https://api.github.com/orgs/vbyazilim/hooks/523968606/pings",
22+
"deliveries_url": "https://api.github.com/orgs/vbyazilim/hooks/523968606/deliveries"
23+
},
24+
"organization": {
25+
"login": "vbyazilim",
26+
"id": 34422235,
27+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjM0NDIyMjM1",
28+
"url": "https://api.github.com/orgs/vbyazilim",
29+
"repos_url": "https://api.github.com/orgs/vbyazilim/repos",
30+
"events_url": "https://api.github.com/orgs/vbyazilim/events",
31+
"hooks_url": "https://api.github.com/orgs/vbyazilim/hooks",
32+
"issues_url": "https://api.github.com/orgs/vbyazilim/issues",
33+
"members_url": "https://api.github.com/orgs/vbyazilim/members{/member}",
34+
"public_members_url": "https://api.github.com/orgs/vbyazilim/public_members{/member}",
35+
"avatar_url": "https://avatars.githubusercontent.com/u/34422235?v=4",
36+
"description": "Software Development, Training and Consultancy"
37+
},
38+
"sender": {
39+
"login": "vigo",
40+
"id": 82952,
41+
"node_id": "MDQ6VXNlcjgyOTUy",
42+
"avatar_url": "https://avatars.githubusercontent.com/u/82952?v=4",
43+
"gravatar_id": "",
44+
"url": "https://api.github.com/users/vigo",
45+
"html_url": "https://github.com/vigo",
46+
"followers_url": "https://api.github.com/users/vigo/followers",
47+
"following_url": "https://api.github.com/users/vigo/following{/other_user}",
48+
"gists_url": "https://api.github.com/users/vigo/gists{/gist_id}",
49+
"starred_url": "https://api.github.com/users/vigo/starred{/owner}{/repo}",
50+
"subscriptions_url": "https://api.github.com/users/vigo/subscriptions",
51+
"organizations_url": "https://api.github.com/users/vigo/orgs",
52+
"repos_url": "https://api.github.com/users/vigo/repos",
53+
"events_url": "https://api.github.com/users/vigo/events{/privacy}",
54+
"received_events_url": "https://api.github.com/users/vigo/received_events",
55+
"type": "User",
56+
"user_view_type": "public",
57+
"site_admin": false
58+
}
59+
}

0 commit comments

Comments
 (0)