Skip to content

Commit

Permalink
New Project/StarterProject Error Types (devfile#614)
Browse files Browse the repository at this point in the history
* New Error Tests

* Update error message
  • Loading branch information
kim-tsao authored Sep 10, 2021
1 parent 96ab67b commit da620cd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
46 changes: 46 additions & 0 deletions pkg/validation/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,52 @@ func (e *InvalidComponentError) Error() string {
return fmt.Sprintf("the component %q is invalid - %s", e.componentName, e.reason)
}

//MissingProjectRemoteError returns an error if the git remotes object under a project is empty
type MissingProjectRemoteError struct {
projectName string
}

func (e *MissingProjectRemoteError) Error() string {
return fmt.Sprintf("project %s should have at least one remote", e.projectName)
}

//MissingStarterProjectRemoteError returns an error if the git remotes object under a starterProject is empty
type MissingStarterProjectRemoteError struct {
projectName string
}

func (e *MissingStarterProjectRemoteError) Error() string {
return fmt.Sprintf("starterProject %s should have at least one remote", e.projectName)
}

//MultipleStarterProjectRemoteError returns an error if multiple git remotes are specified. There can only be one remote.
type MultipleStarterProjectRemoteError struct {
projectName string
}

func (e *MultipleStarterProjectRemoteError) Error() string {
return fmt.Sprintf("starterProject %s should have one remote only", e.projectName)
}

//MissingProjectCheckoutFromRemoteError returns an error if there are multiple git remotes but the checkoutFrom remote has not been specified
type MissingProjectCheckoutFromRemoteError struct {
projectName string
}

func (e *MissingProjectCheckoutFromRemoteError) Error() string {
return fmt.Sprintf("project %s has more than one remote defined, but has no checkoutfrom remote defined", e.projectName)
}

//InvalidProjectCheckoutRemoteError returns an error if there is an unmatched, checkoutFrom remote specified
type InvalidProjectCheckoutRemoteError struct {
projectName string
checkoutRemote string
}

func (e *InvalidProjectCheckoutRemoteError) Error() string {
return fmt.Sprintf("unable to find the checkout remote %s in the remotes for project %s", e.checkoutRemote, e.projectName)
}

// resolveErrorMessageWithImportAttributes returns an updated error message
// with detailed information on the imported and overriden resource.
// example:
Expand Down
22 changes: 11 additions & 11 deletions pkg/validation/projects.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package validation

import (
"fmt"
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/hashicorp/go-multierror"
)

// ValidateStarterProjects checks if starter project has only one remote configured
// and if the checkout remote matches the renote configured
// and if the checkout remote matches the remote configured
func ValidateStarterProjects(starterProjects []v1alpha2.StarterProject) (returnedErr error) {

for _, starterProject := range starterProjects {
Expand All @@ -20,8 +19,8 @@ func ValidateStarterProjects(starterProjects []v1alpha2.StarterProject) (returne

switch len(gitSource.Remotes) {
case 0:
starterProjectErr := fmt.Errorf("starterProject %s should have at least one remote", starterProject.Name)
newErr := resolveErrorMessageWithImportAttributes(starterProjectErr, starterProject.Attributes)

newErr := resolveErrorMessageWithImportAttributes(&MissingStarterProjectRemoteError{projectName: starterProject.Name}, starterProject.Attributes)
returnedErr = multierror.Append(returnedErr, newErr)
case 1:
if gitSource.CheckoutFrom != nil && gitSource.CheckoutFrom.Remote != "" {
Expand All @@ -32,8 +31,8 @@ func ValidateStarterProjects(starterProjects []v1alpha2.StarterProject) (returne
}
}
default: // len(gitSource.Remotes) >= 2
starterProjectErr := fmt.Errorf("starterProject %s should have one remote only", starterProject.Name)
newErr := resolveErrorMessageWithImportAttributes(starterProjectErr, starterProject.Attributes)

newErr := resolveErrorMessageWithImportAttributes(&MultipleStarterProjectRemoteError{projectName: starterProject.Name}, starterProject.Attributes)
returnedErr = multierror.Append(returnedErr, newErr)
}
}
Expand All @@ -54,8 +53,8 @@ func ValidateProjects(projects []v1alpha2.Project) (returnedErr error) {
}
switch len(gitSource.Remotes) {
case 0:
projectErr := fmt.Errorf("projects %s should have at least one remote", project.Name)
newErr := resolveErrorMessageWithImportAttributes(projectErr, project.Attributes)

newErr := resolveErrorMessageWithImportAttributes(&MissingProjectRemoteError{projectName: project.Name}, project.Attributes)
returnedErr = multierror.Append(returnedErr, newErr)
case 1:
if gitSource.CheckoutFrom != nil && gitSource.CheckoutFrom.Remote != "" {
Expand All @@ -66,8 +65,8 @@ func ValidateProjects(projects []v1alpha2.Project) (returnedErr error) {
}
default: // len(gitSource.Remotes) >= 2
if gitSource.CheckoutFrom == nil || gitSource.CheckoutFrom.Remote == "" {
projectErr := fmt.Errorf("project %s has more than one remote defined, but has no checkoutfrom remote defined", project.Name)
newErr := resolveErrorMessageWithImportAttributes(projectErr, project.Attributes)

newErr := resolveErrorMessageWithImportAttributes(&MissingProjectCheckoutFromRemoteError{projectName: project.Name}, project.Attributes)
returnedErr = multierror.Append(returnedErr, newErr)
continue
}
Expand All @@ -85,7 +84,8 @@ func ValidateProjects(projects []v1alpha2.Project) (returnedErr error) {
func validateRemoteMap(remotes map[string]string, checkoutRemote, projectName string) error {

if _, ok := remotes[checkoutRemote]; !ok {
return fmt.Errorf("unable to find the checkout remote %s in the remotes for project %s", checkoutRemote, projectName)

return &InvalidProjectCheckoutRemoteError{projectName: projectName, checkoutRemote: checkoutRemote}
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/validation/projects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestValidateStarterProjects(t *testing.T) {
func TestValidateProjects(t *testing.T) {

wrongCheckoutErr := "unable to find the checkout remote .* in the remotes for project.*"
atleastOneRemoteErr := "projects .* should have at least one remote"
atleastOneRemoteErr := "project .* should have at least one remote"
missingCheckOutFromRemoteErr := "project .* has more than one remote defined, but has no checkoutfrom remote defined"

parentOverridesFromMainDevfile := attributes.Attributes{}.PutString(ImportSourceAttribute,
Expand Down

0 comments on commit da620cd

Please sign in to comment.