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 / 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 / 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
67 changes: 52 additions & 15 deletions action/secret/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"strings"

"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 @@ -83,26 +85,61 @@
// 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:
fallthrough
case constants.EventDeploy:
fallthrough
case constants.EventPull:
fallthrough
case constants.EventPush:
fallthrough
case constants.EventSchedule:
fallthrough
case constants.EventTag:
continue
default:
valid := false
for _, e := range validEvents() {

Check failure on line 91 in action/secret/validate.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] action/secret/validate.go#L91

ranges should only be cuddled with assignments used in the iteration (wsl)
Raw output
action/secret/validate.go:91:4: ranges should only be cuddled with assignments used in the iteration (wsl)
			for _, e := range validEvents() {
			^
plyr4 marked this conversation as resolved.
Show resolved Hide resolved
plyr4 marked this conversation as resolved.
Show resolved Hide resolved
if event == e {
plyr4 marked this conversation as resolved.
Show resolved Hide resolved
valid = true
break
}
}

if !valid {
return fmt.Errorf("invalid secret event provided: %s", event)
}
}
}

return nil
}

// returns a useable list of valid events using a combination of hardcoded shorthand names and AllowEvents.List()

Check failure on line 107 in action/secret/validate.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] action/secret/validate.go#L107

Comment should end in a period (godot)
Raw output
action/secret/validate.go:107:114: Comment should end in a period (godot)
// returns a useable list of valid events using a combination of hardcoded shorthand names and AllowEvents.List()
                                                                                                                 ^
plyr4 marked this conversation as resolved.
Show resolved Hide resolved
func validEvents() []string {
plyr4 marked this conversation as resolved.
Show resolved Hide resolved
shorthands := []string{
"push",
"pull_request",
"deployment",
"comment",
"schedule",
}

t := true

evs := library.Events{
Push: &actions.Push{
Branch: &t,
Tag: &t,
DeleteBranch: &t,
DeleteTag: &t,
},
PullRequest: &actions.Pull{
Opened: &t,
Edited: &t,
Synchronize: &t,
Reopened: &t,
},
Deployment: &actions.Deploy{
Created: &t,
},
Comment: &actions.Comment{
Created: &t,
Edited: &t,
},
Schedule: &actions.Schedule{
Run: &t,
},
}

return append(evs.List(), shorthands...)
}
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
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 / 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 / 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
Loading
Loading