From f5423019eaa3b515b1b00684d75250a83e181f2e Mon Sep 17 00:00:00 2001 From: ecrupper Date: Mon, 20 Nov 2023 10:16:11 -0600 Subject: [PATCH] fix(repo): allow users to disable events --- action/repo/add.go | 2 +- action/repo/repo.go | 4 ++++ action/repo/update.go | 25 ++++++++++++++++++++++++- action/repo/update_test.go | 3 ++- command/repo/add.go | 5 ++--- command/repo/update.go | 18 +++++++++++------- 6 files changed, 44 insertions(+), 13 deletions(-) diff --git a/action/repo/add.go b/action/repo/add.go index 641a5782..edf55385 100644 --- a/action/repo/add.go +++ b/action/repo/add.go @@ -58,7 +58,7 @@ func (c *Config) Add(client *vela.Client) error { } // check if the repository should allow deployment events - if event == constants.EventDeploy { + if event == constants.EventDeploy || event == AlternateDeploy { r.AllowDeploy = vela.Bool(true) } diff --git a/action/repo/repo.go b/action/repo/repo.go index f4421048..92bb74a3 100644 --- a/action/repo/repo.go +++ b/action/repo/repo.go @@ -19,8 +19,12 @@ type Config struct { Trusted bool Active bool Events []string + DropEvents []string PipelineType string Page int PerPage int Output string } + +// AlternateDeploy is a common representation of constants.EventDeploy. +const AlternateDeploy = "deploy" diff --git a/action/repo/update.go b/action/repo/update.go index 9dac98c3..6739e784 100644 --- a/action/repo/update.go +++ b/action/repo/update.go @@ -58,7 +58,7 @@ func (c *Config) Update(client *vela.Client) error { } // check if the repository should allow deployment events - if event == constants.EventDeploy { + if event == constants.EventDeploy || event == AlternateDeploy { r.AllowDeploy = vela.Bool(true) } @@ -68,6 +68,29 @@ func (c *Config) Update(client *vela.Client) error { } } + // iterate through all drop events provided + for _, event := range c.DropEvents { + if event == constants.EventPush { + r.AllowPush = vela.Bool(false) + } + + if event == constants.EventPull { + 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) + } + } + logrus.Tracef("updating repo %s/%s", c.Org, c.Name) // send API call to modify a repository diff --git a/action/repo/update_test.go b/action/repo/update_test.go index a793eae7..aa4536d8 100644 --- a/action/repo/update_test.go +++ b/action/repo/update_test.go @@ -42,6 +42,7 @@ 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: "", }, @@ -61,7 +62,7 @@ func TestRepo_Config_Update(t *testing.T) { Private: false, Trusted: false, Active: true, - Events: []string{"push", "pull_request", "comment", "deployment", "tag"}, + DropEvents: []string{"deployment", "tag"}, PipelineType: "yaml", Output: "dump", }, diff --git a/command/repo/add.go b/command/repo/add.go index 51483f3c..94a4e9c0 100644 --- a/command/repo/add.go +++ b/command/repo/add.go @@ -1,6 +1,5 @@ // SPDX-License-Identifier: Apache-2.0 -//nolint:dupl // ignore similar code with update package repo import ( @@ -104,9 +103,9 @@ var CommandAdd = &cli.Command{ Value: "true", }, &cli.StringSliceFlag{ - EnvVars: []string{"VELA_EVENTS", "REPO_EVENTS"}, + EnvVars: []string{"VELA_EVENTS", "REPO_EVENTS", "VELA_ADD_EVENTS", "REPO_ADD_EVENTS"}, Name: "event", - Aliases: []string{"e"}, + Aliases: []string{"events", "add-event", "add-events", "e"}, Usage: "webhook event(s) repository responds to", }, &cli.StringFlag{ diff --git a/command/repo/update.go b/command/repo/update.go index 2a0325eb..78368073 100644 --- a/command/repo/update.go +++ b/command/repo/update.go @@ -1,6 +1,5 @@ // SPDX-License-Identifier: Apache-2.0 -//nolint:dupl // ignore similar code with add package repo import ( @@ -104,14 +103,16 @@ var CommandUpdate = &cli.Command{ Value: "true", }, &cli.StringSliceFlag{ - EnvVars: []string{"VELA_EVENTS", "REPO_EVENTS"}, + EnvVars: []string{"VELA_EVENTS", "REPO_EVENTS", "VELA_ADD_EVENTS", "REPO_ADD_EVENTS"}, Name: "event", - Aliases: []string{"e"}, + Aliases: []string{"events", "add-event", "add-events", "e"}, Usage: "webhook event(s) repository responds to", - Value: cli.NewStringSlice( - constants.EventPush, - constants.EventPull, - ), + }, + &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"}, @@ -136,6 +137,8 @@ EXAMPLES: $ {{.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 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. @@ -184,6 +187,7 @@ 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), }