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(hook): set webhook_id in process webhook function #625

Merged
merged 2 commits into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions scm/github/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"time"

Expand All @@ -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
jbrockopp marked this conversation as resolved.
Show resolved Hide resolved
}

h.SetWebhookID(int64(hookID))
h.SetCreated(time.Now().UTC().Unix())
h.SetHost("github.com")
h.SetEvent(request.Header.Get("X-GitHub-Event"))
Expand Down
30 changes: 30 additions & 0 deletions scm/github/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand Down