Skip to content

Commit

Permalink
Allow package versions for alpha and beta without a minor version (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
bufdev authored Aug 13, 2021
1 parent 503f631 commit 13bcf0e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
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

0 comments on commit 13bcf0e

Please sign in to comment.