From e9cd33aaba610598fe582605048533cf78b224ea Mon Sep 17 00:00:00 2001 From: Easton Crupper <65553218+ecrupper@users.noreply.github.com> Date: Thu, 21 Mar 2024 10:33:52 -0400 Subject: [PATCH] enhance(events): add NewEventsFromSlice method (#366) * enhance(events): add NewEventsFromSlice method * linter overlord --- constants/action.go | 3 ++ constants/event.go | 8 ++++ library/events.go | 53 +++++++++++++++++++++++ library/events_test.go | 96 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 160 insertions(+) diff --git a/constants/action.go b/constants/action.go index f1e18c6e..802ddf79 100644 --- a/constants/action.go +++ b/constants/action.go @@ -30,4 +30,7 @@ const ( // ActionTag defines the action for deleting a tag. ActionTag = "tag" + + // ActionRun defines the action for running a schedule. + ActionRun = "run" ) diff --git a/constants/event.go b/constants/event.go index 9d59f7e5..c2ec26aa 100644 --- a/constants/event.go +++ b/constants/event.go @@ -27,4 +27,12 @@ const ( // EventTag defines the event type for build and repo tag events. EventTag = "tag" + + // Alternates for common user inputs that do not match our set constants. + + // EventPullAlternate defines the alternate event type for build and repo pull_request events. + EventPullAlternate = "pull" + + // EventDeployAlternate defines the alternate event type for build and repo deployment events. + EventDeployAlternate = "deploy" ) diff --git a/library/events.go b/library/events.go index 88074d80..45f1f908 100644 --- a/library/events.go +++ b/library/events.go @@ -37,6 +37,59 @@ func NewEventsFromMask(mask int64) *Events { return e } +// NewEventsFromSlice is an instantiation function for the Events type that +// takes in a slice of event strings and populates the nested Events struct. +func NewEventsFromSlice(events []string) *Events { + mask := int64(0) + + // iterate through all events provided + for _, event := range events { + switch event { + // push actions + case constants.EventPush, constants.EventPush + ":branch": + mask = mask | constants.AllowPushBranch + case constants.EventTag, constants.EventPush + ":" + constants.EventTag: + mask = mask | constants.AllowPushTag + case constants.EventDelete + ":" + constants.ActionBranch: + mask = mask | constants.AllowPushDeleteBranch + case constants.EventDelete + ":" + constants.ActionTag: + mask = mask | constants.AllowPushDeleteTag + case constants.EventDelete: + mask = mask | constants.AllowPushDeleteBranch | constants.AllowPushDeleteTag + + // pull_request actions + case constants.EventPull, constants.EventPullAlternate: + mask = mask | constants.AllowPullOpen | constants.AllowPullSync | constants.AllowPullReopen + case constants.EventPull + ":" + constants.ActionOpened: + mask = mask | constants.AllowPullOpen + case constants.EventPull + ":" + constants.ActionEdited: + mask = mask | constants.AllowPullEdit + case constants.EventPull + ":" + constants.ActionSynchronize: + mask = mask | constants.AllowPullSync + case constants.EventPull + ":" + constants.ActionReopened: + mask = mask | constants.AllowPullReopen + + // deployment actions + case constants.EventDeploy, constants.EventDeployAlternate, constants.EventDeploy + ":" + constants.ActionCreated: + mask = mask | constants.AllowDeployCreate + + // comment actions + case constants.EventComment: + mask = mask | constants.AllowCommentCreate | constants.AllowCommentEdit + case constants.EventComment + ":" + constants.ActionCreated: + mask = mask | constants.AllowCommentCreate + case constants.EventComment + ":" + constants.ActionEdited: + mask = mask | constants.AllowCommentEdit + + // schedule actions + case constants.EventSchedule, constants.EventSchedule + ":" + constants.ActionRun: + mask = mask | constants.AllowSchedule + } + } + + return NewEventsFromMask(mask) +} + // Allowed determines whether or not an event + action is allowed based on whether // its event:action is set to true in the Events struct. func (e *Events) Allowed(event, action string) bool { diff --git a/library/events_test.go b/library/events_test.go index a26cc4dc..ef60257b 100644 --- a/library/events_test.go +++ b/library/events_test.go @@ -194,6 +194,102 @@ func TestLibrary_Events_NewEventsFromMask_ToDatabase(t *testing.T) { } } +func Test_NewEventsFromSlice(t *testing.T) { + // setup types + tBool := true + fBool := false + + e1, e2 := testEvents() + + // setup tests + tests := []struct { + name string + events []string + want *Events + }{ + { + name: "action specific events to e1", + events: []string{"push:branch", "push:tag", "delete:branch", "pull_request:opened", "pull_request:synchronize", "pull_request:reopened", "comment:created", "schedule:run"}, + want: e1, + }, + { + name: "action specific events to e2", + events: []string{"delete:tag", "pull_request:edited", "deployment:created", "comment:edited"}, + want: e2, + }, + { + name: "general events", + events: []string{"push", "pull", "deploy", "comment", "schedule", "tag", "delete"}, + want: &Events{ + Push: &actions.Push{ + Branch: &tBool, + Tag: &tBool, + DeleteBranch: &tBool, + DeleteTag: &tBool, + }, + PullRequest: &actions.Pull{ + Opened: &tBool, + Reopened: &tBool, + Edited: &fBool, + Synchronize: &tBool, + }, + Deployment: &actions.Deploy{ + Created: &tBool, + }, + Comment: &actions.Comment{ + Created: &tBool, + Edited: &tBool, + }, + Schedule: &actions.Schedule{ + Run: &tBool, + }, + }, + }, + { + name: "double events", + events: []string{"push", "push:branch", "pull_request", "pull_request:opened"}, + want: &Events{ + Push: &actions.Push{ + Branch: &tBool, + Tag: &fBool, + DeleteBranch: &fBool, + DeleteTag: &fBool, + }, + PullRequest: &actions.Pull{ + Opened: &tBool, + Reopened: &tBool, + Edited: &fBool, + Synchronize: &tBool, + }, + Deployment: &actions.Deploy{ + Created: &fBool, + }, + Comment: &actions.Comment{ + Created: &fBool, + Edited: &fBool, + }, + Schedule: &actions.Schedule{ + Run: &fBool, + }, + }, + }, + { + name: "empty events", + events: []string{}, + want: NewEventsFromMask(0), + }, + } + + // run tests + for _, test := range tests { + got := NewEventsFromSlice(test.events) + + if diff := cmp.Diff(test.want, got); diff != "" { + t.Errorf("PopulateEvents failed for %s mismatch (-want +got):\n%s", test.name, diff) + } + } +} + func TestLibrary_Events_Allowed(t *testing.T) { // setup types eventsOne, eventsTwo := testEvents()