From 96350d76005b7ce4e88df77ba8b189890dafd3db Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Fri, 16 Aug 2019 10:04:10 +0200 Subject: [PATCH] cmd/stupgrades: Generate appropriate upgrade data (fixes #5924) (#5960) This is a tiny tool to grab the GitHub releases info and generate a more concise version of it. The conciseness comes from two aspects: - We select only the latest stable and pre. There is no need to offer upgrades to versions that are older than the latest. (There might be, in the future, when we hit 2.0. We can revisit this at that time.) - We use our structs to deserialize and reserialize the data. This means we remove all attributes that we don't understand and hence don't require. All in all the new response is about 10% the size of the previous one and avoids the issue where we only serve a bunch of release candidates and no stable. --- cmd/stupgrades/main.go | 57 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 cmd/stupgrades/main.go diff --git a/cmd/stupgrades/main.go b/cmd/stupgrades/main.go new file mode 100644 index 000000000..429f026e3 --- /dev/null +++ b/cmd/stupgrades/main.go @@ -0,0 +1,57 @@ +// Copyright (C) 2019 The Syncthing Authors. +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this file, +// You can obtain one at https://mozilla.org/MPL/2.0/. + +package main + +import ( + "encoding/json" + "flag" + "os" + "sort" + + "github.com/syncthing/syncthing/lib/upgrade" +) + +const defaultURL = "https://api.github.com/repos/syncthing/syncthing/releases?per_page=25" + +func main() { + url := flag.String("u", defaultURL, "GitHub releases url") + flag.Parse() + + rels := upgrade.FetchLatestReleases(*url, "") + if rels == nil { + // An error was already logged + os.Exit(1) + } + + sort.Sort(upgrade.SortByRelease(rels)) + rels = filterForLatest(rels) + + if err := json.NewEncoder(os.Stdout).Encode(rels); err != nil { + os.Exit(1) + } +} + +// filterForLatest returns the latest stable and prerelease only. If the +// stable version is newer (comes first in the list) there is no need to go +// looking for a prerelease at all. +func filterForLatest(rels []upgrade.Release) []upgrade.Release { + var filtered []upgrade.Release + var havePre bool + for _, rel := range rels { + if !rel.Prerelease { + // We found a stable version, we're good now. + filtered = append(filtered, rel) + break + } + if rel.Prerelease && !havePre { + // We remember the first prerelease we find. + filtered = append(filtered, rel) + havePre = true + } + } + return filtered +}