-
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
215 additions
and
1 deletion.
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,100 @@ | ||
package version | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/hashicorp/go-version" | ||
"github.com/odpf/salt/log" | ||
"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 | ||
} | ||
|
||
// GithubInfo 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 GithubInfo(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 | ||
} | ||
|
||
// NotifyIfNotLatest prints a notification if current application version | ||
// is not latest | ||
func NotifyIfNotLatest(logger log.Logger, currentVersion, githubRepo string) error { | ||
info, err := GithubInfo(fmt.Sprintf(Release, githubRepo)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
isLatest, err := IsCurrentLatest(currentVersion, info.Version) | ||
if err != nil { | ||
return nil | ||
} | ||
if isLatest { | ||
logger.Info(fmt.Sprintf("a newer version is available: %s, consider updating the client", info.Version)) | ||
} | ||
return nil | ||
} |
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,108 @@ | ||
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.GithubInfo("http://" + server.Listener.Addr().String() + "/latest") | ||
assert.Nil(t, err) | ||
assert.Equal(t, "v0.0.2", info.Version) | ||
info, err = version.GithubInfo("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.GithubInfo("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.GithubInfo("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 TestNotifyIfNotLatest(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.GithubInfo("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) | ||
err = version.NotifyIfNotLatest(nil, "v0.0.1", "odpf/optimus") | ||
if err != nil { | ||
t.Errorf("Error in notifyIfNotLatest: %v", err) | ||
} | ||
err = version.NotifyIfNotLatest(nil, "", "odpf/optimus") | ||
assert.Nil(t, err) | ||
}) | ||
} |