Skip to content

Commit

Permalink
feat: support for release version info
Browse files Browse the repository at this point in the history
Signed-off-by: Kush <[email protected]>
  • Loading branch information
kushsharma committed Aug 31, 2021
1 parent 3931d77 commit c246195
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 127 deletions.
75 changes: 0 additions & 75 deletions version/check.go

This file was deleted.

52 changes: 0 additions & 52 deletions version/check_test.go

This file was deleted.

101 changes: 101 additions & 0 deletions version/release.go
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
}
33 changes: 33 additions & 0 deletions version/release_test.go
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)
})
}

0 comments on commit c246195

Please sign in to comment.