Skip to content

Commit

Permalink
feat: adding a delete_event (#340)
Browse files Browse the repository at this point in the history
* feat: adding delete event

* creating a delete event in types

* final touches, fingers crossed

* fixing tests

* fixing final lint errors

* fix: removing old AllowDelete work and removing a naked return

* fix: removing leftover allowDelete items from tests

* fix: changing library/secret.go to reflect library/repo.go when it comes to allowedEvents

* fix: modifying library/secret_test.go to reflect new secret.go changes

* fix: removing fix so that it can be included in another PR

* fix: small error when fixing merge conflicts

* fix: making the linter happy

* fix: changing ruleset.go to go with David May's comment

* rolling back last change

* rolling back last change

---------

Co-authored-by: Claire.Nicholas <[email protected]>
Co-authored-by: Easton Crupper <[email protected]>
  • Loading branch information
3 people authored Jan 11, 2024
1 parent a91bd54 commit 0d0e223
Show file tree
Hide file tree
Showing 12 changed files with 303 additions and 9 deletions.
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 @@ -21,4 +21,6 @@ const (
AllowCommentCreate
AllowCommentEdit
AllowSchedule
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: 2 additions & 1 deletion library/actions/comment.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: Apache-2.0

//
//nolint:dupl // similar code to delete.go
package actions

import "github.com/go-vela/types/constants"
Expand Down
84 changes: 84 additions & 0 deletions library/actions/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// SPDX-License-Identifier: Apache-2.0
//
//nolint:dupl // similar code to push.go
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
}
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 {
deletion := new(Delete)
deletion.SetBranch(true)
deletion.SetTag(true)

return deletion
}
3 changes: 2 additions & 1 deletion library/actions/push.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: Apache-2.0

//
//nolint:dupl // similar code to comment.go
package actions

import "github.com/go-vela/types/constants"
Expand Down
4 changes: 3 additions & 1 deletion library/actions/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ func testMask() int64 {
constants.AllowPullReopen |
constants.AllowDeployCreate |
constants.AllowCommentCreate |
constants.AllowSchedule,
constants.AllowSchedule |
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 @@ -15,6 +15,7 @@ type Events struct {
Deployment *actions.Deploy `json:"deployment"`
Comment *actions.Comment `json:"comment"`
Schedule *actions.Schedule `json:"schedule"`
Delete *actions.Delete `json:"delete"`
}

// NewEventsFromMask is an instatiation function for the Events type that
Expand All @@ -25,6 +26,7 @@ func NewEventsFromMask(mask int64) *Events {
deployActions := new(actions.Deploy).FromMask(mask)
commentActions := new(actions.Comment).FromMask(mask)
scheduleActions := new(actions.Schedule).FromMask(mask)
deleteActions := new(actions.Delete).FromMask(mask)

e := new(Events)

Expand All @@ -33,6 +35,7 @@ func NewEventsFromMask(mask int64) *Events {
e.SetDeployment(deployActions)
e.SetComment(commentActions)
e.SetSchedule(scheduleActions)
e.SetDelete(deleteActions)

return e
}
Expand Down Expand Up @@ -110,12 +113,20 @@ func (e *Events) List() []string {
eventSlice = append(eventSlice, constants.EventSchedule)
}

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 {
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 @@ -173,6 +184,17 @@ func (e *Events) GetSchedule() *actions.Schedule {
return e.Schedule
}

// 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 @@ -237,3 +259,16 @@ func (e *Events) SetSchedule(v *actions.Schedule) {

e.Schedule = 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
}
20 changes: 18 additions & 2 deletions library/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func TestLibrary_Events_Getters(t *testing.T) {
if !reflect.DeepEqual(test.events.GetComment(), test.want.GetComment()) {
t.Errorf("GetComment is %v, want %v", test.events.GetPush(), test.want.GetPush())
}

if !reflect.DeepEqual(test.events.GetDelete(), test.want.GetDelete()) {
t.Errorf("GetDelete is %v, want %v", test.events.GetDelete(), test.want.GetDelete())
}
}
}

Expand Down Expand Up @@ -71,6 +75,7 @@ func TestLibrary_Events_Setters(t *testing.T) {
test.events.SetPullRequest(test.want.GetPullRequest())
test.events.SetDeployment(test.want.GetDeployment())
test.events.SetComment(test.want.GetComment())
test.events.SetDelete(test.want.GetDelete())

if !reflect.DeepEqual(test.events.GetPush(), test.want.GetPush()) {
t.Errorf("SetPush is %v, want %v", test.events.GetPush(), test.want.GetPush())
Expand All @@ -87,14 +92,18 @@ func TestLibrary_Events_Setters(t *testing.T) {
if !reflect.DeepEqual(test.events.GetComment(), test.want.GetComment()) {
t.Errorf("SetComment is %v, want %v", test.events.GetComment(), test.want.GetComment())
}

if !reflect.DeepEqual(test.events.GetDelete(), test.want.GetDelete()) {
t.Errorf("SetDelete is %v, want %v", test.events.GetDelete(), test.want.GetDelete())
}
}
}

func TestLibrary_Events_List(t *testing.T) {
// setup types
e := testEvents()

want := []string{"push", "pull_request:opened", "pull_request:synchronize", "tag"}
want := []string{"push", "pull_request:opened", "pull_request:synchronize", "tag", "delete:branch", "delete:tag"}

// run test
got := e.List()
Expand All @@ -111,7 +120,9 @@ func TestLibrary_Events_NewEventsFromMask(t *testing.T) {
constants.AllowPushTag |
constants.AllowPullOpen |
constants.AllowPullSync |
constants.AllowPullReopen,
constants.AllowPullReopen |
constants.AllowDeleteBranch |
constants.AllowDeleteTag,
)

want := testEvents()
Expand Down Expand Up @@ -182,11 +193,16 @@ func testEvents() *Events {
schedule := new(actions.Schedule)
schedule.SetRun(false)

deletion := new(actions.Delete)
deletion.SetBranch(true)
deletion.SetTag(true)

e.SetPush(push)
e.SetPullRequest(pr)
e.SetDeployment(deploy)
e.SetComment(comment)
e.SetSchedule(schedule)
e.SetDelete(deletion)

return e
}
Loading

0 comments on commit 0d0e223

Please sign in to comment.