Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

Commit

Permalink
fix incorrect version info on check-update
Browse files Browse the repository at this point in the history
Fixes #282
  • Loading branch information
profclems committed Nov 16, 2020
1 parent 1e7c17e commit ff2e936
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 100 deletions.
2 changes: 1 addition & 1 deletion cmd/glab/main.go
Expand Up @@ -125,7 +125,7 @@ func main() {

checkUpdate, _ := cfg.Get("", "check_update")
if checkUpdate, err := strconv.ParseBool(checkUpdate); err == nil && checkUpdate {
err = update.CheckUpdate(rootCmd, cmdFactory.IO, version, build, true)
err = update.CheckUpdate(cmdFactory.IO, version, true)
if err != nil && debug {
printError(os.Stderr, err, rootCmd, debug)
}
Expand Down
2 changes: 1 addition & 1 deletion commands/root.go
Expand Up @@ -83,7 +83,7 @@ func NewCmdRoot(f *cmdutils.Factory, version, buildDate string) *cobra.Command {
rootCmd.AddCommand(configCmd.NewCmdConfig(f))
rootCmd.AddCommand(completionCmd.NewCmdCompletion(f.IO))
rootCmd.AddCommand(versionCmd.NewCmdVersion(f.IO, version, buildDate))
rootCmd.AddCommand(updateCmd.NewCheckUpdateCmd(f.IO, version, buildDate))
rootCmd.AddCommand(updateCmd.NewCheckUpdateCmd(f.IO, version))
rootCmd.AddCommand(authCmd.NewCmdAuth(f))

// the commands below require apiClient and resolved repos
Expand Down
47 changes: 47 additions & 0 deletions commands/update/check_update.go
@@ -0,0 +1,47 @@
package update

import (
"errors"
"fmt"

"github.com/profclems/glab/internal/utils"
"github.com/spf13/cobra"
)

func NewCheckUpdateCmd(s *utils.IOStreams, version string) *cobra.Command {
var cmd = &cobra.Command{
Use: "check-update",
Short: "Check for latest glab releases",
Long: ``,
Aliases: []string{"update"},
RunE: func(cmd *cobra.Command, args []string) error {
return CheckUpdate(s, version, false)
},
}

return cmd
}

func CheckUpdate(s *utils.IOStreams, version string, silentErr bool) error {
latestRelease, err := GetUpdateInfo()
if err != nil {
if silentErr {
return nil
}
return errors.New("could not check for update! Make sure you have a stable internet connection")
}

if isOlderVersion(latestRelease.Version, version) {
fmt.Fprintf(s.StdOut, "%s %s → %s\n%s\n",
utils.Yellow("A new version of glab has been released:"),
utils.Red(version), utils.Green(latestRelease.Version),
latestRelease.URL)
} else {
if silentErr {
return nil
}
fmt.Fprintf(s.StdOut, "%v %v", utils.GreenCheck(),
utils.Green("You are already using the latest version of glab"))
}
return nil
}
105 changes: 105 additions & 0 deletions commands/update/check_update_test.go
@@ -0,0 +1,105 @@
package update

import (
"fmt"
"testing"

"github.com/alecthomas/assert"
"github.com/jarcoal/httpmock"
"github.com/profclems/glab/internal/utils"
)

func TestNewCheckUpdateCmd(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder("GET", `https://api.github.com/repos/profclems/glab/releases/latest`,
httpmock.NewStringResponder(200, `{
"url": "https://api.github.com/repos/profclems/glab/releases/33385584",
"html_url": "https://github.com/profclems/glab/releases/tag/v1.11.1",
"tag_name": "v1.11.1",
"name": "v1.11.1",
"draft": false,
"prerelease": false,
"created_at": "2020-11-03T05:33:29Z",
"published_at": "2020-11-03T05:39:04Z"}`))

ioStream, _, stdout, stderr := utils.IOTest()
type args struct {
s *utils.IOStreams
version string
}
tests := []struct {
name string
args args
stdOut string
stdErr string
wantErr bool
}{
{
name: "same version",
args: args{
s: ioStream,
version: "v1.11.1",
},
stdOut: "✓ You are already using the latest version of glab",
stdErr: "",
},
{
name: "older version",
args: args{
s: ioStream,
version: "v1.11.0",
},
stdOut: "A new version of glab has been released: v1.11.0 → v1.11.1\nhttps://github.com/profclems/glab/releases/tag/v1.11.1\n",
stdErr: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := NewCheckUpdateCmd(tt.args.s, tt.args.version).Execute()
if tt.wantErr {
assert.Nil(t, err)
}

assert.Equal(t, tt.stdOut, stdout.String())
assert.Equal(t, tt.stdErr, stderr.String())

// clean up
stdout.Reset()
stderr.Reset()
})
}
}

func TestNewCheckUpdateCmd_error(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder("GET", `https://api.github.com/repos/profclems/glab/releases/latest`,
httpmock.NewErrorResponder(fmt.Errorf("an error expected")))

ioStream, _, stdout, stderr := utils.IOTest()

err := NewCheckUpdateCmd(ioStream, "1.11.0").Execute()
assert.NotNil(t, err)
assert.Equal(t, "could not check for update! Make sure you have a stable internet connection", err.Error())
assert.Equal(t, "", stdout.String())
assert.Equal(t, "", stderr.String())
}

func TestNewCheckUpdateCmd_json_error(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder("GET", `https://api.github.com/repos/profclems/glab/releases/latest`,
httpmock.NewStringResponder(200, ``))

ioStream, _, stdout, stderr := utils.IOTest()

err := NewCheckUpdateCmd(ioStream, "1.11.0").Execute()
assert.NotNil(t, err)
assert.Equal(t, "could not check for update! Make sure you have a stable internet connection", err.Error())
assert.Equal(t, "", stdout.String())
assert.Equal(t, "", stderr.String())
}
61 changes: 0 additions & 61 deletions commands/update/checkupdate.go

This file was deleted.

32 changes: 0 additions & 32 deletions commands/update/checkupdate_test.go

This file was deleted.

19 changes: 16 additions & 3 deletions commands/update/update.go
Expand Up @@ -2,19 +2,22 @@ package update

import (
"encoding/json"
"strings"
"time"

"github.com/hashicorp/go-version"

"github.com/profclems/glab/internal/request"
)

type ReleaseInfo struct {
Name string `json:"name"`
Version string `json:"tag_name"`
PreRelease bool `json:"prerelease"`
HTMLUrl string `json:"html_url"`
URL string `json:"html_url"`
PublishedAt time.Time `json:"published_at"`
}

// GetUpdateInfo checks for latest glab releases
// GetUpdateInfo checks for latest glab release and returns the ReleaseInfo
func GetUpdateInfo() (ReleaseInfo, error) {
releasesUrl := "https://api.github.com/repos/profclems/glab/releases/latest"
resp, err := request.MakeRequest("{}", releasesUrl, "GET")
Expand All @@ -28,3 +31,13 @@ func GetUpdateInfo() (ReleaseInfo, error) {
}
return releaseInfo, nil
}

func isOlderVersion(latestVersion, appVersion string) bool {
latestVersion = strings.TrimSpace(latestVersion)
appVersion = strings.TrimSpace(appVersion)

vv, ve := version.NewVersion(latestVersion)
vw, we := version.NewVersion(appVersion)

return ve == nil && we == nil && vv.GreaterThan(vw)
}
4 changes: 2 additions & 2 deletions commands/update/update_test.go
Expand Up @@ -40,8 +40,8 @@ func Test_isLatestVersion(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isLatestVersion(tt.args.latestVersion, tt.args.currentVersion); got != tt.want {
t.Errorf("isLatestVersion(%s, %s) = %v, want %v",
if got := isOlderVersion(tt.args.latestVersion, tt.args.currentVersion); got != tt.want {
t.Errorf("isOlderVersion(%s, %s) = %v, want %v",
tt.args.latestVersion, tt.args.currentVersion, got, tt.want)
}
})
Expand Down
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -6,6 +6,7 @@ require (
github.com/AlecAivazis/survey/v2 v2.2.2
github.com/MakeNowJust/heredoc v1.0.0
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38
github.com/charmbracelet/glamour v0.2.0
github.com/cli/safeexec v1.0.0
github.com/dustin/go-humanize v1.0.0
Expand Down

0 comments on commit ff2e936

Please sign in to comment.