Skip to content

Commit e0ddfcf

Browse files
authored
fix(cli): Fix cli version checking to work with monorepo (#1510)
This updates the CLI to use https://versions.cloudquery.io/v1/cli.json when checking for newer versions, rather than Github releases (which now uses a different tag format).
1 parent eafea83 commit e0ddfcf

File tree

5 files changed

+55
-15
lines changed

5 files changed

+55
-15
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ func NewClient() *Client {
4343
}
4444
}
4545

46+
// GetLatestCLIRelease returns the latest release version string for CloudQuery CLI
47+
func (c *Client) GetLatestCLIRelease(ctx context.Context) (string, error) {
48+
return c.readCLIManifest(ctx)
49+
}
50+
4651
// GetLatestProviderRelease returns the latest release version string for the given organization, plugin type
4752
// and plugin.
4853
func (c *Client) GetLatestProviderRelease(ctx context.Context, org, pluginType, pluginName string) (string, error) {
@@ -52,6 +57,20 @@ func (c *Client) GetLatestProviderRelease(ctx context.Context, org, pluginType,
5257
return c.readGithubLatest(ctx, org, pluginType, pluginName)
5358
}
5459

60+
func (c *Client) readCLIManifest(ctx context.Context) (string, error) {
61+
url := fmt.Sprintf(c.cloudQueryBaseURL + "/v1/cli.json")
62+
b, err := c.doRequest(ctx, url)
63+
if err != nil {
64+
return "", fmt.Errorf("reading manifest for cli: %w", err)
65+
}
66+
mr := &manifestResponse{}
67+
err = json.Unmarshal(b, mr)
68+
if err != nil {
69+
return "", fmt.Errorf("unmarshaling manifest response: %w", err)
70+
}
71+
return extractVersionFromTag(mr.Latest), nil
72+
}
73+
5574
func (c *Client) readManifest(ctx context.Context, providerName string) (string, error) {
5675
url := fmt.Sprintf(c.cloudQueryBaseURL+"/v1/%s-%s.json", "source", providerName)
5776
b, err := c.doRequest(ctx, url)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,26 @@ func TestClient_GetLatestProviderRelease(t *testing.T) {
4848
t.Errorf("got community provider version = %q, want %q", version, "v4.5.6")
4949
}
5050
}
51+
52+
func TestClient_GetLatestCLIRelease(t *testing.T) {
53+
cloudQueryServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
54+
if r.URL.Path != "/v1/cli.json" {
55+
w.WriteHeader(http.StatusNotFound)
56+
return
57+
}
58+
fmt.Fprintf(w, `{"latest":"cli/v1.2.3"}`)
59+
}))
60+
defer cloudQueryServer.Close()
61+
62+
c := NewClient()
63+
c.cloudQueryBaseURL = cloudQueryServer.URL
64+
65+
ctx := context.Background()
66+
version, err := c.GetLatestCLIRelease(ctx)
67+
if err != nil {
68+
t.Fatalf("error calling GetLatestCLIRelease: %v", err)
69+
}
70+
if version != "v1.2.3" {
71+
t.Errorf("got cloudquery cli version = %q, want %q", version, "v1.2.3")
72+
}
73+
}

cli/pkg/core/provider_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ resources:
3030
`
3131

3232
func Test_CheckAvailableUpdates(t *testing.T) {
33-
latestVersion := getLatestVersion(t, "test")
33+
latestVersion := mockGetLatestVersion(t, "test")
3434

3535
testCases := []struct {
3636
Name string
@@ -133,7 +133,7 @@ func Test_GetProviderConfig(t *testing.T) {
133133
assert.NoError(t, err)
134134
}
135135

136-
func getLatestVersion(t *testing.T, name string) string {
136+
func mockGetLatestVersion(t *testing.T, name string) string {
137137
reg := registry.NewRegistryHub(firebase.CloudQueryRegistryURL, registry.WithPluginDirectory(t.TempDir()))
138138
latest, diags := CheckAvailableUpdates(context.Background(), reg, &CheckUpdatesOptions{Providers: []registry.Provider{
139139
{Name: name, Version: "v0.0.0", Source: registry.DefaultOrganization},

cli/pkg/core/version.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"time"
1010

1111
"github.com/cloudquery/cloudquery/cli/internal/persistentdata"
12-
"github.com/google/go-github/v35/github"
12+
"github.com/cloudquery/cloudquery/cli/internal/versions"
1313
"github.com/hashicorp/go-version"
1414
"github.com/spf13/afero"
1515
)
@@ -34,12 +34,11 @@ var (
3434
)
3535

3636
// unit tests helper
37-
var getLatestRelease = doGetLatestRelease
37+
var getLatestVersion = doGetLatestVersion
3838

39-
func doGetLatestRelease(ctx context.Context, client *http.Client, owner, repo string) (*github.RepositoryRelease, error) {
40-
gh := github.NewClient(client)
41-
r, _, err := gh.Repositories.GetLatestRelease(ctx, owner, repo)
42-
return r, err
39+
func doGetLatestVersion(ctx context.Context, client *http.Client, owner, repo string) (string, error) {
40+
c := versions.NewClient()
41+
return c.GetLatestCLIRelease(ctx)
4342
}
4443

4544
// CheckCoreUpdate checks if an update to CloudQuery core is available and returns its (new) version.
@@ -90,11 +89,11 @@ func CheckCoreUpdate(ctx context.Context, fs afero.Afero, nowUnix, period int64)
9089
return lastVersion, nil
9190
}
9291
if nowUnix-lastTime > period {
93-
release, err := getLatestRelease(ctx, &http.Client{Timeout: versionCheckHTTPTimeout}, cloudQueryGithubOrg, cloudQueryRepoName)
92+
vn, err := getLatestVersion(ctx, &http.Client{Timeout: versionCheckHTTPTimeout}, cloudQueryGithubOrg, cloudQueryRepoName)
9493
if err != nil {
9594
return nil, err
9695
}
97-
newVersion, err := version.NewSemver(release.GetTagName())
96+
newVersion, err := version.NewSemver(vn)
9897
if err != nil {
9998
// not really expected
10099
return nil, err

cli/pkg/core/version_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"net/http"
77
"testing"
88

9-
"github.com/google/go-github/v35/github"
109
"github.com/hashicorp/go-version"
1110
"github.com/spf13/afero"
1211
"github.com/spf13/viper"
@@ -167,10 +166,10 @@ func TestMaybeCheckForUpdate(t *testing.T) {
167166
defer func() { Version = saveVersion }()
168167
Version = tt.currentVersion
169168

170-
saveGetLatestRelease := getLatestRelease
171-
defer func() { getLatestRelease = saveGetLatestRelease }()
172-
getLatestRelease = func(ctx context.Context, client *http.Client, owner, repo string) (*github.RepositoryRelease, error) {
173-
return &github.RepositoryRelease{TagName: &tt.githubVersion}, tt.githubError
169+
saveGetLatestVersion := getLatestVersion
170+
defer func() { getLatestVersion = saveGetLatestVersion }()
171+
getLatestVersion = func(ctx context.Context, client *http.Client, owner, repo string) (string, error) {
172+
return tt.githubVersion, tt.githubError
174173
}
175174

176175
fs := afero.Afero{Fs: afero.NewMemMapFs()}

0 commit comments

Comments
 (0)