Skip to content

Commit

Permalink
feat: remodling EventDelete to resemble EventTag (#348)
Browse files Browse the repository at this point in the history
* fix: enabling a delete event to be allowed

* fix: remodling delete event to resemble tag

* fix: making linter happy

* fix: fixing tests

* fix: changing some syntax in push.go

* fix: changing some syntax in push.go

* fix: making sure to only set BUILD_TAG for a delete:tag as opposed to all delete events

* fix: making sure to only set BUILD_TAG for a delete:tag as opposed to all delete events

* fix: clarifying comment

---------

Co-authored-by: Claire.Nicholas <Z00B3R3@w6nxmk4t6t.target.com>
  • Loading branch information
claire1618 and Claire.Nicholas authored Jan 22, 2024
1 parent 8a6ef2d commit d058de2
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 299 deletions.
4 changes: 2 additions & 2 deletions constants/allow_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ const (
AllowCommentCreate
AllowCommentEdit
AllowSchedule
AllowDeleteBranch
AllowDeleteTag
AllowPushDeleteBranch
AllowPushDeleteTag
)
3 changes: 1 addition & 2 deletions library/actions/comment.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// 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: 0 additions & 84 deletions library/actions/delete.go

This file was deleted.

108 changes: 0 additions & 108 deletions library/actions/delete_test.go

This file was deleted.

3 changes: 2 additions & 1 deletion library/actions/deploy.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 schedule.go
package actions

import "github.com/go-vela/types/constants"
Expand Down
3 changes: 2 additions & 1 deletion library/actions/pull.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 push.go
package actions

import "github.com/go-vela/types/constants"
Expand Down
64 changes: 62 additions & 2 deletions library/actions/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ import "github.com/go-vela/types/constants"
// Push is the library representation of the various actions associated
// with the push event webhook from the SCM.
type Push struct {
Branch *bool `json:"branch"`
Tag *bool `json:"tag"`
Branch *bool `json:"branch"`
Tag *bool `json:"tag"`
DeleteBranch *bool `json:"delete_branch"`
DeleteTag *bool `json:"delete_tag"`
}

// FromMask returns the Push type resulting from the provided integer mask.
func (a *Push) FromMask(mask int64) *Push {
a.SetBranch(mask&constants.AllowPushBranch > 0)
a.SetTag(mask&constants.AllowPushTag > 0)
a.SetDeleteBranch(mask&constants.AllowPushDeleteBranch > 0)
a.SetDeleteTag(mask&constants.AllowPushDeleteTag > 0)

return a
}
Expand All @@ -32,6 +36,14 @@ func (a *Push) ToMask() int64 {
mask = mask | constants.AllowPushTag
}

if a.GetDeleteBranch() {
mask = mask | constants.AllowPushDeleteBranch
}

if a.GetDeleteTag() {
mask = mask | constants.AllowPushDeleteTag
}

return mask
}

Expand All @@ -57,6 +69,28 @@ func (a *Push) GetTag() bool {
return *a.Tag
}

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

return *a.DeleteBranch
}

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

return *a.DeleteTag
}

// SetBranch sets the Push Branch field.
//
// When the provided Push type is nil, it
Expand All @@ -82,3 +116,29 @@ func (a *Push) SetTag(v bool) {

a.Tag = &v
}

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

a.DeleteBranch = &v
}

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

a.DeleteTag = &v
}
20 changes: 16 additions & 4 deletions library/actions/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ func TestLibrary_Push_Setters(t *testing.T) {
for _, test := range tests {
test.actions.SetBranch(test.want.GetBranch())
test.actions.SetTag(test.want.GetTag())
test.actions.SetDeleteBranch(test.want.GetDeleteBranch())
test.actions.SetDeleteTag(test.want.GetDeleteTag())

if test.actions.GetBranch() != test.want.GetBranch() {
t.Errorf("SetBranch is %v, want %v", test.actions.GetBranch(), test.want.GetBranch())
Expand All @@ -68,6 +70,14 @@ func TestLibrary_Push_Setters(t *testing.T) {
if test.actions.GetTag() != test.want.GetTag() {
t.Errorf("SetTag is %v, want %v", test.actions.GetTag(), test.want.GetTag())
}

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

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

Expand All @@ -89,7 +99,7 @@ func TestLibrary_Push_ToMask(t *testing.T) {
// setup types
actions := testPush()

want := int64(constants.AllowPushBranch | constants.AllowPushTag)
want := int64(constants.AllowPushBranch | constants.AllowPushTag | constants.AllowPushDeleteBranch | constants.AllowPushDeleteTag)

// run test
got := actions.ToMask()
Expand All @@ -103,6 +113,8 @@ func testPush() *Push {
push := new(Push)
push.SetBranch(true)
push.SetTag(true)
push.SetDeleteBranch(true)
push.SetDeleteTag(true)

return push
}
Expand All @@ -111,13 +123,13 @@ func testMask() int64 {
return int64(
constants.AllowPushBranch |
constants.AllowPushTag |
constants.AllowPushDeleteBranch |
constants.AllowPushDeleteTag |
constants.AllowPullOpen |
constants.AllowPullSync |
constants.AllowPullReopen |
constants.AllowDeployCreate |
constants.AllowCommentCreate |
constants.AllowSchedule |
constants.AllowDeleteBranch |
constants.AllowDeleteTag,
constants.AllowSchedule,
)
}
3 changes: 2 additions & 1 deletion library/actions/schedule.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 deploy.go
package actions

import "github.com/go-vela/types/constants"
Expand Down
Loading

0 comments on commit d058de2

Please sign in to comment.