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

enhance(deploy): validate on CreateDeployment #1236

Merged
merged 2 commits into from
Jan 8, 2025
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
23 changes: 23 additions & 0 deletions api/deployment/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,29 @@ func CreateDeployment(c *gin.Context) {
input.SetRef(fmt.Sprintf("refs/heads/%s", r.GetBranch()))
}

deployConfigYAML, err := getDeploymentConfig(c, l, u, r, input.GetRef())
if err != nil {
retErr := fmt.Errorf("unable to get deployment config for %s: %w", r.GetFullName(), err)

util.HandleError(c, http.StatusInternalServerError, retErr)

return
}

deployConfig := deployConfigYAML.ToPipeline()

if !deployConfig.Empty() {
err := deployConfig.Validate(input.GetTarget(), input.GetPayload())

if err != nil {
retErr := fmt.Errorf("unable to validate deployment config for %s: %w", r.GetFullName(), err)

util.HandleError(c, http.StatusBadRequest, retErr)

return
}
}

// send API call to create the deployment
err = scm.FromContext(c).CreateDeployment(ctx, u, r, input)
if err != nil {
Expand Down
40 changes: 22 additions & 18 deletions api/deployment/get_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
"github.com/sirupsen/logrus"
"gorm.io/gorm"

"github.com/go-vela/server/api/types"
"github.com/go-vela/server/compiler"
"github.com/go-vela/server/compiler/types/yaml/yaml"
"github.com/go-vela/server/database"
"github.com/go-vela/server/router/middleware/repo"
"github.com/go-vela/server/router/middleware/user"
Expand Down Expand Up @@ -72,11 +74,25 @@ func GetDeploymentConfig(c *gin.Context) {
r := repo.Retrieve(c)
u := user.Retrieve(c)

ctx := c.Request.Context()

// capture ref from parameters - use default branch if not provided
ref := util.QueryParameter(c, "ref", r.GetBranch())

deployConfig, err := getDeploymentConfig(c, l, u, r, ref)
if err != nil {
retErr := fmt.Errorf("unable to get deployment config for %s: %w", r.GetFullName(), err)

util.HandleError(c, http.StatusInternalServerError, retErr)

return
}

c.JSON(http.StatusOK, deployConfig)
}

// getDeploymentConfig is a helper function that lightly compiles a Vela pipeline to retrieve the deployment configuration.
func getDeploymentConfig(c *gin.Context, l *logrus.Entry, u *types.User, r *types.Repo, ref string) (yaml.Deployment, error) {
ctx := c.Request.Context()

entry := fmt.Sprintf("%s@%s", r.GetFullName(), ref)

l.Debugf("reading deployment config %s", entry)
Expand All @@ -91,19 +107,11 @@ func GetDeploymentConfig(c *gin.Context) {

config, err = scm.FromContext(c).ConfigBackoff(ctx, u, r, ref)
if err != nil {
retErr := fmt.Errorf("unable to get pipeline configuration for %s: %w", entry, err)

util.HandleError(c, http.StatusNotFound, retErr)

return
return yaml.Deployment{}, fmt.Errorf("unable to get pipeline configuration for %s: %w", entry, err)
}
} else {
// some other error
retErr := fmt.Errorf("unable to get pipeline for %s: %w", entry, err)

util.HandleError(c, http.StatusInternalServerError, retErr)

return
return yaml.Deployment{}, fmt.Errorf("unable to get pipeline for %s: %w", entry, err)
}
} else {
l.Debugf("pipeline %s found in database", entry)
Expand All @@ -117,12 +125,8 @@ func GetDeploymentConfig(c *gin.Context) {
// compile the pipeline
pipeline, _, err := compiler.CompileLite(ctx, config, nil, true)
if err != nil {
retErr := fmt.Errorf("unable to compile pipeline %s: %w", entry, err)

util.HandleError(c, http.StatusBadRequest, retErr)

return
return yaml.Deployment{}, fmt.Errorf("unable to compile pipeline %s: %w", entry, err)
}

c.JSON(http.StatusOK, pipeline.Deployment)
return pipeline.Deployment, nil
}
2 changes: 1 addition & 1 deletion compiler/types/pipeline/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (d *Deployment) Validate(target string, inputParams map[string]string) erro

// validate targets
if len(d.Targets) > 0 && !slices.Contains(d.Targets, target) {
return fmt.Errorf("deployment target %s not found in deployment config targets", target)
return fmt.Errorf("deployment target `%s` not found in deployment config targets", target)
}

// validate params
Expand Down
Loading