Skip to content

Commit

Permalink
enhance(deploy): validate on CreateDeployment (#1236)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrupper authored Jan 8, 2025
1 parent f34ffb9 commit 355199f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
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

0 comments on commit 355199f

Please sign in to comment.