|
| 1 | +// Package release checks GitHub for new atvloadly releases, keeps the latest |
| 2 | +// result for the web UI and notifies once per new version. |
| 3 | +package release |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + "sync" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/bitxeno/atvloadly/internal/app" |
| 13 | + atvhttp "github.com/bitxeno/atvloadly/internal/http" |
| 14 | + "github.com/bitxeno/atvloadly/internal/i18n" |
| 15 | + "github.com/bitxeno/atvloadly/internal/log" |
| 16 | + "github.com/bitxeno/atvloadly/internal/notify" |
| 17 | + "golang.org/x/mod/semver" |
| 18 | +) |
| 19 | + |
| 20 | +// Repo is the GitHub repository that publishes atvloadly releases. |
| 21 | +const Repo = "bitxeno/atvloadly" |
| 22 | + |
| 23 | +const requestTimeout = 30 * time.Second |
| 24 | + |
| 25 | +// apiBaseURL is the GitHub REST API root; tests point it to a local server. |
| 26 | +var apiBaseURL = "https://api.github.com" |
| 27 | + |
| 28 | +// sendNotification is replaced in tests. |
| 29 | +var sendNotification = notify.Send |
| 30 | + |
| 31 | +// Info describes the running version and the latest published release. |
| 32 | +type Info struct { |
| 33 | + Current string `json:"current"` |
| 34 | + Latest string `json:"latest"` |
| 35 | + URL string `json:"url"` |
| 36 | + PublishedAt time.Time `json:"published_at"` |
| 37 | + // Available reports whether Latest is newer than Current. It is false for |
| 38 | + // development and nightly builds, whose version is not a release tag. |
| 39 | + Available bool `json:"available"` |
| 40 | +} |
| 41 | + |
| 42 | +type githubRelease struct { |
| 43 | + TagName string `json:"tag_name"` |
| 44 | + HTMLURL string `json:"html_url"` |
| 45 | + PublishedAt time.Time `json:"published_at"` |
| 46 | +} |
| 47 | + |
| 48 | +var ( |
| 49 | + mu sync.RWMutex |
| 50 | + latest githubRelease |
| 51 | + checked []func(Info) |
| 52 | + announced []func(Info) |
| 53 | +) |
| 54 | + |
| 55 | +// OnCheck registers fn to be called with the result of every successful |
| 56 | +// check. The desktop app uses it to show the release in its tray menu. |
| 57 | +func OnCheck(fn func(Info)) { |
| 58 | + mu.Lock() |
| 59 | + checked = append(checked, fn) |
| 60 | + mu.Unlock() |
| 61 | +} |
| 62 | + |
| 63 | +// OnNewRelease registers fn to be called once per newer release, together |
| 64 | +// with the configured notification. The desktop app uses it to show a |
| 65 | +// native notification. |
| 66 | +func OnNewRelease(fn func(Info)) { |
| 67 | + mu.Lock() |
| 68 | + announced = append(announced, fn) |
| 69 | + mu.Unlock() |
| 70 | +} |
| 71 | + |
| 72 | +// Latest returns the result of the last successful check. |
| 73 | +func Latest() Info { |
| 74 | + mu.RLock() |
| 75 | + r := latest |
| 76 | + mu.RUnlock() |
| 77 | + return newInfo(app.Version.Version, r) |
| 78 | +} |
| 79 | + |
| 80 | +// Check fetches the latest release, stores it and sends a notification the |
| 81 | +// first time a newer version is seen. |
| 82 | +func Check() error { |
| 83 | + r, err := fetchLatest() |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + mu.Lock() |
| 88 | + latest = r |
| 89 | + onCheck, onNew := checked, announced |
| 90 | + mu.Unlock() |
| 91 | + |
| 92 | + info := newInfo(app.Version.Version, r) |
| 93 | + for _, fn := range onCheck { |
| 94 | + fn(info) |
| 95 | + } |
| 96 | + if !info.Available { |
| 97 | + return nil |
| 98 | + } |
| 99 | + log.Infof("A new atvloadly release is available: %s (running %s)", info.Latest, info.Current) |
| 100 | + announce(info, onNew) |
| 101 | + return nil |
| 102 | +} |
| 103 | + |
| 104 | +// announce sends the release notification and calls the OnNewRelease |
| 105 | +// handlers unless this version was already announced, which survives |
| 106 | +// restarts through the settings file. A failed notification is retried at |
| 107 | +// the next check, unless a handler already announced the release. Nothing is |
| 108 | +// announced while release notifications are disabled. |
| 109 | +func announce(info Info, handlers []func(Info)) { |
| 110 | + if !app.Settings.Update.ReleaseNotify || app.Settings.Update.NotifiedRelease == info.Latest { |
| 111 | + return |
| 112 | + } |
| 113 | + done := false |
| 114 | + if app.Settings.Notification.Enabled { |
| 115 | + title := i18n.LocalizeF("notify.release_title", map[string]any{"version": info.Latest}) |
| 116 | + message := i18n.LocalizeF("notify.release_content", map[string]any{"current": info.Current, "url": info.URL}) |
| 117 | + if err := sendNotification(title, message); err != nil { |
| 118 | + log.Err(err).Msg("Failed to send the release notification") |
| 119 | + } else { |
| 120 | + done = true |
| 121 | + } |
| 122 | + } |
| 123 | + for _, fn := range handlers { |
| 124 | + fn(info) |
| 125 | + done = true |
| 126 | + } |
| 127 | + if done { |
| 128 | + app.Settings.Update.NotifiedRelease = info.Latest |
| 129 | + app.SaveSettings() |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func newInfo(current string, r githubRelease) Info { |
| 134 | + info := Info{ |
| 135 | + Current: current, |
| 136 | + Latest: r.TagName, |
| 137 | + URL: r.HTMLURL, |
| 138 | + PublishedAt: r.PublishedAt, |
| 139 | + } |
| 140 | + cur, next := canonical(current), canonical(r.TagName) |
| 141 | + info.Available = cur != "" && next != "" && semver.Compare(next, cur) > 0 |
| 142 | + return info |
| 143 | +} |
| 144 | + |
| 145 | +// canonical returns v as a "vX.Y.Z" semantic version, or "" when it is not |
| 146 | +// one. Desktop builds carry the version without the "v" prefix. |
| 147 | +func canonical(v string) string { |
| 148 | + v = strings.TrimSpace(v) |
| 149 | + if v != "" && !strings.HasPrefix(v, "v") { |
| 150 | + v = "v" + v |
| 151 | + } |
| 152 | + if !semver.IsValid(v) { |
| 153 | + return "" |
| 154 | + } |
| 155 | + return v |
| 156 | +} |
| 157 | + |
| 158 | +// fetchLatest returns the latest stable release; GitHub excludes drafts and |
| 159 | +// pre-releases from this endpoint. |
| 160 | +func fetchLatest() (githubRelease, error) { |
| 161 | + var r githubRelease |
| 162 | + req := atvhttp.NewClient().SetTimeout(requestTimeout).R(). |
| 163 | + SetHeader("Accept", "application/vnd.github+json"). |
| 164 | + SetHeader("X-GitHub-Api-Version", "2022-11-28"). |
| 165 | + SetResult(&r) |
| 166 | + if token := app.GetGitHubToken(); token != "" { |
| 167 | + req.SetHeader("Authorization", "Bearer "+token) |
| 168 | + } |
| 169 | + resp, err := req.Get(fmt.Sprintf("%s/repos/%s/releases/latest", apiBaseURL, Repo)) |
| 170 | + if err != nil { |
| 171 | + return r, fmt.Errorf("failed to fetch the latest atvloadly release: %w", err) |
| 172 | + } |
| 173 | + if resp.StatusCode() != http.StatusOK { |
| 174 | + return r, fmt.Errorf("failed to fetch the latest atvloadly release: HTTP %d", resp.StatusCode()) |
| 175 | + } |
| 176 | + if r.TagName == "" { |
| 177 | + return r, fmt.Errorf("the latest atvloadly release has no tag") |
| 178 | + } |
| 179 | + return r, nil |
| 180 | +} |
0 commit comments