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
Merged
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
5 changes: 5 additions & 0 deletions action/pipeline/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

"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/server/compiler"
Expand Down Expand Up @@ -69,6 +70,10 @@
return fmt.Errorf("invalid format for template file: %s (valid format: <name>:<source>)", file)
}
}
case "exec":
if strings.EqualFold(c.Event, constants.EventTag) && len(c.Tag) == 0 {
return fmt.Errorf("no tag provided for tag event")
}
}

return nil
Expand Down Expand Up @@ -141,7 +146,7 @@
}

// ValidateRemote validates a remote pipeline based off the provided configuration.
func (c *Config) ValidateRemote(client *vela.Client) error {

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

View workflow job for this annotation

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)
	}
}
logrus.Debug("executing validate for remote pipeline configuration")

logrus.Tracef("validating pipeline %s/%s@%s", c.Org, c.Repo, c.Ref)
Expand Down
19 changes: 19 additions & 0 deletions action/pipeline/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@
Output: "",
},
},
{
failure: true,
config: &Config{
Action: "exec",
Org: "github",
Repo: "octocat",
Event: "tag",
},
},
{
failure: false,
config: &Config{
Action: "exec",
Org: "github",
Repo: "octocat",
Event: "tag",
Tag: "v1.0.0",
},
},
{
failure: false,
config: &Config{
Expand Down Expand Up @@ -423,12 +442,12 @@
t.Error(err)
}
}
got, err := validateFile(tt.args.path)

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

View workflow job for this annotation

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)
			^
if (err != nil) != tt.wantErr {

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

View workflow job for this annotation

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 {
			^
t.Errorf("validateFile() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {

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

View workflow job for this annotation

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 {
			^
t.Errorf("validateFile() got = %v, want %v", got, tt.want)
}
})
Expand Down
Loading