Skip to content

Commit

Permalink
fix(hook): clean up old repo rename code by adding action field to ho…
Browse files Browse the repository at this point in the history
…ok (#252)

* fix(hook): clean up old repo rename code by adding action field to hook

* fix nullify test
  • Loading branch information
ecrupper committed May 18, 2022
1 parent c60972a commit 0cf0e47
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 95 deletions.
3 changes: 3 additions & 0 deletions constants/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const (
// ActionEdited defines the action for the editing of pull requests or issue comments.
ActionEdited = "edited"

// ActionRenamed defines the action for renaming a repository.
ActionRenamed = "renamed"

// ActionSynchronized defines the action for the synchronizing of pull requests.
ActionSynchronized = "synchronized"
)
3 changes: 0 additions & 3 deletions constants/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ const (
// EventComment defines the event type for comments added to a pull request.
EventComment = "comment"

// EventRepositoryRename defines the event type for a repo being renamed.
EventRepositoryRename = "repositoryRename"

// EventRepository defines the general event type for repo management.
EventRepository = "repository"
)
61 changes: 35 additions & 26 deletions database/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,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"`
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"`
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
Expand Down Expand Up @@ -97,6 +98,11 @@ func (h *Hook) Nullify() *Hook {
h.Event.Valid = false
}

// check if the EventAction field should be false
if len(h.EventAction.String) == 0 {
h.EventAction.Valid = false
}

// check if the Branch field should be false
if len(h.Branch.String) == 0 {
h.Branch.Valid = false
Expand Down Expand Up @@ -138,6 +144,7 @@ func (h *Hook) ToLibrary() *library.Hook {
hook.SetCreated(h.Created.Int64)
hook.SetHost(h.Host.String)
hook.SetEvent(h.Event.String)
hook.SetEventAction(h.EventAction.String)
hook.SetBranch(h.Branch.String)
hook.SetError(h.Error.String)
hook.SetStatus(h.Status.String)
Expand Down Expand Up @@ -176,6 +183,7 @@ func (h *Hook) Validate() error {
h.SourceID = sql.NullString{String: sanitize(h.SourceID.String), Valid: h.SourceID.Valid}
h.Host = sql.NullString{String: sanitize(h.Host.String), Valid: h.Host.Valid}
h.Event = sql.NullString{String: sanitize(h.Event.String), Valid: h.Event.Valid}
h.EventAction = sql.NullString{String: sanitize(h.EventAction.String), Valid: h.EventAction.Valid}
h.Branch = sql.NullString{String: sanitize(h.Branch.String), Valid: h.Branch.Valid}
h.Error = sql.NullString{String: sanitize(h.Error.String), Valid: h.Error.Valid}
h.Status = sql.NullString{String: sanitize(h.Status.String), Valid: h.Status.Valid}
Expand All @@ -188,19 +196,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},
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},
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()
Expand Down
112 changes: 59 additions & 53 deletions database/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,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},
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},
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
Expand Down Expand Up @@ -73,26 +74,28 @@ func TestDatabase_Hook_ToLibrary(t *testing.T) {
want.SetCreated(time.Now().UTC().Unix())
want.SetHost("github.com")
want.SetEvent("push")
want.SetEventAction("")
want.SetBranch("master")
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},
WebhookID: sql.NullInt64{Int64: 123456, 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},
EventAction: sql.NullString{String: "", 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
Expand Down Expand Up @@ -172,19 +175,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: "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},
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: "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)
Expand All @@ -195,7 +199,8 @@ func TestDatabase_HookFromLibrary(t *testing.T) {
h.SetSourceID("c8da1302-07d6-11ea-882f-4893bca275b8")
h.SetCreated(time.Now().UTC().Unix())
h.SetHost("github.com")
h.SetEvent("push")
h.SetEvent("pull_request")
h.SetEventAction("opened")
h.SetBranch("master")
h.SetError("")
h.SetStatus("success")
Expand All @@ -214,18 +219,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},
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},
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: "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},
}
}
55 changes: 42 additions & 13 deletions library/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,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"`
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"`
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.
Expand Down Expand Up @@ -131,6 +132,19 @@ func (h *Hook) GetEvent() string {
return *h.Event
}

// GetEventAction returns the EventAction 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) GetEventAction() string {
// return zero value if Hook type or EventAction field is nil
if h == nil || h.EventAction == nil {
return ""
}

return *h.EventAction
}

// GetBranch returns the Branch field.
//
// When the provided Hook type is nil, or the field within
Expand Down Expand Up @@ -300,6 +314,19 @@ func (h *Hook) SetEvent(v string) {
h.Event = &v
}

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

h.EventAction = &v
}

// SetBranch sets the Branch field.
//
// When the provided Hook type is nil, it
Expand Down Expand Up @@ -373,6 +400,7 @@ func (h *Hook) String() string {
Created: %d,
Error: %s,
Event: %s,
EventAction: %s,
Host: %s,
ID: %d,
Link: %s,
Expand All @@ -387,6 +415,7 @@ func (h *Hook) String() string {
h.GetCreated(),
h.GetError(),
h.GetEvent(),
h.GetEventAction(),
h.GetHost(),
h.GetID(),
h.GetLink(),
Expand Down
Loading

0 comments on commit 0cf0e47

Please sign in to comment.