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 8 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
2 changes: 2 additions & 0 deletions database/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
want.SetAllowDeploy(false)
want.SetAllowTag(false)
want.SetAllowComment(false)
want.SetAllowDelete(false)

Check failure on line 183 in database/repo_test.go

View workflow job for this annotation

GitHub Actions / test

want.SetAllowDelete undefined (type *library.Repo has no field or method SetAllowDelete)

Check failure on line 183 in database/repo_test.go

View workflow job for this annotation

GitHub Actions / diff-review

want.SetAllowDelete undefined (type *library.Repo has no field or method SetAllowDelete)

Check failure on line 183 in database/repo_test.go

View workflow job for this annotation

GitHub Actions / diff-review

want.SetAllowDelete undefined (type *library.Repo has no field or method SetAllowDelete)

Check failure on line 183 in database/repo_test.go

View workflow job for this annotation

GitHub Actions / test

want.SetAllowDelete undefined (type *library.Repo has no field or method SetAllowDelete)

Check failure on line 183 in database/repo_test.go

View workflow job for this annotation

GitHub Actions / full-review

want.SetAllowDelete undefined (type *library.Repo has no field or method SetAllowDelete)

Check failure on line 183 in database/repo_test.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] database/repo_test.go#L183

want.SetAllowDelete undefined (type *library.Repo has no field or method SetAllowDelete)
Raw output
database/repo_test.go:183:7: want.SetAllowDelete undefined (type *library.Repo has no field or method SetAllowDelete)
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 @@
r.SetAllowDeploy(false)
r.SetAllowTag(false)
r.SetAllowComment(false)
r.SetAllowDelete(false)

Check failure on line 341 in database/repo_test.go

View workflow job for this annotation

GitHub Actions / test

r.SetAllowDelete undefined (type *library.Repo has no field or method SetAllowDelete)

Check failure on line 341 in database/repo_test.go

View workflow job for this annotation

GitHub Actions / diff-review

r.SetAllowDelete undefined (type *library.Repo has no field or method SetAllowDelete) (typecheck)

Check failure on line 341 in database/repo_test.go

View workflow job for this annotation

GitHub Actions / diff-review

r.SetAllowDelete undefined (type *library.Repo has no field or method SetAllowDelete) (typecheck)

Check failure on line 341 in database/repo_test.go

View workflow job for this annotation

GitHub Actions / test

r.SetAllowDelete undefined (type *library.Repo has no field or method SetAllowDelete)

Check failure on line 341 in database/repo_test.go

View workflow job for this annotation

GitHub Actions / full-review

r.SetAllowDelete undefined (type *library.Repo has no field or method SetAllowDelete) (typecheck)

Check failure on line 341 in database/repo_test.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] database/repo_test.go#L341

r.SetAllowDelete undefined (type *library.Repo has no field or method SetAllowDelete) (typecheck)
Raw output
database/repo_test.go:341:4: r.SetAllowDelete undefined (type *library.Repo has no field or method SetAllowDelete) (typecheck)
// SPDX-License-Identifier: Apache-2.0
claire1618 marked this conversation as resolved.
Show resolved Hide resolved
r.SetAllowEvents(e)
r.SetPipelineType("yaml")
r.SetPreviousName("oldName")
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
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 {
deletion := new(Delete)
deletion.SetBranch(true)
deletion.SetTag(true)

return deletion
}
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
}
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 @@ -144,10 +155,15 @@ func testEvents() *Events {
comment.SetCreated(false)
comment.SetEdited(false)

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

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

return e
}
Loading
Loading