#!/bin/sh # SPDX-License-Identifier: GPL-3.0-only # SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® # release-version.sh — prints the version this tree releases, checked. # # packaging/release-version.sh # any tree # packaging/release-version.sh # CI's run for a pushed tag # # The version is that of the metainfo's newest , its first one, # and the app's `version` has to be the same. The entry's description is # the release notes: it has to render some, and they are written as # Markdown to the file $RELEASE_NOTES names, when it names one. # # For a tag the release is checked as well: the tag is that version, # annotated and signed by $RELEASE_SIGNER as Forgejo verifies it, on main, # and newer than every release the forge has published. Those checks read # $GITHUB_SERVER_URL, $GITHUB_REPOSITORY and $GITHUB_SHA, and the # checkout's tags and origin/main. Needs python3, and for a tag curl, jq # and git. set -eu SRC=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd) METAINFO=packaging/net.catcrafts.ImsdMessages.metainfo.xml # The metainfo is parsed, once, for both the version and the notes: a # comment or another attribute order would mislead a text search. # AppStream has no headings, so a paragraph that introduces a list is one: #

New

comes out as "## New" over its items, and a # paragraph on its own stays a paragraph. and get their # Markdown spelling. newest=$(python3 - "$SRC/$METAINFO" "${RELEASE_NOTES:-}" <<'EOF' import sys import xml.etree.ElementTree as ET release = ET.parse(sys.argv[1]).getroot().find("releases/release") desc = None if release is None else release.find("description") def text(el): parts = [el.text or ""] for child in el: inner = text(child) parts.append({"em": f"*{inner}*", "code": f"`{inner}`"}.get(child.tag, inner)) parts.append(child.tail or "") return " ".join("".join(parts).split()) blocks = [] if desc is None else list(desc) out = [] for i, el in enumerate(blocks): heads_list = i + 1 < len(blocks) and blocks[i + 1].tag in ("ul", "ol") if el.tag == "p": out.append(f"## {text(el)}" if heads_list else text(el)) elif el.tag == "ul": out.append("\n".join(f"- {text(li)}" for li in el)) elif el.tag == "ol": out.append("\n".join(f"{n}. {text(li)}" for n, li in enumerate(el, 1))) notes = "\n\n".join(out) if not notes.strip(): sys.exit("the metainfo needs a at the top of whose renders release notes") if sys.argv[2]: with open(sys.argv[2], "w") as f: print(notes, file=f) print(release.get("version", "")) EOF ) app=$(sed -n 's/.*constexpr std::string_view version = "\(.*\)";.*/\1/p' "$SRC/interfaces/ImsdMessages-Model.cppm") if [ "$app" != "$newest" ]; then echo "the app's version is \"$app\", but the metainfo's newest release is \"$newest\": a release bumps \`version\` in interfaces/ImsdMessages-Model.cppm and adds its entry at the top of $METAINFO" >&2 exit 1 fi [ $# -ge 1 ] || { echo "$newest"; exit 0; } # A release. The tag itself is read from the forge: the checkout fetches a # pushed tag again as a plain ref to its commit, so the local one is never # the annotated tag. version=$1 if ! printf '%s\n' "$version" | grep -qxE '[0-9]+\.[0-9]+\.[0-9]+'; then echo "the tag $version is not a version of the form X.Y.Z" >&2 exit 1 fi api="$GITHUB_SERVER_URL/api/v1/repos/$GITHUB_REPOSITORY" tag=$(curl -sSf "$api/tags/$version") object=$(printf '%s' "$tag" | jq -r .id) commit=$(printf '%s' "$tag" | jq -r .commit.sha) if [ "$commit" != "$GITHUB_SHA" ]; then echo "the forge has the tag $version on $commit, not on this run's commit $GITHUB_SHA" >&2 exit 1 fi if [ "$object" = "$commit" ]; then echo "$version is a lightweight tag; a release tag is annotated and signed: git tag -s $version -m 'imsd-messages $version'" >&2 exit 1 fi # Forgejo verifies a signed tag's own signature, and for an unsigned one # falls back to its commit's, so the verdict also has to cover the tag's # payload, not the commit's. signed=$(curl -sSf "$api/git/tags/$object" | jq -r --arg c "$commit" --arg v "$version" '"\(.verification.verified) \(.verification.signer.email // "") \(.verification.payload // "" | startswith("object \($c)\ntype commit\ntag \($v)\n"))"') if [ "$signed" != "true $RELEASE_SIGNER true" ]; then echo "Forgejo does not verify the tag $version itself as signed by $RELEASE_SIGNER (verified, signer, over the tag's payload: $signed)" >&2 exit 1 fi if ! git -C "$SRC" merge-base --is-ancestor "$GITHUB_SHA" origin/main; then echo "the tag $version is on $(git -C "$SRC" rev-parse --short "$GITHUB_SHA"), which is not on main: push the tag with the main it releases" >&2 exit 1 fi # Newer than every release the forge has published, its own included, and # not every tag: a later tag whose run still queues behind this one has no # release yet, and a tag that is no X.Y.Z version never gets one. for t in $(git -C "$SRC" tag --list | awk '/^[0-9]+\.[0-9]+\.[0-9]+$/' | sort -V); do [ "$(printf '%s\n%s\n' "$t" "$version" | sort -V | tail -n 1)" = "$t" ] || continue code=$(curl -s -o /dev/null -w '%{http_code}' "$api/releases/tags/$t") case "$code" in 404) ;; 200) if [ "$t" = "$version" ]; then echo "$version is released already; to re-run after a release left half made, delete that release first" >&2 else echo "$version is not newer than the release $t" >&2 fi exit 1 ;; *) echo "the forge answered $code when asked whether $t is released" >&2; exit 1 ;; esac done if [ "$newest" != "$version" ]; then echo "the metainfo's newest release is \"$newest\", but the tag is $version: the tagged commit needs the entry for $version at the top of $METAINFO" >&2 exit 1 fi echo "$version"