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

fix(secrets): abstract populateEvents for secret usage #542

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 2 additions & 70 deletions action/repo/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ package repo
import (
"fmt"

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

"github.com/go-vela/sdk-go/vela"

"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 @@ -45,7 +44,7 @@ func (c *Config) Add(client *vela.Client) error {
logrus.Tracef("adding repo %s/%s", c.Org, c.Name)

if len(c.Events) > 0 {
populateEvents(r, c.Events)
r.SetAllowEvents(internal.PopulateEvents(c.Events))
}

// send API call to add a repository
Expand Down Expand Up @@ -85,70 +84,3 @@ 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.EventDelete:
push.SetDeleteBranch(true)
push.SetDeleteTag(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)
case constants.EventDelete + ":" + constants.ActionBranch:
push.SetDeleteBranch(true)
case constants.EventDelete + ":" + constants.ActionTag:
push.SetDeleteTag(true)
}
}

result.SetPush(push)
result.SetPullRequest(pull)
result.SetDeployment(deploy)
result.SetComment(comment)

r.SetAllowEvents(result)
}
87 changes: 0 additions & 87 deletions action/repo/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ 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"
)
Expand Down Expand Up @@ -155,87 +152,3 @@ 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", "delete"},
want: &library.Repo{
AllowPush: &tBool,
AllowPull: &tBool,
AllowTag: &tBool,
AllowDeploy: &tBool,
AllowComment: &tBool,
AllowEvents: &library.Events{
Push: &actions.Push{
Branch: &tBool,
Tag: &tBool,
DeleteBranch: &tBool,
DeleteTag: &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", "delete:branch", "delete:tag"},
want: &library.Repo{
AllowPush: &tBool,
AllowPull: &fBool,
AllowTag: &tBool,
AllowDeploy: &tBool,
AllowComment: &fBool,
AllowEvents: &library.Events{
Push: &actions.Push{
Branch: &tBool,
Tag: &tBool,
DeleteBranch: &tBool,
DeleteTag: &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)
}
}
}
6 changes: 0 additions & 6 deletions action/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,3 @@ type Config struct {
PerPage int
Output string
}

// Alternate constants for webhook events.
const (
AlternateDeploy = "deploy"
AlternatePull = "pull"
)
3 changes: 2 additions & 1 deletion action/repo/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package repo
import (
"fmt"

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

"github.com/go-vela/sdk-go/vela"
Expand Down Expand Up @@ -41,7 +42,7 @@ func (c *Config) Update(client *vela.Client) error {
}

if len(c.Events) > 0 {
populateEvents(r, c.Events)
r.SetAllowEvents(internal.PopulateEvents(c.Events))
}

logrus.Tracef("updating repo %s/%s", c.Org, c.Name)
Expand Down
6 changes: 6 additions & 0 deletions action/secret/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"strings"

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

"github.com/go-vela/sdk-go/vela"
Expand Down Expand Up @@ -62,6 +63,11 @@ func (c *Config) Add(client *vela.Client) error {
AllowSubstitution: c.AllowSubstitution,
}

// populate events if provided
if len(c.Events) > 0 {
s.SetAllowEvents(internal.PopulateEvents(c.Events))
}

logrus.Tracef("adding secret %s/%s/%s/%s/%s", c.Engine, c.Type, c.Org, name, c.Name)

// send API call to add a secret
Expand Down
6 changes: 6 additions & 0 deletions action/secret/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"strings"

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

"github.com/go-vela/sdk-go/vela"
Expand Down Expand Up @@ -62,6 +63,11 @@ func (c *Config) Update(client *vela.Client) error {
AllowSubstitution: c.AllowSubstitution,
}

// populate events if provided
if len(c.Events) > 0 {
s.SetAllowEvents(internal.PopulateEvents(c.Events))
}

logrus.Tracef("modifying secret %s/%s/%s/%s/%s", c.Engine, c.Type, c.Org, name, c.Name)

// send API call to update a secret
Expand Down
2 changes: 1 addition & 1 deletion command/repo/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ var CommandAdd = &cli.Command{
&cli.StringSliceFlag{
EnvVars: []string{"VELA_EVENTS", "REPO_EVENTS", "VELA_ADD_EVENTS", "REPO_ADD_EVENTS"},
Name: "event",
Aliases: []string{"events", "add-event", "add-events", "e"},
Aliases: []string{"events", "e"},
Usage: "webhook event(s) repository responds to",
},
&cli.StringFlag{
Expand Down
2 changes: 1 addition & 1 deletion command/repo/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ var CommandUpdate = &cli.Command{
&cli.StringSliceFlag{
EnvVars: []string{"VELA_EVENTS", "REPO_EVENTS", "VELA_ADD_EVENTS", "REPO_ADD_EVENTS"},
Name: "event",
Aliases: []string{"events", "add-event", "add-events", "e"},
Aliases: []string{"events", "e"},
Usage: "webhook event(s) repository responds to",
},
&cli.StringFlag{
Expand Down
7 changes: 1 addition & 6 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"),
		Events: 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 @@ -82,13 +82,8 @@
&cli.StringSliceFlag{
EnvVars: []string{"VELA_EVENTS", "SECRET_EVENTS"},
Name: "event",
Aliases: []string{"ev"},
Aliases: []string{"events", "ev"},
Usage: "provide the event(s) that can access this secret",
Value: cli.NewStringSlice(
constants.EventDeploy,
constants.EventPush,
constants.EventTag,
),
},
&cli.StringFlag{
EnvVars: []string{"VELA_COMMAND", "SECRET_COMMAND"},
Expand Down Expand Up @@ -152,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 Down
2 changes: 1 addition & 1 deletion 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"),
		Events: 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 @@ -82,7 +82,7 @@
&cli.StringSliceFlag{
EnvVars: []string{"VELA_EVENTS", "SECRET_EVENTS"},
Name: "event",
Aliases: []string{"ev"},
Aliases: []string{"events", "ev"},
Usage: "provide the event(s) that can access this secret",
},
&cli.StringFlag{
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 Down
84 changes: 84 additions & 0 deletions internal/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// SPDX-License-Identifier: Apache-2.0

package internal

import (
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/go-vela/types/library/actions"
)

const (
// AlternatePull is an alternate name for the pull_request event.
AlternatePull = "pull"
// AlternateDeploy is an alternate name for the deployment event.
AlternateDeploy = "deploy"
)

// 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(events []string) *library.Events {
result := new(library.Events)
push := new(actions.Push)
pull := new(actions.Pull)
comment := new(actions.Comment)
deploy := new(actions.Deploy)
schedule := new(actions.Schedule)

// iterate through all events provided
for _, event := range events {
switch event {
// push actions
case constants.EventPush, constants.EventPush + ":branch":
push.SetBranch(true)
case constants.EventTag, constants.EventPush + ":" + constants.EventTag:
push.SetTag(true)
case constants.EventDelete + ":" + constants.ActionBranch:
push.SetDeleteBranch(true)
case constants.EventDelete + ":" + constants.ActionTag:
push.SetDeleteTag(true)
case constants.EventDelete:
push.SetDeleteBranch(true)
push.SetDeleteTag(true)

// pull_request actions
case constants.EventPull, AlternatePull:
pull.SetOpened(true)
pull.SetReopened(true)
pull.SetSynchronize(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)

// deployment actions
case constants.EventDeploy, AlternateDeploy, constants.EventDeploy + ":" + constants.ActionCreated:
deploy.SetCreated(true)

// comment actions
case constants.EventComment:
comment.SetCreated(true)
comment.SetEdited(true)
case constants.EventComment + ":" + constants.ActionCreated:
comment.SetCreated(true)
case constants.EventComment + ":" + constants.ActionEdited:
comment.SetEdited(true)

// schedule actions
case constants.EventSchedule, constants.EventSchedule + ":run":
schedule.SetRun(true)
}
}

result.SetPush(push)
result.SetPullRequest(pull)
result.SetDeployment(deploy)
result.SetComment(comment)
result.SetSchedule(schedule)

return result
}
Loading
Loading