-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support for release version info
- Loading branch information
Showing
4 changed files
with
211 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package version | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/hashicorp/go-version" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
var ( | ||
ReleaseInfoTimeout = time.Second * 1 | ||
Release = "https://api.github.com/repos/%s/releases/latest" | ||
) | ||
|
||
type Info struct { | ||
Version string | ||
TarURL string | ||
} | ||
|
||
// ReleaseInfo fetches details related to provided release URL | ||
// releaseURL should point to a specific version | ||
// for example: https://api.github.com/repos/odpf/optimus/releases/latest | ||
func ReleaseInfo(releaseURL string) (*Info, error) { | ||
httpClient := http.Client{ | ||
Timeout: ReleaseInfoTimeout, | ||
} | ||
req, err := http.NewRequest(http.MethodGet, releaseURL, nil) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to create request") | ||
} | ||
req.Header.Set("User-Agent", "odpf/salt") | ||
resp, err := httpClient.Do(req) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to reach releaseURL: %s", releaseURL) | ||
} | ||
if resp.StatusCode != http.StatusOK { | ||
return nil, errors.Wrapf(err, "failed to reach releaseURL: %s, returned: %d", releaseURL, resp.StatusCode) | ||
} | ||
if resp.Body != nil { | ||
defer resp.Body.Close() | ||
} | ||
|
||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to read response body") | ||
} | ||
|
||
var releaseBody struct { | ||
TagName string `json:"tag_name"` | ||
Tarball string `json:"tarball_url"` | ||
} | ||
if err = json.Unmarshal(body, &releaseBody); err != nil { | ||
return nil, errors.Wrapf(err, "failed to parse: %s", string(body)) | ||
} | ||
|
||
return &Info{ | ||
Version: releaseBody.TagName, | ||
TarURL: releaseBody.Tarball, | ||
}, nil | ||
} | ||
|
||
// IsCurrentLatest returns true if the current version string is greater than | ||
// or equal to latestVersion as per semantic versioning | ||
func IsCurrentLatest(currVersion, latestVersion string) (bool, error) { | ||
currentV, err := version.NewVersion(currVersion) | ||
if err != nil { | ||
return false, errors.Wrapf(err, "failed to parse current version") | ||
} | ||
latestV, err := version.NewVersion(latestVersion) | ||
if err != nil { | ||
return false, errors.Wrapf(err, "failed to parse latest version") | ||
} | ||
if currentV.GreaterThanOrEqual(latestV) { | ||
return true, nil | ||
} | ||
return false, nil | ||
} | ||
|
||
// UpdateMsg returns notification message if there is a greater version available | ||
// then current in github release channel | ||
// Note: all errors are silently ignored | ||
func UpdateMsg(currentVersion, githubRepo string) string { | ||
info, err := ReleaseInfo(fmt.Sprintf(Release, githubRepo)) | ||
if err != nil { | ||
return "" | ||
} | ||
isLatest, err := IsCurrentLatest(currentVersion, info.Version) | ||
if err != nil { | ||
return "" | ||
} | ||
if isLatest { | ||
return fmt.Sprintf("a newer version is available: %s, consider updating the client", info.Version) | ||
} | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package version_test | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/odpf/salt/version" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGithubInfo(t *testing.T) { | ||
muxRouter := mux.NewRouter() | ||
server := httptest.NewServer(muxRouter) | ||
|
||
t.Run("should check for latest version availability by extracting correct version tag for valid json response on release URL", func(t *testing.T) { | ||
muxRouter.HandleFunc("/latest", func(rw http.ResponseWriter, r *http.Request) { | ||
rw.Header().Set("Content-Type", "application/json") | ||
response, _ := json.Marshal(struct { | ||
TagName string `json:"tag_name"` | ||
}{ | ||
TagName: "v0.0.2", | ||
}) | ||
rw.Write(response) | ||
}) | ||
info, err := version.ReleaseInfo("http://" + server.Listener.Addr().String() + "/latest") | ||
assert.Nil(t, err) | ||
assert.Equal(t, "v0.0.2", info.Version) | ||
info, err = version.ReleaseInfo("http://" + server.Listener.Addr().String() + "/latest") | ||
assert.Nil(t, err) | ||
assert.NotEqual(t, "v0.0.1", info.Version) | ||
}) | ||
} | ||
func TestIsCurrentLatest(t *testing.T) { | ||
muxRouter := mux.NewRouter() | ||
server := httptest.NewServer(muxRouter) | ||
|
||
t.Run("should return true for current version as the latest version ", func(t *testing.T) { | ||
muxRouter.HandleFunc("/latest", func(rw http.ResponseWriter, r *http.Request) { | ||
rw.Header().Set("Content-Type", "application/json") | ||
response, _ := json.Marshal(struct { | ||
TagName string `json:"tag_name"` | ||
}{ | ||
TagName: "v0.0.2", | ||
}) | ||
rw.Write(response) | ||
}) | ||
info, err := version.ReleaseInfo("http://" + server.Listener.Addr().String() + "/latest") | ||
assert.Nil(t, err) | ||
assert.Equal(t, "v0.0.2", info.Version) | ||
res, err := version.IsCurrentLatest("v0.0.2", info.Version) | ||
assert.Nil(t, err) | ||
assert.True(t, res) | ||
}) | ||
t.Run("should return false for current version not same as the latest version", func(t *testing.T) { | ||
muxRouter.HandleFunc("/latest", func(rw http.ResponseWriter, r *http.Request) { | ||
rw.Header().Set("Content-Type", "application/json") | ||
response, _ := json.Marshal(struct { | ||
TagName string `json:"tag_name"` | ||
}{ | ||
TagName: "v0.0.2", | ||
}) | ||
rw.Write(response) | ||
}) | ||
info, err := version.ReleaseInfo("http://" + server.Listener.Addr().String() + "/latest") | ||
assert.Nil(t, err) | ||
assert.Equal(t, "v0.0.2", info.Version) | ||
res, err := version.IsCurrentLatest("v0.0.1", info.Version) | ||
assert.Nil(t, err) | ||
assert.False(t, res) | ||
res, err = version.IsCurrentLatest("", info.Version) | ||
assert.NotNil(t, err) | ||
assert.False(t, res) | ||
res, err = version.IsCurrentLatest("v0.0.3", "") | ||
assert.NotNil(t, err) | ||
assert.False(t, res) | ||
}) | ||
} | ||
|
||
func TestUpdateMsg(t *testing.T) { | ||
muxRouter := mux.NewRouter() | ||
server := httptest.NewServer(muxRouter) | ||
t.Run("basic check for notify latest version", func(t *testing.T) { | ||
muxRouter.HandleFunc("/latest", func(rw http.ResponseWriter, r *http.Request) { | ||
rw.Header().Set("Content-Type", "application/json") | ||
response, _ := json.Marshal(struct { | ||
TagName string `json:"tag_name"` | ||
}{ | ||
TagName: "v0.0.1", | ||
}) | ||
rw.Write(response) | ||
}) | ||
info, err := version.ReleaseInfo("http://" + server.Listener.Addr().String() + "/latest") | ||
assert.Nil(t, err) | ||
assert.Equal(t, "v0.0.1", info.Version) | ||
res, err := version.IsCurrentLatest("v0.0.1", info.Version) | ||
assert.Nil(t, err) | ||
assert.True(t, res) | ||
s := version.UpdateMsg("v0.0.1", "odpf/optimus") | ||
assert.NotEqual(t, "v0.0.1", s) | ||
}) | ||
} |