-
Notifications
You must be signed in to change notification settings - Fork 14
loosen version verification #3750
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ package buildplanner | |
import ( | ||
"encoding/json" | ||
"errors" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
@@ -172,25 +171,16 @@ func processBuildPlannerError(bpErr error, fallbackMessage string) error { | |
return &response.BuildPlannerError{Err: locale.NewExternalError("err_buildplanner", "{{.V0}}: Encountered unexpected error: {{.V1}}", fallbackMessage, bpErr.Error())} | ||
} | ||
|
||
var versionRe = regexp.MustCompile(`^\d+(\.\d+)*$`) | ||
|
||
func isExactVersion(version string) bool { | ||
return versionRe.MatchString(version) | ||
func isRangeVersion(version string) bool { | ||
return strings.Contains(version, "=") || strings.Contains(version, "<") || strings.Contains(version, ">") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This doesn't indicate a range version does it? I suppose you could have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no, these three characters are the minimum sufficient to determine if any of the universal version range operators are being used. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry I don't think I'm following, let me ask a different way; Wouldn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That one is a bit different, true, but IMO it does and should return So the practical reason to send even |
||
} | ||
|
||
func isWildcardVersion(version string) bool { | ||
return strings.Contains(version, ".x") || strings.Contains(version, ".X") | ||
} | ||
|
||
func VersionStringToRequirements(version string) ([]types.VersionRequirement, error) { | ||
if isExactVersion(version) { | ||
return []types.VersionRequirement{{ | ||
types.VersionRequirementComparatorKey: "eq", | ||
types.VersionRequirementVersionKey: version, | ||
}}, nil | ||
} | ||
|
||
if !isWildcardVersion(version) { | ||
if isRangeVersion(version) { | ||
// Ask the Platform to translate a string like ">=1.2,<1.3" into a list of requirements. | ||
// Note that: | ||
// - The given requirement name does not matter; it is not looked up. | ||
|
@@ -210,33 +200,41 @@ func VersionStringToRequirements(version string) ([]types.VersionRequirement, er | |
return requirements, nil | ||
} | ||
|
||
// Construct version constraints to be >= given version, and < given version's last part + 1. | ||
// For example, given a version number of 3.10.x, constraints should be >= 3.10, < 3.11. | ||
// Given 2.x, constraints should be >= 2, < 3. | ||
requirements := []types.VersionRequirement{} | ||
parts := strings.Split(version, ".") | ||
for i, part := range parts { | ||
if part != "x" && part != "X" { | ||
continue | ||
} | ||
if i == 0 { | ||
return nil, locale.NewInputError("err_version_wildcard_start", "A version number cannot start with a wildcard") | ||
} | ||
requirements = append(requirements, types.VersionRequirement{ | ||
types.VersionRequirementComparatorKey: types.ComparatorGTE, | ||
types.VersionRequirementVersionKey: strings.Join(parts[:i], "."), | ||
}) | ||
previousPart, err := strconv.Atoi(parts[i-1]) | ||
if err != nil { | ||
return nil, locale.WrapInputError(err, "err_version_number_expected", "Version parts are expected to be numeric") | ||
if isWildcardVersion(version) { | ||
// Construct version constraints to be >= given version, and < given version's last part + 1. | ||
// For example, given a version number of 3.10.x, constraints should be >= 3.10, < 3.11. | ||
// Given 2.x, constraints should be >= 2, < 3. | ||
requirements := []types.VersionRequirement{} | ||
parts := strings.Split(version, ".") | ||
for i, part := range parts { | ||
if part != "x" && part != "X" { | ||
continue | ||
} | ||
if i == 0 { | ||
return nil, locale.NewInputError("err_version_wildcard_start", "A version number cannot start with a wildcard") | ||
} | ||
requirements = append(requirements, types.VersionRequirement{ | ||
types.VersionRequirementComparatorKey: types.ComparatorGTE, | ||
types.VersionRequirementVersionKey: strings.Join(parts[:i], "."), | ||
}) | ||
previousPart, err := strconv.Atoi(parts[i-1]) | ||
if err != nil { | ||
return nil, locale.WrapInputError(err, "err_version_number_expected", "Version parts are expected to be numeric") | ||
} | ||
parts[i-1] = strconv.Itoa(previousPart + 1) | ||
requirements = append(requirements, types.VersionRequirement{ | ||
types.VersionRequirementComparatorKey: types.ComparatorLT, | ||
types.VersionRequirementVersionKey: strings.Join(parts[:i], "."), | ||
}) | ||
} | ||
parts[i-1] = strconv.Itoa(previousPart + 1) | ||
requirements = append(requirements, types.VersionRequirement{ | ||
types.VersionRequirementComparatorKey: types.ComparatorLT, | ||
types.VersionRequirementVersionKey: strings.Join(parts[:i], "."), | ||
}) | ||
return requirements, nil | ||
} | ||
return requirements, nil | ||
|
||
return []types.VersionRequirement{{ | ||
types.VersionRequirementComparatorKey: "eq", | ||
types.VersionRequirementVersionKey: version, | ||
}}, nil | ||
|
||
} | ||
|
||
// pollBuildPlanned polls the buildplan until it has passed the planning stage (ie. it's either planned or further along). | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The regex does seem like it was overkill here, but reading this code I think we still want to check if version is non-empty? In most cases we run
state install
without a specific version, in which case this it doesn't seem like we should run this logic.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, I ran a single test of
state install namespace:name
(no dynamic, no version) and that worked. But I would be okay to check for the empty string.