From 3c55490067ae752dab92587756874ad03d1fac9d Mon Sep 17 00:00:00 2001 From: Easton Crupper <65553218+ecrupper@users.noreply.github.com> Date: Tue, 12 Dec 2023 16:08:41 -0500 Subject: [PATCH] feat(repo): support new allow_events struct (#509) * feat(repo): support new allow_events struct * init legacy allow events * make clean and fix a test --- action/repo/add.go | 91 +++++++++++++++++++++++++++----------- action/repo/add_test.go | 83 ++++++++++++++++++++++++++++++++++ action/repo/repo.go | 1 - action/repo/update.go | 52 +--------------------- action/repo/update_test.go | 2 - command/repo/update.go | 11 +---- go.mod | 2 +- 7 files changed, 151 insertions(+), 91 deletions(-) diff --git a/action/repo/add.go b/action/repo/add.go index a7a9e98f..d7a50832 100644 --- a/action/repo/add.go +++ b/action/repo/add.go @@ -12,6 +12,7 @@ import ( "github.com/go-vela/types/constants" "github.com/go-vela/types/library" + "github.com/go-vela/types/library/actions" "github.com/sirupsen/logrus" ) @@ -40,36 +41,12 @@ func (c *Config) Add(client *vela.Client) error { PipelineType: vela.String(c.PipelineType), } - // iterate through all events provided - for _, event := range c.Events { - // check if the repository should allow push events - if event == constants.EventPush { - r.AllowPush = vela.Bool(true) - } - - // check if the repository should allow pull_request events - if event == constants.EventPull || event == AlternatePull { - r.AllowPull = vela.Bool(true) - } - - // check if the repository should allow tag events - if event == constants.EventTag { - r.AllowTag = vela.Bool(true) - } - - // check if the repository should allow deployment events - if event == constants.EventDeploy || event == AlternateDeploy { - r.AllowDeploy = vela.Bool(true) - } + logrus.Tracef("adding repo %s/%s", c.Org, c.Name) - // check if the repository should allow comment events - if event == constants.EventComment { - r.AllowComment = vela.Bool(true) - } + if len(c.Events) > 0 { + populateEvents(r, c.Events) } - logrus.Tracef("adding repo %s/%s", c.Org, c.Name) - // send API call to add a repository // // https://pkg.go.dev/github.com/go-vela/sdk-go/vela?tab=doc#RepoService.Add @@ -107,3 +84,63 @@ 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.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) + } + } + + result.SetPush(push) + result.SetPullRequest(pull) + result.SetDeployment(deploy) + result.SetComment(comment) + + r.SetAllowEvents(result) +} diff --git a/action/repo/add_test.go b/action/repo/add_test.go index cce76c5d..2dc1a498 100644 --- a/action/repo/add_test.go +++ b/action/repo/add_test.go @@ -7,6 +7,9 @@ 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" ) @@ -149,3 +152,83 @@ 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"}, + want: &library.Repo{ + AllowPush: &tBool, + AllowPull: &tBool, + AllowTag: &tBool, + AllowDeploy: &tBool, + AllowComment: &tBool, + AllowEvents: &library.Events{ + Push: &actions.Push{ + Branch: &tBool, + Tag: &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"}, + want: &library.Repo{ + AllowPush: &tBool, + AllowPull: &fBool, + AllowTag: &tBool, + AllowDeploy: &tBool, + AllowComment: &fBool, + AllowEvents: &library.Events{ + Push: &actions.Push{ + Branch: &tBool, + Tag: &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) + } + } +} diff --git a/action/repo/repo.go b/action/repo/repo.go index ee46a634..9157fc28 100644 --- a/action/repo/repo.go +++ b/action/repo/repo.go @@ -19,7 +19,6 @@ type Config struct { Trusted bool Active bool Events []string - DropEvents []string PipelineType string Page int PerPage int diff --git a/action/repo/update.go b/action/repo/update.go index 0c1b897d..027338f1 100644 --- a/action/repo/update.go +++ b/action/repo/update.go @@ -10,7 +10,6 @@ import ( "github.com/go-vela/sdk-go/vela" - "github.com/go-vela/types/constants" "github.com/go-vela/types/library" "github.com/sirupsen/logrus" @@ -40,55 +39,8 @@ func (c *Config) Update(client *vela.Client) error { PipelineType: vela.String(c.PipelineType), } - // iterate through all events provided - for _, event := range c.Events { - // check if the repository should allow push events - if event == constants.EventPush { - r.AllowPush = vela.Bool(true) - } - - // check if the repository should allow pull_request events - if event == constants.EventPull || event == AlternatePull { - r.AllowPull = vela.Bool(true) - } - - // check if the repository should allow tag events - if event == constants.EventTag { - r.AllowTag = vela.Bool(true) - } - - // check if the repository should allow deployment events - if event == constants.EventDeploy || event == AlternateDeploy { - r.AllowDeploy = vela.Bool(true) - } - - // check if the repository should allow comment events - if event == constants.EventComment { - r.AllowComment = vela.Bool(true) - } - } - - // iterate through all drop events provided - for _, event := range c.DropEvents { - if event == constants.EventPush { - r.AllowPush = vela.Bool(false) - } - - if event == constants.EventPull || event == AlternatePull { - r.AllowPull = vela.Bool(false) - } - - if event == constants.EventTag { - r.AllowTag = vela.Bool(false) - } - - if event == constants.EventDeploy || event == AlternateDeploy { - r.AllowDeploy = vela.Bool(false) - } - - if event == constants.EventComment { - r.AllowComment = vela.Bool(false) - } + if len(c.Events) > 0 { + populateEvents(r, c.Events) } logrus.Tracef("updating repo %s/%s", c.Org, c.Name) diff --git a/action/repo/update_test.go b/action/repo/update_test.go index aa4536d8..bba236d7 100644 --- a/action/repo/update_test.go +++ b/action/repo/update_test.go @@ -42,7 +42,6 @@ func TestRepo_Config_Update(t *testing.T) { Trusted: false, Active: true, Events: []string{"push", "pull_request", "comment", "deployment", "tag"}, - DropEvents: []string{"push", "pull_request", "comment", "deployment", "tag"}, PipelineType: "yaml", Output: "", }, @@ -62,7 +61,6 @@ func TestRepo_Config_Update(t *testing.T) { Private: false, Trusted: false, Active: true, - DropEvents: []string{"deployment", "tag"}, PipelineType: "yaml", Output: "dump", }, diff --git a/command/repo/update.go b/command/repo/update.go index 78368073..244acf9e 100644 --- a/command/repo/update.go +++ b/command/repo/update.go @@ -108,12 +108,6 @@ var CommandUpdate = &cli.Command{ Aliases: []string{"events", "add-event", "add-events", "e"}, Usage: "webhook event(s) repository responds to", }, - &cli.StringSliceFlag{ - EnvVars: []string{"VELA_DROP_EVENTS", "REPO_DROP_EVENTS"}, - Name: "drop-event", - Aliases: []string{"drop-events", "de"}, - Usage: "remove webhook event(s) repository responds to", - }, &cli.StringFlag{ EnvVars: []string{"VELA_PIPELINE_TYPE", "PIPELINE_TYPE"}, Name: "pipeline-type", @@ -136,9 +130,7 @@ EXAMPLES: 1. Update a repository with push and pull request enabled. $ {{.HelpName}} --org MyOrg --repo MyRepo --event push --event pull_request 2. Update a repository with all event types enabled. - $ {{.HelpName}} --org MyOrg --repo MyRepo --event push --event pull_request --event tag --event deployment --event comment - 3. Update a repository to disable the comment and deploy events. - $ {{.HelpName}} --org MyOrg --repo MyRepo --event push --drop-events deployment,comment + $ {{.HelpName}} --org MyOrg --repo MyRepo --event push,pull_request,tag,deployment,comment 3. Update a repository with a longer build timeout. $ {{.HelpName}} --org MyOrg --repo MyRepo --timeout 90 4. Update a repository when config or environment variables are set. @@ -187,7 +179,6 @@ func update(c *cli.Context) error { Trusted: c.Bool("trusted"), Active: c.Bool("active"), Events: c.StringSlice("event"), - DropEvents: c.StringSlice("drop-event"), PipelineType: c.String("pipeline-type"), Output: c.String(internal.FlagOutput), } diff --git a/go.mod b/go.mod index b53a4bbd..2f390b55 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( github.com/go-vela/types v0.22.1-0.20231211143329-1eae2f5e371b github.com/go-vela/worker v0.22.0 github.com/golang-jwt/jwt/v5 v5.1.0 + 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 @@ -71,7 +72,6 @@ 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/v56 v56.0.0 // indirect github.com/google/go-querystring v1.1.0 // indirect github.com/google/gofuzz v1.2.0 // indirect