From c43cbf0e464991fbe44bcc835c2e3c385e99540d Mon Sep 17 00:00:00 2001 From: "Claire.Nicholas" Date: Fri, 15 Dec 2023 14:29:01 -0600 Subject: [PATCH] test --- database/hook.go | 64 ++++++++++------------- database/hook_test.go | 118 ++++++++++++++++++++---------------------- library/hook.go | 57 +++++--------------- library/hook_test.go | 7 --- webhook.go | 9 ++-- 5 files changed, 103 insertions(+), 152 deletions(-) diff --git a/database/hook.go b/database/hook.go index 83e491c9..6a10f5d1 100644 --- a/database/hook.go +++ b/database/hook.go @@ -29,21 +29,20 @@ var ( // Hook is the database representation of a webhook for a repo. type Hook struct { - ID sql.NullInt64 `sql:"id"` - RepoID sql.NullInt64 `sql:"repo_id"` - BuildID sql.NullInt64 `sql:"build_id"` - Number sql.NullInt32 `sql:"number"` - SourceID sql.NullString `sql:"source_id"` - Created sql.NullInt64 `sql:"created"` - Host sql.NullString `sql:"host"` - Event sql.NullString `sql:"event"` - EventAction sql.NullString `sql:"event_action"` - Branch sql.NullString `sql:"branch"` - Error sql.NullString `sql:"error"` - Status sql.NullString `sql:"status"` - Link sql.NullString `sql:"link"` - WebhookID sql.NullInt64 `sql:"webhook_id"` - DeploymentID sql.NullInt64 `sql:"deployment_id"` + ID sql.NullInt64 `sql:"id"` + RepoID sql.NullInt64 `sql:"repo_id"` + BuildID sql.NullInt64 `sql:"build_id"` + Number sql.NullInt32 `sql:"number"` + SourceID sql.NullString `sql:"source_id"` + Created sql.NullInt64 `sql:"created"` + Host sql.NullString `sql:"host"` + Event sql.NullString `sql:"event"` + EventAction sql.NullString `sql:"event_action"` + Branch sql.NullString `sql:"branch"` + Error sql.NullString `sql:"error"` + Status sql.NullString `sql:"status"` + Link sql.NullString `sql:"link"` + WebhookID sql.NullInt64 `sql:"webhook_id"` } // Nullify ensures the valid flag for @@ -127,11 +126,6 @@ func (h *Hook) Nullify() *Hook { h.WebhookID.Valid = false } - // check if the DeploymentID field should be false - if h.DeploymentID.Int64 == 0 { - h.DeploymentID.Valid = false - } - return h } @@ -154,7 +148,6 @@ func (h *Hook) ToLibrary() *library.Hook { hook.SetStatus(h.Status.String) hook.SetLink(h.Link.String) hook.SetWebhookID(h.WebhookID.Int64) - hook.SetDeploymentID(h.DeploymentID.Int64) return hook } @@ -201,21 +194,20 @@ func (h *Hook) Validate() error { // to a library Hook type. func HookFromLibrary(h *library.Hook) *Hook { hook := &Hook{ - ID: sql.NullInt64{Int64: h.GetID(), Valid: true}, - RepoID: sql.NullInt64{Int64: h.GetRepoID(), Valid: true}, - BuildID: sql.NullInt64{Int64: h.GetBuildID(), Valid: true}, - Number: sql.NullInt32{Int32: int32(h.GetNumber()), Valid: true}, - SourceID: sql.NullString{String: h.GetSourceID(), Valid: true}, - Created: sql.NullInt64{Int64: h.GetCreated(), Valid: true}, - Host: sql.NullString{String: h.GetHost(), Valid: true}, - Event: sql.NullString{String: h.GetEvent(), Valid: true}, - EventAction: sql.NullString{String: h.GetEventAction(), Valid: true}, - Branch: sql.NullString{String: h.GetBranch(), Valid: true}, - Error: sql.NullString{String: h.GetError(), Valid: true}, - Status: sql.NullString{String: h.GetStatus(), Valid: true}, - Link: sql.NullString{String: h.GetLink(), Valid: true}, - WebhookID: sql.NullInt64{Int64: h.GetWebhookID(), Valid: true}, - DeploymentID: sql.NullInt64{Int64: h.GetDeploymentID(), Valid: true}, + ID: sql.NullInt64{Int64: h.GetID(), Valid: true}, + RepoID: sql.NullInt64{Int64: h.GetRepoID(), Valid: true}, + BuildID: sql.NullInt64{Int64: h.GetBuildID(), Valid: true}, + Number: sql.NullInt32{Int32: int32(h.GetNumber()), Valid: true}, + SourceID: sql.NullString{String: h.GetSourceID(), Valid: true}, + Created: sql.NullInt64{Int64: h.GetCreated(), Valid: true}, + Host: sql.NullString{String: h.GetHost(), Valid: true}, + Event: sql.NullString{String: h.GetEvent(), Valid: true}, + EventAction: sql.NullString{String: h.GetEventAction(), Valid: true}, + Branch: sql.NullString{String: h.GetBranch(), Valid: true}, + Error: sql.NullString{String: h.GetError(), Valid: true}, + Status: sql.NullString{String: h.GetStatus(), Valid: true}, + Link: sql.NullString{String: h.GetLink(), Valid: true}, + WebhookID: sql.NullInt64{Int64: h.GetWebhookID(), Valid: true}, } return hook.Nullify() diff --git a/database/hook_test.go b/database/hook_test.go index a137022c..9247ba0d 100644 --- a/database/hook_test.go +++ b/database/hook_test.go @@ -16,21 +16,20 @@ func TestDatabase_Hook_Nullify(t *testing.T) { var h *Hook want := &Hook{ - ID: sql.NullInt64{Int64: 0, Valid: false}, - RepoID: sql.NullInt64{Int64: 0, Valid: false}, - BuildID: sql.NullInt64{Int64: 0, Valid: false}, - Number: sql.NullInt32{Int32: 0, Valid: false}, - SourceID: sql.NullString{String: "", Valid: false}, - Created: sql.NullInt64{Int64: 0, Valid: false}, - Host: sql.NullString{String: "", Valid: false}, - Event: sql.NullString{String: "", Valid: false}, - EventAction: sql.NullString{String: "", Valid: false}, - Branch: sql.NullString{String: "", Valid: false}, - Error: sql.NullString{String: "", Valid: false}, - Status: sql.NullString{String: "", Valid: false}, - Link: sql.NullString{String: "", Valid: false}, - WebhookID: sql.NullInt64{Int64: 0, Valid: false}, - DeploymentID: sql.NullInt64{Int64: 0, Valid: false}, + ID: sql.NullInt64{Int64: 0, Valid: false}, + RepoID: sql.NullInt64{Int64: 0, Valid: false}, + BuildID: sql.NullInt64{Int64: 0, Valid: false}, + Number: sql.NullInt32{Int32: 0, Valid: false}, + SourceID: sql.NullString{String: "", Valid: false}, + Created: sql.NullInt64{Int64: 0, Valid: false}, + Host: sql.NullString{String: "", Valid: false}, + Event: sql.NullString{String: "", Valid: false}, + EventAction: sql.NullString{String: "", Valid: false}, + Branch: sql.NullString{String: "", Valid: false}, + Error: sql.NullString{String: "", Valid: false}, + Status: sql.NullString{String: "", Valid: false}, + Link: sql.NullString{String: "", Valid: false}, + WebhookID: sql.NullInt64{Int64: 0, Valid: false}, } // setup tests @@ -79,24 +78,22 @@ func TestDatabase_Hook_ToLibrary(t *testing.T) { want.SetStatus("success") want.SetLink("https://github.com/github/octocat/settings/hooks/1") want.SetWebhookID(123456) - want.SetDeploymentID(0) h := &Hook{ - ID: sql.NullInt64{Int64: 1, Valid: true}, - RepoID: sql.NullInt64{Int64: 1, Valid: true}, - BuildID: sql.NullInt64{Int64: 1, Valid: true}, - Number: sql.NullInt32{Int32: 1, Valid: true}, - SourceID: sql.NullString{String: "c8da1302-07d6-11ea-882f-4893bca275b8", Valid: true}, - Created: sql.NullInt64{Int64: time.Now().UTC().Unix(), Valid: true}, - Host: sql.NullString{String: "github.com", Valid: true}, - Event: sql.NullString{String: "push", Valid: true}, - EventAction: sql.NullString{String: "", Valid: true}, - Branch: sql.NullString{String: "main", Valid: true}, - Error: sql.NullString{String: "", Valid: true}, - Status: sql.NullString{String: "success", Valid: true}, - Link: sql.NullString{String: "https://github.com/github/octocat/settings/hooks/1", Valid: true}, - WebhookID: sql.NullInt64{Int64: 123456, Valid: true}, - DeploymentID: sql.NullInt64{Int64: 0, Valid: false}, + ID: sql.NullInt64{Int64: 1, Valid: true}, + RepoID: sql.NullInt64{Int64: 1, Valid: true}, + BuildID: sql.NullInt64{Int64: 1, Valid: true}, + Number: sql.NullInt32{Int32: 1, Valid: true}, + SourceID: sql.NullString{String: "c8da1302-07d6-11ea-882f-4893bca275b8", Valid: true}, + Created: sql.NullInt64{Int64: time.Now().UTC().Unix(), Valid: true}, + Host: sql.NullString{String: "github.com", Valid: true}, + Event: sql.NullString{String: "push", Valid: true}, + EventAction: sql.NullString{String: "", Valid: true}, + Branch: sql.NullString{String: "main", Valid: true}, + Error: sql.NullString{String: "", Valid: true}, + Status: sql.NullString{String: "success", Valid: true}, + Link: sql.NullString{String: "https://github.com/github/octocat/settings/hooks/1", Valid: true}, + WebhookID: sql.NullInt64{Int64: 123456, Valid: true}, } // run test @@ -176,21 +173,20 @@ func TestDatabase_Hook_Validate(t *testing.T) { func TestDatabase_HookFromLibrary(t *testing.T) { // setup types want := &Hook{ - ID: sql.NullInt64{Int64: 1, Valid: true}, - RepoID: sql.NullInt64{Int64: 1, Valid: true}, - BuildID: sql.NullInt64{Int64: 1, Valid: true}, - Number: sql.NullInt32{Int32: 1, Valid: true}, - SourceID: sql.NullString{String: "c8da1302-07d6-11ea-882f-4893bca275b8", Valid: true}, - Created: sql.NullInt64{Int64: time.Now().UTC().Unix(), Valid: true}, - Host: sql.NullString{String: "github.com", Valid: true}, - Event: sql.NullString{String: "pull_request", Valid: true}, - EventAction: sql.NullString{String: "opened", Valid: true}, - Branch: sql.NullString{String: "main", Valid: true}, - Error: sql.NullString{String: "", Valid: false}, - Status: sql.NullString{String: "success", Valid: true}, - Link: sql.NullString{String: "https://github.com/github/octocat/settings/hooks/1", Valid: true}, - WebhookID: sql.NullInt64{Int64: 123456, Valid: true}, - DeploymentID: sql.NullInt64{Int64: 0, Valid: false}, + ID: sql.NullInt64{Int64: 1, Valid: true}, + RepoID: sql.NullInt64{Int64: 1, Valid: true}, + BuildID: sql.NullInt64{Int64: 1, Valid: true}, + Number: sql.NullInt32{Int32: 1, Valid: true}, + SourceID: sql.NullString{String: "c8da1302-07d6-11ea-882f-4893bca275b8", Valid: true}, + Created: sql.NullInt64{Int64: time.Now().UTC().Unix(), Valid: true}, + Host: sql.NullString{String: "github.com", Valid: true}, + Event: sql.NullString{String: "pull_request", Valid: true}, + EventAction: sql.NullString{String: "opened", Valid: true}, + Branch: sql.NullString{String: "main", Valid: true}, + Error: sql.NullString{String: "", Valid: false}, + Status: sql.NullString{String: "success", Valid: true}, + Link: sql.NullString{String: "https://github.com/github/octocat/settings/hooks/1", Valid: true}, + WebhookID: sql.NullInt64{Int64: 123456, Valid: true}, } h := new(library.Hook) @@ -208,7 +204,6 @@ func TestDatabase_HookFromLibrary(t *testing.T) { h.SetStatus("success") h.SetLink("https://github.com/github/octocat/settings/hooks/1") h.SetWebhookID(123456) - h.SetDeploymentID(0) // run test got := HookFromLibrary(h) @@ -222,20 +217,19 @@ func TestDatabase_HookFromLibrary(t *testing.T) { // type with all fields set to a fake value. func testHook() *Hook { return &Hook{ - ID: sql.NullInt64{Int64: 1, Valid: true}, - RepoID: sql.NullInt64{Int64: 1, Valid: true}, - BuildID: sql.NullInt64{Int64: 1, Valid: true}, - Number: sql.NullInt32{Int32: 1, Valid: true}, - SourceID: sql.NullString{String: "c8da1302-07d6-11ea-882f-4893bca275b8", Valid: true}, - Created: sql.NullInt64{Int64: time.Now().UTC().Unix(), Valid: true}, - Host: sql.NullString{String: "github.com", Valid: true}, - Event: sql.NullString{String: "push", Valid: true}, - EventAction: sql.NullString{String: "", Valid: false}, - Branch: sql.NullString{String: "main", Valid: true}, - Error: sql.NullString{String: "", Valid: false}, - Status: sql.NullString{String: "success", Valid: true}, - Link: sql.NullString{String: "https://github.com/github/octocat/settings/hooks/1", Valid: true}, - WebhookID: sql.NullInt64{Int64: 123456, Valid: true}, - DeploymentID: sql.NullInt64{Int64: 0, Valid: false}, + ID: sql.NullInt64{Int64: 1, Valid: true}, + RepoID: sql.NullInt64{Int64: 1, Valid: true}, + BuildID: sql.NullInt64{Int64: 1, Valid: true}, + Number: sql.NullInt32{Int32: 1, Valid: true}, + SourceID: sql.NullString{String: "c8da1302-07d6-11ea-882f-4893bca275b8", Valid: true}, + Created: sql.NullInt64{Int64: time.Now().UTC().Unix(), Valid: true}, + Host: sql.NullString{String: "github.com", Valid: true}, + Event: sql.NullString{String: "push", Valid: true}, + EventAction: sql.NullString{String: "", Valid: false}, + Branch: sql.NullString{String: "main", Valid: true}, + Error: sql.NullString{String: "", Valid: false}, + Status: sql.NullString{String: "success", Valid: true}, + Link: sql.NullString{String: "https://github.com/github/octocat/settings/hooks/1", Valid: true}, + WebhookID: sql.NullInt64{Int64: 123456, Valid: true}, } } diff --git a/library/hook.go b/library/hook.go index e66334c3..f9d6b5e5 100644 --- a/library/hook.go +++ b/library/hook.go @@ -10,21 +10,20 @@ import ( // // swagger:model Webhook type Hook struct { - ID *int64 `json:"id,omitempty"` - RepoID *int64 `json:"repo_id,omitempty"` - BuildID *int64 `json:"build_id,omitempty"` - Number *int `json:"number,omitempty"` - SourceID *string `json:"source_id,omitempty"` - Created *int64 `json:"created,omitempty"` - Host *string `json:"host,omitempty"` - Event *string `json:"event,omitempty"` - EventAction *string `json:"event_action,omitempty"` - Branch *string `json:"branch,omitempty"` - Error *string `json:"error,omitempty"` - Status *string `json:"status,omitempty"` - Link *string `json:"link,omitempty"` - WebhookID *int64 `json:"webhook_id,omitempty"` - DeploymentID *int64 `json:"deployment,omitempty"` + ID *int64 `json:"id,omitempty"` + RepoID *int64 `json:"repo_id,omitempty"` + BuildID *int64 `json:"build_id,omitempty"` + Number *int `json:"number,omitempty"` + SourceID *string `json:"source_id,omitempty"` + Created *int64 `json:"created,omitempty"` + Host *string `json:"host,omitempty"` + Event *string `json:"event,omitempty"` + EventAction *string `json:"event_action,omitempty"` + Branch *string `json:"branch,omitempty"` + Error *string `json:"error,omitempty"` + Status *string `json:"status,omitempty"` + Link *string `json:"link,omitempty"` + WebhookID *int64 `json:"webhook_id,omitempty"` } // GetID returns the ID field. @@ -209,19 +208,6 @@ func (h *Hook) GetWebhookID() int64 { return *h.WebhookID } -// GetDeploymentID returns the DeploymentID field. -// -// When the provided Hook type is nil, or the field within -// the type is nil, it returns the zero value for the field. -func (h *Hook) GetDeploymentID() int64 { - // return zero value if Hook type or WebhookID field is nil - if h == nil || h.DeploymentID == nil { - return 0 - } - - return *h.DeploymentID -} - // SetID sets the ID field. // // When the provided Hook type is nil, it @@ -404,19 +390,6 @@ func (h *Hook) SetWebhookID(v int64) { h.WebhookID = &v } -// SetDeploymentID sets the DeploymentID field. -// -// When the provided Hook type is nil, it -// will set nothing and immediately return. -func (h *Hook) SetDeploymentID(v int64) { - // return if Hook type is nil - if h == nil { - return - } - - h.DeploymentID = &v -} - // String implements the Stringer interface for the Hook type. func (h *Hook) String() string { return fmt.Sprintf(`{ @@ -434,7 +407,6 @@ func (h *Hook) String() string { SourceID: %s, Status: %s, WebhookID: %d, - DeploymentID: %d, }`, h.GetBranch(), h.GetBuildID(), @@ -450,6 +422,5 @@ func (h *Hook) String() string { h.GetSourceID(), h.GetStatus(), h.GetWebhookID(), - h.GetDeploymentID(), ) } diff --git a/library/hook_test.go b/library/hook_test.go index 6093d555..0abecf9a 100644 --- a/library/hook_test.go +++ b/library/hook_test.go @@ -120,7 +120,6 @@ func TestLibrary_Hook_Setters(t *testing.T) { test.hook.SetStatus(test.want.GetStatus()) test.hook.SetLink(test.want.GetLink()) test.hook.SetWebhookID(test.want.GetWebhookID()) - test.hook.SetDeploymentID(test.want.GetDeploymentID()) if test.hook.GetID() != test.want.GetID() { t.Errorf("SetID is %v, want %v", test.hook.GetID(), test.want.GetID()) @@ -178,9 +177,6 @@ func TestLibrary_Hook_Setters(t *testing.T) { t.Errorf("SetWebhookID is %v, want %v", test.hook.GetWebhookID(), test.want.GetWebhookID()) } - if test.hook.GetDeploymentID() != test.want.GetDeploymentID() { - t.Errorf("SetDeploymentID is %v, want %v", test.hook.GetDeploymentID(), test.want.GetDeploymentID()) - } } } @@ -203,7 +199,6 @@ func TestLibrary_Hook_String(t *testing.T) { SourceID: %s, Status: %s, WebhookID: %d, - DeploymentID: %d, }`, h.GetBranch(), h.GetBuildID(), @@ -219,7 +214,6 @@ func TestLibrary_Hook_String(t *testing.T) { h.GetSourceID(), h.GetStatus(), h.GetWebhookID(), - h.GetDeploymentID(), ) // run test @@ -249,7 +243,6 @@ func testHook() *Hook { h.SetStatus("success") h.SetLink("https://github.com/github/octocat/settings/hooks/1") h.SetWebhookID(123456) - h.SetWebhookID(0) return h } diff --git a/webhook.go b/webhook.go index aaddd5c4..ef7c342a 100644 --- a/webhook.go +++ b/webhook.go @@ -26,10 +26,11 @@ type PullRequest struct { // the required data when processing webhook event // a for a source provider event. type Webhook struct { - Hook *library.Hook - Repo *library.Repo - Build *library.Build - PullRequest PullRequest + Hook *library.Hook + Repo *library.Repo + Build *library.Build + PullRequest PullRequest + DeploymentID *int64 } // ShouldSkip uses the build information