Skip to content

Find new versions by parsing json #249

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

Merged
merged 4 commits into from
Mar 7, 2025
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
builder-bumper
builder-bumper/builder-bumper
/docker-images
97 changes: 70 additions & 27 deletions cmd/builder-bumper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package main

import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io"
Expand Down Expand Up @@ -46,6 +47,10 @@ type goVersion struct {
minor int
}

type VersionInfo struct {
Version string `json:"version"`
}

func newGoVersion(v string) *goVersion {
c := semver.Canonical("v" + v)
if c == "" {
Expand Down Expand Up @@ -101,6 +106,47 @@ func (g *goVersion) url() string {
return fmt.Sprintf("https://dl.google.com/go/go%s.linux-amd64.tar.gz", g.golangVersion())
}

func fetchJSON(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

return body, nil
}

func availableVersions() []goVersion {
const url = "https://go.dev/dl/?mode=json"

jsonData, err := fetchJSON(url)
if err != nil {
fmt.Println("Error fetching JSON:", err)
}

var availableVersionsJSON []VersionInfo
var availableVersions []goVersion
if err := json.Unmarshal(jsonData, &availableVersionsJSON); err != nil {
fmt.Println("Error parsing JSON:", err)
}

for i := range availableVersionsJSON {
// remove "go" from a string like "go1.22.0"
newGoVersion := newGoVersion(strings.TrimLeft(availableVersionsJSON[i].Version, "go"))
availableVersions = append(availableVersions, *newGoVersion)
}
return availableVersions
}

// getSHA256 returns the SHA256 of the Go archive.
func (g *goVersion) getSHA256() (string, error) {
resp, err := http.Get(g.url() + ".sha256")
Expand All @@ -116,40 +162,36 @@ func (g *goVersion) getSHA256() (string, error) {
}

// getLastMinorVersion returns the last minor version for a given Go version.
func (g *goVersion) getLastMinorVersion() (*goVersion, error) {
last := *g
for {
next := last
next.minor++
resp, err := http.Head(next.url())
if err != nil {
return nil, err
// if no new minor version available, it will return the given Go version back.
func (g *goVersion) getLastMinorVersion(availableVersions []goVersion) *goVersion {
sort.Slice(availableVersions, func(i, j int) bool {
if availableVersions[i].major == availableVersions[j].major {
return availableVersions[i].minor < availableVersions[j].minor
}
defer resp.Body.Close()
return availableVersions[i].major < availableVersions[j].major
})

if resp.StatusCode/100 != 2 {
return &last, nil
for _, availableVersion := range availableVersions {
if availableVersion.major == g.major && availableVersion.minor > g.minor {
return &availableVersion
}
last = next
}

return g
}

// getNextMajor returns the next Go major version for a given Go version.
// It returns nil if the current version is already the latest.
func (g *goVersion) getNextMajor() *goVersion {
func (g *goVersion) getNextMajor(availableVersions []goVersion) *goVersion {
version := newGoVersion(g.Major() + ".0")
version.major++

resp, err := http.Head(version.url())
if err != nil {
return nil
}
defer resp.Body.Close()
if resp.StatusCode/100 != 2 {
return nil
for _, availableVersion := range availableVersions {
if version.major == availableVersion.major {
return version
}
}

return version
return nil
}

// getExactVersionFromDir reads the current Go version from a directory.
Expand Down Expand Up @@ -301,10 +343,8 @@ func updateNextMinor(dir string) (*goVersion, error) {
return nil, fmt.Errorf("failed to detect current version of %s: %w", dir, err)
}

next, err := current.getLastMinorVersion()
if err != nil {
return nil, err
}
next := current.getLastMinorVersion(availableVersions())

if next.equal(current) {
log.Printf("no version change for Go %s", next.golangVersion())
return nil, nil
Expand Down Expand Up @@ -374,9 +414,12 @@ func run() error {
return fmt.Errorf("Expected 2 versions of Go but got %d\n", len(dirs))
}

// Get list of available versions
availableVersions := availableVersions()

// Check if a new major Go version exists.
nexts := make([]*goVersion, 0)
if next := newGoVersion(dirs[1] + ".0").getNextMajor(); next != nil {
if next := newGoVersion(dirs[1] + ".0").getNextMajor(availableVersions); next != nil {
log.Printf("found a new major version of Go: %s", next)
old, err := getExactVersionFromDir(dirs[0])
if err != nil {
Expand Down
46 changes: 46 additions & 0 deletions cmd/builder-bumper/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"reflect"
"strings"
"testing"
)

func TestNewGoVersion(t *testing.T) {
tests := []struct {
input string
want *goVersion
}{
{"1.22.1", &goVersion{22, 1}},
{"1.20.5", &goVersion{20, 5}},
{"1.19.0", &goVersion{19, 0}},
{"1.25.3", &goVersion{25, 3}},
{"1.18.10", &goVersion{18, 10}},
}

for _, tt := range tests {
t.Run("Parsing "+tt.input, func(t *testing.T) {
got := newGoVersion(tt.input)

if !reflect.DeepEqual(got, tt.want) {
t.Errorf("newGoVersion(%q) = %v, want %v", tt.input, got, tt.want)
}
})
}
}

func TestAvailableVersions(t *testing.T) {
t.Run("Test that available versions scan correctly removes 'go' prefix", func(t *testing.T) {
got := availableVersions()

if len(got) == 0 {
t.Fatalf("Expected some versions, got none")
}

for _, v := range got {
if strings.Contains(v.String(), "go") {
t.Errorf("Version still contains 'go' prefix: %v", v)
}
}
})
}