Skip to content

Commit

Permalink
enhance(secret): use allow event system (#341)
Browse files Browse the repository at this point in the history
* enhance(secret): use allow event system

* fix some typos and linter changes

* remove repo version of 'Allowed'
  • Loading branch information
ecrupper authored Jan 5, 2024
1 parent 26e54c8 commit a91bd54
Show file tree
Hide file tree
Showing 14 changed files with 512 additions and 147 deletions.
1 change: 1 addition & 0 deletions constants/allow_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ const (
AllowDeployCreate
AllowCommentCreate
AllowCommentEdit
AllowSchedule
)
8 changes: 8 additions & 0 deletions database/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Secret struct {
Type sql.NullString `sql:"type"`
Images pq.StringArray `sql:"images" gorm:"type:varchar(1000)"`
Events pq.StringArray `sql:"events" gorm:"type:varchar(1000)"`
AllowEvents sql.NullInt64 `sql:"allow_events"`
AllowCommand sql.NullBool `sql:"allow_command"`
CreatedAt sql.NullInt64 `sql:"created_at"`
CreatedBy sql.NullString `sql:"created_by"`
Expand Down Expand Up @@ -151,6 +152,11 @@ func (s *Secret) Nullify() *Secret {
s.Type.Valid = false
}

// check if the AllowEvents field should be false
if s.AllowEvents.Int64 == 0 {
s.AllowEvents.Valid = false
}

// check if the CreatedAt field should be false
if s.CreatedAt.Int64 == 0 {
s.CreatedAt.Valid = false
Expand Down Expand Up @@ -188,6 +194,7 @@ func (s *Secret) ToLibrary() *library.Secret {
secret.SetType(s.Type.String)
secret.SetImages(s.Images)
secret.SetEvents(s.Events)
secret.SetAllowEvents(library.NewEventsFromMask(s.AllowEvents.Int64))
secret.SetAllowCommand(s.AllowCommand.Bool)
secret.SetCreatedAt(s.CreatedAt.Int64)
secret.SetCreatedBy(s.CreatedBy.String)
Expand Down Expand Up @@ -274,6 +281,7 @@ func SecretFromLibrary(s *library.Secret) *Secret {
Type: sql.NullString{String: s.GetType(), Valid: true},
Images: pq.StringArray(s.GetImages()),
Events: pq.StringArray(s.GetEvents()),
AllowEvents: sql.NullInt64{Int64: s.GetAllowEvents().ToDatabase(), Valid: true},
AllowCommand: sql.NullBool{Bool: s.GetAllowCommand(), Valid: true},
CreatedAt: sql.NullInt64{Int64: s.GetCreatedAt(), Valid: true},
CreatedBy: sql.NullString{String: s.GetCreatedBy(), Valid: true},
Expand Down
26 changes: 15 additions & 11 deletions database/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,18 @@ func TestDatabase_Secret_Nullify(t *testing.T) {
var s *Secret

want := &Secret{
ID: sql.NullInt64{Int64: 0, Valid: false},
Org: sql.NullString{String: "", Valid: false},
Repo: sql.NullString{String: "", Valid: false},
Team: sql.NullString{String: "", Valid: false},
Name: sql.NullString{String: "", Valid: false},
Value: sql.NullString{String: "", Valid: false},
Type: sql.NullString{String: "", Valid: false},
CreatedAt: sql.NullInt64{Int64: 0, Valid: false},
CreatedBy: sql.NullString{String: "", Valid: false},
UpdatedAt: sql.NullInt64{Int64: 0, Valid: false},
UpdatedBy: sql.NullString{String: "", Valid: false},
ID: sql.NullInt64{Int64: 0, Valid: false},
Org: sql.NullString{String: "", Valid: false},
Repo: sql.NullString{String: "", Valid: false},
Team: sql.NullString{String: "", Valid: false},
Name: sql.NullString{String: "", Valid: false},
Value: sql.NullString{String: "", Valid: false},
Type: sql.NullString{String: "", Valid: false},
AllowEvents: sql.NullInt64{Int64: 0, Valid: false},
CreatedAt: sql.NullInt64{Int64: 0, Valid: false},
CreatedBy: sql.NullString{String: "", Valid: false},
UpdatedAt: sql.NullInt64{Int64: 0, Valid: false},
UpdatedBy: sql.NullString{String: "", Valid: false},
}

// setup tests
Expand Down Expand Up @@ -168,6 +169,7 @@ func TestDatabase_Secret_ToLibrary(t *testing.T) {
want.SetType("repo")
want.SetImages([]string{"alpine"})
want.SetEvents([]string{"push", "tag", "deployment"})
want.SetAllowEvents(library.NewEventsFromMask(1))
want.SetAllowCommand(true)
want.SetCreatedAt(tsCreate)
want.SetCreatedBy("octocat")
Expand Down Expand Up @@ -291,6 +293,7 @@ func TestDatabase_SecretFromLibrary(t *testing.T) {
s.SetType("repo")
s.SetImages([]string{"alpine"})
s.SetEvents([]string{"push", "tag", "deployment"})
s.SetAllowEvents(library.NewEventsFromMask(1))
s.SetAllowCommand(true)
s.SetCreatedAt(tsCreate)
s.SetCreatedBy("octocat")
Expand Down Expand Up @@ -320,6 +323,7 @@ func testSecret() *Secret {
Type: sql.NullString{String: "repo", Valid: true},
Images: []string{"alpine"},
Events: []string{"push", "tag", "deployment"},
AllowEvents: sql.NullInt64{Int64: 1, Valid: true},
AllowCommand: sql.NullBool{Bool: true, Valid: true},
CreatedAt: sql.NullInt64{Int64: tsCreate, Valid: true},
CreatedBy: sql.NullString{String: "octocat", Valid: true},
Expand Down
3 changes: 1 addition & 2 deletions library/actions/comment.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
//
//nolint:dupl // ignore dup code

package actions

import "github.com/go-vela/types/constants"
Expand Down
3 changes: 1 addition & 2 deletions library/actions/push.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
//
//nolint:dupl // ignore dup code

package actions

import "github.com/go-vela/types/constants"
Expand Down
3 changes: 2 additions & 1 deletion library/actions/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func testMask() int64 {
constants.AllowPullSync |
constants.AllowPullReopen |
constants.AllowDeployCreate |
constants.AllowCommentCreate,
constants.AllowCommentCreate |
constants.AllowSchedule,
)
}
53 changes: 53 additions & 0 deletions library/actions/schedule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// SPDX-License-Identifier: Apache-2.0

package actions

import "github.com/go-vela/types/constants"

// Schedule is the library representation of the various actions associated
// with the schedule event.
type Schedule struct {
Run *bool `json:"run"`
}

// FromMask returns the Schedule type resulting from the provided integer mask.
func (a *Schedule) FromMask(mask int64) *Schedule {
a.SetRun(mask&constants.AllowSchedule > 0)

return a
}

// ToMask returns the integer mask of the values for the Schedule set.
func (a *Schedule) ToMask() int64 {
mask := int64(0)

if a.GetRun() {
mask = mask | constants.AllowSchedule
}

return mask
}

// GetRun returns the Run field from the provided Schedule. If the object is nil,
// or the field within the object is nil, it returns the zero value instead.
func (a *Schedule) GetRun() bool {
// return zero value if Schedule type or Run field is nil
if a == nil || a.Run == nil {
return false
}

return *a.Run
}

// SetRun sets the Schedule Run field.
//
// When the provided Schedule type is nil, it
// will set nothing and immediately return.
func (a *Schedule) SetRun(v bool) {
// return if Schedule type is nil
if a == nil {
return
}

a.Run = &v
}
98 changes: 98 additions & 0 deletions library/actions/schedule_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// SPDX-License-Identifier: Apache-2.0

package actions

import (
"reflect"
"testing"

"github.com/go-vela/types/constants"
)

func TestLibrary_Schedule_Getters(t *testing.T) {
// setup tests
tests := []struct {
actions *Schedule
want *Schedule
}{
{
actions: testSchedule(),
want: testSchedule(),
},
{
actions: new(Schedule),
want: new(Schedule),
},
}

// run tests
for _, test := range tests {
if test.actions.GetRun() != test.want.GetRun() {
t.Errorf("GetRun is %v, want %v", test.actions.GetRun(), test.want.GetRun())
}
}
}

func TestLibrary_Schedule_Setters(t *testing.T) {
// setup types
var a *Schedule

// setup tests
tests := []struct {
actions *Schedule
want *Schedule
}{
{
actions: testSchedule(),
want: testSchedule(),
},
{
actions: a,
want: new(Schedule),
},
}

// run tests
for _, test := range tests {
test.actions.SetRun(test.want.GetRun())

if test.actions.GetRun() != test.want.GetRun() {
t.Errorf("SetRun is %v, want %v", test.actions.GetRun(), test.want.GetRun())
}
}
}

func TestLibrary_Schedule_FromMask(t *testing.T) {
// setup types
mask := testMask()

want := testSchedule()

// run test
got := new(Schedule).FromMask(mask)

if !reflect.DeepEqual(got, want) {
t.Errorf("FromMask is %v, want %v", got, want)
}
}

func TestLibrary_Schedule_ToMask(t *testing.T) {
// setup types
actions := testSchedule()

want := int64(constants.AllowSchedule)

// run test
got := actions.ToMask()

if want != got {
t.Errorf("ToMask is %v, want %v", got, want)
}
}

func testSchedule() *Schedule {
schedule := new(Schedule)
schedule.SetRun(true)

return schedule
}
71 changes: 67 additions & 4 deletions library/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (
// Events is the library representation of the various events that generate a
// webhook from the SCM.
type Events struct {
Push *actions.Push `json:"push"`
PullRequest *actions.Pull `json:"pull_request"`
Deployment *actions.Deploy `json:"deployment"`
Comment *actions.Comment `json:"comment"`
Push *actions.Push `json:"push"`
PullRequest *actions.Pull `json:"pull_request"`
Deployment *actions.Deploy `json:"deployment"`
Comment *actions.Comment `json:"comment"`
Schedule *actions.Schedule `json:"schedule"`
}

// NewEventsFromMask is an instatiation function for the Events type that
Expand All @@ -23,17 +24,51 @@ func NewEventsFromMask(mask int64) *Events {
pullActions := new(actions.Pull).FromMask(mask)
deployActions := new(actions.Deploy).FromMask(mask)
commentActions := new(actions.Comment).FromMask(mask)
scheduleActions := new(actions.Schedule).FromMask(mask)

e := new(Events)

e.SetPush(pushActions)
e.SetPullRequest(pullActions)
e.SetDeployment(deployActions)
e.SetComment(commentActions)
e.SetSchedule(scheduleActions)

return e
}

// EventAllowed determines whether or not an event is allowed based on the repository settings.
func (e *Events) Allowed(event, action string) bool {
allowed := false

if len(action) > 0 {
event = event + ":" + action
}

switch event {
case constants.EventPush:
allowed = e.GetPush().GetBranch()
case constants.EventPull + ":" + constants.ActionOpened:
allowed = e.GetPullRequest().GetOpened()
case constants.EventPull + ":" + constants.ActionSynchronize:
allowed = e.GetPullRequest().GetSynchronize()
case constants.EventPull + ":" + constants.ActionEdited:
allowed = e.GetPullRequest().GetEdited()
case constants.EventTag:
allowed = e.GetPush().GetTag()
case constants.EventComment + ":" + constants.ActionCreated:
allowed = e.GetComment().GetCreated()
case constants.EventComment + ":" + constants.ActionEdited:
allowed = e.GetComment().GetEdited()
case constants.EventDeploy:
allowed = e.GetDeployment().GetCreated()
case constants.EventSchedule:
allowed = e.GetSchedule().GetRun()
}

return allowed
}

// List is an Events method that generates a comma-separated list of event:action
// combinations that are allowed for the repo.
func (e *Events) List() []string {
Expand Down Expand Up @@ -71,6 +106,10 @@ func (e *Events) List() []string {
eventSlice = append(eventSlice, constants.EventComment+":"+constants.ActionEdited)
}

if e.GetSchedule().GetRun() {
eventSlice = append(eventSlice, constants.EventSchedule)
}

return eventSlice
}

Expand Down Expand Up @@ -123,6 +162,17 @@ func (e *Events) GetComment() *actions.Comment {
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() *actions.Schedule {
// return zero value if Events type or Schedule field is nil
if e == nil || e.Schedule == nil {
return new(actions.Schedule)
}

return e.Schedule
}

// SetPush sets the Events Push field.
//
// When the provided Events type is nil, it
Expand Down Expand Up @@ -174,3 +224,16 @@ func (e *Events) SetComment(v *actions.Comment) {

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 *actions.Schedule) {
// return if Events type is nil
if e == nil {
return
}

e.Schedule = v
}
Loading

0 comments on commit a91bd54

Please sign in to comment.