Skip to content

Commit

Permalink
fix(secrets): use NewEventsFromSlice function to handle both secrets …
Browse files Browse the repository at this point in the history
…and repo AllowEvents (#543)

* fix(secrets): abstract populateEvents for secret usage

* use types NewEventsFromSlice

* make clean
  • Loading branch information
ecrupper authored Mar 21, 2024
1 parent 379fce6 commit 408fbfb
Show file tree
Hide file tree
Showing 12 changed files with 20 additions and 177 deletions.
71 changes: 1 addition & 70 deletions action/repo/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import (

"github.com/go-vela/sdk-go/vela"

"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/go-vela/types/library/actions"

"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -45,7 +43,7 @@ func (c *Config) Add(client *vela.Client) error {
logrus.Tracef("adding repo %s/%s", c.Org, c.Name)

if len(c.Events) > 0 {
populateEvents(r, c.Events)
r.SetAllowEvents(library.NewEventsFromSlice(c.Events))
}

// send API call to add a repository
Expand Down Expand Up @@ -85,70 +83,3 @@ func (c *Config) Add(client *vela.Client) error {
return output.Stdout(repo)
}
}

// populateEvents is a helper function designed to fill both the legacy `Allow<_>` fields
// as well as the `AllowEvents` struct with the correct values based on a slice input.
func populateEvents(r *library.Repo, events []string) {
result := new(library.Events)
push := new(actions.Push)
pull := new(actions.Pull)
comment := new(actions.Comment)
deploy := new(actions.Deploy)

// -- legacy allow events init --
r.SetAllowPush(false)
r.SetAllowPull(false)
r.SetAllowTag(false)
r.SetAllowDeploy(false)
r.SetAllowComment(false)

// iterate through all events provided
for _, event := range events {
switch event {
case constants.EventPush, constants.EventPush + ":branch":
r.SetAllowPush(true)
push.SetBranch(true)
case constants.EventTag, constants.EventPush + ":" + constants.EventTag:
r.SetAllowTag(true)
push.SetTag(true)
case constants.EventPull, AlternatePull:
r.SetAllowPull(true)
pull.SetOpened(true)
pull.SetReopened(true)
pull.SetSynchronize(true)
case constants.EventDeploy, AlternateDeploy, constants.EventDeploy + ":" + constants.ActionCreated:
r.SetAllowDeploy(true)
deploy.SetCreated(true)
case constants.EventComment:
r.SetAllowComment(true)
comment.SetCreated(true)
comment.SetEdited(true)
case constants.EventDelete:
push.SetDeleteBranch(true)
push.SetDeleteTag(true)
case constants.EventPull + ":" + constants.ActionOpened:
pull.SetOpened(true)
case constants.EventPull + ":" + constants.ActionEdited:
pull.SetEdited(true)
case constants.EventPull + ":" + constants.ActionSynchronize:
pull.SetSynchronize(true)
case constants.EventPull + ":" + constants.ActionReopened:
pull.SetReopened(true)
case constants.EventComment + ":" + constants.ActionCreated:
comment.SetCreated(true)
case constants.EventComment + ":" + constants.ActionEdited:
comment.SetEdited(true)
case constants.EventDelete + ":" + constants.ActionBranch:
push.SetDeleteBranch(true)
case constants.EventDelete + ":" + constants.ActionTag:
push.SetDeleteTag(true)
}
}

result.SetPush(push)
result.SetPullRequest(pull)
result.SetDeployment(deploy)
result.SetComment(comment)

r.SetAllowEvents(result)
}
87 changes: 0 additions & 87 deletions action/repo/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import (
"testing"

"github.com/go-vela/server/mock/server"
"github.com/go-vela/types/library"
"github.com/go-vela/types/library/actions"
"github.com/google/go-cmp/cmp"

"github.com/go-vela/sdk-go/vela"
)
Expand Down Expand Up @@ -155,87 +152,3 @@ func TestRepo_Config_Add(t *testing.T) {
}
}
}

func TestRepo_populateEvents(t *testing.T) {
// setup types
tBool := true
fBool := false

// setup tests
tests := []struct {
name string
events []string
want *library.Repo
}{
{
name: "happy path legacy events",
events: []string{"push", "pull_request", "tag", "deploy", "comment", "delete"},
want: &library.Repo{
AllowPush: &tBool,
AllowPull: &tBool,
AllowTag: &tBool,
AllowDeploy: &tBool,
AllowComment: &tBool,
AllowEvents: &library.Events{
Push: &actions.Push{
Branch: &tBool,
Tag: &tBool,
DeleteBranch: &tBool,
DeleteTag: &tBool,
},
PullRequest: &actions.Pull{
Opened: &tBool,
Reopened: &tBool,
Synchronize: &tBool,
},
Deployment: &actions.Deploy{
Created: &tBool,
},
Comment: &actions.Comment{
Created: &tBool,
Edited: &tBool,
},
},
},
},
{
name: "action specific",
events: []string{"push:branch", "push:tag", "pull_request:opened", "pull_request:edited", "deployment:created", "comment:created", "delete:branch", "delete:tag"},
want: &library.Repo{
AllowPush: &tBool,
AllowPull: &fBool,
AllowTag: &tBool,
AllowDeploy: &tBool,
AllowComment: &fBool,
AllowEvents: &library.Events{
Push: &actions.Push{
Branch: &tBool,
Tag: &tBool,
DeleteBranch: &tBool,
DeleteTag: &tBool,
},
PullRequest: &actions.Pull{
Opened: &tBool,
Edited: &tBool,
},
Deployment: &actions.Deploy{
Created: &tBool,
},
Comment: &actions.Comment{
Created: &tBool,
},
},
},
},
}

// run tests
for _, test := range tests {
repo := new(library.Repo)
populateEvents(repo, test.events)

if diff := cmp.Diff(test.want, repo); diff != "" {
t.Errorf("populateEvents failed for %s mismatch (-want +got):\n%s", test.name, diff)
}
}
}
6 changes: 0 additions & 6 deletions action/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,3 @@ type Config struct {
PerPage int
Output string
}

// Alternate constants for webhook events.
const (
AlternateDeploy = "deploy"
AlternatePull = "pull"
)
2 changes: 1 addition & 1 deletion action/repo/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (c *Config) Update(client *vela.Client) error {
}

if len(c.Events) > 0 {
populateEvents(r, c.Events)
r.SetAllowEvents(library.NewEventsFromSlice(c.Events))
}

logrus.Tracef("updating repo %s/%s", c.Org, c.Name)
Expand Down
5 changes: 5 additions & 0 deletions action/secret/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ func (c *Config) Add(client *vela.Client) error {
AllowSubstitution: c.AllowSubstitution,
}

// populate events if provided
if len(c.Events) > 0 {
s.SetAllowEvents(library.NewEventsFromSlice(c.Events))
}

logrus.Tracef("adding secret %s/%s/%s/%s/%s", c.Engine, c.Type, c.Org, name, c.Name)

// send API call to add a secret
Expand Down
5 changes: 5 additions & 0 deletions action/secret/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ func (c *Config) Update(client *vela.Client) error {
AllowSubstitution: c.AllowSubstitution,
}

// populate events if provided
if len(c.Events) > 0 {
s.SetAllowEvents(library.NewEventsFromSlice(c.Events))
}

logrus.Tracef("modifying secret %s/%s/%s/%s/%s", c.Engine, c.Type, c.Org, name, c.Name)

// send API call to update a secret
Expand Down
2 changes: 1 addition & 1 deletion command/repo/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ var CommandAdd = &cli.Command{
&cli.StringSliceFlag{
EnvVars: []string{"VELA_EVENTS", "REPO_EVENTS", "VELA_ADD_EVENTS", "REPO_ADD_EVENTS"},
Name: "event",
Aliases: []string{"events", "add-event", "add-events", "e"},
Aliases: []string{"events", "e"},
Usage: "webhook event(s) repository responds to",
},
&cli.StringFlag{
Expand Down
2 changes: 1 addition & 1 deletion command/repo/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ var CommandUpdate = &cli.Command{
&cli.StringSliceFlag{
EnvVars: []string{"VELA_EVENTS", "REPO_EVENTS", "VELA_ADD_EVENTS", "REPO_ADD_EVENTS"},
Name: "event",
Aliases: []string{"events", "add-event", "add-events", "e"},
Aliases: []string{"events", "e"},
Usage: "webhook event(s) repository responds to",
},
&cli.StringFlag{
Expand Down
7 changes: 1 addition & 6 deletions command/secret/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,8 @@ var CommandAdd = &cli.Command{
&cli.StringSliceFlag{
EnvVars: []string{"VELA_EVENTS", "SECRET_EVENTS"},
Name: "event",
Aliases: []string{"ev"},
Aliases: []string{"events", "ev"},
Usage: "provide the event(s) that can access this secret",
Value: cli.NewStringSlice(
constants.EventDeploy,
constants.EventPush,
constants.EventTag,
),
},
&cli.StringFlag{
EnvVars: []string{"VELA_COMMAND", "SECRET_COMMAND"},
Expand Down
2 changes: 1 addition & 1 deletion command/secret/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var CommandUpdate = &cli.Command{
&cli.StringSliceFlag{
EnvVars: []string{"VELA_EVENTS", "SECRET_EVENTS"},
Name: "event",
Aliases: []string{"ev"},
Aliases: []string{"events", "ev"},
Usage: "provide the event(s) that can access this secret",
},
&cli.StringFlag{
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ require (
github.com/go-git/go-git/v5 v5.11.0
github.com/go-vela/sdk-go v0.23.2
github.com/go-vela/server v0.23.2
github.com/go-vela/types v0.23.2
github.com/go-vela/types v0.23.3-0.20240321143352-e9cd33aaba61
github.com/go-vela/worker v0.23.2
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/go-cmp v0.6.0
github.com/gosuri/uitable v0.0.4
github.com/joho/godotenv v1.5.1
github.com/manifoldco/promptui v0.9.0
Expand Down Expand Up @@ -72,6 +71,7 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/go-github/v59 v59.0.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ github.com/go-vela/sdk-go v0.23.2 h1:DQzhz7ggbLXmi2Kg0x0C4h/ujSujSSyHbxUORMgt/hk
github.com/go-vela/sdk-go v0.23.2/go.mod h1:kppIcwY9Bd6qke7sHVWJ0F+SNvYuTUw2sNvAnrkqayg=
github.com/go-vela/server v0.23.2 h1:G/JLZPHgokZxthE4PMfiMIZCKdYadoafYEqh/P91ECk=
github.com/go-vela/server v0.23.2/go.mod h1:Iqs8vB35nPcN6GpjmvWwIjBZZ42oKHgOTrGSpz2wvNI=
github.com/go-vela/types v0.23.2 h1:QDt2lta7FPEfN2RK/Bn++DkqyjGIB4H/Q4XkFAj3hXQ=
github.com/go-vela/types v0.23.2/go.mod h1:aTE6dzssqTGOvU6m2/vsI9NoSW/3hH/yLzf3cCSo0Zk=
github.com/go-vela/types v0.23.3-0.20240321143352-e9cd33aaba61 h1:1LIYUJZBEPnutS/xtgixS/rMBdYRmiQj7hcckALvmOI=
github.com/go-vela/types v0.23.3-0.20240321143352-e9cd33aaba61/go.mod h1:aTE6dzssqTGOvU6m2/vsI9NoSW/3hH/yLzf3cCSo0Zk=
github.com/go-vela/worker v0.23.2 h1:ypYzhLk94xkAUbpBb9d+QU8rw4jVQC9rIOCIZ2XNb90=
github.com/go-vela/worker v0.23.2/go.mod h1:M1GzqlX6bIi/p+Fb6HuJ/uGHzPbaQBS6KVreClInZKg=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
Expand Down

0 comments on commit 408fbfb

Please sign in to comment.