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: adding a delete_event #340

Merged
merged 18 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
6 changes: 6 additions & 0 deletions constants/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ const (

// ActionTransferred defines the action for transferring repository ownership.
ActionTransferred = "transferred"

// ActionBranch defines the action for deleting a branch.
ActionBranch = "branch"

// ActionTag defines the action for deleting a tag.
ActionTag = "tag"
)
2 changes: 2 additions & 0 deletions constants/allow_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ const (
AllowDeployCreate
AllowCommentCreate
AllowCommentEdit
AllowDeleteBranch
AllowDeleteTag
)
3 changes: 3 additions & 0 deletions constants/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const (
// EventComment defines the event type for comments added to a pull request.
EventComment = "comment"

// EventDelete defines the event type for build and repo delete events.
EventDelete = "delete"

// EventDeploy defines the event type for build and repo deployment events.
EventDeploy = "deployment"

Expand Down
3 changes: 3 additions & 0 deletions database/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type Repo struct {
AllowDeploy sql.NullBool `sql:"allow_deploy"`
AllowTag sql.NullBool `sql:"allow_tag"`
AllowComment sql.NullBool `sql:"allow_comment"`
AllowDelete sql.NullBool `sql:"allow_delete"`
claire1618 marked this conversation as resolved.
Show resolved Hide resolved
AllowEvents sql.NullInt64 `sql:"allow_events"`
PipelineType sql.NullString `sql:"pipeline_type"`
PreviousName sql.NullString `sql:"previous_name"`
Expand Down Expand Up @@ -240,6 +241,7 @@ func (r *Repo) ToLibrary() *library.Repo {
repo.SetAllowDeploy(r.AllowDeploy.Bool)
repo.SetAllowTag(r.AllowTag.Bool)
repo.SetAllowComment(r.AllowComment.Bool)
repo.SetAllowDelete(r.AllowDelete.Bool)
repo.SetAllowEvents(library.NewEventsFromMask(r.AllowEvents.Int64))
repo.SetPipelineType(r.PipelineType.String)
repo.SetPreviousName(r.PreviousName.String)
Expand Down Expand Up @@ -337,6 +339,7 @@ func RepoFromLibrary(r *library.Repo) *Repo {
AllowDeploy: sql.NullBool{Bool: r.GetAllowDeploy(), Valid: true},
AllowTag: sql.NullBool{Bool: r.GetAllowTag(), Valid: true},
AllowComment: sql.NullBool{Bool: r.GetAllowComment(), Valid: true},
AllowDelete: sql.NullBool{Bool: r.GetAllowDelete(), Valid: true},
AllowEvents: sql.NullInt64{Int64: r.GetAllowEvents().ToDatabase(), Valid: true},
PipelineType: sql.NullString{String: r.GetPipelineType(), Valid: true},
PreviousName: sql.NullString{String: r.GetPreviousName(), Valid: true},
Expand Down
3 changes: 3 additions & 0 deletions database/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func TestDatabase_Repo_ToLibrary(t *testing.T) {
want.SetAllowDeploy(false)
want.SetAllowTag(false)
want.SetAllowComment(false)
want.SetAllowDelete(false)
claire1618 marked this conversation as resolved.
Show resolved Hide resolved
want.SetAllowEvents(e)
want.SetPipelineType("yaml")
want.SetPreviousName("oldName")
Expand Down Expand Up @@ -337,6 +338,7 @@ func TestDatabase_RepoFromLibrary(t *testing.T) {
r.SetAllowDeploy(false)
r.SetAllowTag(false)
r.SetAllowComment(false)
r.SetAllowDelete(false)
claire1618 marked this conversation as resolved.
Show resolved Hide resolved
r.SetAllowEvents(e)
r.SetPipelineType("yaml")
r.SetPreviousName("oldName")
Expand Down Expand Up @@ -378,6 +380,7 @@ func testRepo() *Repo {
AllowDeploy: sql.NullBool{Bool: false, Valid: true},
AllowTag: sql.NullBool{Bool: false, Valid: true},
AllowComment: sql.NullBool{Bool: false, Valid: true},
AllowDelete: sql.NullBool{Bool: false, Valid: true},
AllowEvents: sql.NullInt64{Int64: 1, Valid: true},
PipelineType: sql.NullString{String: "yaml", Valid: true},
PreviousName: sql.NullString{String: "oldName", Valid: true},
Expand Down
2 changes: 2 additions & 0 deletions item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func TestTypes_ToItem(t *testing.T) {
AllowPush: &booL,
AllowDeploy: &booL,
AllowTag: &booL,
AllowDelete: &booL,
claire1618 marked this conversation as resolved.
Show resolved Hide resolved
AllowEvents: e,
}
u := &library.User{
Expand Down Expand Up @@ -111,6 +112,7 @@ func TestTypes_ToItem(t *testing.T) {
AllowPush: &booL,
AllowDeploy: &booL,
AllowTag: &booL,
AllowDelete: &booL,
AllowEvents: e,
},
User: &library.User{
Expand Down
83 changes: 83 additions & 0 deletions library/actions/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// SPDX-License-Identifier: Apache-2.0

package actions

Check failure on line 3 in library/actions/delete.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] library/actions/delete.go#L3

3-83 lines are duplicate of `library/actions/push.go:4-84` (dupl)
Raw output
library/actions/delete.go:3: 3-83 lines are duplicate of `library/actions/push.go:4-84` (dupl)
package actions

import "github.com/go-vela/types/constants"

// Delete is the library representation of the various actions associated
// with the delete event webhook from the SCM.
type Delete struct {
	Branch *bool `json:"branch"`
	Tag    *bool `json:"tag"`
}

// FromMask returns the Delete type resulting from the provided integer mask.
func (a *Delete) FromMask(mask int64) *Delete {
	a.SetBranch(mask&constants.AllowDeleteBranch > 0)
	a.SetTag(mask&constants.AllowDeleteTag > 0)

	return a
}

// ToMask returns the integer mask of the values for the Delete set.
func (a *Delete) ToMask() int64 {
	mask := int64(0)

	if a.GetBranch() {
		mask = mask | constants.AllowDeleteBranch
	}

	if a.GetTag() {
		mask = mask | constants.AllowDeleteTag
	}

	return mask
}

// GetBranch returns the Branch field from the provided Delete. If the object is nil,
// or the field within the object is nil, it returns the zero value instead.
func (a *Delete) GetBranch() bool {
	// return zero value if Delete type or Branch field is nil
	if a == nil || a.Branch == nil {
		return false
	}

	return *a.Branch
}

// GetTag returns the Tag field from the provided Delete. If the object is nil,
// or the field within the object is nil, it returns the zero value instead.
func (a *Delete) GetTag() bool {
	// return zero value if Delete type or Tag field is nil
	if a == nil || a.Tag == nil {
		return false
	}

	return *a.Tag
}

// SetBranch sets the Delete Branch field.
//
// When the provided Delete type is nil, it
// will set nothing and immediately return.
func (a *Delete) SetBranch(v bool) {
	// return if Events type is nil
	if a == nil {
		return
	}

	a.Branch = &v
}

// SetTag sets the Delete Tag field.
//
// When the provided Delete type is nil, it
// will set nothing and immediately return.
func (a *Delete) SetTag(v bool) {
	// return if Events type is nil
	if a == nil {
		return
	}

	a.Tag = &v
}
claire1618 marked this conversation as resolved.
Show resolved Hide resolved

import "github.com/go-vela/types/constants"

// Delete is the library representation of the various actions associated
// with the delete event webhook from the SCM.
type Delete struct {
Branch *bool `json:"branch"`
Tag *bool `json:"tag"`
}

// FromMask returns the Delete type resulting from the provided integer mask.
func (a *Delete) FromMask(mask int64) *Delete {
a.SetBranch(mask&constants.AllowDeleteBranch > 0)
a.SetTag(mask&constants.AllowDeleteTag > 0)

return a
}

// ToMask returns the integer mask of the values for the Delete set.
func (a *Delete) ToMask() int64 {
mask := int64(0)

if a.GetBranch() {
mask = mask | constants.AllowDeleteBranch
}

if a.GetTag() {
mask = mask | constants.AllowDeleteTag
}

return mask
}

// GetBranch returns the Branch field from the provided Delete. If the object is nil,
// or the field within the object is nil, it returns the zero value instead.
func (a *Delete) GetBranch() bool {
// return zero value if Delete type or Branch field is nil
if a == nil || a.Branch == nil {
return false
}

return *a.Branch
}

// GetTag returns the Tag field from the provided Delete. If the object is nil,
// or the field within the object is nil, it returns the zero value instead.
func (a *Delete) GetTag() bool {
// return zero value if Delete type or Tag field is nil
if a == nil || a.Tag == nil {
return false
}

return *a.Tag
}

// SetBranch sets the Delete Branch field.
//
// When the provided Delete type is nil, it
// will set nothing and immediately return.
func (a *Delete) SetBranch(v bool) {
// return if Events type is nil
if a == nil {
return
}

a.Branch = &v
}

// SetTag sets the Delete Tag field.
//
// When the provided Delete type is nil, it
// will set nothing and immediately return.
func (a *Delete) SetTag(v bool) {
// return if Events type is nil
if a == nil {
return
}

a.Tag = &v
}
108 changes: 108 additions & 0 deletions library/actions/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// SPDX-License-Identifier: Apache-2.0

package actions

import (
"reflect"
"testing"

"github.com/go-vela/types/constants"
)

func TestLibrary_Delete_Getters(t *testing.T) {
// setup tests
tests := []struct {
actions *Delete
want *Delete
}{
{
actions: testDelete(),
want: testDelete(),
},
{
actions: new(Delete),
want: new(Delete),
},
}

// run tests
for _, test := range tests {
if test.actions.GetBranch() != test.want.GetBranch() {
t.Errorf("GetBranch is %v, want %v", test.actions.GetBranch(), test.want.GetBranch())
}

if test.actions.GetTag() != test.want.GetTag() {
t.Errorf("GetTag is %v, want %v", test.actions.GetTag(), test.want.GetTag())
}
}
}

func TestLibrary_Delete_Setters(t *testing.T) {
// setup types
var a *Delete

// setup tests
tests := []struct {
actions *Delete
want *Delete
}{
{
actions: testDelete(),
want: testDelete(),
},
{
actions: a,
want: new(Delete),
},
}

// run tests
for _, test := range tests {
test.actions.SetBranch(test.want.GetBranch())
test.actions.SetTag(test.want.GetTag())

if test.actions.GetBranch() != test.want.GetBranch() {
t.Errorf("SetBranch is %v, want %v", test.actions.GetBranch(), test.want.GetBranch())
}

if test.actions.GetTag() != test.want.GetTag() {
t.Errorf("SetTag is %v, want %v", test.actions.GetTag(), test.want.GetTag())
}
}
}

func TestLibrary_Delete_FromMask(t *testing.T) {
// setup types
mask := testMask()

want := testDelete()

// run test
got := new(Delete).FromMask(mask)

if !reflect.DeepEqual(got, want) {
t.Errorf("FromMask is %v, want %v", got, want)
}
}

func TestLibrary_Delete_ToMask(t *testing.T) {
// setup types
actions := testDelete()

want := int64(constants.AllowDeleteBranch | constants.AllowDeleteTag)

// run test
got := actions.ToMask()

if want != got {
t.Errorf("ToMask is %v, want %v", got, want)
}
}

func testDelete() *Delete {
delete := new(Delete)

Check failure on line 103 in library/actions/delete_test.go

View workflow job for this annotation

GitHub Actions / full-review

redefines-builtin-id: redefinition of the built-in function delete (revive)

Check failure on line 103 in library/actions/delete_test.go

View workflow job for this annotation

GitHub Actions / diff-review

redefines-builtin-id: redefinition of the built-in function delete (revive)

Check failure on line 103 in library/actions/delete_test.go

View workflow job for this annotation

GitHub Actions / diff-review

redefines-builtin-id: redefinition of the built-in function delete (revive)

Check failure on line 103 in library/actions/delete_test.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] library/actions/delete_test.go#L103

redefines-builtin-id: redefinition of the built-in function delete (revive)
Raw output
library/actions/delete_test.go:103:2: redefines-builtin-id: redefinition of the built-in function delete (revive)
	delete := new(Delete)
	^
claire1618 marked this conversation as resolved.
Show resolved Hide resolved
delete.SetBranch(true)
delete.SetTag(true)

return delete
}
4 changes: 3 additions & 1 deletion library/actions/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ func testMask() int64 {
constants.AllowPullSync |
constants.AllowPullReopen |
constants.AllowDeployCreate |
constants.AllowCommentCreate,
constants.AllowCommentCreate |
constants.AllowDeleteBranch |
constants.AllowDeleteTag,
)
}
37 changes: 36 additions & 1 deletion library/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Events struct {
PullRequest *actions.Pull `json:"pull_request"`
Deployment *actions.Deploy `json:"deployment"`
Comment *actions.Comment `json:"comment"`
Delete *actions.Delete `json:"delete"`
}

// NewEventsFromMask is an instatiation function for the Events type that
Expand All @@ -23,13 +24,15 @@ func NewEventsFromMask(mask int64) *Events {
pullActions := new(actions.Pull).FromMask(mask)
deployActions := new(actions.Deploy).FromMask(mask)
commentActions := new(actions.Comment).FromMask(mask)
deleteActions := new(actions.Delete).FromMask(mask)

e := new(Events)

e.SetPush(pushActions)
e.SetPullRequest(pullActions)
e.SetDeployment(deployActions)
e.SetComment(commentActions)
e.SetDelete(deleteActions)

return e
}
Expand Down Expand Up @@ -71,12 +74,20 @@ func (e *Events) List() []string {
eventSlice = append(eventSlice, constants.EventComment+":"+constants.ActionEdited)
}

if e.GetDelete().GetBranch() {
eventSlice = append(eventSlice, constants.EventDelete+":"+constants.ActionBranch)
}

if e.GetDelete().GetTag() {
eventSlice = append(eventSlice, constants.EventDelete+":"+constants.ActionTag)
}

return eventSlice
}

// ToDatabase is an Events method that converts a nested Events struct into an integer event mask.
func (e *Events) ToDatabase() int64 {
claire1618 marked this conversation as resolved.
Show resolved Hide resolved
claire1618 marked this conversation as resolved.
Show resolved Hide resolved
claire1618 marked this conversation as resolved.
Show resolved Hide resolved
claire1618 marked this conversation as resolved.
Show resolved Hide resolved
claire1618 marked this conversation as resolved.
Show resolved Hide resolved
return 0 | e.GetPush().ToMask() | e.GetPullRequest().ToMask() | e.GetComment().ToMask() | e.GetDeployment().ToMask()
return 0 | e.GetPush().ToMask() | e.GetPullRequest().ToMask() | e.GetComment().ToMask() | e.GetDeployment().ToMask() | e.GetDelete().ToMask()
}

// GetPush returns the Push field from the provided Events. If the object is nil,
Expand Down Expand Up @@ -123,6 +134,17 @@ func (e *Events) GetComment() *actions.Comment {
return e.Comment
}

// GetDelete returns the Delete field from the provided Events. If the object is nil,
// or the field within the object is nil, it returns the zero value instead.
func (e *Events) GetDelete() *actions.Delete {
// return zero value if Events type or Comment field is nil
if e == nil || e.Delete == nil {
return new(actions.Delete)
}

return e.Delete
}

// SetPush sets the Events Push field.
//
// When the provided Events type is nil, it
Expand Down Expand Up @@ -174,3 +196,16 @@ func (e *Events) SetComment(v *actions.Comment) {

e.Comment = v
}

// SetDelete sets the Events Delete field.
//
// When the provided Events type is nil, it
// will set nothing and immediately return.
func (e *Events) SetDelete(v *actions.Delete) {
// return if Events type is nil
if e == nil {
return
}

e.Delete = v
}
Loading
Loading