#!/usr/bin/env bash # Publishes a release built by bin/release.sh: draft -> ZIP attached and checked -> published -> checked from outside # (bin/release-check.sh). # # bin/release-publish.sh [version] default: Plugin::VERSION # # The release only turns public once the ZIP hangs on it, so no customer site ever sees a release without its # package. An existing draft for the tag (created by hand or by an earlier run) is reused; a published one is refused. # An existing tag fixes the commit (a release recreated after deleting it), a new tag points at HEAD. # # Needs: curl, jq, bsdtar and a Gitea token with write access to the repository (see bin/token.sh). # Environment: LIT_MCP_GITEA (default https://git.loosen-it.de), only to try the script against another server. set -euo pipefail root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "$root" gitea="${LIT_MCP_GITEA:-https://git.loosen-it.de}" api="$gitea/api/v1/repos/Loosen-IT/WP-LIT-MCP" version="${1:-$(sed -n "s/.*const VERSION = '\(.*\)';/\1/p" src/Plugin.php)}" name="wp-lit-mcp-$version.zip" zip="dist/$name" fail() { echo "ABBRUCH: $*" >&2; exit 1; } step() { echo "==> $*"; } source "$root/bin/token.sh" load_token [[ -r "$zip" ]] || fail "dist/ hat nicht $name: erst bin/release.sh" # A stale file in dist/ is the cheapest way to ship the wrong release. step "ZIP prüfen" [[ "$(bsdtar -xOf "$zip" wp-lit-mcp/wp-lit-mcp.php | sed -n 's/^ \* Version: //p')" == "$version" ]] \ || fail "Das ZIP enthält nicht Version $version" auth=(-H "Authorization: token $GITEA_TOKEN") json=(-H 'Content-Type: application/json') api_get() { curl -fsS "${auth[@]}" "$api/$1"; } step "Entwurf v$version suchen oder anlegen" release="$(api_get 'releases?per_page=50' | jq -c --arg t "v$version" '[.[] | select(.tag_name == $t)][0] // empty')" if [[ -z "$release" ]]; then notes="$(awk -v header="= $version =" '$0 == header { on = 1; next } /^= / { on = 0 } on' readme.txt)" target="$(curl -sS "${auth[@]}" "$api/tags/v$version" | jq -r '.commit.sha // empty')" [[ -n "$target" ]] || target="$(git rev-parse HEAD)" request="$(jq -n --arg tag "v$version" --arg sha "$target" --arg body "$notes" \ '{tag_name: $tag, target_commitish: $sha, name: $tag, body: $body, draft: true}')" release="$(curl -fsS "${auth[@]}" "${json[@]}" -d "$request" "$api/releases")" || fail "Release ließ sich nicht anlegen" fi [[ "$(jq -r .draft <<<"$release")" == "true" ]] || fail "v$version ist schon veröffentlicht" id="$(jq -r .id <<<"$release")" step "ZIP anhängen" old="$(jq -r --arg n "$name" '.assets[]? | select(.name == $n) | .id' <<<"$release")" [[ -z "$old" ]] || curl -fsS -X DELETE "${auth[@]}" "$api/releases/$id/assets/$old" >/dev/null curl -fsS "${auth[@]}" -F "attachment=@$zip" "$api/releases/$id/assets?name=$name" >/dev/null \ || fail "Upload von $name fehlgeschlagen" # Read it back instead of trusting the upload: only a draft that really carries the ZIP goes public. step "Anhang nachprüfen" size="$(api_get "releases/$id" | jq -r --arg n "$name" '.assets[]? | select(.name == $n) | .size')" [[ "$size" == "$(stat -c %s "$zip")" ]] || fail "$name hängt nicht oder mit falscher Größe am Entwurf (Gitea: '$size')" step "Veröffentlichen" curl -fsS -X PATCH "${auth[@]}" "${json[@]}" -d '{"draft": false}' "$api/releases/$id" >/dev/null \ || fail "Veröffentlichen fehlgeschlagen" step "Ergebnis von außen prüfen" exec bash bin/release-check.sh "$version"