Release Reims-Go / release (push) Canceled after 0s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
197 lines
4.7 KiB
Go
197 lines
4.7 KiB
Go
package version
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// Version is the current Reims-Go release version. Release builds may override
|
|
// it at link time with:
|
|
//
|
|
// -ldflags "-X github.com/Nwflower/reims-go/internal/version.Version=v0.2.3"
|
|
var Version = "v0.2.3"
|
|
|
|
const (
|
|
DefaultReleasesRepo = "Nwflower/Reims-Go"
|
|
DefaultAPIBase = "https://api.github.com"
|
|
)
|
|
|
|
// Source configures where app releases are checked.
|
|
type Source struct {
|
|
APIBase string
|
|
Repo string
|
|
Client *http.Client
|
|
}
|
|
|
|
func DefaultSource() Source {
|
|
repo := os.Getenv("REIMS_GO_RELEASES_REPO")
|
|
if repo == "" {
|
|
repo = DefaultReleasesRepo
|
|
}
|
|
return Source{
|
|
APIBase: DefaultAPIBase,
|
|
Repo: repo,
|
|
Client: &http.Client{},
|
|
}
|
|
}
|
|
|
|
// UpdateInfo describes the latest stable Reims-Go application release.
|
|
type UpdateInfo struct {
|
|
Current string `json:"current"`
|
|
Latest string `json:"latest"`
|
|
HasUpdate bool `json:"has_update"`
|
|
PageURL string `json:"page_url,omitempty"`
|
|
DownloadURL string `json:"download_url,omitempty"`
|
|
AssetName string `json:"asset_name,omitempty"`
|
|
AssetSize int64 `json:"asset_size,omitempty"`
|
|
PublishedAt string `json:"published_at,omitempty"`
|
|
}
|
|
|
|
type ghRelease struct {
|
|
TagName string `json:"tag_name"`
|
|
Draft bool `json:"draft"`
|
|
Prerelease bool `json:"prerelease"`
|
|
PublishedAt string `json:"published_at"`
|
|
HTMLURL string `json:"html_url"`
|
|
Assets []ghAsset `json:"assets"`
|
|
}
|
|
|
|
type ghAsset struct {
|
|
Name string `json:"name"`
|
|
Size int64 `json:"size"`
|
|
URL string `json:"browser_download_url"`
|
|
}
|
|
|
|
// ParseSemver parses a tag like "v0.2.1" or "0.2.1-rc1" into major/minor/patch.
|
|
func ParseSemver(v string) (major, minor, patch int, ok bool) {
|
|
s := strings.TrimSpace(v)
|
|
s = strings.TrimPrefix(s, "v")
|
|
if idx := strings.IndexAny(s, "-+"); idx >= 0 {
|
|
s = s[:idx]
|
|
}
|
|
parts := strings.Split(s, ".")
|
|
if len(parts) < 2 || len(parts) > 3 {
|
|
return 0, 0, 0, false
|
|
}
|
|
maj, err1 := strconv.Atoi(parts[0])
|
|
min, err2 := strconv.Atoi(parts[1])
|
|
pat := 0
|
|
var err3 error
|
|
if len(parts) == 3 {
|
|
pat, err3 = strconv.Atoi(parts[2])
|
|
}
|
|
if err1 != nil || err2 != nil || err3 != nil || maj < 0 || min < 0 || pat < 0 {
|
|
return 0, 0, 0, false
|
|
}
|
|
return maj, min, pat, true
|
|
}
|
|
|
|
// Compare compares two semver strings: returns -1 if a < b, 0 if equal, +1 if a > b.
|
|
func Compare(a, b string) int {
|
|
aMaj, aMin, aPat, aOK := ParseSemver(a)
|
|
bMaj, bMin, bPat, bOK := ParseSemver(b)
|
|
if !aOK && !bOK {
|
|
return 0
|
|
}
|
|
if !aOK {
|
|
return -1
|
|
}
|
|
if !bOK {
|
|
return 1
|
|
}
|
|
if aMaj != bMaj {
|
|
if aMaj < bMaj {
|
|
return -1
|
|
}
|
|
return 1
|
|
}
|
|
if aMin != bMin {
|
|
if aMin < bMin {
|
|
return -1
|
|
}
|
|
return 1
|
|
}
|
|
if aPat != bPat {
|
|
if aPat < bPat {
|
|
return -1
|
|
}
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// CheckUpdate queries GitHub Releases for the newest stable (non-draft,
|
|
// non-prerelease) application release tagged "vX.Y.Z".
|
|
func (s Source) CheckUpdate(ctx context.Context, current string) (UpdateInfo, error) {
|
|
if current == "" {
|
|
current = Version
|
|
}
|
|
info := UpdateInfo{Current: current}
|
|
client := s.Client
|
|
if client == nil {
|
|
client = http.DefaultClient
|
|
}
|
|
url := fmt.Sprintf("%s/repos/%s/releases?per_page=30", strings.TrimRight(s.APIBase, "/"), s.Repo)
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
|
if err != nil {
|
|
return info, err
|
|
}
|
|
req.Header.Set("User-Agent", "reims-go")
|
|
req.Header.Set("Accept", "application/vnd.github+json")
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return info, fmt.Errorf("check app updates: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusTooManyRequests {
|
|
return info, fmt.Errorf("GitHub API rate limit reached; try again later (HTTP %d)", resp.StatusCode)
|
|
}
|
|
if resp.StatusCode != http.StatusOK {
|
|
return info, fmt.Errorf("check app updates: HTTP %d", resp.StatusCode)
|
|
}
|
|
|
|
var rels []ghRelease
|
|
if err := json.NewDecoder(resp.Body).Decode(&rels); err != nil {
|
|
return info, fmt.Errorf("read app releases: %w", err)
|
|
}
|
|
|
|
var best *ghRelease
|
|
for i := range rels {
|
|
r := &rels[i]
|
|
if r.Draft || r.Prerelease || !strings.HasPrefix(r.TagName, "v") {
|
|
continue
|
|
}
|
|
if _, _, _, ok := ParseSemver(r.TagName); !ok {
|
|
continue
|
|
}
|
|
if best == nil || Compare(r.TagName, best.TagName) > 0 {
|
|
best = r
|
|
}
|
|
}
|
|
if best == nil {
|
|
info.Latest = current
|
|
return info, nil
|
|
}
|
|
|
|
info.Latest = best.TagName
|
|
info.PageURL = best.HTMLURL
|
|
info.PublishedAt = best.PublishedAt
|
|
for _, a := range best.Assets {
|
|
lower := strings.ToLower(a.Name)
|
|
if strings.HasSuffix(lower, ".exe") && !strings.Contains(lower, "-cli-") {
|
|
info.AssetName = a.Name
|
|
info.AssetSize = a.Size
|
|
info.DownloadURL = a.URL
|
|
break
|
|
}
|
|
}
|
|
info.HasUpdate = Compare(current, best.TagName) < 0
|
|
return info, nil
|
|
}
|