fix(app): build update links from the releases repository address (#7)
CI / Build and test (push) Canceled after 2m45s
CI / PostgreSQL 16 integration (push) Canceled after 0s
CI / PostgreSQL 17 integration (push) Canceled after 0s
CI / Docker image (push) Canceled after 0s
CI / YAML check (push) Canceled after 0s
CI / Windows tests and installer (push) Canceled after 0s

Gitea writes a release's html_url and browser_download_url with its ROOT_URL,
which on the owner's server is the plain-http LAN address
http://192.168.30.23:3000. The update check accepts only https links, so it
ignored v0.1.0-rc.1 entirely. The page and download links now come from
ChronosReleasesUrl, the address the build names for its users; only the
tags, file names and sizes are read from Gitea's answer.

The lease renewal test's timer periods follow TestTiming too: on the shared
CI host a 100 ms renewal timer came 700 ms late once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
This commit was merged in pull request #7.
This commit is contained in:
Alby96andClaude Opus 5.5 authored and Alby96 committed 2026-10-23 14:39:10 +02:00
1 parent f2b58522b9
commit f80fc148bf
7 files changed
+73 -20

No files matched your search

+33 -14
View File
@@ -16,7 +16,10 @@ public sealed record ReleaseAsset(string Name, string DownloadUrl, long Size);
/// <summary>
/// Update check against the Gitea releases API of the releases repository (BRIEF §18, §19.4, amended 2026-10-23, R2):
/// reads the published stable releases, takes the highest version (Gitea's own "latest" is the most recently created
/// one, which a re-published older tag or a backport would become), and compares it with the running version.
/// one, which a re-published older tag or a backport would become), and compares it with the running version. The
/// release page and download links are built from the repository address the build names (ChronosReleasesUrl), not
/// taken from the answer: Gitea writes its links with its ROOT_URL, which may be an address clients cannot reach
/// (the owner's is a plain-http LAN address).
/// </summary>
public static class UpdateCheck
{
@@ -35,30 +38,36 @@ public static class UpdateCheck
/// <summary>
/// The newest release of a releases-list response (an array: the highest version wins) or of one release object;
/// null when there is none. Drafts, pre-releases, unparsable tags and pages without an https link are skipped, and
/// so are assets without an https download link.
/// null when there is none. With <paramref name="repository"/> ("https://host/owner/name"), the release page and
/// the download links are that repository's (<c>/releases/tag/&lt;tag&gt;</c>, <c>/releases/download/&lt;tag&gt;/&lt;name&gt;</c>);
/// without it, the ones in the answer. Drafts, pre-releases, unparsable tags and pages without an https link are
/// skipped, and so are assets without an https download link.
/// </summary>
public static ReleaseInfo? Parse(string json)
public static ReleaseInfo? Parse(string json, string? repository = null)
{
var baseUrl = repository?.Trim().TrimEnd('/');
using var document = JsonDocument.Parse(json);
var root = document.RootElement;
return root.ValueKind switch
{
JsonValueKind.Array => root.EnumerateArray().Select(ParseRelease).OfType<ReleaseInfo>().MaxBy(r => r.Version),
JsonValueKind.Object => ParseRelease(root),
JsonValueKind.Array => root.EnumerateArray().Select(r => ParseRelease(r, baseUrl)).OfType<ReleaseInfo>().MaxBy(r => r.Version),
JsonValueKind.Object => ParseRelease(root, baseUrl),
_ => null,
};
}
private static ReleaseInfo? ParseRelease(JsonElement root)
private static ReleaseInfo? ParseRelease(JsonElement root, string? repository)
{
if (root.ValueKind != JsonValueKind.Object) return null;
if (Flag(root, "draft") || Flag(root, "prerelease")) return null;
var tag = root.TryGetProperty("tag_name", out var t) && t.ValueKind == JsonValueKind.String ? t.GetString() : null;
var url = root.TryGetProperty("html_url", out var u) && u.ValueKind == JsonValueKind.String ? u.GetString() : null;
var version = ParseVersion(tag);
if (version is null || tag is null || url is null || !url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) return null;
return new ReleaseInfo(version, tag, url) { Assets = ParseAssets(root) };
if (version is null || tag is null) return null;
var url = repository is not null
? $"{repository}/releases/tag/{Uri.EscapeDataString(tag)}"
: root.TryGetProperty("html_url", out var u) && u.ValueKind == JsonValueKind.String ? u.GetString() : null;
if (url is null || !url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) return null;
return new ReleaseInfo(version, tag, url) { Assets = ParseAssets(root, repository, tag) };
}
/// <summary>
@@ -91,7 +100,15 @@ public static class UpdateCheck
/// that is a failed check, never "up to date".
/// </summary>
/// <exception cref="HttpRequestException">The request failed, or the repository was not found.</exception>
public static async Task<ReleaseInfo?> FetchAsync(HttpClient http, string apiUrl, string userAgentVersion, CancellationToken ct)
public static Task<ReleaseInfo?> FetchAsync(HttpClient http, string apiUrl, string userAgentVersion, CancellationToken ct) =>
FetchAsync(http, apiUrl, null, userAgentVersion, ct);
/// <summary>
/// As <see cref="FetchAsync(HttpClient, string, string, CancellationToken)"/>, with the release page and download
/// links built from <paramref name="repository"/> (see <see cref="Parse"/>).
/// </summary>
/// <exception cref="HttpRequestException">The request failed, or the repository was not found.</exception>
public static async Task<ReleaseInfo?> FetchAsync(HttpClient http, string apiUrl, string? repository, string userAgentVersion, CancellationToken ct)
{
using var request = new HttpRequestMessage(HttpMethod.Get, apiUrl);
request.Headers.UserAgent.Add(new ProductInfoHeaderValue("Chronos", userAgentVersion));
@@ -103,7 +120,7 @@ public static class UpdateCheck
var json = await response.Content.ReadAsStringAsync(ct).ConfigureAwait(false);
try
{
return Parse(json);
return Parse(json, repository);
}
catch (JsonException)
{
@@ -111,7 +128,7 @@ public static class UpdateCheck
}
}
private static List<ReleaseAsset> ParseAssets(JsonElement root)
private static List<ReleaseAsset> ParseAssets(JsonElement root, string? repository, string tag)
{
var assets = new List<ReleaseAsset>();
if (!root.TryGetProperty("assets", out var list) || list.ValueKind != JsonValueKind.Array) return assets;
@@ -119,7 +136,9 @@ public static class UpdateCheck
{
if (a.ValueKind != JsonValueKind.Object) continue;
var name = a.TryGetProperty("name", out var n) && n.ValueKind == JsonValueKind.String ? n.GetString() : null;
var link = a.TryGetProperty("browser_download_url", out var d) && d.ValueKind == JsonValueKind.String ? d.GetString() : null;
var link = repository is not null && !string.IsNullOrEmpty(name)
? $"{repository}/releases/download/{Uri.EscapeDataString(tag)}/{Uri.EscapeDataString(name)}"
: a.TryGetProperty("browser_download_url", out var d) && d.ValueKind == JsonValueKind.String ? d.GetString() : null;
var size = a.TryGetProperty("size", out var z) && z.ValueKind == JsonValueKind.Number && z.TryGetInt64(out var bytes) ? bytes : -1;
if (string.IsNullOrEmpty(name) || link is null || !link.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) continue;
assets.Add(new ReleaseAsset(name, link, size));
@@ -71,7 +71,7 @@ public sealed class AppServices : IAsyncDisposable
Server = new ServerSession(settings, saveSettings, new ChronosServerApiFactory(), new DpapiTokenProtector());
Encoders = new MediaFoundationInventory();
Formats = new WicFormatInventory(() => _wic);
CheckForUpdateAsync = ct => UpdateCheck.FetchAsync(Http.Value, ProductLinks.StableReleasesApi, VersionText, ct);
CheckForUpdateAsync = ct => UpdateCheck.FetchAsync(Http.Value, ProductLinks.StableReleasesApi, ProductLinks.Repository, VersionText, ct);
DownloadUpdateAsync = (release, progress, ct) =>
UpdateDownload.DownloadAsync(DownloadHttp.Value, release, environment.IsPortable, UpdateDownload.Root(Path.GetTempPath()), VersionText, progress,
UpdateDownload.IdleTimeout, ct);