Only pre-releases (v0.1.0-rc.1, rc.2) are published, and :latest followed the highest stable release only, so the Unraid template and the compose file, which pull :latest, found no image while :nightly was pushed every night. The new Chronos.Tools command release-channels reads the published releases (stable and pre-releases), ignores drafts and tags that are not SemVer, counts the tag itself as published and compares by SemVer 2.0 precedence. It prints latest (the public releases repository: highest stable release only) and image_latest (:latest: the highest stable release, or the highest pre-release while none is stable). Re-publishing an older tag never rolls either back. release.yml uses it in place of curl|grep|sort -V; the image job pushes :latest on image_latest. README, the public README, the compose file and ARCHITECTURE explain :latest, :<version> and :nightly. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
155 lines
7.6 KiB
C#
155 lines
7.6 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Nodes;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace Chronos.Tools.Commands;
|
|
|
|
/// <summary>What a published tag moves (<see cref="ReleaseChannelsCommand"/>).</summary>
|
|
/// <param name="Latest">The public content of the releases repository: the highest stable release only.</param>
|
|
/// <param name="ImageLatest">The image's <c>:latest</c>: the highest stable release, or the highest pre-release while no stable one exists.</param>
|
|
internal sealed record ReleaseChannels(bool Latest, bool ImageLatest);
|
|
|
|
/// <summary>
|
|
/// Decides which channels a release moves (BRIEF §19.3, amended 2026-10-26, R1). Gitea's "latest"
|
|
/// release is the most recently created one, so the choice is made by SemVer 2.0 precedence among the published
|
|
/// releases instead: re-publishing an older tag never rolls a channel back. Prints <c>latest=…</c> and
|
|
/// <c>image_latest=…</c>, ready for <c>$GITHUB_OUTPUT</c>.
|
|
/// </summary>
|
|
internal sealed class ReleaseChannelsCommand : ICommand
|
|
{
|
|
public string Name => "release-channels";
|
|
public string Summary => "Channels a tag moves: --tag vX.Y.Z[-pre] --releases <file.json>; prints latest= and image_latest=";
|
|
|
|
public async Task<int> RunAsync(string[] args)
|
|
{
|
|
var a = new Args(args);
|
|
if (a.Get("tag") is not { Length: > 0 } tag || a.Get("releases") is not { Length: > 0 } file)
|
|
throw new ToolException("Usage: release-channels --tag vMAJOR.MINOR.PATCH[-PRERELEASE] --releases <file.json>");
|
|
// The tag is checked before the file is read: a mistyped tag is the likelier error.
|
|
if (TagVersion.Parse(tag) is null) throw new ToolException(InvalidTag(tag));
|
|
if (!File.Exists(file)) throw new ToolException($"Releases file not found: {file}");
|
|
Console.Out.Write(Render(Decide(tag, await File.ReadAllTextAsync(file))));
|
|
return 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The channels <paramref name="tag"/> moves, given Gitea's releases list (<c>GET /repos/{owner}/{repo}/releases</c>).
|
|
/// Drafts and tags that are not SemVer are ignored; the tag itself always counts as published. A release is a
|
|
/// pre-release when its tag has a pre-release part or Gitea flags it so.
|
|
/// </summary>
|
|
internal static ReleaseChannels Decide(string tag, string releasesJson)
|
|
{
|
|
var current = TagVersion.Parse(tag) ?? throw new ToolException(InvalidTag(tag));
|
|
var listed = Published(releasesJson);
|
|
var currentIsPrerelease = current.IsPrerelease || listed.Any(r => r.Version.Tag == tag && r.Prerelease);
|
|
var all = listed.Where(r => r.Version.Tag != tag).Append((Version: current, Prerelease: currentIsPrerelease)).ToList();
|
|
|
|
// Equal precedence (v1.0.0 and v1.0.0+build.2) counts as highest: publishing the same version again moves nothing back.
|
|
bool Highest(IEnumerable<TagVersion> versions) => versions.All(v => v.CompareTo(current) <= 0);
|
|
var stable = all.Where(r => !r.Prerelease).Select(r => r.Version).ToList();
|
|
var latest = !currentIsPrerelease && Highest(stable);
|
|
var imageLatest = stable.Count > 0 ? latest : Highest(all.Select(r => r.Version));
|
|
return new ReleaseChannels(latest, imageLatest);
|
|
}
|
|
|
|
/// <summary>The two output lines, with <c>\n</c> on every platform.</summary>
|
|
internal static string Render(ReleaseChannels channels) =>
|
|
$"latest={(channels.Latest ? "true" : "false")}\nimage_latest={(channels.ImageLatest ? "true" : "false")}\n";
|
|
|
|
private static List<(TagVersion Version, bool Prerelease)> Published(string json)
|
|
{
|
|
JsonNode? root;
|
|
try
|
|
{
|
|
root = JsonNode.Parse(json);
|
|
}
|
|
catch (JsonException ex)
|
|
{
|
|
throw new ToolException($"The releases file is not JSON: {ex.Message}");
|
|
}
|
|
if (root is not JsonArray releases) throw new ToolException("The releases file is not a JSON array of releases.");
|
|
|
|
var published = new List<(TagVersion, bool)>();
|
|
foreach (var release in releases.OfType<JsonObject>())
|
|
{
|
|
if (Flag(release, "draft")) continue;
|
|
if (release["tag_name"] is not JsonValue name || !name.TryGetValue<string>(out var tagName)) continue;
|
|
if (TagVersion.Parse(tagName) is { } version) published.Add((version, version.IsPrerelease || Flag(release, "prerelease")));
|
|
}
|
|
return published;
|
|
}
|
|
|
|
private static bool Flag(JsonObject release, string name) =>
|
|
release[name] is JsonValue value && value.TryGetValue<bool>(out var flag) && flag;
|
|
|
|
private static string InvalidTag(string tag) =>
|
|
$"--tag '{tag}' is not vMAJOR.MINOR.PATCH[-PRERELEASE][+BUILD] (for example v1.2.0 or v1.2.0-rc.1).";
|
|
}
|
|
|
|
/// <summary>A tag <c>vMAJOR.MINOR.PATCH[-PRERELEASE][+BUILD]</c> ordered by SemVer 2.0 precedence (build metadata ignored).</summary>
|
|
internal sealed partial class TagVersion : IComparable<TagVersion>
|
|
{
|
|
private readonly string[] _core;
|
|
private readonly string[] _prerelease;
|
|
|
|
private TagVersion(string tag, string[] core, string[] prerelease)
|
|
{
|
|
Tag = tag;
|
|
_core = core;
|
|
_prerelease = prerelease;
|
|
}
|
|
|
|
public string Tag { get; }
|
|
|
|
public bool IsPrerelease => _prerelease.Length > 0;
|
|
|
|
/// <summary>The version of <paramref name="tag"/>, or null when it is not SemVer 2.0 with a leading <c>v</c>.</summary>
|
|
public static TagVersion? Parse(string? tag)
|
|
{
|
|
if (tag is null) return null;
|
|
var m = Pattern().Match(tag);
|
|
if (!m.Success) return null;
|
|
string[] core = [m.Groups["major"].Value, m.Groups["minor"].Value, m.Groups["patch"].Value];
|
|
return new TagVersion(tag, core, m.Groups["pre"].Success ? m.Groups["pre"].Value.Split('.') : []);
|
|
}
|
|
|
|
public int CompareTo(TagVersion? other)
|
|
{
|
|
if (other is null) return 1;
|
|
for (var i = 0; i < _core.Length; i++)
|
|
{
|
|
var c = CompareNumbers(_core[i], other._core[i]);
|
|
if (c != 0) return c;
|
|
}
|
|
// A version without a pre-release part is higher than the same version with one.
|
|
if (_prerelease.Length == 0 || other._prerelease.Length == 0) return other._prerelease.Length.CompareTo(_prerelease.Length);
|
|
for (var i = 0; i < Math.Min(_prerelease.Length, other._prerelease.Length); i++)
|
|
{
|
|
var c = CompareIdentifiers(_prerelease[i], other._prerelease[i]);
|
|
if (c != 0) return c;
|
|
}
|
|
return _prerelease.Length.CompareTo(other._prerelease.Length);
|
|
}
|
|
|
|
public override string ToString() => Tag;
|
|
|
|
// Numeric identifiers rank below alphanumeric ones and compare by value; alphanumeric ones compare in ASCII order.
|
|
private static int CompareIdentifiers(string a, string b) => (IsNumeric(a), IsNumeric(b)) switch
|
|
{
|
|
(true, true) => CompareNumbers(a, b),
|
|
(true, false) => -1,
|
|
(false, true) => 1,
|
|
_ => string.CompareOrdinal(a, b),
|
|
};
|
|
|
|
private static bool IsNumeric(string identifier) => identifier.All(char.IsAsciiDigit);
|
|
|
|
// Without leading zeros (the pattern refuses them) the longer number is the larger: no overflow on long ones.
|
|
private static int CompareNumbers(string a, string b) =>
|
|
a.Length != b.Length ? a.Length.CompareTo(b.Length) : string.CompareOrdinal(a, b);
|
|
|
|
// semver.org's pattern with a leading "v", ASCII digits only and no trailing newline.
|
|
[GeneratedRegex(@"^v(?<major>0|[1-9][0-9]*)\.(?<minor>0|[1-9][0-9]*)\.(?<patch>0|[1-9][0-9]*)(?:-(?<pre>(?:0|[1-9][0-9]*|[0-9]*[A-Za-z-][0-9A-Za-z-]*)(?:\.(?:0|[1-9][0-9]*|[0-9]*[A-Za-z-][0-9A-Za-z-]*))*))?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?\z")]
|
|
private static partial Regex Pattern();
|
|
}
|