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

Allow package versions for alpha and beta without a minor version #422

Merged
merged 1 commit into from
Aug 13, 2021
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
46 changes: 24 additions & 22 deletions internal/pkg/protoversion/package_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,38 +64,40 @@ func newPackageVersionForPackage(pkg string) (*packageVersion, bool) {
return newPackageVersion(major, StabilityLevelTest, 0, 0, split[1]), true
}

if strings.Contains(version, "alpha") {
var stabilityLevel StabilityLevel
containsAlpha := strings.Contains(version, "alpha")
containsBeta := strings.Contains(version, "beta")
switch {
case !containsAlpha && !containsBeta:
stabilityLevel = StabilityLevelStable
case containsAlpha && !containsBeta:
stabilityLevel = StabilityLevelAlpha
case !containsAlpha && containsBeta:
stabilityLevel = StabilityLevelBeta
case containsAlpha && containsBeta:
return nil, false
}
if stabilityLevel != StabilityLevelStable {
// 1alpha1 -> [1, 1]
// 1p1alpha1 ->[1p1, 1]
split := strings.SplitN(version, "alpha", 2)
if len(split) != 2 {
return nil, false
}
minor, ok := positiveNumber(split[1])
if !ok {
return nil, false
}
major, patch, ok := getAlphaBetaMajorPatch(split[0])
if !ok {
return nil, false
}
return newPackageVersion(major, StabilityLevelAlpha, minor, patch, ""), true
}

if strings.Contains(version, "beta") {
split := strings.SplitN(version, "beta", 2)
// 1alpha -> [1, ""]
split := strings.SplitN(version, stabilityLevel.String(), 2)
if len(split) != 2 {
return nil, false
}
minor, ok := positiveNumber(split[1])
if !ok {
return nil, false
minor := 0
var ok bool
if split[1] != "" {
minor, ok = positiveNumber(split[1])
if !ok {
return nil, false
}
}
major, patch, ok := getAlphaBetaMajorPatch(split[0])
if !ok {
return nil, false
}
return newPackageVersion(major, StabilityLevelBeta, minor, patch, ""), true
return newPackageVersion(major, stabilityLevel, minor, patch, ""), true
}

// no suffix that is valid, make sure we just have a number
Expand Down
5 changes: 4 additions & 1 deletion internal/pkg/protoversion/protoversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ func (s StabilityLevel) String() string {
// PackageVersion is a package version.
//
// A package has a version if the last component is a version of the form
// v\d+, v\d+test.*, v\d+(alpha|beta)\d+, or v\d+p\d+(alpha|beta)\d+
// v\d+, v\d+test.*, v\d+(alpha|beta)\d*, or v\d+p\d+(alpha|beta)\d*
// where numbers are >=1.
//
// See https://cloud.google.com/apis/design/versioning#channel-based_versioning
// See https://cloud.google.com/apis/design/versioning#release-based_versioning
type PackageVersion interface {
fmt.Stringer

Expand Down
4 changes: 4 additions & 0 deletions internal/pkg/protoversion/protoversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func TestPackageHasPackageVersion(t *testing.T) {
testNewPackageVersionForPackage(t, newPackageVersion(1, StabilityLevelTest, 0, 0, "foo"), true, "foo.bar.v1testfoo")
testNewPackageVersionForPackage(t, newPackageVersion(4, StabilityLevelTest, 0, 0, ""), true, "foo.bar.v4test")
testNewPackageVersionForPackage(t, newPackageVersion(4, StabilityLevelTest, 0, 0, "foo"), true, "foo.bar.v4testfoo")
testNewPackageVersionForPackage(t, newPackageVersion(1, StabilityLevelAlpha, 0, 0, ""), true, "foo.bar.v1alpha")
testNewPackageVersionForPackage(t, newPackageVersion(1, StabilityLevelAlpha, 0, 1, ""), true, "foo.bar.v1p1alpha")
testNewPackageVersionForPackage(t, newPackageVersion(1, StabilityLevelBeta, 0, 0, ""), true, "foo.bar.v1beta")
testNewPackageVersionForPackage(t, newPackageVersion(1, StabilityLevelBeta, 0, 1, ""), true, "foo.bar.v1p1beta")
testNewPackageVersionForPackage(t, nil, false, "foo.v0")
testNewPackageVersionForPackage(t, nil, false, "foo.bar.v0")
testNewPackageVersionForPackage(t, nil, false, "foo.bar.v0alpha1")
Expand Down