mirror of
https://github.com/octoleo/syncthing.git
synced 2025-01-22 22:58:25 +00:00
Only consider relevant releases (fixes #1285).
This commit is contained in:
parent
53f1af0cab
commit
d6030b8d68
@ -586,7 +586,7 @@ func restGetUpgrade(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, upgrade.ErrUpgradeUnsupported.Error(), 500)
|
http.Error(w, upgrade.ErrUpgradeUnsupported.Error(), 500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
rel, err := upgrade.LatestRelease(strings.Contains(Version, "-beta"))
|
rel, err := upgrade.LatestGithubRelease(Version)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), 500)
|
http.Error(w, err.Error(), 500)
|
||||||
return
|
return
|
||||||
@ -628,7 +628,7 @@ func restGetLang(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func restPostUpgrade(w http.ResponseWriter, r *http.Request) {
|
func restPostUpgrade(w http.ResponseWriter, r *http.Request) {
|
||||||
rel, err := upgrade.LatestRelease(strings.Contains(Version, "-beta"))
|
rel, err := upgrade.LatestGithubRelease(Version)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Warnln("getting latest release:", err)
|
l.Warnln("getting latest release:", err)
|
||||||
http.Error(w, err.Error(), 500)
|
http.Error(w, err.Error(), 500)
|
||||||
|
@ -60,7 +60,6 @@ var (
|
|||||||
BuildHost = "unknown"
|
BuildHost = "unknown"
|
||||||
BuildUser = "unknown"
|
BuildUser = "unknown"
|
||||||
IsRelease bool
|
IsRelease bool
|
||||||
IsBeta bool
|
|
||||||
LongVersion string
|
LongVersion string
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -87,9 +86,6 @@ func init() {
|
|||||||
exp := regexp.MustCompile(`^v\d+\.\d+\.\d+(-beta[\d\.]+)?$`)
|
exp := regexp.MustCompile(`^v\d+\.\d+\.\d+(-beta[\d\.]+)?$`)
|
||||||
IsRelease = exp.MatchString(Version)
|
IsRelease = exp.MatchString(Version)
|
||||||
|
|
||||||
// Check for a beta build
|
|
||||||
IsBeta = strings.Contains(Version, "-beta")
|
|
||||||
|
|
||||||
stamp, _ := strconv.Atoi(BuildStamp)
|
stamp, _ := strconv.Atoi(BuildStamp)
|
||||||
BuildDate = time.Unix(int64(stamp), 0)
|
BuildDate = time.Unix(int64(stamp), 0)
|
||||||
|
|
||||||
@ -330,7 +326,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if doUpgrade || doUpgradeCheck {
|
if doUpgrade || doUpgradeCheck {
|
||||||
rel, err := upgrade.LatestRelease(IsBeta)
|
rel, err := upgrade.LatestGithubRelease(Version)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Fatalln("Upgrade:", err) // exits 1
|
l.Fatalln("Upgrade:", err) // exits 1
|
||||||
}
|
}
|
||||||
@ -1051,7 +1047,7 @@ func autoUpgrade() {
|
|||||||
case <-timer.C:
|
case <-timer.C:
|
||||||
}
|
}
|
||||||
|
|
||||||
rel, err := upgrade.LatestRelease(IsBeta)
|
rel, err := upgrade.LatestGithubRelease(Version)
|
||||||
if err == upgrade.ErrUpgradeUnsupported {
|
if err == upgrade.ErrUpgradeUnsupported {
|
||||||
events.Default.Unsubscribe(sub)
|
events.Default.Unsubscribe(sub)
|
||||||
return
|
return
|
||||||
|
3814
internal/upgrade/testdata/github-releases.json
vendored
Normal file
3814
internal/upgrade/testdata/github-releases.json
vendored
Normal file
File diff suppressed because it is too large
Load Diff
@ -32,11 +32,12 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Returns the latest release, including prereleases or not depending on the argument
|
// Returns the latest release, including prereleases or not depending on the argument
|
||||||
func LatestRelease(prerelease bool) (Release, error) {
|
func LatestGithubRelease(version string) (Release, error) {
|
||||||
resp, err := http.Get("https://api.github.com/repos/syncthing/syncthing/releases?per_page=10")
|
resp, err := http.Get("https://api.github.com/repos/syncthing/syncthing/releases?per_page=10")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Release{}, err
|
return Release{}, err
|
||||||
@ -49,19 +50,47 @@ func LatestRelease(prerelease bool) (Release, error) {
|
|||||||
json.NewDecoder(resp.Body).Decode(&rels)
|
json.NewDecoder(resp.Body).Decode(&rels)
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
|
|
||||||
|
return LatestRelease(version, rels)
|
||||||
|
}
|
||||||
|
|
||||||
|
type SortByRelease []Release
|
||||||
|
|
||||||
|
func (s SortByRelease) Len() int {
|
||||||
|
return len(s)
|
||||||
|
}
|
||||||
|
func (s SortByRelease) Swap(i, j int) {
|
||||||
|
s[i], s[j] = s[j], s[i]
|
||||||
|
}
|
||||||
|
func (s SortByRelease) Less(i, j int) bool {
|
||||||
|
return CompareVersions(s[i].Tag, s[j].Tag) > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func LatestRelease(version string, rels []Release) (Release, error) {
|
||||||
if len(rels) == 0 {
|
if len(rels) == 0 {
|
||||||
return Release{}, ErrVersionUnknown
|
return Release{}, ErrVersionUnknown
|
||||||
}
|
}
|
||||||
|
|
||||||
if prerelease {
|
sort.Sort(SortByRelease(rels))
|
||||||
// We are a beta version. Use the latest.
|
// Check for a beta build
|
||||||
return rels[0], nil
|
beta := strings.Contains(version, "-beta")
|
||||||
}
|
|
||||||
|
|
||||||
// We are a regular release. Only consider non-prerelease versions for upgrade.
|
|
||||||
for _, rel := range rels {
|
for _, rel := range rels {
|
||||||
if !rel.Prerelease {
|
if rel.Prerelease && !beta {
|
||||||
return rel, nil
|
continue
|
||||||
|
}
|
||||||
|
for _, asset := range rel.Assets {
|
||||||
|
assetName := path.Base(asset.Name)
|
||||||
|
// Check for the architecture
|
||||||
|
expectedRelease := releaseName(rel.Tag)
|
||||||
|
if debug {
|
||||||
|
l.Debugf("expected release asset %q", expectedRelease)
|
||||||
|
}
|
||||||
|
if debug {
|
||||||
|
l.Debugln("considering release", assetName)
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(assetName, expectedRelease) {
|
||||||
|
return rel, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Release{}, ErrVersionUnknown
|
return Release{}, ErrVersionUnknown
|
||||||
|
@ -15,9 +15,13 @@
|
|||||||
|
|
||||||
package upgrade
|
package upgrade
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
var testcases = []struct {
|
var versions = []struct {
|
||||||
a, b string
|
a, b string
|
||||||
r Relation
|
r Relation
|
||||||
}{
|
}{
|
||||||
@ -36,6 +40,7 @@ var testcases = []struct {
|
|||||||
{"0.10.0", "0.2.0", MajorNewer},
|
{"0.10.0", "0.2.0", MajorNewer},
|
||||||
{"30.10.0", "4.9.0", MajorNewer},
|
{"30.10.0", "4.9.0", MajorNewer},
|
||||||
{"0.9.0-beta7", "0.9.0-beta6", Newer},
|
{"0.9.0-beta7", "0.9.0-beta6", Newer},
|
||||||
|
{"0.9.0-beta7", "1.0.0-alpha", MajorOlder},
|
||||||
{"1.0.0-alpha", "1.0.0-alpha.1", Older},
|
{"1.0.0-alpha", "1.0.0-alpha.1", Older},
|
||||||
{"1.0.0-alpha.1", "1.0.0-alpha.beta", Older},
|
{"1.0.0-alpha.1", "1.0.0-alpha.beta", Older},
|
||||||
{"1.0.0-alpha.beta", "1.0.0-beta", Older},
|
{"1.0.0-alpha.beta", "1.0.0-beta", Older},
|
||||||
@ -53,9 +58,39 @@ var testcases = []struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCompareVersions(t *testing.T) {
|
func TestCompareVersions(t *testing.T) {
|
||||||
for _, tc := range testcases {
|
for _, v := range versions {
|
||||||
if r := CompareVersions(tc.a, tc.b); r != tc.r {
|
if r := CompareVersions(v.a, v.b); r != v.r {
|
||||||
t.Errorf("compareVersions(%q, %q): %d != %d", tc.a, tc.b, r, tc.r)
|
t.Errorf("compareVersions(%q, %q): %d != %d", v.a, v.b, r, v.r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var upgrades = map[string]string{
|
||||||
|
"v0.10.21": "v0.10.30",
|
||||||
|
"v0.10.29": "v0.10.30",
|
||||||
|
"v0.10.31": "v0.10.30",
|
||||||
|
"v0.10.0-alpha": "v0.10.30",
|
||||||
|
"v0.10.0-beta": "v0.11.0-beta0",
|
||||||
|
"v0.11.0-beta0+40-g53cb66e-dirty": "v0.11.0-beta0",
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRelease(t *testing.T) {
|
||||||
|
fd, err := os.Open("testdata/github-releases.json")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Missing github-release test data")
|
||||||
|
}
|
||||||
|
defer fd.Close()
|
||||||
|
|
||||||
|
var rels []Release
|
||||||
|
json.NewDecoder(fd).Decode(&rels)
|
||||||
|
|
||||||
|
for old, target := range upgrades {
|
||||||
|
upgrade, err := LatestRelease(old, rels)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("error retrieving latest version", err)
|
||||||
|
}
|
||||||
|
if upgrade.Tag != target {
|
||||||
|
t.Errorf("Invalid upgrade release: %v -> %v, but got %v", old, target, upgrade.Tag)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user