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

project & starterproject validation bug #341

Merged
merged 4 commits into from
Feb 11, 2021

Conversation

yangcao77
Copy link
Contributor

What does this PR do?

This PR fixes the bug in project & starterproject validation. detailed description in #339

What issues does this PR fix or reference?

#339

Is your PR tested? Consider putting some instruction how to test your changes

Added more unit test cases to increase invalid project/starterproject test coverage

Docs PR

@yangcao77 yangcao77 changed the title project validation bug project & starterproject validation bug Feb 10, 2021
Signed-off-by: Stephanie <[email protected]>
Comment on lines 61 to 62
} else if len(gitSource.Remotes) > 1 || (gitSource.CheckoutFrom != nil && gitSource.CheckoutFrom.Remote != "") {
if len(gitSource.Remotes) > 1 && (gitSource.CheckoutFrom == nil || (gitSource.CheckoutFrom != nil && gitSource.CheckoutFrom.Remote == "")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This conditional sequence is kind of a nightmare, something like if A || (B && C) then: if A && (!B || (B && !C)) then error, and should be simplified. Maybe something like

switch len(gitSource.Remotes) {
case 0:
  errString += fmt.Sprintf("\nprojects %s should have at least one remote", project.Name)
case 1:
  if gitSource.CheckoutFrom != nil {
    if err := validateRemoteMap(gitSource.Remotes, gitSource.CheckoutFrom.Remote, project.Name); err != nil {
      errString += fmt.Sprintf("\n%s", err.Error())
    }
  }
default: // len(gitSource.Remotes) >= 2
  if gitSource.CheckoutFrom == nil || gitSource.CheckoutFrom.Remote == "" {
    errString += fmt.Sprintf("\nproject %s has more than one remote defined, but has no checkoutfrom remote defined", project.Name)
    continue
  }
  if err := validateRemoteMap(gitSource.Remotes, gitSource.CheckoutFrom.Remote, project.Name); err != nil {
    errString += fmt.Sprintf("\n%s", err.Error())
  }
}

Maybe some of this logic can be moved into validateRemoteMap? That function seems to simple to be broken out since it's just a map access.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion. Using a case switch does make the code more readable.
I have also updated the starterProject validation to use case switch as well to be consistent.

Copy link
Member

@maysunfaisal maysunfaisal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generally looks good, just a small suggestion

Comment on lines 61 to 65
} else if len(gitSource.Remotes) > 1 || (gitSource.CheckoutFrom != nil && gitSource.CheckoutFrom.Remote != "") {
if len(gitSource.Remotes) > 1 && (gitSource.CheckoutFrom == nil || (gitSource.CheckoutFrom != nil && gitSource.CheckoutFrom.Remote == "")) {
errString += fmt.Sprintf("\nproject %s has more than one remote defined, but has no checkoutfrom remote defined", project.Name)
continue
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else if len(gitSource.Remotes) > 1 || (gitSource.CheckoutFrom != nil && gitSource.CheckoutFrom.Remote != "") {
if len(gitSource.Remotes) > 1 && (gitSource.CheckoutFrom == nil || (gitSource.CheckoutFrom != nil && gitSource.CheckoutFrom.Remote == "")) {
errString += fmt.Sprintf("\nproject %s has more than one remote defined, but has no checkoutfrom remote defined", project.Name)
continue
}
} else if len(gitSource.Remotes) > 1 && gitSource.CheckoutFrom != nil {
err := validateRemoteMap(gitSource.Remotes, gitSource.CheckoutFrom.Remote, project.Name)
if err != nil {
errString += fmt.Sprintf("\n%s", err.Error())
}
} else if len(gitSource.Remotes) > 1 && gitSource.CheckoutFrom == nil {
errString += fmt.Sprintf("\nproject %s has more than one remote defined, but has no checkout from remote defined", project.Name)
}

I would suggest breaking it to different else if conditions. My suggestion handles all the test cases you wrote but if we want to explicitly handle"" checkoutfrom then we can do it the way you suggested,

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this suggestion doesn't cover

git:
  remotes: ["my-remote"]
  checkoutFrom:
    remote: "another-remote"

but I didn't read too closely.

👍 on splitting this logic up though, it's very complicated.

@yangcao77 yangcao77 force-pushed the 339-project-validation-bug branch from 49b0b1e to e87b34e Compare February 10, 2021 21:16
Copy link
Contributor

@amisevsk amisevsk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@openshift-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: amisevsk, yangcao77
To complete the pull request process, please assign after the PR has been reviewed.
You can assign the PR to them by writing /assign in a comment when ready.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@yangcao77 yangcao77 merged commit 33a78ae into devfile:master Feb 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants