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(pipeline): validate presence of tag on tag events to avoid panic #529

Merged
merged 2 commits into from
Feb 9, 2024

fix switch case

21b602a
Select commit
Loading
Failed to load commit list.
Sign in for the full log view
Merged

fix(pipeline): validate presence of tag on tag events to avoid panic #529

fix switch case
21b602a
Select commit
Loading
Failed to load commit list.
GitHub Actions / golangci failed Feb 8, 2024 in 0s

reviewdog [golangci] report

reported by reviewdog 🐶

Findings (10)

internal/output/yaml_test.go|72 col 39| unused-parameter: parameter 'unmarshal' seems to be unused, consider removing or renaming it as _ (revive)
command/repo/add.go|3| 3-200 lines are duplicate of command/repo/update.go:3-198 (dupl)
command/repo/update.go|3| 3-198 lines are duplicate of command/repo/add.go:3-200 (dupl)
action/pipeline/validate.go|149| 149-197 lines are duplicate of action/pipeline/compile.go:15-63 (dupl)
action/pipeline/exec.go|24| Function 'Exec' is too long (163 > 160) (funlen)
action/pipeline/validate_test.go|450 col 4| if statements should only be cuddled with assignments (wsl)
action/pipeline/validate_test.go|445 col 4| assignments should only be cuddled with other assignments (wsl)
action/pipeline/validate_test.go|446 col 4| only one cuddle assignment allowed before if statement (wsl)
command/worker/add.go|3 col 1| directive //nolint:dupl // ignore similar code with update is unused for linter "dupl" (nolintlint)
command/worker/view.go|70 col 1| directive //nolint:dupl // ignore similar code with chown, get, remove and repair is unused for linter "dupl" (nolintlint)

Filtered Findings (0)

Annotations

Check failure on line 72 in internal/output/yaml_test.go

See this annotation in the file changed.

@github-actions github-actions / golangci

[golangci] internal/output/yaml_test.go#L72

unused-parameter: parameter 'unmarshal' seems to be unused, consider removing or renaming it as _ (revive)
Raw output
internal/output/yaml_test.go:72:39: unused-parameter: parameter 'unmarshal' seems to be unused, consider removing or renaming it as _ (revive)
func (f *failMarshaler) UnmarshalYAML(unmarshal func(interface{}) error) error {
                                      ^

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

See this annotation in the file changed.

@github-actions github-actions / golangci

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

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

import (
	"fmt"

	"github.com/go-vela/cli/action"
	"github.com/go-vela/cli/action/repo"
	"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 repository.
var CommandAdd = &cli.Command{
	Name:        "repo",
	Description: "Use this command to add a repository.",
	Usage:       "Add a new repository from the provided configuration",
	Action:      add,
	Flags: []cli.Flag{

		// Repo Flags

		&cli.StringFlag{
			EnvVars: []string{"VELA_ORG", "REPO_ORG"},
			Name:    internal.FlagOrg,
			Aliases: []string{"o"},
			Usage:   "provide the organization for the repository",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_REPO", "REPO_NAME"},
			Name:    internal.FlagRepo,
			Aliases: []string{"r"},
			Usage:   "provide the name for the repository",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_BRANCH", "REPO_BRANCH"},
			Name:    "branch",
			Aliases: []string{"b"},
			Usage:   "default branch for the repository",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_LINK", "REPO_LINK"},
			Name:    "link",
			Aliases: []string{"l"},
			Usage:   "full URL to repository in source control",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_CLONE", "REPO_CLONE"},
			Name:    "clone",
			Aliases: []string{"c"},
			Usage:   "full clone URL to repository in source control",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_VISIBILITY", "REPO_VISIBILITY"},
			Name:    "visibility",
			Aliases: []string{"v"},
			Usage:   "access level required to view the repository",
			Value:   constants.VisibilityPublic,
		},
		&cli.Int64Flag{
			EnvVars: []string{"VELA_BUILD_LIMIT", "REPO_BUILD_LIMIT"},
			Name:    "build.limit",
			Usage:   "limit of concurrent builds allowed in repository",
			Value:   constants.BuildLimitDefault,
		},
		&cli.Int64Flag{
			EnvVars: []string{"VELA_TIMEOUT", "REPO_TIMEOUT"},
			Name:    "timeout",
			Aliases: []string{"t"},
			Usage:   "max time allowed per build in repository",
			Value:   constants.BuildTimeoutDefault,
		},
		&cli.IntFlag{
			EnvVars: []string{"VELA_COUNTER", "REPO_COUNTER"},
			Name:    "counter",
			Aliases: []string{"ct"},
			Usage:   "set a value for a new build number",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_PRIVATE", "REPO_PRIVATE"},
			Name:    "private",
			Aliases: []string{"p"},
			Usage:   "disable public access to the repository",
			Value:   "false",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_TRUSTED", "REPO_TRUSTED"},
			Name:    "trusted",
			Aliases: []string{"tr"},
			Usage:   "elevated permissions for builds executed for repo",
			Value:   "false",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_ACTIVE", "REPO_ACTIVE"},
			Name:    "active",
			Aliases: []string{"a"},
			Usage:   "current status of the repository",
			Value:   "true",
		},
		&cli.StringSliceFlag{
			EnvVars: []string{"VELA_EVENTS", "REPO_EVENTS", "VELA_ADD_EVENTS", "REPO_ADD_EVENTS"},
			Name:    "event",
			Aliases: []string{"events", "add-event", "add-events", "e"},
			Usage:   "webhook event(s) repository responds to",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_PIPELINE_TYPE", "PIPELINE_TYPE"},
			Name:    "pipeline-type",
			Aliases: []string{"pt"},
			Usage:   "type of base pipeline for the compiler to render",
			Value:   constants.PipelineTypeYAML,
		},

		// Output Flags

		&cli.StringFlag{
			EnvVars: []string{"VELA_OUTPUT", "REPO_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 with push and pull request enabled.
    $ {{.HelpName}} --org MyOrg --repo MyRepo --event push --event pull_request
  2. Add a repository with all event types enabled.
    $ {{.HelpName}} --org MyOrg --repo MyRepo --event push --event pull_request --event tag --event deployment --event comment
  3. Add a repository with a longer build timeout.
    $ {{.HelpName}} --org MyOrg --repo MyRepo --timeout 90
  4. Add a repository when config or environment variables are set.
    $ {{.HelpName}} --event push --event pull_request
  5. Add a repository with a starting build number.
    $ {{.HelpName}} --org MyOrg --repo MyRepo --counter 90
  6. Add a repository with a starlark pipeline file.
    $ {{.HelpName}} --org MyOrg --repo MyRepo --pipeline-type starlark

DOCUMENTATION:

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

// helper function to capture the provided input
// and create the object used to create a repo.
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 repo configuration
	//
	// https://pkg.go.dev/github.com/go-vela/cli/action/repo?tab=doc#Config
	r := &repo.Config{
		Action:       internal.ActionAdd,
		Org:          c.String(internal.FlagOrg),
		Name:         c.String(internal.FlagRepo),
		Branch:       c.String("branch"),
		Link:         c.String("link"),
		Clone:        c.String("clone"),
		Visibility:   c.String("visibility"),
		BuildLimit:   c.Int64("build.limit"),
		Timeout:      c.Int64("timeout"),
		Counter:      c.Int("counter"),
		Private:      c.Bool("private"),
		Trusted:      c.Bool("trusted"),
		Active:       c.Bool("active"),
		Events:       c.StringSlice("event"),
		PipelineType: c.String("pipeline-type"),
		Output:       c.String(internal.FlagOutput),
	}

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

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

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

See this annotation in the file changed.

@github-actions github-actions / golangci

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

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

import (
	"fmt"

	"github.com/go-vela/cli/action"
	"github.com/go-vela/cli/action/repo"
	"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 modifying a repository.
var CommandUpdate = &cli.Command{
	Name:        "repo",
	Description: "Use this command to update a repository.",
	Usage:       "Update a new repository from the provided configuration",
	Action:      update,
	Flags: []cli.Flag{

		// Repo Flags

		&cli.StringFlag{
			EnvVars: []string{"VELA_ORG", "REPO_ORG"},
			Name:    internal.FlagOrg,
			Aliases: []string{"o"},
			Usage:   "provide the organization for the repository",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_REPO", "REPO_NAME"},
			Name:    internal.FlagRepo,
			Aliases: []string{"r"},
			Usage:   "provide the name for the repository",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_BRANCH", "REPO_BRANCH"},
			Name:    "branch",
			Aliases: []string{"b"},
			Usage:   "default branch for the repository",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_LINK", "REPO_LINK"},
			Name:    "link",
			Aliases: []string{"l"},
			Usage:   "full URL to repository in source control",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_CLONE", "REPO_CLONE"},
			Name:    "clone",
			Aliases: []string{"c"},
			Usage:   "full clone URL to repository in source control",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_VISIBILITY", "REPO_VISIBILITY"},
			Name:    "visibility",
			Aliases: []string{"v"},
			Usage:   "access level required to view the repository",
			Value:   constants.VisibilityPublic,
		},
		&cli.Int64Flag{
			EnvVars: []string{"VELA_BUILD_LIMIT", "REPO_BUILD_LIMIT"},
			Name:    "build.limit",
			Usage:   "limit of concurrent builds allowed in repository",
			Value:   constants.BuildLimitDefault,
		},
		&cli.Int64Flag{
			EnvVars: []string{"VELA_TIMEOUT", "REPO_TIMEOUT"},
			Name:    "timeout",
			Aliases: []string{"t"},
			Usage:   "max time allowed per build in repository",
			Value:   constants.BuildTimeoutDefault,
		},
		&cli.IntFlag{
			EnvVars: []string{"VELA_COUNTER", "REPO_COUNTER"},
			Name:    "counter",
			Aliases: []string{"ct"},
			Usage:   "set a value for a new build number",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_PRIVATE", "REPO_PRIVATE"},
			Name:    "private",
			Aliases: []string{"p"},
			Usage:   "disable public access to the repository",
			Value:   "false",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_TRUSTED", "REPO_TRUSTED"},
			Name:    "trusted",
			Aliases: []string{"tr"},
			Usage:   "elevated permissions for builds executed for repo",
			Value:   "false",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_ACTIVE", "REPO_ACTIVE"},
			Name:    "active",
			Aliases: []string{"a"},
			Usage:   "current status of the repository",
			Value:   "true",
		},
		&cli.StringSliceFlag{
			EnvVars: []string{"VELA_EVENTS", "REPO_EVENTS", "VELA_ADD_EVENTS", "REPO_ADD_EVENTS"},
			Name:    "event",
			Aliases: []string{"events", "add-event", "add-events", "e"},
			Usage:   "webhook event(s) repository responds to",
		},
		&cli.StringFlag{
			EnvVars: []string{"VELA_PIPELINE_TYPE", "PIPELINE_TYPE"},
			Name:    "pipeline-type",
			Aliases: []string{"pt"},
			Usage:   "type of base pipeline for the compiler to render",
			Value:   constants.PipelineTypeYAML,
		},

		// Output Flags

		&cli.StringFlag{
			EnvVars: []string{"VELA_OUTPUT", "REPO_OUTPUT"},
			Name:    internal.FlagOutput,
			Aliases: []string{"op"},
			Usage:   "format the output in json, spew or yaml",
		},
	},
	CustomHelpTemplate: fmt.Sprintf(`%s
EXAMPLES:
  1. Update a repository with push and pull request enabled.
    $ {{.HelpName}} --org MyOrg --repo MyRepo --event push --event pull_request
  2. Update a repository with all event types enabled.
    $ {{.HelpName}} --org MyOrg --repo MyRepo --event push,pull_request,tag,deployment,comment
  3. Update a repository with a longer build timeout.
    $ {{.HelpName}} --org MyOrg --repo MyRepo --timeout 90
  4. Update a repository when config or environment variables are set.
    $ {{.HelpName}}
  5. Update a repository with a new build number.
    $ {{.HelpName}} --org MyOrg --repo MyRepo --counter 200

DOCUMENTATION:

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

// helper function to capture the provided input
// and create the object used to modify a repository.
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 repo configuration
	//
	// https://pkg.go.dev/github.com/go-vela/cli/action/repo?tab=doc#Config
	r := &repo.Config{
		Action:       internal.ActionUpdate,
		Org:          c.String(internal.FlagOrg),
		Name:         c.String(internal.FlagRepo),
		Branch:       c.String("branch"),
		Link:         c.String("link"),
		Clone:        c.String("clone"),
		Visibility:   c.String("visibility"),
		BuildLimit:   c.Int64("build.limit"),
		Timeout:      c.Int64("timeout"),
		Counter:      c.Int("counter"),
		Private:      c.Bool("private"),
		Trusted:      c.Bool("trusted"),
		Active:       c.Bool("active"),
		Events:       c.StringSlice("event"),
		PipelineType: c.String("pipeline-type"),
		Output:       c.String(internal.FlagOutput),
	}

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

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

Check failure on line 149 in action/pipeline/validate.go

See this annotation in the file changed.

@github-actions github-actions / golangci

[golangci] action/pipeline/validate.go#L149

149-197 lines are duplicate of `action/pipeline/compile.go:15-63` (dupl)
Raw output
action/pipeline/validate.go:149: 149-197 lines are duplicate of `action/pipeline/compile.go:15-63` (dupl)
func (c *Config) ValidateRemote(client *vela.Client) error {
	logrus.Debug("executing validate for remote pipeline configuration")

	logrus.Tracef("validating pipeline %s/%s@%s", c.Org, c.Repo, c.Ref)

	// set the pipeline options for the call
	//
	// https://pkg.go.dev/github.com/go-vela/sdk-go/vela?tab=doc#PipelineOptions
	opts := &vela.PipelineOptions{
		Output: c.Output,
	}

	// send API call to validate a pipeline
	//
	// https://pkg.go.dev/github.com/go-vela/sdk-go/vela?tab=doc#PipelineService.Validate
	pipeline, _, err := client.Pipeline.Validate(c.Org, c.Repo, c.Ref, opts)
	if err != nil {
		return err
	}

	// handle the output based off the provided configuration
	switch c.Output {
	case output.DriverDump:
		// output the pipeline in dump format
		//
		// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Dump
		return output.Dump(pipeline)
	case output.DriverJSON:
		// output the pipeline in JSON format
		//
		// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#JSON
		return output.JSON(pipeline)
	case output.DriverSpew:
		// output the pipeline in spew format
		//
		// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Spew
		return output.Spew(pipeline)
	case output.DriverYAML:
		// output the pipeline in YAML format
		//
		// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#YAML
		return output.YAML(pipeline)
	default:
		// output the pipeline in stdout format
		//
		// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Stdout
		return output.Stdout(pipeline)
	}
}

Check failure on line 24 in action/pipeline/exec.go

See this annotation in the file changed.

@github-actions github-actions / golangci

[golangci] action/pipeline/exec.go#L24

Function 'Exec' is too long (163 > 160) (funlen)
Raw output
action/pipeline/exec.go:24: Function 'Exec' is too long (163 > 160) (funlen)
func (c *Config) Exec(client compiler.Engine) error {

Check failure on line 450 in action/pipeline/validate_test.go

See this annotation in the file changed.

@github-actions github-actions / golangci

[golangci] action/pipeline/validate_test.go#L450

if statements should only be cuddled with assignments (wsl)
Raw output
action/pipeline/validate_test.go:450:4: if statements should only be cuddled with assignments (wsl)
			if got != tt.want {
			^

Check failure on line 445 in action/pipeline/validate_test.go

See this annotation in the file changed.

@github-actions github-actions / golangci

[golangci] action/pipeline/validate_test.go#L445

assignments should only be cuddled with other assignments (wsl)
Raw output
action/pipeline/validate_test.go:445:4: assignments should only be cuddled with other assignments (wsl)
			got, err := validateFile(tt.args.path)
			^

Check failure on line 446 in action/pipeline/validate_test.go

See this annotation in the file changed.

@github-actions github-actions / golangci

[golangci] action/pipeline/validate_test.go#L446

only one cuddle assignment allowed before if statement (wsl)
Raw output
action/pipeline/validate_test.go:446:4: only one cuddle assignment allowed before if statement (wsl)
			if (err != nil) != tt.wantErr {
			^

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

See this annotation in the file changed.

@github-actions github-actions / golangci

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

directive `//nolint:dupl // ignore similar code with update` is unused for linter "dupl" (nolintlint)
Raw output
command/worker/add.go:3:1: directive `//nolint:dupl // ignore similar code with update` is unused for linter "dupl" (nolintlint)
//nolint:dupl // ignore similar code with update
^

Check failure on line 70 in command/worker/view.go

See this annotation in the file changed.

@github-actions github-actions / golangci

[golangci] command/worker/view.go#L70

directive `//nolint:dupl // ignore similar code with chown, get, remove and repair` is unused for linter "dupl" (nolintlint)
Raw output
command/worker/view.go:70:1: directive `//nolint:dupl // ignore similar code with chown, get, remove and repair` is unused for linter "dupl" (nolintlint)
//nolint:dupl // ignore similar code with chown, get, remove and repair
^