-
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
Signed-off-by: Kush <[email protected]>
- Loading branch information
1 parent
3931d77
commit c246195
Showing
4 changed files
with
134 additions
and
127 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,101 @@ | ||
package version | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/odpf/salt/log" | ||
|
||
"github.com/hashicorp/go-version" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
var ( | ||
ReleaseInfoTimeout = time.Second * 1 | ||
GithubReleaseURL = "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.GreaterThan(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(GithubReleaseURL, 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,33 @@ | ||
package version_test | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/odpf/salt/version" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetLatest(t *testing.T) { | ||
muxRouter := mux.NewRouter() | ||
server := httptest.NewServer(muxRouter) | ||
|
||
t.Run("should return 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) | ||
}) | ||
} |