Skip to content

Commit

Permalink
add regex for go1.21 versioning change
Browse files Browse the repository at this point in the history
as part of the go1.21 versioning convention change, go versions <= 1.20
and >=1.21.0 need to be handled separately.

All go versions >= 1.21.0, should have a patch version if it is not a
prerelease.

Signed-off-by: Akhil Mohan <akhilerm@gmail.com>
  • Loading branch information
akhilerm committed Jul 21, 2023
1 parent 9eca09d commit bb380ce
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
59 changes: 57 additions & 2 deletions cmd/publishing-bot/config/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/url"
"os"
"regexp"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -49,6 +50,11 @@ func (c Source) String() string {
type BranchRule struct {
Name string `yaml:"name"`
// a valid go version string like 1.10.2 or 1.10
//
// From go 1.21 onwards there is a change in the versioning format.
// The version displayed by `go version` should be used here:
// 1. 1.21.0 is valid and 1.21 is invalid
// 2. 1.21rc1 and 1.21.0rc1 are valid
GoVersion string `yaml:"go,omitempty"`
// k8s.io/* repos the branch rule depends on
Dependencies []Dependency `yaml:"dependencies,omitempty"`
Expand Down Expand Up @@ -172,12 +178,61 @@ func validateGoVersions(rules *RepositoryRules) (errs []error) {
// go versions don't follow semver. Examples:
// 1. 1.15.0 is invalid, 1.15 is valid
// 2. 1.15.0-rc.1 is invalid, 1.15rc1 is valid
var goVerRegex = regexp.MustCompile(`^([0-9]+)\.([0-9]\w*)(\.[1-9]\d*\w*)*$`)
//
// From go 1.21 onwards there is a change in the versioning format
// Ref: https://tip.golang.org/doc/toolchain#versions
//
// The version displayed by `go version` is what we care about and use in the config.
// This is the version in the *name of the go tool chain* (of the form goV, V is what we
// care about). For Go *language versions* >= 1.21, the following are the rules for versions
// in the go tool chain name:
// 1. 1.21 is invalid, and 1.21.0 is valid
// 2. 1.21rc1 and 1.21.0rc1 are valid
var goVerRegex = regexp.MustCompile(`^(?P<major>\d+)\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?(?:(?P<pre>alpha|beta|rc)\d+)?$`)

func ensureValidGoVersion(version string) error {
if !goVerRegex.MatchString(version) {
match := goVerRegex.FindStringSubmatch(version)
if len(match) == 0 {
return fmt.Errorf("specified go version %s is invalid", version)
}

var majorVersion, minorVersion, patchVersion int
var preRelease string
patchVersionExists := false

majorVersion, err := strconv.Atoi(match[1])
if err != nil {
return fmt.Errorf("error parsing major version '%s' : %s", match[1], err)
}
minorVersion, err = strconv.Atoi(match[2])
if err != nil {
return fmt.Errorf("error parsing minor version '%s' : %s", match[2], err)
}
if match[3] != "" {
patchVersion, err = strconv.Atoi(match[3])
if err != nil {
return fmt.Errorf("error parsing patch version '%s' : %s", match[3], err)
}
patchVersionExists = true
}
preRelease = match[4]

// for go versions <= 1.20, patch version .0 should not exist
if majorVersion <= 1 && minorVersion <= 20 {
if patchVersionExists && patchVersion == 0 {
languageVersion := fmt.Sprintf("%d.%d", majorVersion, minorVersion)
return fmt.Errorf("go language version %s below 1.21; should not have a 0th patch release, got %s", languageVersion, version)
}
}

// for go versions >= 1.21.0, patch versions should exist. If there is no patch version,
// then it should be a prerelease
if (majorVersion == 1 && minorVersion >= 21) || majorVersion >= 2 {
if !patchVersionExists && preRelease == "" {
return fmt.Errorf("patch version should always be present for go language version >= 1.21")
}
}

return nil
}

Expand Down
16 changes: 15 additions & 1 deletion cmd/publishing-bot/config/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,27 @@ func TestValidateGoVersion(t *testing.T) {
{"1.15.10", true},
{"1.15.11", true},
{"1.15.20", true},
{"2.0", true},
{"2.0", false},
{"2.0alpha1", true},
{"2.0-alpha1", false},
{"12.12.100", true},
{"999.999.9999", true},
{"999.999.9999-beta.1", false},
{"999.999.9999beta1", true},
{"1.21.0", true},
{"1.21", false},
{"1.21rc1", true},
{"1.21.0rc1", true},
{"1.20.0", false},
{"1.20rc1", true},
{"1.22.0", true},
{"1.22rc1", true},
{"1.122.0", true},
{"1.122", false},
{"1.30.0", true},
{"1.20rc.20rc2", false},
{"2.20", false},
{"2.21", false},
}

for _, test := range tests {
Expand Down

0 comments on commit bb380ce

Please sign in to comment.