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): add hook id to hook object #239

Merged
merged 6 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions database/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ var (
// ErrEmptyHookSourceID defines the error type when a
// Hook type has an empty SourceID field provided.
ErrEmptyHookSourceID = errors.New("empty webhook source_id provided")

// ErrEmptyHookAddress defines the error type when a
// Hook type has an empty Address field provided.
ErrEmptyHookAddress = errors.New("empty webhook address provided")
)

// Hook is the database representation of a webhook for a repo.
Expand All @@ -39,6 +43,7 @@ type Hook struct {
Error sql.NullString `sql:"error"`
Status sql.NullString `sql:"status"`
Link sql.NullString `sql:"link"`
Address sql.NullInt64 `sql:"address"`
}

// Nullify ensures the valid flag for
Expand Down Expand Up @@ -112,6 +117,11 @@ func (h *Hook) Nullify() *Hook {
h.Link.Valid = false
}

// check if the Address field should be false
if h.Address.Int64 == 0 {
h.Address.Valid = false
}

return h
}

Expand All @@ -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.SetAddress(h.Address.Int64)

return hook
}
Expand All @@ -154,6 +165,11 @@ func (h *Hook) Validate() error {
return ErrEmptyHookSourceID
}

// verify the Address field is populated
if h.Address.Int64 <= 0 {
return ErrEmptyHookAddress
}

// ensure that all Hook string fields
// that can be returned as JSON are sanitized
// to avoid unsafe HTML content
Expand Down Expand Up @@ -184,6 +200,7 @@ func HookFromLibrary(h *library.Hook) *Hook {
Error: sql.NullString{String: h.GetError(), Valid: true},
Status: sql.NullString{String: h.GetStatus(), Valid: true},
Link: sql.NullString{String: h.GetLink(), Valid: true},
Address: sql.NullInt64{Int64: h.GetAddress(), Valid: true},
}

return hook.Nullify()
Expand Down
24 changes: 21 additions & 3 deletions database/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestDatabase_Hook_Nullify(t *testing.T) {
Error: sql.NullString{String: "", Valid: false},
Status: sql.NullString{String: "", Valid: false},
Link: sql.NullString{String: "", Valid: false},
Address: sql.NullInt64{Int64: 0, Valid: false},
}

// setup tests
Expand Down Expand Up @@ -76,6 +77,7 @@ func TestDatabase_Hook_ToLibrary(t *testing.T) {
want.SetError("")
want.SetStatus("success")
want.SetLink("https://github.com/github/octocat/settings/hooks/1")
want.SetAddress(123456)

h := &Hook{
ID: sql.NullInt64{Int64: 1, Valid: true},
Expand All @@ -90,6 +92,7 @@ func TestDatabase_Hook_ToLibrary(t *testing.T) {
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},
Address: sql.NullInt64{Int64: 123456, Valid: true},
}

// run test
Expand All @@ -116,6 +119,7 @@ func TestDatabase_Hook_Validate(t *testing.T) {
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},
Address: sql.NullInt64{Int64: 1, Valid: true},
},
},
{ // no repo_id set for hook
Expand All @@ -124,14 +128,25 @@ func TestDatabase_Hook_Validate(t *testing.T) {
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},
Address: 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},
Address: sql.NullInt64{Int64: 1, Valid: true},
},
},
{ // no address 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},
},
},
}
Expand Down Expand Up @@ -169,6 +184,7 @@ func TestDatabase_HookFromLibrary(t *testing.T) {
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},
Address: sql.NullInt64{Int64: 123456, Valid: true},
}

h := new(library.Hook)
Expand All @@ -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.SetAddress(123456)

// run test
got := HookFromLibrary(h)
Expand All @@ -209,5 +226,6 @@ func testHook() *Hook {
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},
Address: sql.NullInt64{Int64: 123456, Valid: true},
}
}
4 changes: 2 additions & 2 deletions library/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
29 changes: 29 additions & 0 deletions library/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Hook struct {
Error *string `json:"error,omitempty"`
Status *string `json:"status,omitempty"`
Link *string `json:"link,omitempty"`
Address *int64 `json:"address,omitempty"`
}

// GetID returns the ID field.
Expand Down Expand Up @@ -182,6 +183,19 @@ func (h *Hook) GetLink() string {
return *h.Link
}

// GetAddress returns the Address 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) GetAddress() int64 {
// return zero value if Hook type or Address field is nil
if h == nil || h.ID == nil {
plyr4 marked this conversation as resolved.
Show resolved Hide resolved
return 0
}

return *h.Address
}

// SetID sets the ID field.
//
// When the provided Hook type is nil, it
Expand Down Expand Up @@ -338,9 +352,23 @@ func (h *Hook) SetLink(v string) {
h.Link = &v
}

// SetAddress sets the Address field.
//
// When the provided Hook type is nil, it
// will set nothing and immediately return.
func (h *Hook) SetAddress(v int64) {
// return if Hook type is nil
if h == nil {
return
}

h.Address = &v
}

// String implements the Stringer interface for the Hook type.
func (h *Hook) String() string {
return fmt.Sprintf(`{
Address: %d,
Branch: %s,
BuildID: %d,
Created: %d,
Expand All @@ -354,6 +382,7 @@ func (h *Hook) String() string {
SourceID: %s,
Status: %s,
}`,
h.GetAddress(),
h.GetBranch(),
h.GetBuildID(),
h.GetCreated(),
Expand Down
12 changes: 12 additions & 0 deletions library/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.GetAddress() != test.want.GetAddress() {
t.Errorf("GetAddress is %v, want %v", test.hook.GetAddress(), test.want.GetAddress())
}
}
}

Expand Down Expand Up @@ -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.SetAddress(test.want.GetAddress())

if test.hook.GetID() != test.want.GetID() {
t.Errorf("SetID is %v, want %v", test.hook.GetID(), test.want.GetID())
Expand Down Expand Up @@ -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.GetAddress() != test.want.GetAddress() {
t.Errorf("SetAddress is %v, want %v", test.hook.GetAddress(), test.want.GetAddress())
}
}
}

Expand All @@ -168,6 +177,7 @@ func TestLibrary_Hook_String(t *testing.T) {
h := testHook()

want := fmt.Sprintf(`{
Address: %d,
Branch: %s,
BuildID: %d,
Created: %d,
Expand All @@ -181,6 +191,7 @@ func TestLibrary_Hook_String(t *testing.T) {
SourceID: %s,
Status: %s,
}`,
h.GetAddress(),
h.GetBranch(),
h.GetBuildID(),
h.GetCreated(),
Expand Down Expand Up @@ -220,6 +231,7 @@ func testHook() *Hook {
h.SetError("")
h.SetStatus("success")
h.SetLink("https://github.com/github/octocat/settings/hooks/1")
h.SetAddress(123456)

return h
}