Repository: replicatedhq/kots
Pull request #85 (2019-10-07 22:45:13)

use the installer endpoint to determine current kots version


Patch:
diff --git a/pkg/version/version.go b/pkg/version/version.go
index 74156fdc9..a2ee77c3b 100644
--- a/pkg/version/version.go
+++ b/pkg/version/version.go
@@ -1,12 +1,12 @@
 package version
 
 import (
-	"context"
+	"io/ioutil"
+	"net/http"
 	"runtime"
 	"time"
 
 	semver "github.com/Masterminds/semver/v3"
-	"github.com/google/go-github/v28/github"
 	"github.com/pkg/errors"
 )
 
@@ -79,27 +79,31 @@ func getGoInfo() GoInfo {
 // IsLatestRelease queries github for the latest release in the project repo. If that release has a semver greater
 // than the current release, it returns false and the new latest release semver. Otherwise, it returns true or error
 func IsLatestRelease() (bool, string, error) {
-	client := github.NewClient(nil)
-	latest, _, err := client.Repositories.GetLatestRelease(context.Background(), "replicatedhq", "kots")
+	return isLatestRelease(http.DefaultClient, "https://kots.io")
+}
+
+func isLatestRelease(client *http.Client, upstream string) (bool, string, error) {
+	resp, err := client.Get(upstream + "/install?version")
 	if err != nil {
-		return false, "", errors.Wrap(err, "find latest release")
+		return false, "", errors.Wrapf(err, "find latest release")
 	}
-	if latest.GetName() == "" {
-		return false, "", errors.New("latest release name was empty")
+	body, err := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		return false, "", errors.Wrapf(err, "read latest release body")
 	}
 
-	latestSemver, err := semver.NewVersion(latest.GetName())
+	currentSemver, err := semver.NewVersion(Version())
 	if err != nil {
-		return false, "", errors.Wrapf(err, "latest release %s does not parse as semver", latest.GetName())
+		return false, "", errors.Wrapf(err, "current release %s does not parse as semver", Version())
 	}
 
-	currentSemver, err := semver.NewVersion(Version())
+	latestSemver, err := semver.NewVersion(string(body))
 	if err != nil {
-		return false, "", errors.Wrapf(err, "current release %s does not parse as semver", latest.GetName())
+		return false, "", errors.Wrapf(err, "latest release %s does not parse as semver", string(body))
 	}
 
 	if currentSemver.LessThan(latestSemver) {
-		return false, latest.GetName(), nil
+		return false, string(body), nil
 	}
 
 	return true, "", nil


Test patch:
diff --git a/pkg/version/version_test.go b/pkg/version/version_test.go
index e36f029c3..7cf50c059 100644
--- a/pkg/version/version_test.go
+++ b/pkg/version/version_test.go
@@ -1,6 +1,8 @@
 package version
 
 import (
+	"net/http"
+	"net/http/httptest"
 	"testing"
 	"time"
 
@@ -141,3 +143,58 @@ func TestGetBuild(t *testing.T) {
 		})
 	}
 }
+
+func TestIsLatestRelease(t *testing.T) {
+	tests := []struct {
+		name            string
+		version         string
+		upstreamVersion string
+		isLatest        bool
+		latest          string
+		wantErr         bool
+	}{
+		{
+			name:            "not the latest",
+			version:         "v0.9.1",
+			upstreamVersion: "v0.10.0",
+			isLatest:        false,
+			latest:          "v0.10.0",
+		},
+		{
+			name:            "local version",
+			version:         "v0.10.0-dirty",
+			upstreamVersion: "v0.10.0",
+			isLatest:        false,
+			latest:          "v0.10.0",
+		},
+		{
+			name:            "actually the latest version",
+			version:         "v0.10.1",
+			upstreamVersion: "v0.10.1",
+			isLatest:        true,
+			latest:          "",
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			req := require.New(t)
+
+			h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+				w.Write([]byte(tt.upstreamVersion))
+			})
+			server := httptest.NewTLSServer(h)
+			defer server.Close()
+			client := server.Client()
+			upstreamURL := server.URL
+
+			version = tt.version
+			initBuild()
+
+			isLatest, latest, err := isLatestRelease(client, upstreamURL)
+			req.NoError(err)
+
+			req.Equal(tt.isLatest, isLatest)
+			req.Equal(tt.latest, latest)
+		})
+	}
+}