Skip to content

Commit

Permalink
fix: Handle github ping webhook event (#7555) (#10082)
Browse files Browse the repository at this point in the history
- Add github ping event handler
- Move webhook data to testdata folder

Signed-off-by: Liam Jarvis <jarviliam@gmail.com>
  • Loading branch information
jarviliam committed Jul 28, 2022
1 parent e3940cd commit b8ad57f
Show file tree
Hide file tree
Showing 11 changed files with 314 additions and 7 deletions.
140 changes: 140 additions & 0 deletions applicationset/utils/testdata/github-ping-event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
{
"zen": "Anything added dilutes everything else.",
"hook_id": 109948940,
"hook": {
"type": "Repository",
"id": 109948940,
"name": "web",
"active": true,
"events": ["*"],
"config": {
"content_type": "json",
"url": "https://smee.io/****************",
"insecure_ssl": "0"
},
"updated_at": "2019-05-15T15:20:49Z",
"created_at": "2019-05-15T15:20:49Z",
"url": "https://api.github.com/repos/Octocoders/Hello-World/hooks/109948940",
"test_url": "https://api.github.com/repos/Octocoders/Hello-World/hooks/109948940/test",
"ping_url": "https://api.github.com/repos/Octocoders/Hello-World/hooks/109948940/pings",
"last_response": {
"code": null,
"status": "unused",
"message": null
}
},
"repository": {
"id": 186853261,
"node_id": "MDEwOlJlcG9zaXRvcnkxODY4NTMyNjE=",
"name": "Hello-World",
"full_name": "Octocoders/Hello-World",
"private": false,
"owner": {
"login": "Octocoders",
"id": 38302899,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjM4MzAyODk5",
"avatar_url": "https://avatars1.githubusercontent.com/u/38302899?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/Octocoders",
"html_url": "https://github.com/Octocoders",
"followers_url": "https://api.github.com/users/Octocoders/followers",
"following_url": "https://api.github.com/users/Octocoders/following{/other_user}",
"gists_url": "https://api.github.com/users/Octocoders/gists{/gist_id}",
"starred_url": "https://api.github.com/users/Octocoders/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/Octocoders/subscriptions",
"organizations_url": "https://api.github.com/users/Octocoders/orgs",
"repos_url": "https://api.github.com/users/Octocoders/repos",
"events_url": "https://api.github.com/users/Octocoders/events{/privacy}",
"received_events_url": "https://api.github.com/users/Octocoders/received_events",
"type": "Organization",
"site_admin": false
},
"html_url": "https://github.com/Octocoders/Hello-World",
"description": null,
"fork": true,
"url": "https://api.github.com/repos/Octocoders/Hello-World",
"forks_url": "https://api.github.com/repos/Octocoders/Hello-World/forks",
"keys_url": "https://api.github.com/repos/Octocoders/Hello-World/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/Octocoders/Hello-World/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/Octocoders/Hello-World/teams",
"hooks_url": "https://api.github.com/repos/Octocoders/Hello-World/hooks",
"issue_events_url": "https://api.github.com/repos/Octocoders/Hello-World/issues/events{/number}",
"events_url": "https://api.github.com/repos/Octocoders/Hello-World/events",
"assignees_url": "https://api.github.com/repos/Octocoders/Hello-World/assignees{/user}",
"branches_url": "https://api.github.com/repos/Octocoders/Hello-World/branches{/branch}",
"tags_url": "https://api.github.com/repos/Octocoders/Hello-World/tags",
"blobs_url": "https://api.github.com/repos/Octocoders/Hello-World/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/Octocoders/Hello-World/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/Octocoders/Hello-World/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/Octocoders/Hello-World/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/Octocoders/Hello-World/statuses/{sha}",
"languages_url": "https://api.github.com/repos/Octocoders/Hello-World/languages",
"stargazers_url": "https://api.github.com/repos/Octocoders/Hello-World/stargazers",
"contributors_url": "https://api.github.com/repos/Octocoders/Hello-World/contributors",
"subscribers_url": "https://api.github.com/repos/Octocoders/Hello-World/subscribers",
"subscription_url": "https://api.github.com/repos/Octocoders/Hello-World/subscription",
"commits_url": "https://api.github.com/repos/Octocoders/Hello-World/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/Octocoders/Hello-World/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/Octocoders/Hello-World/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/Octocoders/Hello-World/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/Octocoders/Hello-World/contents/{+path}",
"compare_url": "https://api.github.com/repos/Octocoders/Hello-World/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/Octocoders/Hello-World/merges",
"archive_url": "https://api.github.com/repos/Octocoders/Hello-World/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/Octocoders/Hello-World/downloads",
"issues_url": "https://api.github.com/repos/Octocoders/Hello-World/issues{/number}",
"pulls_url": "https://api.github.com/repos/Octocoders/Hello-World/pulls{/number}",
"milestones_url": "https://api.github.com/repos/Octocoders/Hello-World/milestones{/number}",
"notifications_url": "https://api.github.com/repos/Octocoders/Hello-World/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/Octocoders/Hello-World/labels{/name}",
"releases_url": "https://api.github.com/repos/Octocoders/Hello-World/releases{/id}",
"deployments_url": "https://api.github.com/repos/Octocoders/Hello-World/deployments",
"created_at": "2019-05-15T15:20:42Z",
"updated_at": "2019-05-15T15:20:45Z",
"pushed_at": "2019-05-15T15:20:33Z",
"git_url": "git://github.com/Octocoders/Hello-World.git",
"ssh_url": "git@github.com:Octocoders/Hello-World.git",
"clone_url": "https://github.com/Octocoders/Hello-World.git",
"svn_url": "https://github.com/Octocoders/Hello-World",
"homepage": null,
"size": 0,
"stargazers_count": 0,
"watchers_count": 0,
"language": "Ruby",
"has_issues": false,
"has_projects": true,
"has_downloads": true,
"has_wiki": true,
"has_pages": false,
"forks_count": 0,
"mirror_url": null,
"archived": false,
"disabled": false,
"open_issues_count": 0,
"license": null,
"forks": 0,
"open_issues": 0,
"watchers": 0,
"default_branch": "master"
},
"sender": {
"login": "Codertocat",
"id": 21031067,
"node_id": "MDQ6VXNlcjIxMDMxMDY3",
"avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/Codertocat",
"html_url": "https://github.com/Codertocat",
"followers_url": "https://api.github.com/users/Codertocat/followers",
"following_url": "https://api.github.com/users/Codertocat/following{/other_user}",
"gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions",
"organizations_url": "https://api.github.com/users/Codertocat/orgs",
"repos_url": "https://api.github.com/users/Codertocat/repos",
"events_url": "https://api.github.com/users/Codertocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/Codertocat/received_events",
"type": "User",
"site_admin": false
}
}
2 changes: 1 addition & 1 deletion applicationset/utils/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (h *WebhookHandler) Handler(w http.ResponseWriter, r *http.Request) {

switch {
case r.Header.Get("X-GitHub-Event") != "":
payload, err = h.github.Parse(r, github.PushEvent, github.PullRequestEvent)
payload, err = h.github.Parse(r, github.PushEvent, github.PullRequestEvent, github.PingEvent)
case r.Header.Get("X-Gitlab-Event") != "":
payload, err = h.gitlab.Parse(r, gitlab.PushEvents, gitlab.TagEvents)
default:
Expand Down
9 changes: 9 additions & 0 deletions applicationset/utils/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ func TestWebhookHandler(t *testing.T) {
expectedStatusCode: http.StatusOK,
expectedRefresh: true,
},
{
desc: "WebHook from a GitHub ping event",
headerKey: "X-GitHub-Event",
headerValue: "ping",
payloadFile: "github-ping-event.json",
effectedAppSets: []string{"git-github"},
expectedStatusCode: http.StatusOK,
expectedRefresh: false,
},
{
desc: "WebHook from a GitLab repository via Commit",
headerKey: "X-Gitlab-Event",
Expand Down
File renamed without changes.
File renamed without changes.
142 changes: 142 additions & 0 deletions util/webhook/testdata/github-ping-event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
{
"zen": "Anything added dilutes everything else.",
"hook_id": 109948940,
"hook": {
"type": "Repository",
"id": 109948940,
"name": "web",
"active": true,
"events": [
"*"
],
"config": {
"content_type": "json",
"url": "https://smee.io/****************",
"insecure_ssl": "0"
},
"updated_at": "2019-05-15T15:20:49Z",
"created_at": "2019-05-15T15:20:49Z",
"url": "https://api.github.com/repos/Octocoders/Hello-World/hooks/109948940",
"test_url": "https://api.github.com/repos/Octocoders/Hello-World/hooks/109948940/test",
"ping_url": "https://api.github.com/repos/Octocoders/Hello-World/hooks/109948940/pings",
"last_response": {
"code": null,
"status": "unused",
"message": null
}
},
"repository": {
"id": 186853261,
"node_id": "MDEwOlJlcG9zaXRvcnkxODY4NTMyNjE=",
"name": "Hello-World",
"full_name": "Octocoders/Hello-World",
"private": false,
"owner": {
"login": "Octocoders",
"id": 38302899,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjM4MzAyODk5",
"avatar_url": "https://avatars1.githubusercontent.com/u/38302899?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/Octocoders",
"html_url": "https://github.com/Octocoders",
"followers_url": "https://api.github.com/users/Octocoders/followers",
"following_url": "https://api.github.com/users/Octocoders/following{/other_user}",
"gists_url": "https://api.github.com/users/Octocoders/gists{/gist_id}",
"starred_url": "https://api.github.com/users/Octocoders/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/Octocoders/subscriptions",
"organizations_url": "https://api.github.com/users/Octocoders/orgs",
"repos_url": "https://api.github.com/users/Octocoders/repos",
"events_url": "https://api.github.com/users/Octocoders/events{/privacy}",
"received_events_url": "https://api.github.com/users/Octocoders/received_events",
"type": "Organization",
"site_admin": false
},
"html_url": "https://github.com/Octocoders/Hello-World",
"description": null,
"fork": true,
"url": "https://api.github.com/repos/Octocoders/Hello-World",
"forks_url": "https://api.github.com/repos/Octocoders/Hello-World/forks",
"keys_url": "https://api.github.com/repos/Octocoders/Hello-World/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/Octocoders/Hello-World/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/Octocoders/Hello-World/teams",
"hooks_url": "https://api.github.com/repos/Octocoders/Hello-World/hooks",
"issue_events_url": "https://api.github.com/repos/Octocoders/Hello-World/issues/events{/number}",
"events_url": "https://api.github.com/repos/Octocoders/Hello-World/events",
"assignees_url": "https://api.github.com/repos/Octocoders/Hello-World/assignees{/user}",
"branches_url": "https://api.github.com/repos/Octocoders/Hello-World/branches{/branch}",
"tags_url": "https://api.github.com/repos/Octocoders/Hello-World/tags",
"blobs_url": "https://api.github.com/repos/Octocoders/Hello-World/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/Octocoders/Hello-World/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/Octocoders/Hello-World/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/Octocoders/Hello-World/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/Octocoders/Hello-World/statuses/{sha}",
"languages_url": "https://api.github.com/repos/Octocoders/Hello-World/languages",
"stargazers_url": "https://api.github.com/repos/Octocoders/Hello-World/stargazers",
"contributors_url": "https://api.github.com/repos/Octocoders/Hello-World/contributors",
"subscribers_url": "https://api.github.com/repos/Octocoders/Hello-World/subscribers",
"subscription_url": "https://api.github.com/repos/Octocoders/Hello-World/subscription",
"commits_url": "https://api.github.com/repos/Octocoders/Hello-World/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/Octocoders/Hello-World/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/Octocoders/Hello-World/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/Octocoders/Hello-World/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/Octocoders/Hello-World/contents/{+path}",
"compare_url": "https://api.github.com/repos/Octocoders/Hello-World/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/Octocoders/Hello-World/merges",
"archive_url": "https://api.github.com/repos/Octocoders/Hello-World/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/Octocoders/Hello-World/downloads",
"issues_url": "https://api.github.com/repos/Octocoders/Hello-World/issues{/number}",
"pulls_url": "https://api.github.com/repos/Octocoders/Hello-World/pulls{/number}",
"milestones_url": "https://api.github.com/repos/Octocoders/Hello-World/milestones{/number}",
"notifications_url": "https://api.github.com/repos/Octocoders/Hello-World/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/Octocoders/Hello-World/labels{/name}",
"releases_url": "https://api.github.com/repos/Octocoders/Hello-World/releases{/id}",
"deployments_url": "https://api.github.com/repos/Octocoders/Hello-World/deployments",
"created_at": "2019-05-15T15:20:42Z",
"updated_at": "2019-05-15T15:20:45Z",
"pushed_at": "2019-05-15T15:20:33Z",
"git_url": "git://github.com/Octocoders/Hello-World.git",
"ssh_url": "git@github.com:Octocoders/Hello-World.git",
"clone_url": "https://github.com/Octocoders/Hello-World.git",
"svn_url": "https://github.com/Octocoders/Hello-World",
"homepage": null,
"size": 0,
"stargazers_count": 0,
"watchers_count": 0,
"language": "Ruby",
"has_issues": false,
"has_projects": true,
"has_downloads": true,
"has_wiki": true,
"has_pages": false,
"forks_count": 0,
"mirror_url": null,
"archived": false,
"disabled": false,
"open_issues_count": 0,
"license": null,
"forks": 0,
"open_issues": 0,
"watchers": 0,
"default_branch": "master"
},
"sender": {
"login": "Codertocat",
"id": 21031067,
"node_id": "MDQ6VXNlcjIxMDMxMDY3",
"avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/Codertocat",
"html_url": "https://github.com/Codertocat",
"followers_url": "https://api.github.com/users/Codertocat/followers",
"following_url": "https://api.github.com/users/Codertocat/following{/other_user}",
"gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions",
"organizations_url": "https://api.github.com/users/Codertocat/orgs",
"repos_url": "https://api.github.com/users/Codertocat/repos",
"events_url": "https://api.github.com/users/Codertocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/Codertocat/received_events",
"type": "User",
"site_admin": false
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion util/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func (a *ArgoCDWebhookHandler) Handler(w http.ResponseWriter, r *http.Request) {
case r.Header.Get("X-Gogs-Event") != "":
payload, err = a.gogs.Parse(r, gogs.PushEvent)
case r.Header.Get("X-GitHub-Event") != "":
payload, err = a.github.Parse(r, github.PushEvent)
payload, err = a.github.Parse(r, github.PushEvent, github.PingEvent)
case r.Header.Get("X-Gitlab-Event") != "":
payload, err = a.gitlab.Parse(r, gitlab.PushEvents, gitlab.TagEvents)
case r.Header.Get("X-Hook-UUID") != "":
Expand Down
26 changes: 21 additions & 5 deletions util/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestGitHubCommitEvent(t *testing.T) {
h := NewMockHandler()
req := httptest.NewRequest("POST", "/api/webhook", nil)
req.Header.Set("X-GitHub-Event", "push")
eventJSON, err := os.ReadFile("github-commit-event.json")
eventJSON, err := os.ReadFile("testdata/github-commit-event.json")
assert.NoError(t, err)
req.Body = io.NopCloser(bytes.NewReader(eventJSON))
w := httptest.NewRecorder()
Expand All @@ -76,7 +76,7 @@ func TestGitHubTagEvent(t *testing.T) {
h := NewMockHandler()
req := httptest.NewRequest("POST", "/api/webhook", nil)
req.Header.Set("X-GitHub-Event", "push")
eventJSON, err := os.ReadFile("github-tag-event.json")
eventJSON, err := os.ReadFile("testdata/github-tag-event.json")
assert.NoError(t, err)
req.Body = io.NopCloser(bytes.NewReader(eventJSON))
w := httptest.NewRecorder()
Expand All @@ -87,12 +87,28 @@ func TestGitHubTagEvent(t *testing.T) {
hook.Reset()
}

func TestGitHubPingEvent(t *testing.T) {
hook := test.NewGlobal()
h := NewMockHandler()
req := httptest.NewRequest("POST", "/api/webhook", nil)
req.Header.Set("X-GitHub-Event", "ping")
eventJSON, err := os.ReadFile("testdata/github-ping-event.json")
assert.NoError(t, err)
req.Body = io.NopCloser(bytes.NewReader(eventJSON))
w := httptest.NewRecorder()
h.Handler(w, req)
assert.Equal(t, w.Code, http.StatusOK)
expectedLogResult := "Ignoring webhook event"
assert.Equal(t, expectedLogResult, hook.LastEntry().Message)
hook.Reset()
}

func TestBitbucketServerRepositoryReferenceChangedEvent(t *testing.T) {
hook := test.NewGlobal()
h := NewMockHandler()
req := httptest.NewRequest("POST", "/api/webhook", nil)
req.Header.Set("X-Event-Key", "repo:refs_changed")
eventJSON, err := os.ReadFile("bitbucket-server-event.json")
eventJSON, err := os.ReadFile("testdata/bitbucket-server-event.json")
assert.NoError(t, err)
req.Body = io.NopCloser(bytes.NewReader(eventJSON))
w := httptest.NewRecorder()
Expand Down Expand Up @@ -124,7 +140,7 @@ func TestGogsPushEvent(t *testing.T) {
h := NewMockHandler()
req := httptest.NewRequest("POST", "/api/webhook", nil)
req.Header.Set("X-Gogs-Event", "push")
eventJSON, err := os.ReadFile("gogs-event.json")
eventJSON, err := os.ReadFile("testdata/gogs-event.json")
assert.NoError(t, err)
req.Body = io.NopCloser(bytes.NewReader(eventJSON))
w := httptest.NewRecorder()
Expand All @@ -140,7 +156,7 @@ func TestGitLabPushEvent(t *testing.T) {
h := NewMockHandler()
req := httptest.NewRequest("POST", "/api/webhook", nil)
req.Header.Set("X-Gitlab-Event", "Push Hook")
eventJSON, err := os.ReadFile("gitlab-event.json")
eventJSON, err := os.ReadFile("testdata/gitlab-event.json")
assert.NoError(t, err)
req.Body = io.NopCloser(bytes.NewReader(eventJSON))
w := httptest.NewRecorder()
Expand Down

0 comments on commit b8ad57f

Please sign in to comment.