Skip to content

Commit

Permalink
[release-14.0] Small fixes to the auto-upgrade golang tool (#12838) (#…
Browse files Browse the repository at this point in the history
…12846)

* fix float issue with bootstrap version increment

Signed-off-by: Florent Poinsard <florent.poinsard@outlook.fr>

* handle case where there are no minor bootstrap version

Signed-off-by: Florent Poinsard <florent.poinsard@outlook.fr>

* avoid re-creating Pull Requests

Signed-off-by: Florent Poinsard <florent.poinsard@outlook.fr>

* remove parse float

Signed-off-by: Florent Poinsard <florent.poinsard@outlook.fr>

---------

Signed-off-by: Florent Poinsard <florent.poinsard@outlook.fr>
Co-authored-by: Florent Poinsard <florent.poinsard@outlook.fr>
  • Loading branch information
vitess-bot[bot] and frouioui authored Apr 12, 2023
1 parent bd71623 commit 7b87680
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 22 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/update_golang_version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,20 @@ jobs:
if [ -z "${output}" ]; then
exit 0
fi
echo "create-pr=true" >> $GITHUB_OUTPUT
go_version=$(go run ./go/tools/go-upgrade/go-upgrade.go get go-version)
bootstrap_version=$(go run ./go/tools/go-upgrade/go-upgrade.go get bootstrap-version)
echo "go-version=${go_version}" >> $GITHUB_OUTPUT
echo "bootstrap-version=${bootstrap_version}" >> $GITHUB_OUTPUT
# Check if the PR already exists, if it does then do not create new PR.
gh pr list -S "is:open [${{ matrix.branch }}] Upgrade the Golang version to go${go_version}" | grep "OPEN"
if [ $? -eq 0 ]; then
exit 0
fi
echo "create-pr=true" >> $GITHUB_OUTPUT
- name: Create Pull Request
if: steps.detect-and-update.outputs.create-pr == 'true'
uses: peter-evans/create-pull-request@v4
Expand Down
67 changes: 47 additions & 20 deletions go/tools/go-upgrade/go-upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,16 @@ const (
goDevAPI = "https://go.dev/dl/?mode=json"
)

type latestGolangRelease struct {
Version string `json:"version"`
Stable bool `json:"stable"`
}
type (
latestGolangRelease struct {
Version string `json:"version"`
Stable bool `json:"stable"`
}

bootstrapVersion struct {
major, minor int // when minor == -1, it means there are no minor version
}
)

var (
workflowUpdate = true
Expand Down Expand Up @@ -151,7 +157,7 @@ func runGetBootstrapCmd(_ *cobra.Command, _ []string) {
if err != nil {
log.Fatal(err)
}
fmt.Println(currentVersion)
fmt.Println(currentVersion.toString())
}

func runUpgradeWorkflowsCmd(_ *cobra.Command, _ []string) {
Expand Down Expand Up @@ -218,11 +224,11 @@ func upgradePath(allowMajorUpgrade, workflowUpdate, isMainBranch bool) error {
}
nextBootstrapVersionF := currentBootstrapVersionF
if isMainBranch {
nextBootstrapVersionF += 1
nextBootstrapVersionF.major += 1
} else {
nextBootstrapVersionF += 0.1
nextBootstrapVersionF.minor += 1
}
err = updateBootstrapVersionInCodebase(currentBootstrapVersionF, nextBootstrapVersionF, upgradeTo)
err = updateBootstrapVersionInCodebase(currentBootstrapVersionF.toString(), nextBootstrapVersionF.toString(), upgradeTo)
if err != nil {
return err
}
Expand Down Expand Up @@ -251,23 +257,37 @@ func currentGolangVersion() (*version.Version, error) {
return version.NewVersion(versionStr[1])
}

func currentBootstrapVersion() (float64, error) {
func currentBootstrapVersion() (bootstrapVersion, error) {
contentRaw, err := os.ReadFile("Makefile")
if err != nil {
return 0, err
return bootstrapVersion{}, err
}
content := string(contentRaw)

versre := regexp.MustCompile("(?i).*BOOTSTRAP_VERSION[[:space:]]*=[[:space:]]*([0-9.]+).*")
versionStr := versre.FindStringSubmatch(content)
if len(versionStr) != 2 {
return 0, fmt.Errorf("malformatted error, got: %v", versionStr)
return bootstrapVersion{}, fmt.Errorf("malformatted error, got: %v", versionStr)
}
f, err := strconv.ParseFloat(versionStr[1], 64)

vs := strings.Split(versionStr[1], ".")
major, err := strconv.Atoi(vs[0])
if err != nil {
return 0, err
return bootstrapVersion{}, err
}

minor := -1
if len(vs) > 1 {
minor, err = strconv.Atoi(vs[1])
if err != nil {
return bootstrapVersion{}, err
}
}
return f, nil

return bootstrapVersion{
major: major,
minor: minor,
}, nil
}

// getLatestStableGolangReleases fetches the latest stable releases of Golang from
Expand Down Expand Up @@ -371,7 +391,7 @@ func replaceGoVersionInCodebase(old, new *version.Version, workflowUpdate bool)
return nil
}

func updateBootstrapVersionInCodebase(old, new float64, newGoVersion *version.Version) error {
func updateBootstrapVersionInCodebase(old, new string, newGoVersion *version.Version) error {
if old == new {
return nil
}
Expand All @@ -394,8 +414,8 @@ func updateBootstrapVersionInCodebase(old, new float64, newGoVersion *version.Ve
regexp.MustCompile(`BOOTSTRAP_VERSION[[:space:]]*=[[:space:]]*[0-9.]+`), // Makefile
},
[]string{
fmt.Sprintf("ARG bootstrap_version=%-1g", new), // Dockerfile
fmt.Sprintf("BOOTSTRAP_VERSION=%-1g", new), // Makefile
fmt.Sprintf("ARG bootstrap_version=%s", new), // Dockerfile
fmt.Sprintf("BOOTSTRAP_VERSION=%s", new), // Makefile
},
file,
)
Expand All @@ -406,7 +426,7 @@ func updateBootstrapVersionInCodebase(old, new float64, newGoVersion *version.Ve

err = replaceInFile(
[]*regexp.Regexp{regexp.MustCompile(`\"bootstrap-version\",[[:space:]]*\"([0-9.]+)\"`)},
[]string{fmt.Sprintf("\"bootstrap-version\", \"%-1g\"", new)},
[]string{fmt.Sprintf("\"bootstrap-version\", \"%s\"", new)},
"./test.go",
)
if err != nil {
Expand All @@ -421,7 +441,7 @@ func updateBootstrapVersionInCodebase(old, new float64, newGoVersion *version.Ve
return nil
}

func updateBootstrapChangelog(new float64, goVersion *version.Version) error {
func updateBootstrapChangelog(new string, goVersion *version.Version) error {
file, err := os.OpenFile("./docker/bootstrap/CHANGELOG.md", os.O_RDWR, 0600)
if err != nil {
return err
Expand All @@ -434,7 +454,7 @@ func updateBootstrapChangelog(new float64, goVersion *version.Version) error {
}
newContent := fmt.Sprintf(`
## [%-1g] - %s
## [%s] - %s
### Changes
- Update build to golang %s`, new, time.Now().Format("2006-01-02"), goVersion.String())

Expand Down Expand Up @@ -502,3 +522,10 @@ func replaceInFile(oldexps []*regexp.Regexp, new []string, fileToChange string)
}
return nil
}

func (b bootstrapVersion) toString() string {
if b.minor == -1 {
return fmt.Sprintf("%d", b.major)
}
return fmt.Sprintf("%d.%d", b.major, b.minor)
}

0 comments on commit 7b87680

Please sign in to comment.