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: return err in NewEventsFromSlice #369

Merged
merged 1 commit into from
Apr 5, 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
9 changes: 7 additions & 2 deletions library/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package library

import (
"fmt"

"github.com/go-vela/types/constants"
"github.com/go-vela/types/library/actions"
)
Expand Down Expand Up @@ -39,7 +41,7 @@ func NewEventsFromMask(mask int64) *Events {

// 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 {
func NewEventsFromSlice(events []string) (*Events, error) {
mask := int64(0)

// iterate through all events provided
Expand Down Expand Up @@ -88,10 +90,13 @@ func NewEventsFromSlice(events []string) *Events {
// schedule actions
case constants.EventSchedule, constants.EventSchedule + ":" + constants.ActionRun:
mask = mask | constants.AllowSchedule

default:
return nil, fmt.Errorf("invalid event provided: %s", event)
}
}

return NewEventsFromMask(mask)
return NewEventsFromMask(mask), nil
}

// Allowed determines whether or not an event + action is allowed based on whether
Expand Down
43 changes: 33 additions & 10 deletions library/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,19 +207,22 @@ func Test_NewEventsFromSlice(t *testing.T) {

// setup tests
tests := []struct {
name string
events []string
want *Events
name string
events []string
want *Events
failure bool
}{
{
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", "pull_request:unlabeled"},
want: e1,
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", "pull_request:unlabeled"},
want: e1,
failure: false,
},
{
name: "action specific events to e2",
events: []string{"delete:tag", "pull_request:edited", "deployment:created", "comment:edited", "pull_request:labeled"},
want: e2,
name: "action specific events to e2",
events: []string{"delete:tag", "pull_request:edited", "deployment:created", "comment:edited", "pull_request:labeled"},
want: e2,
failure: false,
},
{
name: "general events",
Expand Down Expand Up @@ -250,6 +253,7 @@ func Test_NewEventsFromSlice(t *testing.T) {
Run: &tBool,
},
},
failure: false,
},
{
name: "double events",
Expand Down Expand Up @@ -280,17 +284,36 @@ func Test_NewEventsFromSlice(t *testing.T) {
Run: &fBool,
},
},
failure: false,
},
{
name: "empty events",
events: []string{},
want: NewEventsFromMask(0),
},
{
name: "invalid events",
events: []string{"foo:bar"},
want: nil,
failure: true,
},
}

// run tests
for _, test := range tests {
got := NewEventsFromSlice(test.events)
got, err := NewEventsFromSlice(test.events)

if test.failure {
if err == nil {
t.Errorf("NewEventsFromSlice should have returned err")
}

continue
}

if err != nil {
t.Errorf("NewEventsFromSlice returned err: %v", err)
}

if diff := cmp.Diff(test.want, got); diff != "" {
t.Errorf("PopulateEvents failed for %s mismatch (-want +got):\n%s", test.name, diff)
Expand Down
Loading