diff --git a/constants/allow_events.go b/constants/allow_events.go index 3e00f2d2..4a163a73 100644 --- a/constants/allow_events.go +++ b/constants/allow_events.go @@ -21,6 +21,6 @@ const ( AllowCommentCreate AllowCommentEdit AllowSchedule - AllowDeleteBranch - AllowDeleteTag + AllowPushDeleteBranch + AllowPushDeleteTag ) diff --git a/library/actions/comment.go b/library/actions/comment.go index c0e04298..499cfed6 100644 --- a/library/actions/comment.go +++ b/library/actions/comment.go @@ -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" diff --git a/library/actions/delete.go b/library/actions/delete.go deleted file mode 100644 index fa88046d..00000000 --- a/library/actions/delete.go +++ /dev/null @@ -1,84 +0,0 @@ -// 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 -} diff --git a/library/actions/delete_test.go b/library/actions/delete_test.go deleted file mode 100644 index 3c7cd539..00000000 --- a/library/actions/delete_test.go +++ /dev/null @@ -1,108 +0,0 @@ -// 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 -} diff --git a/library/actions/deploy.go b/library/actions/deploy.go index e2b663bf..d4970611 100644 --- a/library/actions/deploy.go +++ b/library/actions/deploy.go @@ -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" diff --git a/library/actions/pull.go b/library/actions/pull.go index 9a134a6f..609c6248 100644 --- a/library/actions/pull.go +++ b/library/actions/pull.go @@ -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" diff --git a/library/actions/push.go b/library/actions/push.go index 9853f454..ed19c48f 100644 --- a/library/actions/push.go +++ b/library/actions/push.go @@ -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 } @@ -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 } @@ -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 @@ -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 +} diff --git a/library/actions/push_test.go b/library/actions/push_test.go index f8cd4c3b..330444ba 100644 --- a/library/actions/push_test.go +++ b/library/actions/push_test.go @@ -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()) @@ -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()) + } } } @@ -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() @@ -103,6 +113,8 @@ func testPush() *Push { push := new(Push) push.SetBranch(true) push.SetTag(true) + push.SetDeleteBranch(true) + push.SetDeleteTag(true) return push } @@ -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, ) } diff --git a/library/actions/schedule.go b/library/actions/schedule.go index a3ca9d36..634e638f 100644 --- a/library/actions/schedule.go +++ b/library/actions/schedule.go @@ -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" diff --git a/library/build.go b/library/build.go index 3c872933..2ca9c368 100644 --- a/library/build.go +++ b/library/build.go @@ -202,6 +202,17 @@ func (b *Build) Environment(workspace, channel string) map[string]string { envs["VELA_BUILD_TAG"] = tag } + // check if the Build event is delete:tag + if strings.EqualFold(b.GetEvent(), constants.EventDelete) && strings.EqualFold(b.GetEventAction(), constants.ActionTag) { + // capture the tag reference, which has been stored in the Branch variable due to issues that arose + // when the Ref is set to the deleted tag + tag := b.GetBranch() + + // add the tag reference to the list + envs["BUILD_TAG"] = tag + envs["VELA_BUILD_TAG"] = tag + } + return envs } diff --git a/library/events.go b/library/events.go index fbcb23d0..88074d80 100644 --- a/library/events.go +++ b/library/events.go @@ -15,7 +15,6 @@ 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 @@ -26,7 +25,6 @@ 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) @@ -35,7 +33,6 @@ func NewEventsFromMask(mask int64) *Events { e.SetDeployment(deployActions) e.SetComment(commentActions) e.SetSchedule(scheduleActions) - e.SetDelete(deleteActions) return e } @@ -72,9 +69,9 @@ func (e *Events) Allowed(event, action string) bool { case constants.EventSchedule: allowed = e.GetSchedule().GetRun() case constants.EventDelete + ":" + constants.ActionBranch: - allowed = e.GetDelete().GetBranch() + allowed = e.GetPush().GetDeleteBranch() case constants.EventDelete + ":" + constants.ActionTag: - allowed = e.GetDelete().GetTag() + allowed = e.GetPush().GetDeleteTag() } return allowed @@ -125,11 +122,11 @@ func (e *Events) List() []string { eventSlice = append(eventSlice, constants.EventSchedule) } - if e.GetDelete().GetBranch() { + if e.GetPush().GetDeleteBranch() { eventSlice = append(eventSlice, constants.EventDelete+":"+constants.ActionBranch) } - if e.GetDelete().GetTag() { + if e.GetPush().GetDeleteTag() { eventSlice = append(eventSlice, constants.EventDelete+":"+constants.ActionTag) } @@ -143,8 +140,7 @@ func (e *Events) ToDatabase() int64 { e.GetPullRequest().ToMask() | e.GetComment().ToMask() | e.GetDeployment().ToMask() | - e.GetSchedule().ToMask() | - e.GetDelete().ToMask() + e.GetSchedule().ToMask() } // GetPush returns the Push field from the provided Events. If the object is nil, @@ -202,17 +198,6 @@ 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 @@ -277,16 +262,3 @@ 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 -} diff --git a/library/events_test.go b/library/events_test.go index c1bafe94..a26cc4dc 100644 --- a/library/events_test.go +++ b/library/events_test.go @@ -55,10 +55,6 @@ func TestLibrary_Events_Getters(t *testing.T) { if !reflect.DeepEqual(test.events.GetSchedule(), test.want.GetSchedule()) { t.Errorf("GetSchedule is %v, want %v", test.events.GetSchedule(), test.want.GetSchedule()) } - - if !reflect.DeepEqual(test.events.GetDelete(), test.want.GetDelete()) { - t.Errorf("GetDelete is %v, want %v", test.events.GetDelete(), test.want.GetDelete()) - } } } @@ -94,7 +90,6 @@ func TestLibrary_Events_Setters(t *testing.T) { test.events.SetDeployment(test.want.GetDeployment()) test.events.SetComment(test.want.GetComment()) test.events.SetSchedule(test.want.GetSchedule()) - 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()) @@ -115,10 +110,6 @@ func TestLibrary_Events_Setters(t *testing.T) { if !reflect.DeepEqual(test.events.GetSchedule(), test.want.GetSchedule()) { t.Errorf("SetSchedule is %v, want %v", test.events.GetSchedule(), test.want.GetSchedule()) } - - if !reflect.DeepEqual(test.events.GetDelete(), test.want.GetDelete()) { - t.Errorf("SetDelete is %v, want %v", test.events.GetDelete(), test.want.GetDelete()) - } } } @@ -163,19 +154,19 @@ func TestLibrary_Events_NewEventsFromMask_ToDatabase(t *testing.T) { maskOne := int64( constants.AllowPushBranch | constants.AllowPushTag | + constants.AllowPushDeleteBranch | constants.AllowPullOpen | constants.AllowPullSync | constants.AllowPullReopen | constants.AllowCommentCreate | - constants.AllowSchedule | - constants.AllowDeleteBranch, + constants.AllowSchedule, ) maskTwo := int64( - constants.AllowPullEdit | + constants.AllowPushDeleteTag | + constants.AllowPullEdit | constants.AllowCommentEdit | - constants.AllowDeployCreate | - constants.AllowDeleteTag, + constants.AllowDeployCreate, ) wantOne, wantTwo := testEvents() @@ -248,8 +239,10 @@ func testEvents() (*Events, *Events) { e1 := &Events{ Push: &actions.Push{ - Branch: &tBool, - Tag: &tBool, + Branch: &tBool, + Tag: &tBool, + DeleteBranch: &tBool, + DeleteTag: &fBool, }, PullRequest: &actions.Pull{ Opened: &tBool, @@ -267,16 +260,14 @@ func testEvents() (*Events, *Events) { Schedule: &actions.Schedule{ Run: &tBool, }, - Delete: &actions.Delete{ - Branch: &tBool, - Tag: &fBool, - }, } e2 := &Events{ Push: &actions.Push{ - Branch: &fBool, - Tag: &fBool, + Branch: &fBool, + Tag: &fBool, + DeleteBranch: &fBool, + DeleteTag: &tBool, }, PullRequest: &actions.Pull{ Opened: &fBool, @@ -294,10 +285,6 @@ func testEvents() (*Events, *Events) { Schedule: &actions.Schedule{ Run: &fBool, }, - Delete: &actions.Delete{ - Branch: &fBool, - Tag: &tBool, - }, } return e1, e2 diff --git a/webhook.go b/webhook.go index d4802cfa..1941e38f 100644 --- a/webhook.go +++ b/webhook.go @@ -10,8 +10,7 @@ import ( ) var ( - skipDeleteEventMsg = "tag/branch delete event" - skipDirectiveMsg = "skip ci directive found in commit title/message" + skipDirectiveMsg = "skip ci directive found in commit title/message" ) // PullRequest defines the data pulled from PRs while @@ -39,13 +38,6 @@ type Webhook struct { func (w *Webhook) ShouldSkip() (bool, string) { // push or tag event if strings.EqualFold(constants.EventPush, w.Build.GetEvent()) || strings.EqualFold(constants.EventTag, w.Build.GetEvent()) { - // the head commit will return null in the hook - // payload from the scm when the event is - // associated with a branch/tag delete - if len(w.Build.GetCommit()) == 0 { - return true, skipDeleteEventMsg - } - // check for skip ci directive in message or title if hasSkipDirective(w.Build.GetMessage()) || hasSkipDirective(w.Build.GetTitle()) { diff --git a/webhook_test.go b/webhook_test.go index 061ad8a4..7beb39be 100644 --- a/webhook_test.go +++ b/webhook_test.go @@ -17,72 +17,62 @@ func TestWebhook_ShouldSkip(t *testing.T) { wantString string }{ { - &Webhook{Build: testPushBuild("testing [SKIP CI]", "", constants.EventPush, true)}, + &Webhook{Build: testPushBuild("testing [SKIP CI]", "", constants.EventPush)}, true, skipDirectiveMsg, }, { - &Webhook{Build: testPushBuild("testing", "wip [ci skip]", constants.EventPush, true)}, + &Webhook{Build: testPushBuild("testing", "wip [ci skip]", constants.EventPush)}, true, skipDirectiveMsg, }, { - &Webhook{Build: testPushBuild("testing [skip VELA]", "", constants.EventPush, true)}, + &Webhook{Build: testPushBuild("testing [skip VELA]", "", constants.EventPush)}, true, skipDirectiveMsg, }, { - &Webhook{Build: testPushBuild("testing", "wip [vela skip]", constants.EventPush, true)}, + &Webhook{Build: testPushBuild("testing", "wip [vela skip]", constants.EventPush)}, true, skipDirectiveMsg, }, { - &Webhook{Build: testPushBuild("testing ***NO_CI*** ok", "nothing", constants.EventPush, true)}, + &Webhook{Build: testPushBuild("testing ***NO_CI*** ok", "nothing", constants.EventPush)}, true, skipDirectiveMsg, }, { - &Webhook{Build: testPushBuild("testing ok", "nothing", constants.EventPush, false)}, - true, - skipDeleteEventMsg, - }, - { - &Webhook{Build: testPushBuild("testing ok", "nothing", constants.EventPush, true)}, + &Webhook{Build: testPushBuild("testing ok", "nothing", constants.EventPush)}, false, "", }, { - &Webhook{Build: testPushBuild("testing [SKIP CI]", "", constants.EventTag, true)}, + &Webhook{Build: testPushBuild("testing [SKIP CI]", "", constants.EventTag)}, true, skipDirectiveMsg, }, { - &Webhook{Build: testPushBuild("testing", "wip [ci skip]", constants.EventTag, true)}, + &Webhook{Build: testPushBuild("testing", "wip [ci skip]", constants.EventTag)}, true, skipDirectiveMsg, }, { - &Webhook{Build: testPushBuild("testing [skip VELA]", "", constants.EventTag, true)}, + &Webhook{Build: testPushBuild("testing [skip VELA]", "", constants.EventTag)}, true, skipDirectiveMsg, }, { - &Webhook{Build: testPushBuild("testing", "wip [vela skip]", constants.EventTag, true)}, + &Webhook{Build: testPushBuild("testing", "wip [vela skip]", constants.EventTag)}, true, skipDirectiveMsg, }, { - &Webhook{Build: testPushBuild("testing ***NO_CI*** ok", "nothing", constants.EventTag, true)}, + &Webhook{Build: testPushBuild("testing ***NO_CI*** ok", "nothing", constants.EventTag)}, true, skipDirectiveMsg, }, { - &Webhook{Build: testPushBuild("testing ok", "nothing", constants.EventTag, false)}, - true, - skipDeleteEventMsg, - }, - { - &Webhook{Build: testPushBuild("testing ok", "nothing", constants.EventTag, true)}, + &Webhook{Build: testPushBuild("testing ok", "nothing", constants.EventTag)}, false, "", }, @@ -102,7 +92,7 @@ func TestWebhook_ShouldSkip(t *testing.T) { } } -func testPushBuild(message, title, event string, hasCommit bool) *library.Build { +func testPushBuild(message, title, event string) *library.Build { b := new(library.Build) b.SetEvent(event) @@ -115,9 +105,7 @@ func testPushBuild(message, title, event string, hasCommit bool) *library.Build b.SetTitle(title) } - if hasCommit { - b.SetCommit("deadbeef") - } + b.SetCommit("deadbeef") return b }