diff --git a/database/hook.go b/database/hook.go index 4cbeddc2..c8ed09f5 100644 --- a/database/hook.go +++ b/database/hook.go @@ -23,22 +23,27 @@ var ( // ErrEmptyHookSourceID defines the error type when a // Hook type has an empty SourceID field provided. ErrEmptyHookSourceID = errors.New("empty webhook source_id provided") + + // ErrEmptyHookWebhookID defines the error type when a + // Hook type has an empty WebhookID field provided. + ErrEmptyHookWebhookID = errors.New("empty webhook webhook_id provided") ) // 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"` - Branch sql.NullString `sql:"branch"` - Error sql.NullString `sql:"error"` - Status sql.NullString `sql:"status"` - Link sql.NullString `sql:"link"` + 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"` + 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 @@ -112,6 +117,11 @@ func (h *Hook) Nullify() *Hook { h.Link.Valid = false } + // check if the WebhookID field should be false + if h.WebhookID.Int64 == 0 { + h.WebhookID.Valid = false + } + return h } @@ -132,6 +142,7 @@ func (h *Hook) ToLibrary() *library.Hook { hook.SetError(h.Error.String) hook.SetStatus(h.Status.String) hook.SetLink(h.Link.String) + hook.SetWebhookID(h.WebhookID.Int64) return hook } @@ -154,6 +165,11 @@ func (h *Hook) Validate() error { return ErrEmptyHookSourceID } + // verify the WebhookID field is populated + if h.WebhookID.Int64 <= 0 { + return ErrEmptyHookWebhookID + } + // ensure that all Hook string fields // that can be returned as JSON are sanitized // to avoid unsafe HTML content @@ -172,18 +188,19 @@ 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}, - 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}, + 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}, + 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 c2da0e80..f76f0e7a 100644 --- a/database/hook_test.go +++ b/database/hook_test.go @@ -18,18 +18,19 @@ 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}, - Branch: sql.NullString{String: "", Valid: false}, - Error: sql.NullString{String: "", Valid: false}, - Status: sql.NullString{String: "", Valid: false}, - Link: sql.NullString{String: "", 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}, + 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 @@ -76,20 +77,22 @@ func TestDatabase_Hook_ToLibrary(t *testing.T) { want.SetError("") want.SetStatus("success") want.SetLink("https://github.com/github/octocat/settings/hooks/1") + want.SetWebhookID(123456) 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}, - Branch: sql.NullString{String: "master", 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}, + 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}, + Branch: sql.NullString{String: "master", 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 @@ -113,25 +116,37 @@ func TestDatabase_Hook_Validate(t *testing.T) { { // no number set for hook failure: true, hook: &Hook{ - ID: sql.NullInt64{Int64: 1, Valid: true}, - RepoID: sql.NullInt64{Int64: 1, Valid: true}, - SourceID: sql.NullString{String: "c8da1302-07d6-11ea-882f-4893bca275b8", Valid: true}, + ID: sql.NullInt64{Int64: 1, Valid: true}, + RepoID: sql.NullInt64{Int64: 1, Valid: true}, + SourceID: sql.NullString{String: "c8da1302-07d6-11ea-882f-4893bca275b8", Valid: true}, + WebhookID: sql.NullInt64{Int64: 1, Valid: true}, }, }, { // no repo_id set for hook failure: true, hook: &Hook{ - ID: sql.NullInt64{Int64: 1, Valid: true}, - Number: sql.NullInt32{Int32: 1, Valid: true}, - SourceID: sql.NullString{String: "c8da1302-07d6-11ea-882f-4893bca275b8", Valid: true}, + ID: sql.NullInt64{Int64: 1, Valid: true}, + Number: sql.NullInt32{Int32: 1, Valid: true}, + SourceID: sql.NullString{String: "c8da1302-07d6-11ea-882f-4893bca275b8", Valid: true}, + WebhookID: sql.NullInt64{Int64: 1, Valid: true}, }, }, { // no source_id set for hook failure: true, hook: &Hook{ - ID: sql.NullInt64{Int64: 1, Valid: true}, - Number: sql.NullInt32{Int32: 1, Valid: true}, - RepoID: sql.NullInt64{Int64: 1, Valid: true}, + ID: sql.NullInt64{Int64: 1, Valid: true}, + Number: sql.NullInt32{Int32: 1, Valid: true}, + RepoID: sql.NullInt64{Int64: 1, Valid: true}, + WebhookID: sql.NullInt64{Int64: 1, Valid: true}, + }, + }, + { // no webhook_id set for hook + failure: true, + hook: &Hook{ + ID: sql.NullInt64{Int64: 1, Valid: true}, + Number: sql.NullInt32{Int32: 1, Valid: true}, + RepoID: sql.NullInt64{Int64: 1, Valid: true}, + SourceID: sql.NullString{String: "c8da1302-07d6-11ea-882f-4893bca275b8", Valid: true}, }, }, } @@ -157,18 +172,19 @@ 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: "push", Valid: true}, - Branch: sql.NullString{String: "master", 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}, + 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}, + Branch: sql.NullString{String: "master", 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) @@ -184,6 +200,7 @@ func TestDatabase_HookFromLibrary(t *testing.T) { h.SetError("") h.SetStatus("success") h.SetLink("https://github.com/github/octocat/settings/hooks/1") + h.SetWebhookID(123456) // run test got := HookFromLibrary(h) @@ -197,17 +214,18 @@ 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}, - Branch: sql.NullString{String: "master", 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}, + 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}, + Branch: sql.NullString{String: "master", 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/build.go b/library/build.go index e48d4341..680336c0 100644 --- a/library/build.go +++ b/library/build.go @@ -560,7 +560,7 @@ func (b *Build) GetRuntime() string { return *b.Runtime } -// GetDistribution returns the Runtime field. +// GetDistribution returns the Distribution field. // // When the provided Build type is nil, or the field within // the type is nil, it returns the zero value for the field. @@ -937,7 +937,7 @@ func (b *Build) SetRuntime(v string) { b.Runtime = &v } -// SetDistribution sets the Runtime field. +// SetDistribution sets the Distribution field. // // When the provided Build type is nil, it // will set nothing and immediately return. diff --git a/library/hook.go b/library/hook.go index 9605e91e..0eef3ea1 100644 --- a/library/hook.go +++ b/library/hook.go @@ -12,18 +12,19 @@ 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"` - Branch *string `json:"branch,omitempty"` - Error *string `json:"error,omitempty"` - Status *string `json:"status,omitempty"` - Link *string `json:"link,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"` + 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. @@ -182,6 +183,19 @@ func (h *Hook) GetLink() string { return *h.Link } +// GetWebhookID returns the WebhookID 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) GetWebhookID() int64 { + // return zero value if Hook type or WebhookID field is nil + if h == nil || h.WebhookID == nil { + return 0 + } + + return *h.WebhookID +} + // SetID sets the ID field. // // When the provided Hook type is nil, it @@ -338,6 +352,19 @@ func (h *Hook) SetLink(v string) { h.Link = &v } +// SetWebhookID sets the WebhookID field. +// +// When the provided Hook type is nil, it +// will set nothing and immediately return. +func (h *Hook) SetWebhookID(v int64) { + // return if Hook type is nil + if h == nil { + return + } + + h.WebhookID = &v +} + // String implements the Stringer interface for the Hook type. func (h *Hook) String() string { return fmt.Sprintf(`{ @@ -353,6 +380,7 @@ func (h *Hook) String() string { RepoID: %d, SourceID: %s, Status: %s, + WebhookID: %d, }`, h.GetBranch(), h.GetBuildID(), @@ -366,5 +394,6 @@ func (h *Hook) String() string { h.GetRepoID(), h.GetSourceID(), h.GetStatus(), + h.GetWebhookID(), ) } diff --git a/library/hook_test.go b/library/hook_test.go index 5b348d54..aae8b27b 100644 --- a/library/hook_test.go +++ b/library/hook_test.go @@ -76,6 +76,10 @@ func TestLibrary_Hook_Getters(t *testing.T) { if test.hook.GetLink() != test.want.GetLink() { t.Errorf("GetLink is %v, want %v", test.hook.GetLink(), test.want.GetLink()) } + + if test.hook.GetWebhookID() != test.want.GetWebhookID() { + t.Errorf("GetWebhookID is %v, want %v", test.hook.GetWebhookID(), test.want.GetWebhookID()) + } } } @@ -112,6 +116,7 @@ func TestLibrary_Hook_Setters(t *testing.T) { test.hook.SetError(test.want.GetError()) test.hook.SetStatus(test.want.GetStatus()) test.hook.SetLink(test.want.GetLink()) + test.hook.SetWebhookID(test.want.GetWebhookID()) if test.hook.GetID() != test.want.GetID() { t.Errorf("SetID is %v, want %v", test.hook.GetID(), test.want.GetID()) @@ -160,6 +165,10 @@ func TestLibrary_Hook_Setters(t *testing.T) { if test.hook.GetLink() != test.want.GetLink() { t.Errorf("SetLink is %v, want %v", test.hook.GetLink(), test.want.GetLink()) } + + if test.hook.GetWebhookID() != test.want.GetWebhookID() { + t.Errorf("SetWebhookID is %v, want %v", test.hook.GetWebhookID(), test.want.GetWebhookID()) + } } } @@ -180,6 +189,7 @@ func TestLibrary_Hook_String(t *testing.T) { RepoID: %d, SourceID: %s, Status: %s, + WebhookID: %d, }`, h.GetBranch(), h.GetBuildID(), @@ -193,6 +203,7 @@ func TestLibrary_Hook_String(t *testing.T) { h.GetRepoID(), h.GetSourceID(), h.GetStatus(), + h.GetWebhookID(), ) // run test @@ -220,6 +231,7 @@ func testHook() *Hook { h.SetError("") h.SetStatus("success") h.SetLink("https://github.com/github/octocat/settings/hooks/1") + h.SetWebhookID(123456) return h }