diff --git a/.forgejo/workflows/build_release.yml b/.forgejo/workflows/build_release.yml index d51b87f54..e04aa70cc 100755 --- a/.forgejo/workflows/build_release.yml +++ b/.forgejo/workflows/build_release.yml @@ -273,72 +273,97 @@ jobs: run: | set -euo pipefail + export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH:-}" + + discover_jq() { + if command -v jq >/dev/null 2>&1; then + JQ_BIN="$(command -v jq)" + export JQ_BIN + return 0 + fi + for cand in /usr/bin/jq /usr/local/bin/jq /bin/jq; do + if [ -x "$cand" ]; then + JQ_BIN="$cand" + export JQ_BIN + return 0 + fi + done + return 1 + } + ensure_jq() { + if discover_jq; then + return 0 + fi + run_as_root() { + if [ "$(id -u)" -eq 0 ]; then + "$@" + elif command -v sudo >/dev/null 2>&1; then + sudo "$@" + else + return 1 + fi + } + if command -v dnf >/dev/null 2>&1; then + run_as_root dnf install -y jq + elif command -v apt-get >/dev/null 2>&1; then + run_as_root apt-get update + run_as_root apt-get install -y jq + elif command -v apk >/dev/null 2>&1; then + run_as_root apk add --no-cache jq + elif command -v pacman >/dev/null 2>&1; then + run_as_root pacman -Sy --noconfirm jq + fi + discover_jq + } + ensure_jq || { echo "jq not found; install jq on the runner (dnf install -y jq)."; exit 1; } + api_base="${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/releases" payload_file="$(mktemp)" response_file="$(mktemp)" - python3 - <<'PY' > "$payload_file" - import json - import os - import textwrap + commit_logs="${COMMIT_LOGS:-}" + commit_logs="$(printf '%s' "$commit_logs" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')" + if [ -z "$commit_logs" ]; then + commit_logs="- Initial release" + fi - version_tag = os.environ["VERSION_TAG"] - version_display = os.environ.get("VERSION_DISPLAY", version_tag) - previous_tag = os.environ.get("PREV_TAG_NAME", "").strip() - commit_logs = os.environ.get("COMMIT_LOGS", "").strip() or "- Initial release" - repo_url = os.environ["REPO_URL"] - repository_name = os.environ["REPOSITORY_NAME"] + full_changelog="" + if [ -n "${PREV_TAG_NAME:-}" ]; then + full_changelog="**Full Changelog**: [${REPOSITORY_NAME}@${PREV_TAG_NAME}...${VERSION_TAG}](${REPO_URL}/compare/${PREV_TAG_NAME}...${VERSION_TAG})" + fi - if previous_tag: - full_changelog = ( - f"**Full Changelog**: " - f"[{repository_name}@{previous_tag}...{version_tag}]" - f"({repo_url}/compare/{previous_tag}...{version_tag})" - ) - else: - full_changelog = "" + release_body="$(printf '%s\n' \ + "#### What's Changed" \ + "" \ + "##### New" \ + "" \ + "##### Improve" \ + "" \ + "##### Fix" \ + "" \ + "${commit_logs}" \ + "" \ + "##### Based on" \ + "" \ + "${full_changelog}" \ + "" \ + "" \ + "> [!TIP]" \ + ">" \ + "> ### If you are unsure which version to download then go with \`Komikku-universal.apk\`")" - body = textwrap.dedent( - f"""\ - #### What's Changed - ##### New - - ##### Improve - - ##### Fix - - {commit_logs} - - ##### Based on - - {full_changelog} - - - > [!TIP] - > - > ### If you are unsure which version to download then go with `Komikku-universal.apk` - """, - ) - - print( - json.dumps( - { - "tag_name": version_tag, - "name": f"Komikku {version_display}", - "body": body, - "draft": True, - "prerelease": False, - }, - ), - ) - PY + "${JQ_BIN}" -n \ + --arg tag "${VERSION_TAG}" \ + --arg title "Komikku ${VERSION_DISPLAY}" \ + --arg body "${release_body}" \ + '{tag_name: $tag, name: $title, body: $body, draft: true, prerelease: false}' > "${payload_file}" status="$(curl -sS -o "$response_file" -w '%{http_code}' \ -H "Authorization: token ${FORGEJO_TOKEN}" \ "${api_base}/tags/${VERSION_TAG}")" if [ "$status" = "200" ]; then - release_id="$(python3 -c 'import json, sys; print(json.load(sys.stdin)["id"])' < "$response_file")" + release_id="$("${JQ_BIN}" -r '.id' < "$response_file")" curl -fsS \ -X PATCH \ -H "Authorization: token ${FORGEJO_TOKEN}" \ @@ -354,7 +379,7 @@ jobs: --data @"$payload_file" \ "${api_base}" \ > "$response_file" - release_id="$(python3 -c 'import json, sys; print(json.load(sys.stdin)["id"])' < "$response_file")" + release_id="$("${JQ_BIN}" -r '.id' < "$response_file")" else cat "$response_file" exit 1 @@ -370,6 +395,50 @@ jobs: run: | set -euo pipefail + export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH:-}" + + discover_jq() { + if command -v jq >/dev/null 2>&1; then + JQ_BIN="$(command -v jq)" + export JQ_BIN + return 0 + fi + for cand in /usr/bin/jq /usr/local/bin/jq /bin/jq; do + if [ -x "$cand" ]; then + JQ_BIN="$cand" + export JQ_BIN + return 0 + fi + done + return 1 + } + ensure_jq() { + if discover_jq; then + return 0 + fi + run_as_root() { + if [ "$(id -u)" -eq 0 ]; then + "$@" + elif command -v sudo >/dev/null 2>&1; then + sudo "$@" + else + return 1 + fi + } + if command -v dnf >/dev/null 2>&1; then + run_as_root dnf install -y jq + elif command -v apt-get >/dev/null 2>&1; then + run_as_root apt-get update + run_as_root apt-get install -y jq + elif command -v apk >/dev/null 2>&1; then + run_as_root apk add --no-cache jq + elif command -v pacman >/dev/null 2>&1; then + run_as_root pacman -Sy --noconfirm jq + fi + discover_jq + } + ensure_jq || { echo "jq not found; install jq on the runner (dnf install -y jq)."; exit 1; } + release_api="${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/releases/${RELEASE_ID}" assets_file="$(mktemp)" @@ -389,13 +458,9 @@ jobs: fi asset_name="$(basename "$file")" - asset_id="$( - python3 -c 'import json, sys; assets = json.load(open(sys.argv[1], encoding="utf-8")); name = sys.argv[2]; print(next((str(asset["id"]) for asset in assets if asset.get("name") == name), ""))' \ - "$assets_file" \ - "$asset_name" - )" + asset_id="$("${JQ_BIN}" -r --arg n "${asset_name}" '(.[] | select(.name == $n) | .id) // empty' "$assets_file")" - if [ -n "$asset_id" ]; then + if [ -n "$asset_id" ] && [ "$asset_id" != "null" ]; then curl -fsS \ -X DELETE \ -H "Authorization: token ${FORGEJO_TOKEN}" \ @@ -403,7 +468,7 @@ jobs: > /dev/null fi - encoded_name="$(python3 -c 'import sys, urllib.parse; print(urllib.parse.quote(sys.argv[1]))' "$asset_name")" + encoded_name="$("${JQ_BIN}" -nr --arg s "${asset_name}" '$s | @uri')" curl -fsS \ -X POST \