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

enhance(events): add NewEventsFromSlice method #366

Merged
merged 2 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions constants/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
8 changes: 8 additions & 0 deletions constants/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
53 changes: 53 additions & 0 deletions library/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
96 changes: 96 additions & 0 deletions library/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading