Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ping payload for organization #204

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const (
RepositoryEvent Event = "repository"
RepositoryVulnerabilityAlertEvent Event = "repository_vulnerability_alert"
SecurityAdvisoryEvent Event = "security_advisory"
StarEvent Event = "star"
StatusEvent Event = "status"
TeamEvent Event = "team"
TeamAddEvent Event = "team_add"
Expand All @@ -89,6 +90,15 @@ const (
IssueSubtype EventSubtype = "issues"
)

// InstallationTargetType defines a GitHub Hook HTTP Header type
type InstallationTargetType string

// GitHub hook HTTP header installation target types
const (
InstallationTargetTypeRepository InstallationTargetType = "repository"
InstallationTargetTypeOrganization InstallationTargetType = "organization"
)

// Option is a configuration option for the webhook
type Option func(*Webhook) error

Expand Down Expand Up @@ -177,6 +187,12 @@ func (hook Webhook) Parse(r *http.Request, events ...Event) (interface{}, error)
}
}

targetType := InstallationTargetTypeRepository
instTargetType := InstallationTargetType(r.Header.Get("X-GitHub-Hook-Installation-Target-Type"))
if instTargetType == InstallationTargetTypeOrganization {
targetType = InstallationTargetTypeOrganization
}

switch gitHubEvent {
case CheckRunEvent:
var pl CheckRunPayload
Expand Down Expand Up @@ -271,7 +287,12 @@ func (hook Webhook) Parse(r *http.Request, events ...Event) (interface{}, error)
err = json.Unmarshal([]byte(payload), &pl)
return pl, err
case PingEvent:
var pl PingPayload
if targetType == InstallationTargetTypeRepository {
var pl PingPayload
err = json.Unmarshal([]byte(payload), &pl)
return pl, err
}
var pl PingOrganizationPayload
err = json.Unmarshal([]byte(payload), &pl)
return pl, err
case ProjectCardEvent:
Expand Down Expand Up @@ -322,6 +343,10 @@ func (hook Webhook) Parse(r *http.Request, events ...Event) (interface{}, error)
var pl SecurityAdvisoryPayload
err = json.Unmarshal([]byte(payload), &pl)
return pl, err
case StarEvent:
var pl StarPayload
err = json.Unmarshal([]byte(payload), &pl)
return pl, err
case StatusEvent:
var pl StatusPayload
err = json.Unmarshal([]byte(payload), &pl)
Expand Down
28 changes: 28 additions & 0 deletions github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,16 @@ func TestWebhooks(t *testing.T) {
"X-Github-Event": []string{"ping"},
},
},
{
name: "PingEvent",
event: PingEvent,
typ: PingOrganizationPayload{},
filename: "../testdata/github/ping-organization.json",
headers: http.Header{
"X-Github-Event": []string{"ping"},
"X-GitHub-Hook-Installation-Target-Type": []string{"organization"},
},
},
{
name: "ProjectCardEvent",
event: ProjectCardEvent,
Expand Down Expand Up @@ -483,6 +493,24 @@ func TestWebhooks(t *testing.T) {
"X-Github-Event": []string{"security_advisory"},
},
},
{
name: "StarEvent",
event: StarEvent,
typ: StarPayload{},
filename: "../testdata/github/star.json",
headers: http.Header{
"X-Github-Event": []string{"star"},
},
},
{
name: "StarEventDeleted",
event: StarEvent,
typ: StarPayload{},
filename: "../testdata/github/star-deleted.json",
headers: http.Header{
"X-Github-Event": []string{"star"},
},
},
{
name: "StatusEvent",
event: StatusEvent,
Expand Down
210 changes: 210 additions & 0 deletions github/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -3336,6 +3336,64 @@ type PingPayload struct {
} `json:"sender"`
}

// PingOrganizationPayload contains the information for GitHub's organization ping hook event
type PingOrganizationPayload struct {
Zen string `json:"zen"`
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"`
Config struct {
ContentType string `json:"content_type"`
InsecureSSL string `json:"insecure_ssl"`
Secret string `json:"secret"`
URL string `json:"url"`
} `json:"config"`
UpdatedAt time.Time `json:"updated_at"`
CreatedAt time.Time `json:"created_at"`
URL string `json:"url"`
PingURL string `json:"ping_url"`
DeliveriesURL string `json:"deliveries_url"`
} `json:"hook"`
Organization struct {
Login string `json:"login"`
ID int64 `json:"id"`
NodeID string `json:"node_id"`
URL string `json:"url"`
ReposURL string `json:"repos_url"`
EventsURL string `json:"events_url"`
HooksURL string `json:"hooks_url"`
IssuesURL string `json:"issues_url"`
MembersURL string `json:"members_url"`
PublicMembersURL string `json:"public_members_url"`
AvatarURL string `json:"avatar_url"`
Description string `json:"description"`
} `json:"organization"`
Sender struct {
Login string `json:"login"`
ID int64 `json:"id"`
NodeID string `json:"node_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"`
}

// ProjectCardPayload contains the information for GitHub's project_payload hook event
type ProjectCardPayload struct {
Action string `json:"action"`
Expand Down Expand Up @@ -5858,6 +5916,158 @@ type SecurityAdvisoryPayload struct {
} `json:"security_advisory"`
}

// StarPayload contains the information for GitHub's star hook event
type StarPayload struct {
Action string `json:"action"`
StarredAt *time.Time `json:"starred_at"`
Repository struct {
ID int64 `json:"id"`
NodeID string `json:"node_id"`
Name string `json:"name"`
FullName string `json:"full_name"`
Private bool `json:"private"`
Owner struct {
Login string `json:"login"`
ID int64 `json:"id"`
NodeID string `json:"node_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:"owner"`
HTMLURL string `json:"html_url"`
Description string `json:"description"`
Fork bool `json:"fork"`
URL string `json:"url"`
ForksURL string `json:"forks_url"`
KeysURL string `json:"keys_url"`
CollaboratorsURL string `json:"collaborators_url"`
TeamsURL string `json:"teams_url"`
HooksURL string `json:"hooks_url"`
IssueEventsURL string `json:"issue_events_url"`
EventsURL string `json:"events_url"`
AssigneesURL string `json:"assignees_url"`
BranchesURL string `json:"branches_url"`
TagsURL string `json:"tags_url"`
BlobsURL string `json:"blobs_url"`
GitTagsURL string `json:"git_tags_url"`
GitRefsURL string `json:"git_refs_url"`
TreesURL string `json:"trees_url"`
StatusesURL string `json:"statuses_url"`
LanguagesURL string `json:"languages_url"`
StargazersURL string `json:"stargazers_url"`
ContributorsURL string `json:"contributors_url"`
SubscribersURL string `json:"subscribers_url"`
SubscriptionURL string `json:"subscription_url"`
CommitsURL string `json:"commits_url"`
GitCommitsURL string `json:"git_commits_url"`
CommentsURL string `json:"comments_url"`
IssueCommentURL string `json:"issue_comment_url"`
ContentsURL string `json:"contents_url"`
CompareURL string `json:"compare_url"`
MergesURL string `json:"merges_url"`
ArchiveURL string `json:"archive_url"`
DownloadsURL string `json:"downloads_url"`
IssuesURL string `json:"issues_url"`
PullsURL string `json:"pulls_url"`
MilestonesURL string `json:"milestones_url"`
NotificationsURL string `json:"notifications_url"`
LabelsURL string `json:"labels_url"`
ReleasesURL string `json:"releases_url"`
DeploymentsURL string `json:"deployments_url"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
PushedAt time.Time `json:"pushed_at"`
GitURL string `json:"git_url"`
SshURL string `json:"ssh_url"`
CloneURL string `json:"clone_url"`
SvnURL string `json:"svn_url"`
Homepage string `json:"homepage"`
Size int `json:"size"`
StargazersCount int `json:"stargazers_count"`
WatchersCount int `json:"watchers_count"`
Language string `json:"language"`
HasIssues bool `json:"has_issues"`
HasProjects bool `json:"has_projects"`
HasDownloads bool `json:"has_downloads"`
HasWiki bool `json:"has_wiki"`
HasPages bool `json:"has_pages"`
HasDiscussions bool `json:"has_discussions"`
ForksCount int `json:"forks_count"`
MirrorURL interface{} `json:"mirror_url"`
Archived bool `json:"archived"`
Disabled bool `json:"disabled"`
OpenIssuesCount int `json:"open_issues_count"`
License interface{} `json:"license"`
AllowForking bool `json:"allow_forking"`
IsTemplate bool `json:"is_template"`
WebCommitSignoffRequired bool `json:"web_commit_signoff_required"`
Topics []string `json:"topics"`
Visibility string `json:"visibility"`
Forks int `json:"forks"`
OpenIssues int `json:"open_issues"`
Watchers int `json:"watchers"`
DefaultBranch string `json:"default_branch"`
} `json:"repository"`
Organization struct {
Login string `json:"login"`
ID int64 `json:"id"`
NodeID string `json:"node_id"`
URL string `json:"url"`
ReposURL string `json:"repos_url"`
EventsURL string `json:"events_url"`
HooksURL string `json:"hooks_url"`
IssuesURL string `json:"issues_url"`
MembersURL string `json:"members_url"`
PublicMembersURL string `json:"public_members_url"`
AvatarURL string `json:"avatar_url"`
Description string `json:"description"`
} `json:"organization"`
Enterprise *struct {
ID int64 `json:"id"`
Slug string `json:"slug"`
Name string `json:"name"`
NodeID string `json:"node_id"`
AvatarURL string `json:"avatar_url"`
Description string `json:"description"`
WebsiteURL string `json:"website_url"`
HtmlURL string `json:"html_url"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
} `json:"enterprise"`
Sender struct {
Login string `json:"login"`
ID int64 `json:"id"`
NodeID string `json:"node_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"`
}

// StatusPayload contains the information for GitHub's status hook event
type StatusPayload struct {
ID int64 `json:"id"`
Expand Down
59 changes: 59 additions & 0 deletions testdata/github/ping-organization.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"zen": "Avoid administrative distraction.",
"hook_id": 523968606,
"hook": {
"type": "Organization",
"id": 523968606,
"name": "web",
"active": true,
"events": [
"*"
],
"config": {
"content_type": "json",
"insecure_ssl": "0",
"secret": "********",
"url": "https://github.devchain.network/v1/webhook/github"
},
"updated_at": "2025-01-13T07:08:59Z",
"created_at": "2025-01-13T07:08:59Z",
"url": "https://api.github.com/orgs/vbyazilim/hooks/523968606",
"ping_url": "https://api.github.com/orgs/vbyazilim/hooks/523968606/pings",
"deliveries_url": "https://api.github.com/orgs/vbyazilim/hooks/523968606/deliveries"
},
"organization": {
"login": "vbyazilim",
"id": 34422235,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjM0NDIyMjM1",
"url": "https://api.github.com/orgs/vbyazilim",
"repos_url": "https://api.github.com/orgs/vbyazilim/repos",
"events_url": "https://api.github.com/orgs/vbyazilim/events",
"hooks_url": "https://api.github.com/orgs/vbyazilim/hooks",
"issues_url": "https://api.github.com/orgs/vbyazilim/issues",
"members_url": "https://api.github.com/orgs/vbyazilim/members{/member}",
"public_members_url": "https://api.github.com/orgs/vbyazilim/public_members{/member}",
"avatar_url": "https://avatars.githubusercontent.com/u/34422235?v=4",
"description": "Software Development, Training and Consultancy"
},
"sender": {
"login": "vigo",
"id": 82952,
"node_id": "MDQ6VXNlcjgyOTUy",
"avatar_url": "https://avatars.githubusercontent.com/u/82952?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/vigo",
"html_url": "https://github.com/vigo",
"followers_url": "https://api.github.com/users/vigo/followers",
"following_url": "https://api.github.com/users/vigo/following{/other_user}",
"gists_url": "https://api.github.com/users/vigo/gists{/gist_id}",
"starred_url": "https://api.github.com/users/vigo/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/vigo/subscriptions",
"organizations_url": "https://api.github.com/users/vigo/orgs",
"repos_url": "https://api.github.com/users/vigo/repos",
"events_url": "https://api.github.com/users/vigo/events{/privacy}",
"received_events_url": "https://api.github.com/users/vigo/received_events",
"type": "User",
"user_view_type": "public",
"site_admin": false
}
}
Loading
Loading