#!/usr/bin/env bash # Publish a Furtka apps-catalog release to the Forgejo releases page. # # Usage: ./scripts/publish-catalog-release.sh # # Preconditions: # - $FORGEJO_TOKEN set (PAT with write:repository on daniel/furtka-apps) # - dist/furtka-apps-.tar.gz + .sha256 + release.json already built # # Behaviour: mirrors daniel/furtka's scripts/publish-release.sh — create a # release via the Forgejo API, then upload all three assets sequentially. # Python for JSON assembly so we don't drag jq onto the runner. set -euo pipefail VERSION="${1:?usage: $0 }" : "${FORGEJO_TOKEN:?FORGEJO_TOKEN must be set}" HOST="${FORGEJO_HOST:-forgejo.sourcegate.online}" REPO="${FORGEJO_REPO:-daniel/furtka-apps}" REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" DIST_DIR="$REPO_ROOT/dist" TARBALL="$DIST_DIR/furtka-apps-$VERSION.tar.gz" SHA_FILE="$TARBALL.sha256" RELEASE_JSON="$DIST_DIR/release.json" for f in "$TARBALL" "$SHA_FILE" "$RELEASE_JSON"; do [ -f "$f" ] || { echo "missing: $f"; exit 1; } done # Extract the changelog section for this version from CHANGELOG.md — matches # `## []` up to (but not including) the next `## [` line. BODY="$(awk -v v="$VERSION" ' BEGIN { inside=0 } /^## \[/ { if (inside) exit if (index($0, "[" v "]") > 0) { inside=1; next } } inside { print } ' "$REPO_ROOT/CHANGELOG.md")" if [ -z "$BODY" ]; then BODY="Catalog release $VERSION" fi PRERELEASE=false if [[ "$VERSION" == *-alpha* || "$VERSION" == *-beta* || "$VERSION" == *-rc* ]]; then PRERELEASE=true fi api() { curl --silent --show-error --fail-with-body \ --header "Authorization: token $FORGEJO_TOKEN" \ "$@" } base="https://$HOST/api/v1/repos/$REPO" release_body_json="$( VERSION="$VERSION" BODY="$BODY" PRE="$PRERELEASE" python3 -c ' import json, os print(json.dumps({ "tag_name": os.environ["VERSION"], "name": os.environ["VERSION"], "body": os.environ["BODY"], "prerelease": os.environ["PRE"] == "true", })) ' )" echo "==> Creating catalog release $VERSION" release_response="$(api --request POST "$base/releases" \ --header "Content-Type: application/json" \ --data "$release_body_json")" release_id="$(echo "$release_response" | python3 -c 'import json, sys; print(json.load(sys.stdin).get("id", ""))')" if [ -z "$release_id" ] || [ "$release_id" = "null" ]; then echo "error: couldn't parse release id from response:" echo "$release_response" exit 1 fi echo " release id: $release_id" upload_asset() { local path="$1" local name name="$(basename "$path")" echo "==> Uploading $name" api --request POST "$base/releases/$release_id/assets?name=$name" \ --form "attachment=@$path" > /dev/null } upload_asset "$TARBALL" upload_asset "$SHA_FILE" upload_asset "$RELEASE_JSON" echo "Catalog release $VERSION published: https://$HOST/$REPO/releases/tag/$VERSION"