diff --git a/constants/action.go b/constants/action.go index ad4997a2..f47fca45 100644 --- a/constants/action.go +++ b/constants/action.go @@ -15,21 +15,12 @@ const ( // ActionEdited defines the action for the editing of pull requests or issue comments. ActionEdited = "edited" - // ActionLabeled defines the action for labeling a pull request. - ActionLabeled = "labeled" - // ActionRenamed defines the action for renaming a repository. ActionRenamed = "renamed" - // ActionReviewRequested defines the action for requesting a pull request review. - ActionReviewRequested = "review_requested" - // ActionSynchronize defines the action for the synchronizing of pull requests. ActionSynchronize = "synchronize" - // ActionSubmitted defines the action for submitting pull request reviews. - ActionSubmitted = "submitted" - // ActionTransferred defines the action for transferring repository ownership. ActionTransferred = "transferred" ) diff --git a/constants/allow_events.go b/constants/allow_events.go index f99a26f8..432dd2e1 100644 --- a/constants/allow_events.go +++ b/constants/allow_events.go @@ -6,17 +6,12 @@ package constants // Allowed repo events. const ( - AllowPush = 1 << iota // 00000001 = 1 - AllowPROpen // 00000010 = 2 - AllowPREdit // 00000100 = 4 - AllowPRSync // ... - AllowPRLabel - AllowPRReviewRequest - AllowTag - AllowDeploy + AllowPushBranch = 1 << iota // 00000001 = 1 + AllowPullOpen // 00000010 = 2 + AllowPullEdit // 00000100 = 4 + AllowPullSync // ... + AllowPushTag + AllowDeployCreate AllowCommentCreate AllowCommentEdit - AllowReviewSubmit - AllowReviewEdit - AllowSchedule ) diff --git a/database/repo_test.go b/database/repo_test.go index 39479ee7..69b2b1d9 100644 --- a/database/repo_test.go +++ b/database/repo_test.go @@ -306,7 +306,7 @@ func TestDatabase_RepoFromLibrary(t *testing.T) { // setup types r := new(library.Repo) e := new(library.Events) - e.SetPush(true) + e.SetPush(new(library.PushActions).FromMask(1)) r.SetID(1) r.SetUserID(1) diff --git a/library/event_actions.go b/library/event_actions.go new file mode 100644 index 00000000..3d5de863 --- /dev/null +++ b/library/event_actions.go @@ -0,0 +1,341 @@ +// Copyright (c) 2023 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package library + +import "github.com/go-vela/types/constants" + +// PushActions is the library representation of the various actions associated +// with the push event webhook from the SCM. +type PushActions struct { + Branch *bool `json:"branch"` + Tag *bool `json:"tag"` +} + +// PullActions is the library representation of the various actions associated +// with the pull_request event webhook from the SCM. +type PullActions struct { + Opened *bool `json:"opened"` + Edited *bool `json:"edited"` + Synchronize *bool `json:"synchronize"` +} + +// DeployActions is the library representation of the various actions associated +// with the deploy event webhook from the SCM. +type DeployActions struct { + Created *bool `json:"created"` +} + +// CommentActions is the library representation of the various actions associated +// with the comment event webhook from the SCM. +type CommentActions struct { + Created *bool `json:"created"` + Edited *bool `json:"edited"` +} + +// ScheduleActions is the library representation of the various actions associated +// with the schedule event processed by the server. +type ScheduleActions struct { + Schedule *bool `json:"schedule"` +} + +// ** +// PUSH ACTIONS +// ** + +// FromMask returns the PushActions type resulting from the provided integer mask. +func (a *PushActions) FromMask(mask int64) *PushActions { + a.SetBranch(mask&constants.AllowPushBranch > 0) + a.SetTag(mask&constants.AllowPushTag > 0) + + return a +} + +// ToMask returns the integer mask of the values for the PushActions set. +func (a *PushActions) ToMask() int64 { + mask := int64(0) + + if a.GetBranch() { + mask = mask | constants.AllowPushBranch + } + + if a.GetTag() { + mask = mask | constants.AllowPushTag + } + + return mask +} + +// GetBranch returns the Branch field from the provided PushActions. If the object is nil, +// or the field within the object is nil, it returns the zero value instead. +func (a *PushActions) GetBranch() bool { + // return zero value if PushActions 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 PushActions. If the object is nil, +// or the field within the object is nil, it returns the zero value instead. +func (a *PushActions) GetTag() bool { + // return zero value if PushActions type or Tag field is nil + if a == nil || a.Tag == nil { + return false + } + + return *a.Tag +} + +// SetBranch sets the PushActions Branch field. +// +// When the provided PushActions type is nil, it +// will set nothing and immediately return. +func (a *PushActions) SetBranch(v bool) { + // return if Events type is nil + if a == nil { + return + } + + a.Branch = &v +} + +// SetTag sets the PushActions Tag field. +// +// When the provided PushActions type is nil, it +// will set nothing and immediately return. +func (a *PushActions) SetTag(v bool) { + // return if Events type is nil + if a == nil { + return + } + + a.Tag = &v +} + +// ** +// PULL ACTIONS +// ** + +// FromMask returns the PullActions type resulting from the provided integer mask. +func (a *PullActions) FromMask(mask int64) *PullActions { + a.SetOpened(mask&constants.AllowPullOpen > 0) + a.SetSynchronize(mask&constants.AllowPullSync > 0) + a.SetEdited(mask&constants.AllowPullEdit > 0) + + return a +} + +// ToMask returns the integer mask of the values for the PullActions set. +func (a *PullActions) ToMask() int64 { + mask := int64(0) + + if a.GetOpened() { + mask = mask | constants.AllowPullOpen + } + + if a.GetSynchronize() { + mask = mask | constants.AllowPullSync + } + + if a.GetEdited() { + mask = mask | constants.AllowPullEdit + } + + return mask +} + +// GetOpened returns the Opened field from the provided PullActions. If the object is nil, +// or the field within the object is nil, it returns the zero value instead. +func (a *PullActions) GetOpened() bool { + // return zero value if PullActions type or Opened field is nil + if a == nil || a.Opened == nil { + return false + } + + return *a.Opened +} + +// GetSynchronize returns the Synchronize field from the provided PullActions. If the object is nil, +// or the field within the object is nil, it returns the zero value instead. +func (a *PullActions) GetSynchronize() bool { + // return zero value if PullActions type or Synchronize field is nil + if a == nil || a.Synchronize == nil { + return false + } + + return *a.Synchronize +} + +// GetEdited returns the Edited field from the provided PullActions. If the object is nil, +// or the field within the object is nil, it returns the zero value instead. +func (a *PullActions) GetEdited() bool { + // return zero value if PullActions type or Edited field is nil + if a == nil || a.Edited == nil { + return false + } + + return *a.Edited +} + +// SetOpened sets the PullActions Opened field. +// +// When the provided PullActions type is nil, it +// will set nothing and immediately return. +func (a *PullActions) SetOpened(v bool) { + // return if PullActions type is nil + if a == nil { + return + } + + a.Opened = &v +} + +// SetSynchronize sets the PullActions Synchronize field. +// +// When the provided PullActions type is nil, it +// will set nothing and immediately return. +func (a *PullActions) SetSynchronize(v bool) { + // return if PullActions type is nil + if a == nil { + return + } + + a.Synchronize = &v +} + +// SetEdited sets the PullActions Edited field. +// +// When the provided PullActions type is nil, it +// will set nothing and immediately return. +func (a *PullActions) SetEdited(v bool) { + // return if PullActions type is nil + if a == nil { + return + } + + a.Edited = &v +} + +// ** +// DEPLOY ACTIONS +// ** + +// FromMask returns the DeployActions type resulting from the provided integer mask. +func (a *DeployActions) FromMask(mask int64) *DeployActions { + a.SetCreated(mask&constants.AllowDeployCreate > 0) + + return a +} + +// ToMask returns the integer mask of the values for the DeployActions set. +func (a *DeployActions) ToMask() int64 { + mask := int64(0) + + if a.GetCreated() { + mask = mask | constants.AllowDeployCreate + } + + return mask +} + +// GetCreated returns the Created field from the provided DeployActions. If the object is nil, +// or the field within the object is nil, it returns the zero value instead. +func (a *DeployActions) GetCreated() bool { + // return zero value if DeployActions type or Created field is nil + if a == nil || a.Created == nil { + return false + } + + return *a.Created +} + +// SetCreated sets the DeployActions Created field. +// +// When the provided DeployActions type is nil, it +// will set nothing and immediately return. +func (a *DeployActions) SetCreated(v bool) { + // return if DeployActions type is nil + if a == nil { + return + } + + a.Created = &v +} + +// ** +// COMMENT ACTIONS +// ** + +// FromMask returns the CommentActions type resulting from the provided integer mask. +func (a *CommentActions) FromMask(mask int64) *CommentActions { + a.SetCreated(mask&constants.AllowCommentCreate > 0) + a.SetEdited(mask&constants.AllowCommentEdit > 0) + + return a +} + +// ToMask returns the integer mask of the values for the CommentActions set. +func (a *CommentActions) ToMask() int64 { + mask := int64(0) + + if a.GetCreated() { + mask = mask | constants.AllowCommentCreate + } + + if a.GetEdited() { + mask = mask | constants.AllowCommentEdit + } + + return mask +} + +// GetCreated returns the Created field from the provided CommentActions. If the object is nil, +// or the field within the object is nil, it returns the zero value instead. +func (a *CommentActions) GetCreated() bool { + // return zero value if Events type or Created field is nil + if a == nil || a.Created == nil { + return false + } + + return *a.Created +} + +// GetEdited returns the Edited field from the provided CommentActions. If the object is nil, +// or the field within the object is nil, it returns the zero value instead. +func (a *CommentActions) GetEdited() bool { + // return zero value if Events type or Edited field is nil + if a == nil || a.Edited == nil { + return false + } + + return *a.Edited +} + +// SetCreated sets the CommentActions Created field. +// +// When the provided Events type is nil, it +// will set nothing and immediately return. +func (a *CommentActions) SetCreated(v bool) { + // return if Events type is nil + if a == nil { + return + } + + a.Created = &v +} + +// SetEdited sets the CommentActions Edited field. +// +// When the provided Events type is nil, it +// will set nothing and immediately return. +func (a *CommentActions) SetEdited(v bool) { + // return if Events type is nil + if a == nil { + return + } + + a.Edited = &v +} diff --git a/library/event_actions_test.go b/library/event_actions_test.go new file mode 100644 index 00000000..bd648b9d --- /dev/null +++ b/library/event_actions_test.go @@ -0,0 +1,408 @@ +// Copyright (c) 2023 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package library + +import ( + "reflect" + "testing" + + "github.com/go-vela/types/constants" +) + +func TestLibrary_PushActions_Getters(t *testing.T) { + // setup tests + tests := []struct { + actions *PushActions + want *PushActions + }{ + { + actions: testPushActions(), + want: testPushActions(), + }, + { + actions: new(PushActions), + want: new(PushActions), + }, + } + + // 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_PushActions_Setters(t *testing.T) { + // setup types + var a *PushActions + + // setup tests + tests := []struct { + actions *PushActions + want *PushActions + }{ + { + actions: testPushActions(), + want: testPushActions(), + }, + { + actions: a, + want: new(PushActions), + }, + } + + // 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_PushActions_FromMask(t *testing.T) { + // setup types + mask := testMask() + + want := testPushActions() + + // run test + got := new(PushActions).FromMask(mask) + + if !reflect.DeepEqual(got, want) { + t.Errorf("FromMask is %v, want %v", got, want) + } +} + +func TestLibrary_PushActions_ToMask(t *testing.T) { + // setup types + actions := testPushActions() + + want := int64(constants.AllowPushBranch | constants.AllowPushTag) + + // run test + got := actions.ToMask() + + if want != got { + t.Errorf("ToMask is %v, want %v", got, want) + } +} + +func TestLibrary_PullActions_Getters(t *testing.T) { + // setup tests + tests := []struct { + actions *PullActions + want *PullActions + }{ + { + actions: testPullActions(), + want: testPullActions(), + }, + { + actions: new(PullActions), + want: new(PullActions), + }, + } + + // run tests + for _, test := range tests { + if test.actions.GetOpened() != test.want.GetOpened() { + t.Errorf("GetOpened is %v, want %v", test.actions.GetOpened(), test.want.GetOpened()) + } + + if test.actions.GetSynchronize() != test.want.GetSynchronize() { + t.Errorf("GetSynchronize is %v, want %v", test.actions.GetSynchronize(), test.want.GetSynchronize()) + } + + if test.actions.GetEdited() != test.want.GetEdited() { + t.Errorf("GetEdited is %v, want %v", test.actions.GetEdited(), test.want.GetEdited()) + } + } +} + +func TestLibrary_PullActions_Setters(t *testing.T) { + // setup types + var a *PullActions + + // setup tests + tests := []struct { + actions *PullActions + want *PullActions + }{ + { + actions: testPullActions(), + want: testPullActions(), + }, + { + actions: a, + want: new(PullActions), + }, + } + + // run tests + for _, test := range tests { + test.actions.SetOpened(test.want.GetOpened()) + test.actions.SetSynchronize(test.want.GetSynchronize()) + test.actions.SetEdited(test.want.GetEdited()) + + if test.actions.GetOpened() != test.want.GetOpened() { + t.Errorf("SetOpened is %v, want %v", test.actions.GetOpened(), test.want.GetOpened()) + } + + if test.actions.GetSynchronize() != test.want.GetSynchronize() { + t.Errorf("SetSynchronize is %v, want %v", test.actions.GetSynchronize(), test.want.GetSynchronize()) + } + + if test.actions.GetEdited() != test.want.GetEdited() { + t.Errorf("SetEdited is %v, want %v", test.actions.GetEdited(), test.want.GetEdited()) + } + } +} + +func TestLibrary_PullActions_FromMask(t *testing.T) { + // setup types + mask := testMask() + + want := testPullActions() + + // run test + got := new(PullActions).FromMask(mask) + + if !reflect.DeepEqual(got, want) { + t.Errorf("FromMask is %v, want %v", got, want) + } +} + +func TestLibrary_PullActions_ToMask(t *testing.T) { + // setup types + actions := testPullActions() + + want := int64(constants.AllowPullOpen | constants.AllowPullSync) + + // run test + got := actions.ToMask() + + if want != got { + t.Errorf("ToMask is %v, want %v", got, want) + } +} + +func TestLibrary_DeployActions_Getters(t *testing.T) { + // setup tests + tests := []struct { + actions *DeployActions + want *DeployActions + }{ + { + actions: testDeployActions(), + want: testDeployActions(), + }, + { + actions: new(DeployActions), + want: new(DeployActions), + }, + } + + // run tests + for _, test := range tests { + if test.actions.GetCreated() != test.want.GetCreated() { + t.Errorf("GetCreated is %v, want %v", test.actions.GetCreated(), test.want.GetCreated()) + } + } +} + +func TestLibrary_DeployActions_Setters(t *testing.T) { + // setup types + var a *DeployActions + + // setup tests + tests := []struct { + actions *DeployActions + want *DeployActions + }{ + { + actions: testDeployActions(), + want: testDeployActions(), + }, + { + actions: a, + want: new(DeployActions), + }, + } + + // run tests + for _, test := range tests { + test.actions.SetCreated(test.want.GetCreated()) + + if test.actions.GetCreated() != test.want.GetCreated() { + t.Errorf("SetCreated is %v, want %v", test.actions.GetCreated(), test.want.GetCreated()) + } + } +} + +func TestLibrary_DeployActions_FromMask(t *testing.T) { + // setup types + mask := testMask() + + want := testDeployActions() + + // run test + got := new(DeployActions).FromMask(mask) + + if !reflect.DeepEqual(got, want) { + t.Errorf("FromMask is %v, want %v", got, want) + } +} + +func TestLibrary_DeployActions_ToMask(t *testing.T) { + // setup types + actions := testDeployActions() + + want := int64(constants.AllowDeployCreate) + + // run test + got := actions.ToMask() + + if want != got { + t.Errorf("ToMask is %v, want %v", got, want) + } +} + +func TestLibrary_CommentActions_Getters(t *testing.T) { + // setup tests + tests := []struct { + actions *CommentActions + want *CommentActions + }{ + { + actions: testCommentActions(), + want: testCommentActions(), + }, + { + actions: new(CommentActions), + want: new(CommentActions), + }, + } + + // run tests + for _, test := range tests { + if test.actions.GetCreated() != test.want.GetCreated() { + t.Errorf("GetCreated is %v, want %v", test.actions.GetCreated(), test.want.GetCreated()) + } + + if test.actions.GetEdited() != test.want.GetEdited() { + t.Errorf("GetEdited is %v, want %v", test.actions.GetEdited(), test.want.GetEdited()) + } + } +} + +func TestLibrary_CommentActions_Setters(t *testing.T) { + // setup types + var a *CommentActions + + // setup tests + tests := []struct { + actions *CommentActions + want *CommentActions + }{ + { + actions: testCommentActions(), + want: testCommentActions(), + }, + { + actions: a, + want: new(CommentActions), + }, + } + + // run tests + for _, test := range tests { + test.actions.SetCreated(test.want.GetCreated()) + test.actions.SetEdited(test.want.GetEdited()) + + if test.actions.GetCreated() != test.want.GetCreated() { + t.Errorf("SetCreated is %v, want %v", test.actions.GetCreated(), test.want.GetCreated()) + } + + if test.actions.GetEdited() != test.want.GetEdited() { + t.Errorf("SetEdited is %v, want %v", test.actions.GetEdited(), test.want.GetEdited()) + } + } +} + +func TestLibrary_CommentActions_FromMask(t *testing.T) { + // setup types + mask := testMask() + + want := testCommentActions() + + // run test + got := new(CommentActions).FromMask(mask) + + if !reflect.DeepEqual(got, want) { + t.Errorf("FromMask is %v, want %v", got, want) + } +} + +func TestLibrary_CommentActions_ToMask(t *testing.T) { + // setup types + actions := testCommentActions() + + want := int64(constants.AllowCommentCreate) + + // run test + got := actions.ToMask() + + if want != got { + t.Errorf("ToMask is %v, want %v", got, want) + } +} + +func testPushActions() *PushActions { + push := new(PushActions) + push.SetBranch(true) + push.SetTag(true) + + return push +} + +func testPullActions() *PullActions { + pr := new(PullActions) + pr.SetOpened(true) + pr.SetSynchronize(true) + pr.SetEdited(false) + + return pr +} + +func testDeployActions() *DeployActions { + deploy := new(DeployActions) + deploy.SetCreated(true) + + return deploy +} + +func testCommentActions() *CommentActions { + comment := new(CommentActions) + comment.SetCreated(true) + comment.SetEdited(false) + + return comment +} + +func testMask() int64 { + return int64(constants.AllowPushBranch | constants.AllowPushTag | constants.AllowPullOpen | constants.AllowPullSync | constants.AllowDeployCreate | constants.AllowCommentCreate) +} diff --git a/library/events.go b/library/events.go index 6e253c7c..eb216607 100644 --- a/library/events.go +++ b/library/events.go @@ -6,69 +6,29 @@ package library import "github.com/go-vela/types/constants" -// PRActions is the library representation of the various actions associated -// with the pull_request event webhook from the SCM. -type PRActions struct { - Opened *bool `json:"opened"` - Edited *bool `json:"edited"` - Synchronize *bool `json:"synchronize"` - Labeled *bool `json:"labeled"` - ReviewRequest *bool `json:"review_request"` -} - -// CommentActions is the library representation of the various actions associated -// with the comment event webhook from the SCM. -type CommentActions struct { - Created *bool `json:"created"` - Edited *bool `json:"edited"` -} - -// ReviewActions is the library representation of the various actions associated -// with the pull_request_review event webhook from the SCM. -type ReviewActions struct { - Submitted *bool `json:"submitted"` - Edited *bool `json:"edited"` -} - // Events is the library representation of the various events that generate a // webhook from the SCM. type Events struct { - Push *bool `json:"push"` - PullRequest *PRActions `json:"pull_request"` - Tag *bool `json:"tag"` - Deployment *bool `json:"deployment"` + Push *PushActions `json:"push"` + PullRequest *PullActions `json:"pull_request"` + Deployment *DeployActions `json:"deployment"` Comment *CommentActions `json:"comment"` - Schedule *bool `json:"schedule"` - PullReview *ReviewActions `json:"pull_review"` } // NewEventsFromMask is an instatiation function for the Events type that // takes in an event mask integer value and populates the nested Events struct. func NewEventsFromMask(mask int64) *Events { - prActions := new(PRActions) - commentActions := new(CommentActions) - reviewActions := new(ReviewActions) - e := new(Events) + pushActions := new(PushActions).FromMask(mask) + pullActions := new(PullActions).FromMask(mask) + deployActions := new(DeployActions).FromMask(mask) + commentActions := new(CommentActions).FromMask(mask) - prActions.SetOpened(mask&constants.AllowPROpen > 0) - prActions.SetSynchronize(mask&constants.AllowPRSync > 0) - prActions.SetEdited(mask&constants.AllowPREdit > 0) - prActions.SetLabeled(mask&constants.AllowPRLabel > 0) - prActions.SetReviewRequest(mask&constants.AllowPRReviewRequest > 0) - - commentActions.SetCreated(mask&constants.AllowCommentCreate > 0) - commentActions.SetEdited(mask&constants.AllowCommentEdit > 0) - - reviewActions.SetSubmitted(mask&constants.AllowReviewSubmit > 0) - reviewActions.SetEdited(mask&constants.AllowReviewEdit > 0) + e := new(Events) - e.SetPush(mask&constants.AllowPush > 0) - e.SetPullRequest(prActions) - e.SetTag(mask&constants.AllowTag > 0) - e.SetDeployment(mask&constants.AllowDeploy > 0) + e.SetPush(pushActions) + e.SetPullRequest(pullActions) + e.SetDeployment(deployActions) e.SetComment(commentActions) - e.SetPullReview(reviewActions) - e.SetSchedule(mask&constants.AllowSchedule > 0) return e } @@ -78,7 +38,7 @@ func NewEventsFromMask(mask int64) *Events { func (e *Events) List() []string { eventSlice := []string{} - if e.GetPush() { + if e.GetPush().GetBranch() { eventSlice = append(eventSlice, constants.EventPush) } @@ -94,19 +54,11 @@ func (e *Events) List() []string { eventSlice = append(eventSlice, constants.EventPull+":"+constants.ActionEdited) } - if e.GetPullRequest().GetLabeled() { - eventSlice = append(eventSlice, constants.EventPull+":"+constants.ActionLabeled) - } - - if e.GetPullRequest().GetReviewRequest() { - eventSlice = append(eventSlice, constants.EventPull+":"+constants.ActionReviewRequested) - } - - if e.GetTag() { + if e.GetPush().GetTag() { eventSlice = append(eventSlice, constants.EventTag) } - if e.GetDeployment() { + if e.GetDeployment().GetCreated() { eventSlice = append(eventSlice, constants.EventDeploy) } @@ -118,123 +70,45 @@ func (e *Events) List() []string { eventSlice = append(eventSlice, constants.EventComment+":"+constants.ActionEdited) } - if e.GetPullReview().GetSubmitted() { - eventSlice = append(eventSlice, constants.EventPullReview+":"+constants.ActionSubmitted) - } - - if e.GetPullReview().GetEdited() { - eventSlice = append(eventSlice, constants.EventPullReview+":"+constants.ActionEdited) - } - - if e.GetSchedule() { - eventSlice = append(eventSlice, constants.EventSchedule) - } - return eventSlice } // ToDatabase is an Events method that converts a nested Events struct into an integer event mask. func (e *Events) ToDatabase() int64 { - events := int64(0) - - // OR operator adds a "1" flag for each event (e.g. 0001 | 0101 == 0101) - if e.GetPush() { - events = events | constants.AllowPush - } - - if e.GetPullRequest().GetOpened() { - events = events | constants.AllowPROpen - } - - if e.GetPullRequest().GetSynchronize() { - events = events | constants.AllowPRSync - } - - if e.GetPullRequest().GetEdited() { - events = events | constants.AllowPREdit - } - - if e.GetPullRequest().GetLabeled() { - events = events | constants.AllowPRLabel - } - - if e.GetPullRequest().GetReviewRequest() { - events = events | constants.AllowPRReviewRequest - } - - if e.GetTag() { - events = events | constants.AllowTag - } - - if e.GetDeployment() { - events = events | constants.AllowDeploy - } - - if e.GetComment().GetCreated() { - events = events | constants.AllowCommentCreate - } - - if e.GetComment().GetEdited() { - events = events | constants.AllowCommentEdit - } - - if e.GetPullReview().GetSubmitted() { - events = events | constants.AllowReviewSubmit - } - - if e.GetPullReview().GetEdited() { - events = events | constants.AllowReviewEdit - } - - if e.GetSchedule() { - events = events | constants.AllowSchedule - } - - return events + return 0 | e.GetPush().ToMask() | e.GetPullRequest().ToMask() | e.GetComment().ToMask() | e.GetDeployment().ToMask() } // GetPush returns the Push 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) GetPush() bool { +func (e *Events) GetPush() *PushActions { // return zero value if Events type or Push field is nil if e == nil || e.Push == nil { - return false + return new(PushActions) } - return *e.Push + return e.Push } // GetPullRequest returns the PullRequest 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) GetPullRequest() *PRActions { +func (e *Events) GetPullRequest() *PullActions { // return zero value if Events type or PullRequest field is nil if e == nil || e.PullRequest == nil { - return new(PRActions) + return new(PullActions) } return e.PullRequest } -// GetTag returns the Tag 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) GetTag() bool { - // return zero value if Events type or Tag field is nil - if e == nil || e.Tag == nil { - return false - } - - return *e.Tag -} - // GetDeployment returns the Deployment 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) GetDeployment() bool { +func (e *Events) GetDeployment() *DeployActions { // return zero value if Events type or Deployment field is nil if e == nil || e.Deployment == nil { - return false + return new(DeployActions) } - return *e.Deployment + return e.Deployment } // GetComment returns the Comment field from the provided Events. If the object is nil, @@ -248,145 +122,24 @@ func (e *Events) GetComment() *CommentActions { return e.Comment } -// GetSchedule returns the Schedule 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) GetSchedule() bool { - // return zero value if Events type or Schedule field is nil - if e == nil || e.Schedule == nil { - return false - } - - return *e.Schedule -} - -// GetPullReview returns the PullReview 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) GetPullReview() *ReviewActions { - // return zero value if Events type or PullReview field is nil - if e == nil || e.PullReview == nil { - return new(ReviewActions) - } - - return e.PullReview -} - -// GetOpened returns the Opened field from the provided PRActions. If the object is nil, -// or the field within the object is nil, it returns the zero value instead. -func (a *PRActions) GetOpened() bool { - // return zero value if PRActions type or Opened field is nil - if a == nil || a.Opened == nil { - return false - } - - return *a.Opened -} - -// GetSynchronize returns the Synchronize field from the provided PRActions. If the object is nil, -// or the field within the object is nil, it returns the zero value instead. -func (a *PRActions) GetSynchronize() bool { - // return zero value if PRActions type or Synchronize field is nil - if a == nil || a.Synchronize == nil { - return false - } - - return *a.Synchronize -} - -// GetEdited returns the Edited field from the provided PRActions. If the object is nil, -// or the field within the object is nil, it returns the zero value instead. -func (a *PRActions) GetEdited() bool { - // return zero value if PRActions type or Edited field is nil - if a == nil || a.Edited == nil { - return false - } - - return *a.Edited -} - -// GetLabeled returns the Labeled field from the provided PRActions. If the object is nil, -// or the field within the object is nil, it returns the zero value instead. -func (a *PRActions) GetLabeled() bool { - // return zero value if PRActions type or Labeled field is nil - if a == nil || a.Labeled == nil { - return false - } - - return *a.Labeled -} - -// GetReviewRequest returns the ReviewRequest field from the provided PRActions. If the object is nil, -// or the field within the object is nil, it returns the zero value instead. -func (a *PRActions) GetReviewRequest() bool { - // return zero value if PRActions type or ReviewRequest field is nil - if a == nil || a.ReviewRequest == nil { - return false - } - - return *a.ReviewRequest -} - -// GetCreated returns the Created field from the provided CommentActions. If the object is nil, -// or the field within the object is nil, it returns the zero value instead. -func (a *CommentActions) GetCreated() bool { - // return zero value if Events type or Created field is nil - if a == nil || a.Created == nil { - return false - } - - return *a.Created -} - -// GetEdited returns the Edited field from the provided CommentActions. If the object is nil, -// or the field within the object is nil, it returns the zero value instead. -func (a *CommentActions) GetEdited() bool { - // return zero value if Events type or Edited field is nil - if a == nil || a.Edited == nil { - return false - } - - return *a.Edited -} - -// GetSubmitted returns the Submitted field from the provided ReviewActions. If the object is nil, -// or the field within the object is nil, it returns the zero value instead. -func (a *ReviewActions) GetSubmitted() bool { - // return zero value if Events type or Submitted field is nil - if a == nil || a.Submitted == nil { - return false - } - - return *a.Submitted -} - -// GetEdited returns the Edited field from the provided ReviewActions. If the object is nil, -// or the field within the object is nil, it returns the zero value instead. -func (a *ReviewActions) GetEdited() bool { - // return zero value if Events type or Edited field is nil - if a == nil || a.Edited == nil { - return false - } - - return *a.Edited -} - // SetPush sets the Events Push field. // // When the provided Events type is nil, it // will set nothing and immediately return. -func (e *Events) SetPush(v bool) { +func (e *Events) SetPush(v *PushActions) { // return if Events type is nil if e == nil { return } - e.Push = &v + e.Push = v } // SetPullRequest sets the Events PullRequest field. // // When the provided Events type is nil, it // will set nothing and immediately return. -func (e *Events) SetPullRequest(v *PRActions) { +func (e *Events) SetPullRequest(v *PullActions) { // return if Events type is nil if e == nil { return @@ -395,30 +148,17 @@ func (e *Events) SetPullRequest(v *PRActions) { e.PullRequest = v } -// SetTag sets the Events Tag field. -// -// When the provided Events type is nil, it -// will set nothing and immediately return. -func (e *Events) SetTag(v bool) { - // return if Events type is nil - if e == nil { - return - } - - e.Tag = &v -} - // SetDeployment sets the Events Deployment field. // // When the provided Events type is nil, it // will set nothing and immediately return. -func (e *Events) SetDeployment(v bool) { +func (e *Events) SetDeployment(v *DeployActions) { // return if Events type is nil if e == nil { return } - e.Deployment = &v + e.Deployment = v } // SetComment sets the Events Comment field. @@ -433,146 +173,3 @@ func (e *Events) SetComment(v *CommentActions) { e.Comment = v } - -// SetSchedule sets the Events Schedule field. -// -// When the provided Events type is nil, it -// will set nothing and immediately return. -func (e *Events) SetSchedule(v bool) { - // return if Events type is nil - if e == nil { - return - } - - e.Schedule = &v -} - -// SetPullReview sets the Events PullReview field. -// -// When the provided Events type is nil, it -// will set nothing and immediately return. -func (e *Events) SetPullReview(v *ReviewActions) { - // return if Events type is nil - if e == nil { - return - } - - e.PullReview = v -} - -// SetOpened sets the PRActions Opened field. -// -// When the provided Events type is nil, it -// will set nothing and immediately return. -func (a *PRActions) SetOpened(v bool) { - // return if Events type is nil - if a == nil { - return - } - - a.Opened = &v -} - -// SetSynchronize sets the PRActions Synchronize field. -// -// When the provided Events type is nil, it -// will set nothing and immediately return. -func (a *PRActions) SetSynchronize(v bool) { - // return if Events type is nil - if a == nil { - return - } - - a.Synchronize = &v -} - -// SetEdited sets the PRActions Edited field. -// -// When the provided Events type is nil, it -// will set nothing and immediately return. -func (a *PRActions) SetEdited(v bool) { - // return if Events type is nil - if a == nil { - return - } - - a.Edited = &v -} - -// SetLabeled sets the PRActions Labeled field. -// -// When the provided Events type is nil, it -// will set nothing and immediately return. -func (a *PRActions) SetLabeled(v bool) { - // return if Events type is nil - if a == nil { - return - } - - a.Labeled = &v -} - -// SetReviewRequest sets the PRActions ReviewRequest field. -// -// When the provided Events type is nil, it -// will set nothing and immediately return. -func (a *PRActions) SetReviewRequest(v bool) { - // return if Events type is nil - if a == nil { - return - } - - a.ReviewRequest = &v -} - -// SetCreated sets the CommentActions Created field. -// -// When the provided Events type is nil, it -// will set nothing and immediately return. -func (a *CommentActions) SetCreated(v bool) { - // return if Events type is nil - if a == nil { - return - } - - a.Created = &v -} - -// SetEdited sets the CommentActions Edited field. -// -// When the provided Events type is nil, it -// will set nothing and immediately return. -func (a *CommentActions) SetEdited(v bool) { - // return if Events type is nil - if a == nil { - return - } - - a.Edited = &v -} - -// SetSubmitted sets the ReviewActions Submitted field. -// -// When the provided Events type is nil, it -// will set nothing and immediately return. -func (a *ReviewActions) SetSubmitted(v bool) { - // return if Events type is nil - if a == nil { - return - } - - a.Submitted = &v -} - -// SetEdited sets the ReviewActions Edited field. -// -// When the provided Events type is nil, it -// will set nothing and immediately return. -func (a *ReviewActions) SetEdited(v bool) { - // return if Events type is nil - if a == nil { - return - } - - a.Edited = &v -} diff --git a/library/events_test.go b/library/events_test.go new file mode 100644 index 00000000..3f5c2fa0 --- /dev/null +++ b/library/events_test.go @@ -0,0 +1,147 @@ +// Copyright (c) 2023 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package library + +import ( + "reflect" + "testing" + + "github.com/go-vela/types/constants" +) + +func TestLibrary_Events_Getters(t *testing.T) { + // setup tests + tests := []struct { + events *Events + want *Events + }{ + { + events: testEvents(), + want: testEvents(), + }, + { + events: new(Events), + want: new(Events), + }, + } + + // run tests + for _, test := range tests { + if !reflect.DeepEqual(test.events.GetPush(), test.want.GetPush()) { + t.Errorf("GetPush is %v, want %v", test.events.GetPush(), test.want.GetPush()) + } + + if !reflect.DeepEqual(test.events.GetPullRequest(), test.want.GetPullRequest()) { + t.Errorf("GetPullRequest is %v, want %v", test.events.GetPush(), test.want.GetPush()) + } + + if !reflect.DeepEqual(test.events.GetDeployment(), test.want.GetDeployment()) { + t.Errorf("GetDeployment is %v, want %v", test.events.GetPush(), test.want.GetPush()) + } + + if !reflect.DeepEqual(test.events.GetComment(), test.want.GetComment()) { + t.Errorf("GetComment is %v, want %v", test.events.GetPush(), test.want.GetPush()) + } + } +} + +func TestLibrary_Events_Setters(t *testing.T) { + // setup types + var e *Events + + // setup tests + tests := []struct { + events *Events + want *Events + }{ + { + events: testEvents(), + want: testEvents(), + }, + { + events: e, + want: new(Events), + }, + } + + // run tests + for _, test := range tests { + test.events.SetPush(test.want.GetPush()) + test.events.SetPullRequest(test.want.GetPullRequest()) + test.events.SetDeployment(test.want.GetDeployment()) + test.events.SetComment(test.want.GetComment()) + + if !reflect.DeepEqual(test.events.GetPush(), test.want.GetPush()) { + t.Errorf("SetPush is %v, want %v", test.events.GetPush(), test.want.GetPush()) + } + + if !reflect.DeepEqual(test.events.GetPullRequest(), test.want.GetPullRequest()) { + t.Errorf("SetPullRequest is %v, want %v", test.events.GetPullRequest(), test.want.GetPullRequest()) + } + + if !reflect.DeepEqual(test.events.GetDeployment(), test.want.GetDeployment()) { + t.Errorf("SetDeployment is %v, want %v", test.events.GetDeployment(), test.want.GetDeployment()) + } + + if !reflect.DeepEqual(test.events.GetComment(), test.want.GetComment()) { + t.Errorf("SetComment is %v, want %v", test.events.GetComment(), test.want.GetComment()) + } + } +} + +func TestLibrary_Events_List(t *testing.T) { + // setup types + e := testEvents() + + want := []string{"push", "pull_request:opened", "pull_request:synchronize", "tag"} + + // run test + got := e.List() + + if !reflect.DeepEqual(got, want) { + t.Errorf("List is %v, want %v", got, want) + } +} + +func TestLibrary_Events_NewEventsFromMask(t *testing.T) { + // setup mask + mask := int64(constants.AllowPushBranch | constants.AllowPushTag | constants.AllowPullOpen | constants.AllowPullSync) + + want := testEvents() + + // run test + got := NewEventsFromMask(mask) + + if !reflect.DeepEqual(got, want) { + t.Errorf("NewEventsFromMask is %v, want %v", got, want) + } +} + +func testEvents() *Events { + e := new(Events) + + pr := new(PullActions) + pr.SetOpened(true) + pr.SetSynchronize(true) + pr.SetEdited(false) + + push := new(PushActions) + push.SetBranch(true) + push.SetTag(true) + + deploy := new(DeployActions) + deploy.SetCreated(false) + + comment := new(CommentActions) + comment.SetCreated(false) + comment.SetEdited(false) + + e.SetPush(push) + e.SetPullRequest(pr) + e.SetDeployment(deploy) + e.SetComment(comment) + + return e +} diff --git a/library/repo_test.go b/library/repo_test.go index 187adc51..4bc0b03a 100644 --- a/library/repo_test.go +++ b/library/repo_test.go @@ -348,17 +348,3 @@ func testRepo() *Repo { return r } - -func testEvents() *Events { - e := new(Events) - - pr := new(PRActions) - pr.SetOpened(true) - pr.SetSynchronize(true) - - e.SetPush(true) - e.SetPullRequest(pr) - e.SetTag(true) - - return e -} diff --git a/yaml/ruleset.go b/yaml/ruleset.go index 1cd9eaec..664cdd4b 100644 --- a/yaml/ruleset.go +++ b/yaml/ruleset.go @@ -158,10 +158,6 @@ func (r *Rules) UnmarshalYAML(unmarshal func(interface{}) error) error { events = append(events, constants.EventComment+":"+constants.ActionCreated, constants.EventComment+":"+constants.ActionEdited) - case constants.EventPullReview: - events = append(events, - constants.EventPullReview+":"+constants.ActionSubmitted, - constants.EventPullReview+":"+constants.ActionEdited) default: events = append(events, e) }