Skip to content

Commit cfa5365

Browse files
authored
feat: add GitHub star webhook (#1)
1 parent 02d182a commit cfa5365

File tree

5 files changed

+463
-0
lines changed

5 files changed

+463
-0
lines changed

github/github.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ const (
6666
RepositoryEvent Event = "repository"
6767
RepositoryVulnerabilityAlertEvent Event = "repository_vulnerability_alert"
6868
SecurityAdvisoryEvent Event = "security_advisory"
69+
StarEvent Event = "star"
6970
StatusEvent Event = "status"
7071
TeamEvent Event = "team"
7172
TeamAddEvent Event = "team_add"
@@ -322,6 +323,10 @@ func (hook Webhook) Parse(r *http.Request, events ...Event) (interface{}, error)
322323
var pl SecurityAdvisoryPayload
323324
err = json.Unmarshal([]byte(payload), &pl)
324325
return pl, err
326+
case StarEvent:
327+
var pl StarPayload
328+
err = json.Unmarshal([]byte(payload), &pl)
329+
return pl, err
325330
case StatusEvent:
326331
var pl StatusPayload
327332
err = json.Unmarshal([]byte(payload), &pl)

github/github_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,24 @@ func TestWebhooks(t *testing.T) {
483483
"X-Github-Event": []string{"security_advisory"},
484484
},
485485
},
486+
{
487+
name: "StarEvent",
488+
event: StarEvent,
489+
typ: StarPayload{},
490+
filename: "../testdata/github/star.json",
491+
headers: http.Header{
492+
"X-Github-Event": []string{"star"},
493+
},
494+
},
495+
{
496+
name: "StarEventDeleted",
497+
event: StarEvent,
498+
typ: StarPayload{},
499+
filename: "../testdata/github/star-deleted.json",
500+
headers: http.Header{
501+
"X-Github-Event": []string{"star"},
502+
},
503+
},
486504
{
487505
name: "StatusEvent",
488506
event: StatusEvent,

github/payload.go

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5858,6 +5858,158 @@ type SecurityAdvisoryPayload struct {
58585858
} `json:"security_advisory"`
58595859
}
58605860

5861+
// StarPayload contains the information for GitHub's star hook event
5862+
type StarPayload struct {
5863+
Action string `json:"action"`
5864+
StarredAt *time.Time `json:"starred_at"`
5865+
Repository struct {
5866+
ID int64 `json:"id"`
5867+
NodeID string `json:"node_id"`
5868+
Name string `json:"name"`
5869+
FullName string `json:"full_name"`
5870+
Private bool `json:"private"`
5871+
Owner struct {
5872+
Login string `json:"login"`
5873+
ID int64 `json:"id"`
5874+
NodeID string `json:"node_id"`
5875+
AvatarURL string `json:"avatar_url"`
5876+
GravatarID string `json:"gravatar_id"`
5877+
URL string `json:"url"`
5878+
HTMLURL string `json:"html_url"`
5879+
FollowersURL string `json:"followers_url"`
5880+
FollowingURL string `json:"following_url"`
5881+
GistsURL string `json:"gists_url"`
5882+
StarredURL string `json:"starred_url"`
5883+
SubscriptionsURL string `json:"subscriptions_url"`
5884+
OrganizationsURL string `json:"organizations_url"`
5885+
ReposURL string `json:"repos_url"`
5886+
EventsURL string `json:"events_url"`
5887+
ReceivedEventsURL string `json:"received_events_url"`
5888+
Type string `json:"type"`
5889+
SiteAdmin bool `json:"site_admin"`
5890+
} `json:"owner"`
5891+
HTMLURL string `json:"html_url"`
5892+
Description string `json:"description"`
5893+
Fork bool `json:"fork"`
5894+
URL string `json:"url"`
5895+
ForksURL string `json:"forks_url"`
5896+
KeysURL string `json:"keys_url"`
5897+
CollaboratorsURL string `json:"collaborators_url"`
5898+
TeamsURL string `json:"teams_url"`
5899+
HooksURL string `json:"hooks_url"`
5900+
IssueEventsURL string `json:"issue_events_url"`
5901+
EventsURL string `json:"events_url"`
5902+
AssigneesURL string `json:"assignees_url"`
5903+
BranchesURL string `json:"branches_url"`
5904+
TagsURL string `json:"tags_url"`
5905+
BlobsURL string `json:"blobs_url"`
5906+
GitTagsURL string `json:"git_tags_url"`
5907+
GitRefsURL string `json:"git_refs_url"`
5908+
TreesURL string `json:"trees_url"`
5909+
StatusesURL string `json:"statuses_url"`
5910+
LanguagesURL string `json:"languages_url"`
5911+
StargazersURL string `json:"stargazers_url"`
5912+
ContributorsURL string `json:"contributors_url"`
5913+
SubscribersURL string `json:"subscribers_url"`
5914+
SubscriptionURL string `json:"subscription_url"`
5915+
CommitsURL string `json:"commits_url"`
5916+
GitCommitsURL string `json:"git_commits_url"`
5917+
CommentsURL string `json:"comments_url"`
5918+
IssueCommentURL string `json:"issue_comment_url"`
5919+
ContentsURL string `json:"contents_url"`
5920+
CompareURL string `json:"compare_url"`
5921+
MergesURL string `json:"merges_url"`
5922+
ArchiveURL string `json:"archive_url"`
5923+
DownloadsURL string `json:"downloads_url"`
5924+
IssuesURL string `json:"issues_url"`
5925+
PullsURL string `json:"pulls_url"`
5926+
MilestonesURL string `json:"milestones_url"`
5927+
NotificationsURL string `json:"notifications_url"`
5928+
LabelsURL string `json:"labels_url"`
5929+
ReleasesURL string `json:"releases_url"`
5930+
DeploymentsURL string `json:"deployments_url"`
5931+
CreatedAt time.Time `json:"created_at"`
5932+
UpdatedAt time.Time `json:"updated_at"`
5933+
PushedAt time.Time `json:"pushed_at"`
5934+
GitURL string `json:"git_url"`
5935+
SshURL string `json:"ssh_url"`
5936+
CloneURL string `json:"clone_url"`
5937+
SvnURL string `json:"svn_url"`
5938+
Homepage string `json:"homepage"`
5939+
Size int `json:"size"`
5940+
StargazersCount int `json:"stargazers_count"`
5941+
WatchersCount int `json:"watchers_count"`
5942+
Language string `json:"language"`
5943+
HasIssues bool `json:"has_issues"`
5944+
HasProjects bool `json:"has_projects"`
5945+
HasDownloads bool `json:"has_downloads"`
5946+
HasWiki bool `json:"has_wiki"`
5947+
HasPages bool `json:"has_pages"`
5948+
HasDiscussions bool `json:"has_discussions"`
5949+
ForksCount int `json:"forks_count"`
5950+
MirrorURL interface{} `json:"mirror_url"`
5951+
Archived bool `json:"archived"`
5952+
Disabled bool `json:"disabled"`
5953+
OpenIssuesCount int `json:"open_issues_count"`
5954+
License interface{} `json:"license"`
5955+
AllowForking bool `json:"allow_forking"`
5956+
IsTemplate bool `json:"is_template"`
5957+
WebCommitSignoffRequired bool `json:"web_commit_signoff_required"`
5958+
Topics []string `json:"topics"`
5959+
Visibility string `json:"visibility"`
5960+
Forks int `json:"forks"`
5961+
OpenIssues int `json:"open_issues"`
5962+
Watchers int `json:"watchers"`
5963+
DefaultBranch string `json:"default_branch"`
5964+
} `json:"repository"`
5965+
Organization struct {
5966+
Login string `json:"login"`
5967+
ID int64 `json:"id"`
5968+
NodeID string `json:"node_id"`
5969+
URL string `json:"url"`
5970+
ReposURL string `json:"repos_url"`
5971+
EventsURL string `json:"events_url"`
5972+
HooksURL string `json:"hooks_url"`
5973+
IssuesURL string `json:"issues_url"`
5974+
MembersURL string `json:"members_url"`
5975+
PublicMembersURL string `json:"public_members_url"`
5976+
AvatarURL string `json:"avatar_url"`
5977+
Description string `json:"description"`
5978+
} `json:"organization"`
5979+
Enterprise *struct {
5980+
ID int64 `json:"id"`
5981+
Slug string `json:"slug"`
5982+
Name string `json:"name"`
5983+
NodeID string `json:"node_id"`
5984+
AvatarURL string `json:"avatar_url"`
5985+
Description string `json:"description"`
5986+
WebsiteURL string `json:"website_url"`
5987+
HtmlURL string `json:"html_url"`
5988+
CreatedAt time.Time `json:"created_at"`
5989+
UpdatedAt time.Time `json:"updated_at"`
5990+
} `json:"enterprise"`
5991+
Sender struct {
5992+
Login string `json:"login"`
5993+
ID int64 `json:"id"`
5994+
NodeID string `json:"node_id"`
5995+
AvatarURL string `json:"avatar_url"`
5996+
GravatarID string `json:"gravatar_id"`
5997+
URL string `json:"url"`
5998+
HTMLURL string `json:"html_url"`
5999+
FollowersURL string `json:"followers_url"`
6000+
FollowingURL string `json:"following_url"`
6001+
GistsURL string `json:"gists_url"`
6002+
StarredURL string `json:"starred_url"`
6003+
SubscriptionsURL string `json:"subscriptions_url"`
6004+
OrganizationsURL string `json:"organizations_url"`
6005+
ReposURL string `json:"repos_url"`
6006+
EventsURL string `json:"events_url"`
6007+
ReceivedEventsURL string `json:"received_events_url"`
6008+
Type string `json:"type"`
6009+
SiteAdmin bool `json:"site_admin"`
6010+
} `json:"sender"`
6011+
}
6012+
58616013
// StatusPayload contains the information for GitHub's status hook event
58626014
type StatusPayload struct {
58636015
ID int64 `json:"id"`

testdata/github/star-deleted.json

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
{
2+
"action": "deleted",
3+
"starred_at": null,
4+
"repository": {
5+
"id": 906427850,
6+
"node_id": "R_kgDONgb9yg",
7+
"name": "devchain-webhook-test-repo",
8+
"full_name": "bilusteknoloji/devchain-webhook-test-repo",
9+
"private": true,
10+
"owner": {
11+
"login": "bilusteknoloji",
12+
"id": 159630054,
13+
"node_id": "O_kgDOCYPC5g",
14+
"avatar_url": "https://avatars.githubusercontent.com/u/159630054?v=4",
15+
"gravatar_id": "",
16+
"url": "https://api.github.com/users/bilusteknoloji",
17+
"html_url": "https://github.com/bilusteknoloji",
18+
"followers_url": "https://api.github.com/users/bilusteknoloji/followers",
19+
"following_url": "https://api.github.com/users/bilusteknoloji/following{/other_user}",
20+
"gists_url": "https://api.github.com/users/bilusteknoloji/gists{/gist_id}",
21+
"starred_url": "https://api.github.com/users/bilusteknoloji/starred{/owner}{/repo}",
22+
"subscriptions_url": "https://api.github.com/users/bilusteknoloji/subscriptions",
23+
"organizations_url": "https://api.github.com/users/bilusteknoloji/orgs",
24+
"repos_url": "https://api.github.com/users/bilusteknoloji/repos",
25+
"events_url": "https://api.github.com/users/bilusteknoloji/events{/privacy}",
26+
"received_events_url": "https://api.github.com/users/bilusteknoloji/received_events",
27+
"type": "Organization",
28+
"user_view_type": "public",
29+
"site_admin": false
30+
},
31+
"html_url": "https://github.com/bilusteknoloji/devchain-webhook-test-repo",
32+
"description": "This is a dummy repo for testing github webhook payload + events",
33+
"fork": false,
34+
"url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo",
35+
"forks_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/forks",
36+
"keys_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/keys{/key_id}",
37+
"collaborators_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/collaborators{/collaborator}",
38+
"teams_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/teams",
39+
"hooks_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/hooks",
40+
"issue_events_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/issues/events{/number}",
41+
"events_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/events",
42+
"assignees_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/assignees{/user}",
43+
"branches_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/branches{/branch}",
44+
"tags_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/tags",
45+
"blobs_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/git/blobs{/sha}",
46+
"git_tags_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/git/tags{/sha}",
47+
"git_refs_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/git/refs{/sha}",
48+
"trees_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/git/trees{/sha}",
49+
"statuses_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/statuses/{sha}",
50+
"languages_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/languages",
51+
"stargazers_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/stargazers",
52+
"contributors_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/contributors",
53+
"subscribers_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/subscribers",
54+
"subscription_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/subscription",
55+
"commits_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/commits{/sha}",
56+
"git_commits_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/git/commits{/sha}",
57+
"comments_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/comments{/number}",
58+
"issue_comment_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/issues/comments{/number}",
59+
"contents_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/contents/{+path}",
60+
"compare_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/compare/{base}...{head}",
61+
"merges_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/merges",
62+
"archive_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/{archive_format}{/ref}",
63+
"downloads_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/downloads",
64+
"issues_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/issues{/number}",
65+
"pulls_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/pulls{/number}",
66+
"milestones_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/milestones{/number}",
67+
"notifications_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/notifications{?since,all,participating}",
68+
"labels_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/labels{/name}",
69+
"releases_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/releases{/id}",
70+
"deployments_url": "https://api.github.com/repos/bilusteknoloji/devchain-webhook-test-repo/deployments",
71+
"created_at": "2024-12-20T22:20:56Z",
72+
"updated_at": "2025-01-10T17:24:56Z",
73+
"pushed_at": "2025-01-09T10:09:35Z",
74+
"git_url": "git://github.com/bilusteknoloji/devchain-webhook-test-repo.git",
75+
"ssh_url": "git@github.com:bilusteknoloji/devchain-webhook-test-repo.git",
76+
"clone_url": "https://github.com/bilusteknoloji/devchain-webhook-test-repo.git",
77+
"svn_url": "https://github.com/bilusteknoloji/devchain-webhook-test-repo",
78+
"homepage": null,
79+
"size": 10,
80+
"stargazers_count": 1,
81+
"watchers_count": 1,
82+
"language": null,
83+
"has_issues": true,
84+
"has_projects": false,
85+
"has_downloads": true,
86+
"has_wiki": false,
87+
"has_pages": false,
88+
"has_discussions": false,
89+
"forks_count": 0,
90+
"mirror_url": null,
91+
"archived": false,
92+
"disabled": false,
93+
"open_issues_count": 8,
94+
"license": null,
95+
"allow_forking": false,
96+
"is_template": false,
97+
"web_commit_signoff_required": false,
98+
"topics": [
99+
100+
],
101+
"visibility": "private",
102+
"forks": 0,
103+
"open_issues": 8,
104+
"watchers": 1,
105+
"default_branch": "main",
106+
"custom_properties": {
107+
}
108+
},
109+
"organization": {
110+
"login": "bilusteknoloji",
111+
"id": 159630054,
112+
"node_id": "O_kgDOCYPC5g",
113+
"url": "https://api.github.com/orgs/bilusteknoloji",
114+
"repos_url": "https://api.github.com/orgs/bilusteknoloji/repos",
115+
"events_url": "https://api.github.com/orgs/bilusteknoloji/events",
116+
"hooks_url": "https://api.github.com/orgs/bilusteknoloji/hooks",
117+
"issues_url": "https://api.github.com/orgs/bilusteknoloji/issues",
118+
"members_url": "https://api.github.com/orgs/bilusteknoloji/members{/member}",
119+
"public_members_url": "https://api.github.com/orgs/bilusteknoloji/public_members{/member}",
120+
"avatar_url": "https://avatars.githubusercontent.com/u/159630054?v=4",
121+
"description": "Software Development and Consultancy"
122+
},
123+
"sender": {
124+
"login": "vigo",
125+
"id": 82952,
126+
"node_id": "MDQ6VXNlcjgyOTUy",
127+
"avatar_url": "https://avatars.githubusercontent.com/u/82952?v=4",
128+
"gravatar_id": "",
129+
"url": "https://api.github.com/users/vigo",
130+
"html_url": "https://github.com/vigo",
131+
"followers_url": "https://api.github.com/users/vigo/followers",
132+
"following_url": "https://api.github.com/users/vigo/following{/other_user}",
133+
"gists_url": "https://api.github.com/users/vigo/gists{/gist_id}",
134+
"starred_url": "https://api.github.com/users/vigo/starred{/owner}{/repo}",
135+
"subscriptions_url": "https://api.github.com/users/vigo/subscriptions",
136+
"organizations_url": "https://api.github.com/users/vigo/orgs",
137+
"repos_url": "https://api.github.com/users/vigo/repos",
138+
"events_url": "https://api.github.com/users/vigo/events{/privacy}",
139+
"received_events_url": "https://api.github.com/users/vigo/received_events",
140+
"type": "User",
141+
"user_view_type": "public",
142+
"site_admin": false
143+
}
144+
}

0 commit comments

Comments
 (0)