Skip to content

Commit

Permalink
Merge branch 'main' into feat/pr-labeled-action
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 authored Mar 26, 2024
2 parents 84ad8bb + a7d60df commit 396d35f
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 16 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@3ab4101902695724f9365a384f86c1074d94e18c # v3.24.7
uses: github/codeql-action/init@05963f47d870e2cb19a537396c1f668a348c7d8f # v3.24.8
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -58,7 +58,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@3ab4101902695724f9365a384f86c1074d94e18c # v3.24.7
uses: github/codeql-action/autobuild@05963f47d870e2cb19a537396c1f668a348c7d8f # v3.24.8

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
Expand All @@ -72,4 +72,4 @@ jobs:
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@3ab4101902695724f9365a384f86c1074d94e18c # v3.24.7
uses: github/codeql-action/analyze@05963f47d870e2cb19a537396c1f668a348c7d8f # v3.24.8
3 changes: 3 additions & 0 deletions constants/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,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"
)
2 changes: 2 additions & 0 deletions database/schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,10 @@ func TestDatabase_Schedule_Validate(t *testing.T) {
if err == nil {
t.Errorf("Validate should have returned err")
}

return
}

if err != nil {
t.Errorf("Validate returned err: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/go-vela/types
go 1.21

require (
github.com/adhocore/gronx v1.8.0
github.com/adhocore/gronx v1.8.1
github.com/buildkite/yaml v0.0.0-20181016232759-0caa5f0796e3
github.com/drone/envsubst v1.0.3
github.com/ghodss/yaml v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/adhocore/gronx v1.8.0 h1:BHgzaGyS7zPmuMVqiIxyAwvKpwAX+bR7bCxDVacfhuo=
github.com/adhocore/gronx v1.8.0/go.mod h1:7oUY1WAU8rEJWmAxXR2DN0JaO4gi9khSgKjiRypqteg=
github.com/adhocore/gronx v1.8.1 h1:F2mLTG5sB11z7vplwD4iydz3YCEjstSfYmCrdSm3t6A=
github.com/adhocore/gronx v1.8.1/go.mod h1:7oUY1WAU8rEJWmAxXR2DN0JaO4gi9khSgKjiRypqteg=
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/buildkite/yaml v0.0.0-20181016232759-0caa5f0796e3 h1:q+sMKdA6L8LyGVudTkpGoC73h6ak2iWSPFiFo/pFOU8=
Expand Down
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 @@ -198,6 +198,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
41 changes: 31 additions & 10 deletions library/schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,43 @@ func TestLibrary_Schedule_Getters(t *testing.T) {
if test.schedule.GetID() != test.want.GetID() {
t.Errorf("GetID is %v, want %v", test.schedule.GetID(), test.want.GetID())
}

if test.schedule.GetRepoID() != test.want.GetRepoID() {
t.Errorf("GetRepoID is %v, want %v", test.schedule.GetRepoID(), test.want.GetRepoID())
}

if test.schedule.GetActive() != test.want.GetActive() {
t.Errorf("GetActive is %v, want %v", test.schedule.GetActive(), test.want.GetActive())
}

if test.schedule.GetName() != test.want.GetName() {
t.Errorf("GetName is %v, want %v", test.schedule.GetName(), test.want.GetName())
}

if test.schedule.GetEntry() != test.want.GetEntry() {
t.Errorf("GetEntry is %v, want %v", test.schedule.GetEntry(), test.want.GetEntry())
}

if test.schedule.GetCreatedAt() != test.want.GetCreatedAt() {
t.Errorf("GetCreatedAt is %v, want %v", test.schedule.GetCreatedAt(), test.want.GetCreatedAt())
}

if test.schedule.GetCreatedBy() != test.want.GetCreatedBy() {
t.Errorf("GetCreatedBy is %v, want %v", test.schedule.GetCreatedBy(), test.want.GetCreatedBy())
}

if test.schedule.GetUpdatedAt() != test.want.GetUpdatedAt() {
t.Errorf("GetUpdatedAt is %v, want %v", test.schedule.GetUpdatedAt(), test.want.GetUpdatedAt())
}

if test.schedule.GetUpdatedBy() != test.want.GetUpdatedBy() {
t.Errorf("GetUpdatedBy is %v, want %v", test.schedule.GetUpdatedBy(), test.want.GetUpdatedBy())
}

if test.schedule.GetScheduledAt() != test.want.GetScheduledAt() {
t.Errorf("GetScheduledAt is %v, want %v", test.schedule.GetScheduledAt(), test.want.GetScheduledAt())
}

if test.schedule.GetBranch() != test.want.GetBranch() {
t.Errorf("GetBranch is %v, want %v", test.schedule.GetBranch(), test.want.GetBranch())
}
Expand Down Expand Up @@ -98,46 +108,57 @@ func TestLibrary_Schedule_Setters(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
test.schedule.SetID(test.want.GetID())
test.schedule.SetRepoID(test.want.GetRepoID())
test.schedule.SetActive(test.want.GetActive())
test.schedule.SetName(test.want.GetName())
test.schedule.SetEntry(test.want.GetEntry())
test.schedule.SetCreatedAt(test.want.GetCreatedAt())
test.schedule.SetCreatedBy(test.want.GetCreatedBy())
test.schedule.SetUpdatedAt(test.want.GetUpdatedAt())
test.schedule.SetUpdatedBy(test.want.GetUpdatedBy())
test.schedule.SetScheduledAt(test.want.GetScheduledAt())
test.schedule.SetBranch(test.want.GetBranch())

if test.schedule.GetID() != test.want.GetID() {
t.Errorf("SetID is %v, want %v", test.schedule.GetID(), test.want.GetID())
}
test.schedule.SetRepoID(test.want.GetRepoID())

if test.schedule.GetRepoID() != test.want.GetRepoID() {
t.Errorf("SetRepoID is %v, want %v", test.schedule.GetRepoID(), test.want.GetRepoID())
}
test.schedule.SetActive(test.want.GetActive())

if test.schedule.GetActive() != test.want.GetActive() {
t.Errorf("SetActive is %v, want %v", test.schedule.GetActive(), test.want.GetActive())
}
test.schedule.SetName(test.want.GetName())

if test.schedule.GetName() != test.want.GetName() {
t.Errorf("SetName is %v, want %v", test.schedule.GetName(), test.want.GetName())
}
test.schedule.SetEntry(test.want.GetEntry())

if test.schedule.GetEntry() != test.want.GetEntry() {
t.Errorf("SetEntry is %v, want %v", test.schedule.GetEntry(), test.want.GetEntry())
}
test.schedule.SetCreatedAt(test.want.GetCreatedAt())

if test.schedule.GetCreatedAt() != test.want.GetCreatedAt() {
t.Errorf("SetCreatedAt is %v, want %v", test.schedule.GetCreatedAt(), test.want.GetCreatedAt())
}
test.schedule.SetCreatedBy(test.want.GetCreatedBy())

if test.schedule.GetCreatedBy() != test.want.GetCreatedBy() {
t.Errorf("SetCreatedBy is %v, want %v", test.schedule.GetCreatedBy(), test.want.GetCreatedBy())
}
test.schedule.SetUpdatedAt(test.want.GetUpdatedAt())

if test.schedule.GetUpdatedAt() != test.want.GetUpdatedAt() {
t.Errorf("SetUpdatedAt is %v, want %v", test.schedule.GetUpdatedAt(), test.want.GetUpdatedAt())
}
test.schedule.SetUpdatedBy(test.want.GetUpdatedBy())

if test.schedule.GetUpdatedBy() != test.want.GetUpdatedBy() {
t.Errorf("SetUpdatedBy is %v, want %v", test.schedule.GetUpdatedBy(), test.want.GetUpdatedBy())
}
test.schedule.SetScheduledAt(test.want.GetScheduledAt())

if test.schedule.GetScheduledAt() != test.want.GetScheduledAt() {
t.Errorf("SetScheduledAt is %v, want %v", test.schedule.GetScheduledAt(), test.want.GetScheduledAt())
}
test.schedule.SetBranch(test.want.GetBranch())

if test.schedule.GetBranch() != test.want.GetBranch() {
t.Errorf("SetBranch is %v, want %v", test.schedule.GetBranch(), test.want.GetBranch())
}
Expand Down
1 change: 1 addition & 0 deletions raw/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ func TestStringSliceMap_Value(t *testing.T) {
t.Errorf("StringSliceMap.Value() error = %v, wantErr %v", err, tt.wantErr)
return
}

if !reflect.DeepEqual(got, tt.want) {
t.Errorf("StringSliceMap.Value() = %v, want %v", got, tt.want)
}
Expand Down

0 comments on commit 396d35f

Please sign in to comment.