Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: delete legacy allow event fields and use nested worker types #546

Merged
merged 17 commits into from
Apr 8, 2024
Merged
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 @@

"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 @@ -47,8 +46,8 @@
for _, r := range *repos {
logrus.Tracef("adding repo %s to repo table", r.GetFullName())

//nolint:gosec // ignore memory aliasing

Check failure on line 49 in action/repo/table.go

View workflow job for this annotation

GitHub Actions / diff-review

directive `//nolint:gosec // ignore memory aliasing` is unused for linter "gosec" (nolintlint)

Check failure on line 49 in action/repo/table.go

View workflow job for this annotation

GitHub Actions / full-review

directive `//nolint:gosec // ignore memory aliasing` is unused for linter "gosec" (nolintlint)

Check failure on line 49 in action/repo/table.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] action/repo/table.go#L49

directive `//nolint:gosec // ignore memory aliasing` is unused for linter "gosec" (nolintlint)
Raw output
action/repo/table.go:49:3: directive `//nolint:gosec // ignore memory aliasing` is unused for linter "gosec" (nolintlint)
		//nolint:gosec // ignore memory aliasing
		^
plyr4 marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -94,8 +93,8 @@
for _, r := range *repos {
logrus.Tracef("adding repo %s to wide repo table", r.GetFullName())

//nolint:gosec // ignore memory aliasing

Check failure on line 96 in action/repo/table.go

View workflow job for this annotation

GitHub Actions / diff-review

directive `//nolint:gosec // ignore memory aliasing` is unused for linter "gosec" (nolintlint)

Check failure on line 96 in action/repo/table.go

View workflow job for this annotation

GitHub Actions / full-review

directive `//nolint:gosec // ignore memory aliasing` is unused for linter "gosec" (nolintlint)

Check failure on line 96 in action/repo/table.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] action/repo/table.go#L96

directive `//nolint:gosec // ignore memory aliasing` is unused for linter "gosec" (nolintlint)
Raw output
action/repo/table.go:96:3: directive `//nolint:gosec // ignore memory aliasing` is unused for linter "gosec" (nolintlint)
		//nolint:gosec // ignore memory aliasing
		^
plyr4 marked this conversation as resolved.
Show resolved Hide resolved
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 @@
// 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: 3 additions & 4 deletions action/secret/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,13 @@ 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 {
s.SetAllowEvents(library.NewEventsFromSlice(c.AllowEvents))
}

logrus.Tracef("adding secret %s/%s/%s/%s/%s", c.Engine, c.Type, c.Org, name, c.Name)
Expand Down Expand Up @@ -154,7 +153,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
7 changes: 3 additions & 4 deletions action/secret/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,13 @@ 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 {
s.SetAllowEvents(library.NewEventsFromSlice(c.AllowEvents))
}

logrus.Tracef("modifying secret %s/%s/%s/%s/%s", c.Engine, c.Type, c.Org, name, c.Name)
Expand Down Expand Up @@ -154,7 +153,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
2 changes: 1 addition & 1 deletion action/secret/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ 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 {
for _, event := range c.AllowEvents {
// check if the secret event provided is valid
switch event {
case constants.EventComment:
Expand Down
36 changes: 18 additions & 18 deletions action/secret/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,29 +94,29 @@ func TestSecret_Config_Validate(t *testing.T) {
{
failure: false,
config: &Config{
Action: "add",
Engine: "native",
Type: "repo",
Org: "github",
Repo: "octocat",
Name: "foo",
Value: "bar",
Events: []string{"comment", "push", "pull_request", "tag", "deployment", "schedule"},
Output: "",
Action: "add",
Engine: "native",
Type: "repo",
Org: "github",
Repo: "octocat",
Name: "foo",
Value: "bar",
AllowEvents: []string{"comment", "push", "pull_request", "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
24 changes: 12 additions & 12 deletions command/secret/add.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0

package secret

Check failure on line 3 in command/secret/add.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] command/secret/add.go#L3

3-216 lines are duplicate of `command/secret/update.go:3-216` (dupl)
Raw output
command/secret/add.go:3: 3-216 lines are duplicate of `command/secret/update.go:3-216` (dupl)
package secret

import (
	"fmt"
	"slices"

	"github.com/go-vela/cli/action"
	"github.com/go-vela/cli/action/secret"
	"github.com/go-vela/cli/internal"
	"github.com/go-vela/cli/internal/client"

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

	"github.com/urfave/cli/v2"
)

// CommandAdd defines the command for creating a secret.
var CommandAdd = &cli.Command{
	Name:        "secret",
	Description: "Use this command to create a secret.",
	Usage:       "Add a new secret from the provided configuration",
	Action:      add,
	Flags: []cli.Flag{

		// Repo Flags

		&cli.StringFlag{
			EnvVars: []string{"VELA_ORG", "SECRET_ORG"},
			Name:    internal.FlagOrg,
			Aliases: []string{"o"},
			Usage:   "provide the organization for the secret",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_REPO", "SECRET_REPO"},
			Name:    internal.FlagRepo,
			Aliases: []string{"r"},
			Usage:   "provide the repository for the secret",
		},

		// Secret Flags

		&cli.StringFlag{
			EnvVars: []string{"VELA_ENGINE", "SECRET_ENGINE"},
			Name:    internal.FlagSecretEngine,
			Aliases: []string{"e"},
			Usage:   "provide the engine that stores the secret",
			Value:   constants.DriverNative,
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_TYPE", "SECRET_TYPE"},
			Name:    internal.FlagSecretType,
			Aliases: []string{"ty"},
			Usage:   "provide the type of secret being stored",
			Value:   constants.SecretRepo,
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_TEAM", "SECRET_TEAM"},
			Name:    "team",
			Aliases: []string{"t"},
			Usage:   "provide the team for the secret",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_NAME", "SECRET_NAME"},
			Name:    "name",
			Aliases: []string{"n"},
			Usage:   "provide the name of the secret",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_VALUE", "SECRET_VALUE"},
			Name:    "value",
			Aliases: []string{"v"},
			Usage:   "provide the value for the secret",
		},
		&cli.StringSliceFlag{
			EnvVars: []string{"VELA_IMAGES", "SECRET_IMAGES"},
			Name:    "image",
			Aliases: []string{"i"},
			Usage:   "Provide the image(s) that can access this secret",
		},
		&cli.StringSliceFlag{
			EnvVars: []string{"VELA_EVENTS", "SECRET_EVENTS"},
			Name:    "event",
			Aliases: []string{"events", "ev"},
			Usage:   "provide the event(s) that can access this secret",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_COMMAND", "SECRET_COMMAND"},
			Name:    internal.FlagSecretCommands,
			Aliases: []string{"c"},
			Usage:   "enable a secret to be used for a step with commands (default is false for shared secrets)",
			Value:   "true",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_SUBSTITUTION", "SECRET_SUBSTITUTION"},
			Name:    internal.FlagSecretSubstitution,
			Aliases: []string{"s"},
			Usage:   "enable a secret to be substituted (default is false for shared secrets)",
			Value:   "true",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_FILE", "SECRET_FILE"},
			Name:    "file",
			Aliases: []string{"f"},
			Usage:   "provide a file to add the secret(s)",
		},

		// Output Flags

		&cli.StringFlag{
			EnvVars: []string{"VELA_OUTPUT", "SECRET_OUTPUT"},
			Name:    internal.FlagOutput,
			Aliases: []string{"op"},
			Usage:   "format the output in json, spew or yaml",
		},
	},
	CustomHelpTemplate: fmt.Sprintf(`%s
EXAMPLES:
   1. Add a repository secret.
     $ {{.HelpName}} --secret.engine native --secret.type repo --org MyOrg --repo MyRepo --name foo --value bar
   2. Add a repository secret and disallow usage in commands.
     $ {{.HelpName}} --secret.engine native --secret.type repo --org MyOrg --repo MyRepo --name foo --value bar --commands false
   3. Add an organization secret.
     $ {{.HelpName}} --secret.engine native --secret.type org --org MyOrg --name foo --value bar
   4. Add a shared secret.
     $ {{.HelpName}} --secret.engine native --secret.type shared --org MyOrg --team octokitties --name foo --value bar
   5. Add a repository secret with all event types enabled.
     $ {{.HelpName}} --secret.engine native --secret.type repo --org MyOrg --repo MyRepo --name foo --value bar --event comment --event deployment --event pull_request --event push --event tag
   6. Add a repository secret with an image whitelist.
     $ {{.HelpName}} --secret.engine native --secret.type repo --org MyOrg --repo MyRepo --name foo --value bar --image alpine --image golang:* --image postgres:latest
   7. Add a secret with value from a file.
     $ {{.HelpName}} --secret.engine native --secret.type repo --org MyOrg --repo MyRepo --name foo --value @secret.txt
   8. Add a repository secret with json output.
     $ {{.HelpName}} --secret.engine native --secret.type repo --org MyOrg --repo MyRepo --name foo --value bar --output json
   9. Add a secret or secrets from a file.
     $ {{.HelpName}} --file secret.yml
  10. Add a secret when config or environment variables are set.
     $ {{.HelpName}} --org MyOrg --repo MyRepo --name foo --value bar

DOCUMENTATION:

  https://go-vela.github.io/docs/reference/cli/secret/add/
`, cli.CommandHelpTemplate),
}

// helper function to capture the provided input
// and create the object used to create a secret.
//
//nolint:dupl // ignore similar code with update
func add(c *cli.Context) error {
	// load variables from the config file
	err := action.Load(c)
	if err != nil {
		return err
	}

	// parse the Vela client from the context
	//
	// https://pkg.go.dev/github.com/go-vela/cli/internal/client?tab=doc#Parse
	client, err := client.Parse(c)
	if err != nil {
		return err
	}

	// create the secret configuration
	//
	// https://pkg.go.dev/github.com/go-vela/cli/action/secret?tab=doc#Config
	s := &secret.Config{
		Action:      internal.ActionAdd,
		Engine:      c.String(internal.FlagSecretEngine),
		Type:        c.String(internal.FlagSecretType),
		Org:         c.String(internal.FlagOrg),
		Repo:        c.String(internal.FlagRepo),
		Team:        c.String("team"),
		Name:        c.String("name"),
		Value:       c.String("value"),
		Images:      c.StringSlice("image"),
		AllowEvents: c.StringSlice("event"),
		File:        c.String("file"),
		Output:      c.String(internal.FlagOutput),
	}

	// check if allow_command and allow_substitution are provided
	// if they are not, server will not update the fields
	if slices.Contains(c.FlagNames(), internal.FlagSecretCommands) {
		val := c.Bool(internal.FlagSecretCommands)
		s.AllowCommand = &val
	}

	if slices.Contains(c.FlagNames(), internal.FlagSecretSubstitution) {
		val := c.Bool(internal.FlagSecretSubstitution)
		s.AllowSubstitution = &val
	}

	// validate secret configuration
	//
	// https://pkg.go.dev/github.com/go-vela/cli/action/secret?tab=doc#Config.Validate
	err = s.Validate()
	if err != nil {
		return err
	}

	// check if secret file is provided
	if len(s.File) > 0 {
		// execute the add from file call for the secret configuration
		//
		// https://pkg.go.dev/github.com/go-vela/cli/action/secret?tab=doc#Config.AddFromFile
		return s.AddFromFile(client)
	}

	// execute the add call for the secret configuration
	//
	// https://pkg.go.dev/github.com/go-vela/cli/action/secret?tab=doc#Config.Add
	return s.Add(client)
}

import (
"fmt"
Expand Down Expand Up @@ -147,7 +147,7 @@
// helper function to capture the provided input
// and create the object used to create a secret.
//
//nolint:dupl // ignore similar code with update

Check failure on line 150 in command/secret/add.go

View workflow job for this annotation

GitHub Actions / diff-review

directive `//nolint:dupl // ignore similar code with update` is unused for linter "dupl" (nolintlint)

Check failure on line 150 in command/secret/add.go

View workflow job for this annotation

GitHub Actions / full-review

directive `//nolint:dupl // ignore similar code with update` is unused for linter "dupl" (nolintlint)

Check failure on line 150 in command/secret/add.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] command/secret/add.go#L150

directive `//nolint:dupl // ignore similar code with update` is unused for linter "dupl" (nolintlint)
Raw output
command/secret/add.go:150:1: directive `//nolint:dupl // ignore similar code with update` is unused for linter "dupl" (nolintlint)
//nolint:dupl // ignore similar code with update
^
func add(c *cli.Context) error {
// load variables from the config file
err := action.Load(c)
Expand All @@ -167,18 +167,18 @@
//
// https://pkg.go.dev/github.com/go-vela/cli/action/secret?tab=doc#Config
s := &secret.Config{
Action: internal.ActionAdd,
Engine: c.String(internal.FlagSecretEngine),
Type: c.String(internal.FlagSecretType),
Org: c.String(internal.FlagOrg),
Repo: c.String(internal.FlagRepo),
Team: c.String("team"),
Name: c.String("name"),
Value: c.String("value"),
Images: c.StringSlice("image"),
Events: c.StringSlice("event"),
File: c.String("file"),
Output: c.String(internal.FlagOutput),
Action: internal.ActionAdd,
Engine: c.String(internal.FlagSecretEngine),
Type: c.String(internal.FlagSecretType),
Org: c.String(internal.FlagOrg),
Repo: c.String(internal.FlagRepo),
Team: c.String("team"),
Name: c.String("name"),
Value: c.String("value"),
Images: c.StringSlice("image"),
AllowEvents: c.StringSlice("event"),
File: c.String("file"),
Output: c.String(internal.FlagOutput),
}

// check if allow_command and allow_substitution are provided
Expand Down
24 changes: 12 additions & 12 deletions command/secret/update.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0

package secret

Check failure on line 3 in command/secret/update.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] command/secret/update.go#L3

3-216 lines are duplicate of `command/secret/add.go:3-216` (dupl)
Raw output
command/secret/update.go:3: 3-216 lines are duplicate of `command/secret/add.go:3-216` (dupl)
package secret

import (
	"fmt"
	"slices"

	"github.com/go-vela/cli/action"
	"github.com/go-vela/cli/action/secret"
	"github.com/go-vela/cli/internal"
	"github.com/go-vela/cli/internal/client"

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

	"github.com/urfave/cli/v2"
)

// CommandUpdate defines the command for updating a secret.
var CommandUpdate = &cli.Command{
	Name:        "secret",
	Description: "Use this command to update a secret.",
	Usage:       "Update details of the provided secret",
	Action:      update,
	Flags: []cli.Flag{

		// Repo Flags

		&cli.StringFlag{
			EnvVars: []string{"VELA_ORG", "SECRET_ORG"},
			Name:    internal.FlagOrg,
			Aliases: []string{"o"},
			Usage:   "provide the organization for the secret",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_REPO", "SECRET_REPO"},
			Name:    internal.FlagRepo,
			Aliases: []string{"r"},
			Usage:   "provide the repository for the secret",
		},

		// Secret Flags

		&cli.StringFlag{
			EnvVars: []string{"VELA_ENGINE", "SECRET_ENGINE"},
			Name:    internal.FlagSecretEngine,
			Aliases: []string{"e"},
			Usage:   "provide the engine that stores the secret",
			Value:   constants.DriverNative,
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_TYPE", "SECRET_TYPE"},
			Name:    internal.FlagSecretType,
			Aliases: []string{"ty"},
			Usage:   "provide the type of secret being stored",
			Value:   constants.SecretRepo,
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_TEAM", "SECRET_TEAM"},
			Name:    "team",
			Aliases: []string{"t"},
			Usage:   "provide the team for the secret",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_NAME", "SECRET_NAME"},
			Name:    "name",
			Aliases: []string{"n"},
			Usage:   "provide the name of the secret",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_VALUE", "SECRET_VALUE"},
			Name:    "value",
			Aliases: []string{"v"},
			Usage:   "provide the value for the secret",
		},
		&cli.StringSliceFlag{
			EnvVars: []string{"VELA_IMAGES", "SECRET_IMAGES"},
			Name:    "image",
			Aliases: []string{"i"},
			Usage:   "provide the image(s) that can access this secret",
		},
		&cli.StringSliceFlag{
			EnvVars: []string{"VELA_EVENTS", "SECRET_EVENTS"},
			Name:    "event",
			Aliases: []string{"events", "ev"},
			Usage:   "provide the event(s) that can access this secret",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_COMMAND", "SECRET_COMMAND"},
			Name:    internal.FlagSecretCommands,
			Aliases: []string{"c"},
			Usage:   "enable a secret to be used for a step with commands",
			Value:   "true",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_SUBSTITUTION", "SECRET_SUBSTITUTION"},
			Name:    internal.FlagSecretSubstitution,
			Aliases: []string{"s"},
			Usage:   "enable a secret to be substituted",
			Value:   "true",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_FILE", "SECRET_FILE"},
			Name:    "file",
			Aliases: []string{"f"},
			Usage:   "provide a file to update the secret(s)",
		},

		// Output Flags

		&cli.StringFlag{
			EnvVars: []string{"VELA_OUTPUT", "SECRET_OUTPUT"},
			Name:    internal.FlagOutput,
			Aliases: []string{"op"},
			Usage:   "Print the output in default, yaml or json format",
		},
	},
	CustomHelpTemplate: fmt.Sprintf(`%s
EXAMPLES:
   1. Update a repository secret.
     $ {{.HelpName}} --secret.engine native --secret.type repo --org MyOrg --repo MyRepo --name foo --value bar
   2. Update a repository secret and disallow usage in commands.
     $ {{.HelpName}} --secret.engine native --secret.type repo --org MyOrg --repo MyRepo --name foo --value bar --commands false
   3. Update an organization secret.
     $ {{.HelpName}} --secret.engine native --secret.type org --org MyOrg --name foo --value bar
   4. Update a shared secret.
     $ {{.HelpName}} --secret.engine native --secret.type shared --org MyOrg --team octokitties --name foo --value bar
   5. Update a repository secret with all event types enabled.
     $ {{.HelpName}} --secret.engine native --secret.type repo --org MyOrg --repo MyRepo --name foo --event comment --event deployment --event pull_request --event push --event tag
   6. Update a repository secret with an image whitelist.
     $ {{.HelpName}} --secret.engine native --secret.type repo --org MyOrg --repo MyRepo --name foo --image alpine --image golang:* --image postgres:latest
   7. Update a secret with value from a file.
     $ {{.HelpName}} --secret.engine native --secret.type repo --org MyOrg --repo MyRepo --name foo --value @secret.txt
   8. Update a repository secret with json output.
     $ {{.HelpName}} --secret.engine native --secret.type repo --org MyOrg --repo MyRepo --name foo --value bar --output json
   9. Update a secret or secrets from a file.
     $ {{.HelpName}} --file secret.yml
  10. Update a secret when config or environment variables are set.
     $ {{.HelpName}} --org MyOrg --repo MyRepo --name foo --value bar

DOCUMENTATION:

  https://go-vela.github.io/docs/reference/cli/secret/update/
`, cli.CommandHelpTemplate),
}

// helper function to capture the provided input
// and create the object used to modify a secret.
//
//nolint:dupl // ignore similar code with add
func update(c *cli.Context) error {
	// load variables from the config file
	err := action.Load(c)
	if err != nil {
		return err
	}

	// parse the Vela client from the context
	//
	// https://pkg.go.dev/github.com/go-vela/cli/internal/client?tab=doc#Parse
	client, err := client.Parse(c)
	if err != nil {
		return err
	}

	// create the secret configuration
	//
	// https://pkg.go.dev/github.com/go-vela/cli/action/secret?tab=doc#Config
	s := &secret.Config{
		Action:      internal.ActionUpdate,
		Engine:      c.String(internal.FlagSecretEngine),
		Type:        c.String(internal.FlagSecretType),
		Org:         c.String(internal.FlagOrg),
		Repo:        c.String(internal.FlagRepo),
		Team:        c.String("team"),
		Name:        c.String("name"),
		Value:       c.String("value"),
		Images:      c.StringSlice("image"),
		AllowEvents: c.StringSlice("event"),
		File:        c.String("file"),
		Output:      c.String(internal.FlagOutput),
	}

	// check if allow_command and allow_substitution are provided
	// if they are not, server will not update the fields
	if slices.Contains(c.FlagNames(), internal.FlagSecretCommands) {
		val := c.Bool(internal.FlagSecretCommands)
		s.AllowCommand = &val
	}

	if slices.Contains(c.FlagNames(), internal.FlagSecretSubstitution) {
		val := c.Bool(internal.FlagSecretSubstitution)
		s.AllowSubstitution = &val
	}

	// validate secret configuration
	//
	// https://pkg.go.dev/github.com/go-vela/cli/action/secret?tab=doc#Config.Validate
	err = s.Validate()
	if err != nil {
		return err
	}

	// check if secret file is provided
	if len(s.File) > 0 {
		// execute the update from file call for the secret configuration
		//
		// https://pkg.go.dev/github.com/go-vela/cli/action/secret?tab=doc#Config.UpdateFromFile
		return s.UpdateFromFile(client)
	}

	// execute the update call for the secret configuration
	//
	// https://pkg.go.dev/github.com/go-vela/cli/action/secret?tab=doc#Config.Update
	return s.Update(client)
}

import (
"fmt"
Expand Down Expand Up @@ -147,7 +147,7 @@
// helper function to capture the provided input
// and create the object used to modify a secret.
//
//nolint:dupl // ignore similar code with add

Check failure on line 150 in command/secret/update.go

View workflow job for this annotation

GitHub Actions / diff-review

directive `//nolint:dupl // ignore similar code with add` is unused for linter "dupl" (nolintlint)

Check failure on line 150 in command/secret/update.go

View workflow job for this annotation

GitHub Actions / full-review

directive `//nolint:dupl // ignore similar code with add` is unused for linter "dupl" (nolintlint)

Check failure on line 150 in command/secret/update.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] command/secret/update.go#L150

directive `//nolint:dupl // ignore similar code with add` is unused for linter "dupl" (nolintlint)
Raw output
command/secret/update.go:150:1: directive `//nolint:dupl // ignore similar code with add` is unused for linter "dupl" (nolintlint)
//nolint:dupl // ignore similar code with add
^
func update(c *cli.Context) error {
// load variables from the config file
err := action.Load(c)
Expand All @@ -167,18 +167,18 @@
//
// https://pkg.go.dev/github.com/go-vela/cli/action/secret?tab=doc#Config
s := &secret.Config{
Action: internal.ActionUpdate,
Engine: c.String(internal.FlagSecretEngine),
Type: c.String(internal.FlagSecretType),
Org: c.String(internal.FlagOrg),
Repo: c.String(internal.FlagRepo),
Team: c.String("team"),
Name: c.String("name"),
Value: c.String("value"),
Images: c.StringSlice("image"),
Events: c.StringSlice("event"),
File: c.String("file"),
Output: c.String(internal.FlagOutput),
Action: internal.ActionUpdate,
Engine: c.String(internal.FlagSecretEngine),
Type: c.String(internal.FlagSecretType),
Org: c.String(internal.FlagOrg),
Repo: c.String(internal.FlagRepo),
Team: c.String("team"),
Name: c.String("name"),
Value: c.String("value"),
Images: c.StringSlice("image"),
AllowEvents: c.StringSlice("event"),
File: c.String("file"),
Output: c.String(internal.FlagOutput),
}

// check if allow_command and allow_substitution are provided
Expand Down
11 changes: 7 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ require (
github.com/dustin/go-humanize v1.0.1
github.com/gin-gonic/gin v1.9.1
github.com/go-git/go-git/v5 v5.11.0
github.com/go-vela/sdk-go v0.23.3
github.com/go-vela/server v0.23.4
github.com/go-vela/types v0.23.3
github.com/go-vela/worker v0.23.3
github.com/go-vela/sdk-go v0.23.2
github.com/go-vela/server v0.23.4-0.20240401175144-f591935d2fc9
github.com/go-vela/types v0.23.4-0.20240401132228-9b43c701ab32
github.com/go-vela/worker v0.23.2
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/gosuri/uitable v0.0.4
github.com/joho/godotenv v1.5.1
Expand All @@ -36,6 +36,7 @@ require (
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
Expand Down Expand Up @@ -76,6 +77,7 @@ require (
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/goware/urlx v0.3.2 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
Expand All @@ -93,6 +95,7 @@ require (
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/microcosm-cc/bluemonday v1.0.26 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/reflectwalk v1.0.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
Expand Down
22 changes: 14 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFI
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/buildkite/yaml v0.0.0-20230306222819-0e4e032d4835 h1:Zfkih+Opdv9y5AOob+8iMsaMYnans+Ozrkb8wiPHbj0=
github.com/buildkite/yaml v0.0.0-20230306222819-0e4e032d4835/go.mod h1:AV5wtJnn1/CRaRGlJ8xspkMWfKXV0/pkJVgGleTIrfk=
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
Expand Down Expand Up @@ -114,14 +116,14 @@ github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg
github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/go-vela/sdk-go v0.23.3 h1:0MxD3iFUNaCvjMOW2cJp9uz0Pn5VLQqZPDnma9XTMgg=
github.com/go-vela/sdk-go v0.23.3/go.mod h1:F47QqtcAoQ1aulS+ze4kWcd3/m8QNEIB5IyiZoP7n4U=
github.com/go-vela/server v0.23.4 h1:0ENIF2/B3Ldjae7MJILk6SdMCuG1AJ6dnWvGVYyjucM=
github.com/go-vela/server v0.23.4/go.mod h1:4f6bFTtHgOGBkJdDJ76AYk/Ms+nyBEP8RebTFiYUKZ0=
github.com/go-vela/types v0.23.3 h1:Ktt5g5NW9NVkclP7UiuANblkJB2dDaG5xVnnLEFjIWM=
github.com/go-vela/types v0.23.3/go.mod h1:mEF9dLkk00rUXf/t39n2WvXZgJbxnPEEWy+DHqIlRUo=
github.com/go-vela/worker v0.23.3 h1:dejrxSO3t9/toD2mQ3LWvyJt6NXxoZcceteS9h9vFhY=
github.com/go-vela/worker v0.23.3/go.mod h1:FcM0YPBVU1j683r9B0JomkaBxM/deSGI8nbIXL4Zpb4=
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.4-0.20240401175144-f591935d2fc9 h1:R3TxguOk3JsIRoZn0oQBTLZRDIj4Xpeon+L/YJOF2Vw=
github.com/go-vela/server v0.23.4-0.20240401175144-f591935d2fc9/go.mod h1:Rbe6vgYe3gao8sBcALlhrM2YH4yu2cxJAFpWdDUbdZY=
github.com/go-vela/types v0.23.4-0.20240401132228-9b43c701ab32 h1:fqmNnM1LdH3Zg1zCADfgR7a51EOSZvLFAB2Em4CG+Pg=
github.com/go-vela/types v0.23.4-0.20240401132228-9b43c701ab32/go.mod h1:mEF9dLkk00rUXf/t39n2WvXZgJbxnPEEWy+DHqIlRUo=
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=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
Expand Down Expand Up @@ -152,6 +154,8 @@ github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLe
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
github.com/gosuri/uitable v0.0.4 h1:IG2xLKRvErL3uhY6e1BylFzG+aJiwQviDDTfOKeKTpY=
github.com/gosuri/uitable v0.0.4/go.mod h1:tKR86bXuXPZazfOTG1FIzvjIdXzd0mo4Vtn16vt0PJo=
github.com/goware/urlx v0.3.2 h1:gdoo4kBHlkqZNaf6XlQ12LGtQOmpKJrR04Rc3RnpJEo=
Expand Down Expand Up @@ -208,6 +212,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/microcosm-cc/bluemonday v1.0.26 h1:xbqSvqzQMeEHCqMi64VAs4d8uy6Mequs3rQ0k/Khz58=
github.com/microcosm-cc/bluemonday v1.0.26/go.mod h1:JyzOCs9gkyQyjs+6h10UEVSe02CGwkhd72Xdqh78TWs=
github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ=
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
Expand Down
Loading