Skip to content

Commit

Permalink
Merge pull request #2812 from cli/updater-dev-version-match
Browse files Browse the repository at this point in the history
Update notifier: avoid false positives when gh is built from source
  • Loading branch information
mislav committed Jan 21, 2021
2 parents 6557289 + 82d19b7 commit f1a9da4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
11 changes: 11 additions & 0 deletions internal/update/update.go
Expand Up @@ -3,6 +3,9 @@ package update
import (
"fmt"
"io/ioutil"
"regexp"
"strconv"
"strings"
"time"

"github.com/cli/cli/api"
Expand All @@ -11,6 +14,8 @@ import (
"gopkg.in/yaml.v3"
)

var gitDescribeSuffixRE = regexp.MustCompile(`\d+-\d+-g[a-f0-9]{8}$`)

// ReleaseInfo stores information about a release
type ReleaseInfo struct {
Version string `json:"tag_name"`
Expand Down Expand Up @@ -83,6 +88,12 @@ func setStateEntry(stateFilePath string, t time.Time, r ReleaseInfo) error {
}

func versionGreaterThan(v, w string) bool {
w = gitDescribeSuffixRE.ReplaceAllStringFunc(w, func(m string) string {
idx := strings.IndexRune(m, '-')
n, _ := strconv.Atoi(m[0:idx])
return fmt.Sprintf("%d-pre.0", n+1)
})

vv, ve := version.NewVersion(v)
vw, we := version.NewVersion(w)

Expand Down
21 changes: 21 additions & 0 deletions internal/update/update_test.go
Expand Up @@ -33,6 +33,27 @@ func TestCheckForUpdate(t *testing.T) {
LatestURL: "https://www.spacejam.com/archive/spacejam/movie/jam.htm",
ExpectsResult: true,
},
{
Name: "current is built from source",
CurrentVersion: "v1.2.3-123-gdeadbeef",
LatestVersion: "v1.2.3",
LatestURL: "https://www.spacejam.com/archive/spacejam/movie/jam.htm",
ExpectsResult: false,
},
{
Name: "current is built from source after a prerelease",
CurrentVersion: "v1.2.3-rc.1-123-gdeadbeef",
LatestVersion: "v1.2.3",
LatestURL: "https://www.spacejam.com/archive/spacejam/movie/jam.htm",
ExpectsResult: true,
},
{
Name: "latest is newer than version build from source",
CurrentVersion: "v1.2.3-123-gdeadbeef",
LatestVersion: "v1.2.4",
LatestURL: "https://www.spacejam.com/archive/spacejam/movie/jam.htm",
ExpectsResult: true,
},
{
Name: "latest is current",
CurrentVersion: "v1.0.0",
Expand Down

0 comments on commit f1a9da4

Please sign in to comment.