Skip to content

Commit

Permalink
chore: delete legacy allow event fields and use nested worker types (#…
Browse files Browse the repository at this point in the history
…546)

* chore: delete legacy events fields:

* chore: update secret validate switch

* chore: verbose test types

* chore: more valid events

* fix: use generated validevents list

* fix: use generated validevents list

* fix: pr validator

* chore: bump go.mod

* chore: appease linter

* fix: bundling api worker types changes

* fix: use hardcoded unlisted event tags

* fix: remove debug

* fix: remove debug

* chore: appease linter

* fix: check invalid events using err

* chore: remove unused code

* chore: fix setallow funcs
  • Loading branch information
plyr4 authored Apr 8, 2024
1 parent a70862b commit 63d5835
Show file tree
Hide file tree
Showing 19 changed files with 126 additions and 140 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/validate-pr-title.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ jobs:
steps:
- name: validate title
run: |
echo "${{ github.event.pull_request.title }}" | grep -Eq '^(feat|fix|chore|refactor|enhance|test|docs)\(.*\):.*$' && (echo "Pass"; exit 0) || (echo "Incorrect Format. Please see https://go-vela.github.io/docs/community/contributing_guidelines/#development-workflow"; exit 1)
echo "${{ github.event.pull_request.title }}" | grep -Eq '^(feat|fix|chore|refactor|enhance|test|docs)(\(.*\)|):\s.+$' && (echo "Pass"; exit 0) || (echo "Incorrect Format. Please see https://go-vela.github.io/docs/community/contributing_guidelines/#development-workflow"; exit 1)
7 changes: 6 additions & 1 deletion action/repo/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ func (c *Config) Add(client *vela.Client) error {
logrus.Tracef("adding repo %s/%s", c.Org, c.Name)

if len(c.Events) > 0 {
r.SetAllowEvents(library.NewEventsFromSlice(c.Events))
evs, err := library.NewEventsFromSlice(c.Events)
if err != nil {
return err
}

r.SetAllowEvents(evs)
}

// send API call to add a repository
Expand Down
39 changes: 2 additions & 37 deletions action/repo/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/go-vela/cli/internal/output"

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

"github.com/gosuri/uitable"
Expand Down Expand Up @@ -48,7 +47,7 @@ func table(repos *[]library.Repo) error {
logrus.Tracef("adding repo %s to repo table", r.GetFullName())

//nolint:gosec // ignore memory aliasing
e := strings.Join(events(&r), ",")
e := strings.Join(r.AllowEvents.List(), ",")

// add a row to the table with the specified values
//
Expand Down Expand Up @@ -95,7 +94,7 @@ func wideTable(repos *[]library.Repo) error {
logrus.Tracef("adding repo %s to wide repo table", r.GetFullName())

//nolint:gosec // ignore memory aliasing
e := strings.Join(events(&r), ",")
e := strings.Join(r.AllowEvents.List(), ",")

// add a row to the table with the specified values
//
Expand All @@ -108,37 +107,3 @@ func wideTable(repos *[]library.Repo) error {
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Stdout
return output.Stdout(table)
}

// events is a helper function to output
// the event types a repo is configured
// to trigger builds off of.
func events(r *library.Repo) []string {
e := []string{}

// check if the repository allows comment events
if r.GetAllowComment() {
e = append(e, constants.EventComment)
}

// check if the repository allows deployment events
if r.GetAllowDeploy() {
e = append(e, constants.EventDeploy)
}

// check if the repository allows pull_request events
if r.GetAllowPull() {
e = append(e, constants.EventPull)
}

// check if the repository allows push events
if r.GetAllowPush() {
e = append(e, constants.EventPush)
}

// check if the repository allows tag events
if r.GetAllowTag() {
e = append(e, constants.EventTag)
}

return e
}
24 changes: 13 additions & 11 deletions action/repo/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
func TestRepo_table(t *testing.T) {
// setup types
r1 := testRepo()
r1.SetAllowDeploy(true)
r1.SetAllowTag(true)
r1.SetAllowComment(true)
r1.GetAllowEvents().GetDeployment().SetCreated(true)
r1.GetAllowEvents().GetPush().SetTag(true)
r1.GetAllowEvents().GetComment().SetCreated(true)

r2 := testRepo()
r2.SetID(2)
Expand Down Expand Up @@ -55,9 +55,9 @@ func TestRepo_table(t *testing.T) {
func TestRepo_wideTable(t *testing.T) {
// setup types
r1 := testRepo()
r1.SetAllowDeploy(true)
r1.SetAllowTag(true)
r1.SetAllowComment(true)
r1.GetAllowEvents().GetDeployment().SetCreated(true)
r1.GetAllowEvents().GetPush().SetTag(true)
r1.GetAllowEvents().GetComment().SetCreated(true)

r2 := testRepo()
r2.SetID(2)
Expand Down Expand Up @@ -113,11 +113,13 @@ func testRepo() *library.Repo {
r.SetPrivate(false)
r.SetTrusted(false)
r.SetActive(true)
r.SetAllowPull(true)
r.SetAllowPush(true)
r.SetAllowDeploy(false)
r.SetAllowTag(false)
r.SetAllowComment(false)
r.GetAllowEvents().GetPullRequest().SetOpened(true)
r.GetAllowEvents().GetPullRequest().SetSynchronize(true)
r.GetAllowEvents().GetPullRequest().SetEdited(true)
r.GetAllowEvents().GetPush().SetBranch(true)
r.GetAllowEvents().GetDeployment().SetCreated(false)
r.GetAllowEvents().GetPush().SetTag(false)
r.GetAllowEvents().GetComment().SetCreated(false)

return r
}
7 changes: 6 additions & 1 deletion action/repo/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ func (c *Config) Update(client *vela.Client) error {
}

if len(c.Events) > 0 {
r.SetAllowEvents(library.NewEventsFromSlice(c.Events))
evs, err := library.NewEventsFromSlice(c.Events)
if err != nil {
return err
}

r.SetAllowEvents(evs)
}

logrus.Tracef("updating repo %s/%s", c.Org, c.Name)
Expand Down
12 changes: 8 additions & 4 deletions action/secret/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,18 @@ func (c *Config) Add(client *vela.Client) error {
Name: &c.Name,
Value: &c.Value,
Images: &c.Images,
Events: &c.Events,
AllowCommand: c.AllowCommand,
AllowSubstitution: c.AllowSubstitution,
}

// populate events if provided
if len(c.Events) > 0 {
s.SetAllowEvents(library.NewEventsFromSlice(c.Events))
if len(c.AllowEvents) > 0 {
evs, err := library.NewEventsFromSlice(c.AllowEvents)
if err != nil {
return err
}

s.SetAllowEvents(evs)
}

logrus.Tracef("adding secret %s/%s/%s/%s/%s", c.Engine, c.Type, c.Org, name, c.Name)
Expand Down Expand Up @@ -154,7 +158,7 @@ func (c *Config) AddFromFile(client *vela.Client) error {
Name: s.GetName(),
Value: s.GetValue(),
Images: s.GetImages(),
Events: s.GetEvents(),
AllowEvents: s.GetAllowEvents().List(),
AllowCommand: s.AllowCommand,
AllowSubstitution: s.AllowSubstitution,
Output: c.Output,
Expand Down
2 changes: 1 addition & 1 deletion action/secret/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Config struct {
Name string
Value string
Images []string
Events []string
AllowEvents []string
AllowCommand *bool
AllowSubstitution *bool
File string
Expand Down
2 changes: 1 addition & 1 deletion action/secret/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func wideTable(secrets *[]library.Secret) error {
logrus.Tracef("adding secret %s to wide secret table", s.GetName())

// capture list of events for secret
e := strings.Join(s.GetEvents(), ",")
e := strings.Join(s.GetAllowEvents().List(), ",")

// capture list of images for secret
i := strings.Join(s.GetImages(), ",")
Expand Down
4 changes: 3 additions & 1 deletion action/secret/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ func testSecret() *library.Secret {
s.SetValue("bar")
s.SetType("repo")
s.SetImages([]string{"alpine"})
s.SetEvents([]string{"push", "tag", "deployment"})
s.GetAllowEvents().GetPush().SetBranch(true)
s.GetAllowEvents().GetDeployment().SetCreated(false)
s.GetAllowEvents().GetPush().SetTag(false)
s.SetAllowCommand(true)
s.SetAllowSubstitution(true)

Expand Down
12 changes: 8 additions & 4 deletions action/secret/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,18 @@ func (c *Config) Update(client *vela.Client) error {
Name: &c.Name,
Value: &c.Value,
Images: &c.Images,
Events: &c.Events,
AllowCommand: c.AllowCommand,
AllowSubstitution: c.AllowSubstitution,
}

// populate events if provided
if len(c.Events) > 0 {
s.SetAllowEvents(library.NewEventsFromSlice(c.Events))
if len(c.AllowEvents) > 0 {
evs, err := library.NewEventsFromSlice(c.AllowEvents)
if err != nil {
return err
}

s.SetAllowEvents(evs)
}

logrus.Tracef("modifying secret %s/%s/%s/%s/%s", c.Engine, c.Type, c.Org, name, c.Name)
Expand Down Expand Up @@ -154,7 +158,7 @@ func (c *Config) UpdateFromFile(client *vela.Client) error {
Name: s.GetName(),
Value: s.GetValue(),
Images: s.GetImages(),
Events: s.GetEvents(),
AllowEvents: s.GetAllowEvents().List(),
AllowCommand: s.AllowCommand,
AllowSubstitution: s.AllowSubstitution,
Output: c.Output,
Expand Down
23 changes: 4 additions & 19 deletions action/secret/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

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

"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -82,25 +83,9 @@ func (c *Config) Validate() error {

// check if secret action is add or update
if c.Action == "add" || c.Action == "update" {
// iterate through all secret events
for _, event := range c.Events {
// check if the secret event provided is valid
switch event {
case constants.EventComment:
fallthrough
case constants.EventDeploy:
fallthrough
case constants.EventPull:
fallthrough
case constants.EventPush:
fallthrough
case constants.EventSchedule:
fallthrough
case constants.EventTag:
continue
default:
return fmt.Errorf("invalid secret event provided: %s", event)
}
_, err := library.NewEventsFromSlice(c.AllowEvents)
if err != nil {
return err
}
}

Expand Down
26 changes: 16 additions & 10 deletions action/secret/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,28 @@ func TestSecret_Config_Validate(t *testing.T) {
Repo: "octocat",
Name: "foo",
Value: "bar",
Events: []string{"comment", "push", "pull_request", "tag", "deployment", "schedule"},
AllowEvents: []string{
"comment:created",
"pull_request:opened", "pull_request:synchronize", "pull_request:edited",
"push:branch", "push:tag",
"deployment",
"schedule",
},
Output: "",
},
},
{
failure: true,
config: &Config{
Action: "add",
Engine: "native",
Type: "repo",
Org: "github",
Repo: "octocat",
Name: "foo",
Value: "bar",
Events: []string{"foo"},
Output: "",
Action: "add",
Engine: "native",
Type: "repo",
Org: "github",
Repo: "octocat",
Name: "foo",
Value: "bar",
AllowEvents: []string{"foo"},
Output: "",
},
},
{
Expand Down
6 changes: 3 additions & 3 deletions action/worker/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

"github.com/go-vela/cli/internal/output"
"github.com/go-vela/types/library"
api "github.com/go-vela/server/api/types"

"github.com/dustin/go-humanize"
"github.com/gosuri/uitable"
Expand All @@ -16,7 +16,7 @@ import (
// table is a helper function to output the
// provided workers in a table format with
// a specific set of fields displayed.
func table(workers *[]library.Worker) error {
func table(workers *[]api.Worker) error {
logrus.Debug("creating table for list of workers")

// create a new table
Expand Down Expand Up @@ -60,7 +60,7 @@ func table(workers *[]library.Worker) error {
// wideTable is a helper function to output the
// provided workers in a wide table format with
// a specific set of fields displayed.
func wideTable(workers *[]library.Worker) error {
func wideTable(workers *[]api.Worker) error {
logrus.Debug("creating wide table for list of workers")

// create new wide table
Expand Down
14 changes: 7 additions & 7 deletions action/worker/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package worker
import (
"testing"

"github.com/go-vela/types/library"
api "github.com/go-vela/server/api/types"
)

func TestWorker_table(t *testing.T) {
Expand All @@ -22,11 +22,11 @@ func TestWorker_table(t *testing.T) {
// setup tests
tests := []struct {
failure bool
workers *[]library.Worker
workers *[]api.Worker
}{
{
failure: false,
workers: &[]library.Worker{
workers: &[]api.Worker{
*w1,
*w2,
},
Expand Down Expand Up @@ -66,11 +66,11 @@ func TestWorker_wideTable(t *testing.T) {
// setup tests
tests := []struct {
failure bool
workers *[]library.Worker
workers *[]api.Worker
}{
{
failure: false,
workers: &[]library.Worker{
workers: &[]api.Worker{
*w1,
*w2,
},
Expand All @@ -97,8 +97,8 @@ func TestWorker_wideTable(t *testing.T) {

// testWorker is a test helper function to create a Worker
// type with all fields set to a fake value.
func testWorker() *library.Worker {
w := new(library.Worker)
func testWorker() *api.Worker {
w := new(api.Worker)

w.SetID(1)
w.SetActive(true)
Expand Down
Loading

0 comments on commit 63d5835

Please sign in to comment.