From c166abc5315baf677f7065ea004266d40eb724f8 Mon Sep 17 00:00:00 2001 From: ecrupper Date: Fri, 15 Apr 2022 10:40:26 -0500 Subject: [PATCH 1/2] set webhook_id in process webhook function --- scm/github/webhook.go | 9 +++++++++ scm/github/webhook_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/scm/github/webhook.go b/scm/github/webhook.go index 38c9bca28..1f61c359b 100644 --- a/scm/github/webhook.go +++ b/scm/github/webhook.go @@ -8,6 +8,7 @@ import ( "encoding/json" "fmt" "net/http" + "strconv" "strings" "time" @@ -27,6 +28,14 @@ func (c *client) ProcessWebhook(request *http.Request) (*types.Webhook, error) { h := new(library.Hook) h.SetNumber(1) h.SetSourceID(request.Header.Get("X-GitHub-Delivery")) + + hookID, err := strconv.Atoi(request.Header.Get("X-GitHub-Hook-ID")) + if err != nil { + retErr := fmt.Errorf("unable to convert hook id to int64: %w", err) + return nil, retErr + } + + h.SetWebhookID(int64(hookID)) h.SetCreated(time.Now().UTC().Unix()) h.SetHost("github.com") h.SetEvent(request.Header.Get("X-GitHub-Event")) diff --git a/scm/github/webhook_test.go b/scm/github/webhook_test.go index 3ae551d33..98aab8daa 100644 --- a/scm/github/webhook_test.go +++ b/scm/github/webhook_test.go @@ -39,6 +39,7 @@ func TestGithub_ProcessWebhook_Push(t *testing.T) { request.Header.Set("Content-Type", "application/json") request.Header.Set("User-Agent", "GitHub-Hookshot/a22606a") request.Header.Set("X-GitHub-Delivery", "7bd477e4-4415-11e9-9359-0d41fdf9567e") + request.Header.Set("X-GitHub-Hook-ID", "123456") request.Header.Set("X-GitHub-Event", "push") // setup client @@ -48,6 +49,7 @@ func TestGithub_ProcessWebhook_Push(t *testing.T) { wantHook := new(library.Hook) wantHook.SetNumber(1) wantHook.SetSourceID("7bd477e4-4415-11e9-9359-0d41fdf9567e") + wantHook.SetWebhookID(123456) wantHook.SetCreated(time.Now().UTC().Unix()) wantHook.SetHost("github.com") wantHook.SetEvent("push") @@ -113,6 +115,7 @@ func TestGithub_ProcessWebhook_Push_NoSender(t *testing.T) { request.Header.Set("Content-Type", "application/json") request.Header.Set("User-Agent", "GitHub-Hookshot/a22606a") request.Header.Set("X-GitHub-Delivery", "7bd477e4-4415-11e9-9359-0d41fdf9567e") + request.Header.Set("X-GitHub-Hook-ID", "123456") request.Header.Set("X-GitHub-Host", "github.com") request.Header.Set("X-GitHub-Version", "2.16.0") request.Header.Set("X-GitHub-Event", "push") @@ -124,6 +127,7 @@ func TestGithub_ProcessWebhook_Push_NoSender(t *testing.T) { wantHook := new(library.Hook) wantHook.SetNumber(1) wantHook.SetSourceID("7bd477e4-4415-11e9-9359-0d41fdf9567e") + wantHook.SetWebhookID(123456) wantHook.SetCreated(time.Now().UTC().Unix()) wantHook.SetHost("github.com") wantHook.SetEvent("push") @@ -189,6 +193,7 @@ func TestGithub_ProcessWebhook_PullRequest(t *testing.T) { request.Header.Set("Content-Type", "application/json") request.Header.Set("User-Agent", "GitHub-Hookshot/a22606a") request.Header.Set("X-GitHub-Delivery", "7bd477e4-4415-11e9-9359-0d41fdf9567e") + request.Header.Set("X-GitHub-Hook-ID", "123456") request.Header.Set("X-GitHub-Host", "github.com") request.Header.Set("X-GitHub-Version", "2.16.0") request.Header.Set("X-GitHub-Event", "pull_request") @@ -200,6 +205,7 @@ func TestGithub_ProcessWebhook_PullRequest(t *testing.T) { wantHook := new(library.Hook) wantHook.SetNumber(1) wantHook.SetSourceID("7bd477e4-4415-11e9-9359-0d41fdf9567e") + wantHook.SetWebhookID(123456) wantHook.SetCreated(time.Now().UTC().Unix()) wantHook.SetHost("github.com") wantHook.SetEvent("pull_request") @@ -267,6 +273,7 @@ func TestGithub_ProcessWebhook_PullRequest_ClosedAction(t *testing.T) { request.Header.Set("Content-Type", "application/json") request.Header.Set("User-Agent", "GitHub-Hookshot/a22606a") request.Header.Set("X-GitHub-Delivery", "7bd477e4-4415-11e9-9359-0d41fdf9567e") + request.Header.Set("X-GitHub-Hook-ID", "123456") request.Header.Set("X-GitHub-Host", "github.com") request.Header.Set("X-GitHub-Version", "2.16.0") request.Header.Set("X-GitHub-Event", "pull_request") @@ -278,6 +285,7 @@ func TestGithub_ProcessWebhook_PullRequest_ClosedAction(t *testing.T) { wantHook := new(library.Hook) wantHook.SetNumber(1) wantHook.SetSourceID("7bd477e4-4415-11e9-9359-0d41fdf9567e") + wantHook.SetWebhookID(123456) wantHook.SetCreated(time.Now().UTC().Unix()) wantHook.SetHost("github.com") wantHook.SetEvent("pull_request") @@ -320,6 +328,7 @@ func TestGithub_ProcessWebhook_PullRequest_ClosedState(t *testing.T) { request.Header.Set("Content-Type", "application/json") request.Header.Set("User-Agent", "GitHub-Hookshot/a22606a") request.Header.Set("X-GitHub-Delivery", "7bd477e4-4415-11e9-9359-0d41fdf9567e") + request.Header.Set("X-GitHub-Hook-ID", "123456") request.Header.Set("X-GitHub-Host", "github.com") request.Header.Set("X-GitHub-Version", "2.16.0") request.Header.Set("X-GitHub-Event", "pull_request") @@ -331,6 +340,7 @@ func TestGithub_ProcessWebhook_PullRequest_ClosedState(t *testing.T) { wantHook := new(library.Hook) wantHook.SetNumber(1) wantHook.SetSourceID("7bd477e4-4415-11e9-9359-0d41fdf9567e") + wantHook.SetWebhookID(123456) wantHook.SetCreated(time.Now().UTC().Unix()) wantHook.SetHost("github.com") wantHook.SetEvent("pull_request") @@ -364,6 +374,7 @@ func TestGithub_ProcessWebhook_Deployment(t *testing.T) { wantHook := new(library.Hook) wantHook.SetNumber(1) wantHook.SetSourceID("7bd477e4-4415-11e9-9359-0d41fdf9567e") + wantHook.SetWebhookID(123456) wantHook.SetCreated(time.Now().UTC().Unix()) wantHook.SetBranch("master") wantHook.SetLink("https://github.com/Codertocat/Hello-World/settings/hooks") @@ -425,6 +436,7 @@ func TestGithub_ProcessWebhook_Deployment(t *testing.T) { request.Header.Set("Content-Type", "application/json") request.Header.Set("User-Agent", "GitHub-Hookshot/a22606a") request.Header.Set("X-GitHub-Delivery", "7bd477e4-4415-11e9-9359-0d41fdf9567e") + request.Header.Set("X-GitHub-Hook-ID", "123456") request.Header.Set("X-GitHub-Host", "github.com") request.Header.Set("X-GitHub-Version", "2.16.0") request.Header.Set("X-GitHub-Event", "deployment") @@ -469,6 +481,7 @@ func TestGithub_ProcessWebhook_Deployment_Commit(t *testing.T) { request.Header.Set("Content-Type", "application/json") request.Header.Set("User-Agent", "GitHub-Hookshot/a22606a") request.Header.Set("X-GitHub-Delivery", "7bd477e4-4415-11e9-9359-0d41fdf9567e") + request.Header.Set("X-GitHub-Hook-ID", "123456") request.Header.Set("X-GitHub-Host", "github.com") request.Header.Set("X-GitHub-Version", "2.16.0") request.Header.Set("X-GitHub-Event", "deployment") @@ -480,6 +493,7 @@ func TestGithub_ProcessWebhook_Deployment_Commit(t *testing.T) { wantHook := new(library.Hook) wantHook.SetNumber(1) wantHook.SetSourceID("7bd477e4-4415-11e9-9359-0d41fdf9567e") + wantHook.SetWebhookID(123456) wantHook.SetCreated(time.Now().UTC().Unix()) wantHook.SetBranch("master") wantHook.SetLink("https://github.com/Codertocat/Hello-World/settings/hooks") @@ -545,6 +559,7 @@ func TestGithub_ProcessWebhook_BadGithubEvent(t *testing.T) { request.Header.Set("Content-Type", "application/json") request.Header.Set("User-Agent", "GitHub-Hookshot/a22606a") request.Header.Set("X-GitHub-Delivery", "7bd477e4-4415-11e9-9359-0d41fdf9567e") + request.Header.Set("X-GitHub-Hook-ID", "123456") request.Header.Set("X-GitHub-Host", "github.com") request.Header.Set("X-GitHub-Version", "2.16.0") request.Header.Set("X-GitHub-Event", "foobar") @@ -556,6 +571,7 @@ func TestGithub_ProcessWebhook_BadGithubEvent(t *testing.T) { wantHook := new(library.Hook) wantHook.SetNumber(1) wantHook.SetSourceID("7bd477e4-4415-11e9-9359-0d41fdf9567e") + wantHook.SetWebhookID(123456) wantHook.SetCreated(time.Now().UTC().Unix()) wantHook.SetHost("github.com") wantHook.SetEvent("foobar") @@ -596,6 +612,7 @@ func TestGithub_ProcessWebhook_BadContentType(t *testing.T) { request.Header.Set("Content-Type", "foobar") request.Header.Set("User-Agent", "GitHub-Hookshot/a22606a") request.Header.Set("X-GitHub-Delivery", "7bd477e4-4415-11e9-9359-0d41fdf9567e") + request.Header.Set("X-GitHub-Hook-ID", "123456") request.Header.Set("X-GitHub-Host", "github.com") request.Header.Set("X-GitHub-Version", "2.16.0") request.Header.Set("X-GitHub-Event", "pull_request") @@ -607,6 +624,7 @@ func TestGithub_ProcessWebhook_BadContentType(t *testing.T) { wantHook := new(library.Hook) wantHook.SetNumber(1) wantHook.SetSourceID("7bd477e4-4415-11e9-9359-0d41fdf9567e") + wantHook.SetWebhookID(123456) wantHook.SetCreated(time.Now().UTC().Unix()) wantHook.SetHost("github.com") wantHook.SetEvent("pull_request") @@ -647,6 +665,7 @@ func TestGithub_VerifyWebhook_EmptyRepo(t *testing.T) { request.Header.Set("Content-Type", "application/json") request.Header.Set("User-Agent", "GitHub-Hookshot/a22606a") request.Header.Set("X-GitHub-Delivery", "7bd477e4-4415-11e9-9359-0d41fdf9567e") + request.Header.Set("X-GitHub-Hook-ID", "123456") request.Header.Set("X-GitHub-Host", "github.com") request.Header.Set("X-GitHub-Version", "2.16.0") request.Header.Set("X-GitHub-Event", "deployment") @@ -687,6 +706,7 @@ func TestGithub_VerifyWebhook_NoSecret(t *testing.T) { request.Header.Set("Content-Type", "application/json") request.Header.Set("User-Agent", "GitHub-Hookshot/a22606a") request.Header.Set("X-GitHub-Delivery", "7bd477e4-4415-11e9-9359-0d41fdf9567e") + request.Header.Set("X-GitHub-Hook-ID", "123456") request.Header.Set("X-GitHub-Host", "github.com") request.Header.Set("X-GitHub-Version", "2.16.0") request.Header.Set("X-GitHub-Event", "push") @@ -718,6 +738,7 @@ func TestGithub_ProcessWebhook_IssueComment_PR(t *testing.T) { request.Header.Set("Content-Type", "application/json") request.Header.Set("User-Agent", "GitHub-Hookshot/a22606a") request.Header.Set("X-GitHub-Delivery", "7bd477e4-4415-11e9-9359-0d41fdf9567e") + request.Header.Set("X-GitHub-Hook-ID", "123456") request.Header.Set("X-GitHub-Host", "github.com") request.Header.Set("X-GitHub-Version", "2.16.0") request.Header.Set("X-GitHub-Event", "issue_comment") @@ -729,6 +750,7 @@ func TestGithub_ProcessWebhook_IssueComment_PR(t *testing.T) { wantHook := new(library.Hook) wantHook.SetNumber(1) wantHook.SetSourceID("7bd477e4-4415-11e9-9359-0d41fdf9567e") + wantHook.SetWebhookID(123456) wantHook.SetCreated(time.Now().UTC().Unix()) wantHook.SetHost("github.com") wantHook.SetEvent("comment") @@ -791,6 +813,7 @@ func TestGithub_ProcessWebhook_IssueComment_Created(t *testing.T) { request.Header.Set("Content-Type", "application/json") request.Header.Set("User-Agent", "GitHub-Hookshot/a22606a") request.Header.Set("X-GitHub-Delivery", "7bd477e4-4415-11e9-9359-0d41fdf9567e") + request.Header.Set("X-GitHub-Hook-ID", "123456") request.Header.Set("X-GitHub-Host", "github.com") request.Header.Set("X-GitHub-Version", "2.16.0") request.Header.Set("X-GitHub-Event", "issue_comment") @@ -802,6 +825,7 @@ func TestGithub_ProcessWebhook_IssueComment_Created(t *testing.T) { wantHook := new(library.Hook) wantHook.SetNumber(1) wantHook.SetSourceID("7bd477e4-4415-11e9-9359-0d41fdf9567e") + wantHook.SetWebhookID(123456) wantHook.SetCreated(time.Now().UTC().Unix()) wantHook.SetHost("github.com") wantHook.SetEvent("comment") @@ -864,6 +888,7 @@ func TestGithub_ProcessWebhook_IssueComment_Deleted(t *testing.T) { request.Header.Set("Content-Type", "application/json") request.Header.Set("User-Agent", "GitHub-Hookshot/a22606a") request.Header.Set("X-GitHub-Delivery", "7bd477e4-4415-11e9-9359-0d41fdf9567e") + request.Header.Set("X-GitHub-Hook-ID", "123456") request.Header.Set("X-GitHub-Host", "github.com") request.Header.Set("X-GitHub-Version", "2.16.0") request.Header.Set("X-GitHub-Event", "issue_comment") @@ -875,6 +900,7 @@ func TestGithub_ProcessWebhook_IssueComment_Deleted(t *testing.T) { wantHook := new(library.Hook) wantHook.SetNumber(1) wantHook.SetSourceID("7bd477e4-4415-11e9-9359-0d41fdf9567e") + wantHook.SetWebhookID(123456) wantHook.SetCreated(time.Now().UTC().Unix()) wantHook.SetHost("github.com") wantHook.SetEvent("comment") @@ -917,6 +943,7 @@ func TestGitHub_ProcessWebhook_RepositoryRename(t *testing.T) { request.Header.Set("Content-Type", "application/json") request.Header.Set("User-Agent", "GitHub-Hookshot/a22606a") request.Header.Set("X-GitHub-Delivery", "7bd477e4-4415-11e9-9359-0d41fdf9567e") + request.Header.Set("X-GitHub-Hook-ID", "123456") request.Header.Set("X-GitHub-Event", "repository") // setup client @@ -926,6 +953,7 @@ func TestGitHub_ProcessWebhook_RepositoryRename(t *testing.T) { wantHook := new(library.Hook) wantHook.SetNumber(1) wantHook.SetSourceID("7bd477e4-4415-11e9-9359-0d41fdf9567e") + wantHook.SetWebhookID(123456) wantHook.SetCreated(time.Now().UTC().Unix()) wantHook.SetHost("github.com") wantHook.SetEvent("repositoryRename") @@ -977,6 +1005,7 @@ func TestGitHub_ProcessWebhook_Repository(t *testing.T) { request.Header.Set("Content-Type", "application/json") request.Header.Set("User-Agent", "GitHub-Hookshot/a22606a") request.Header.Set("X-GitHub-Delivery", "7bd477e4-4415-11e9-9359-0d41fdf9567e") + request.Header.Set("X-GitHub-Hook-ID", "123456") request.Header.Set("X-GitHub-Event", "repository") // setup client @@ -986,6 +1015,7 @@ func TestGitHub_ProcessWebhook_Repository(t *testing.T) { wantHook := new(library.Hook) wantHook.SetNumber(1) wantHook.SetSourceID("7bd477e4-4415-11e9-9359-0d41fdf9567e") + wantHook.SetWebhookID(123456) wantHook.SetCreated(time.Now().UTC().Unix()) wantHook.SetHost("github.com") wantHook.SetEvent("repository") From 3cb3d99017b0b97e1149fdaa075be2557f0b4bc3 Mon Sep 17 00:00:00 2001 From: ecrupper Date: Mon, 18 Apr 2022 09:42:25 -0500 Subject: [PATCH 2/2] format error in return statement --- scm/github/webhook.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scm/github/webhook.go b/scm/github/webhook.go index 1f61c359b..9b55c16f0 100644 --- a/scm/github/webhook.go +++ b/scm/github/webhook.go @@ -31,8 +31,7 @@ func (c *client) ProcessWebhook(request *http.Request) (*types.Webhook, error) { hookID, err := strconv.Atoi(request.Header.Get("X-GitHub-Hook-ID")) if err != nil { - retErr := fmt.Errorf("unable to convert hook id to int64: %w", err) - return nil, retErr + return nil, fmt.Errorf("unable to convert hook id to int64: %w", err) } h.SetWebhookID(int64(hookID))