#!/usr/bin/env bash set -e set +u # Load .env if [ -f "$(dirname "$0")/../.env" ]; then # shellcheck disable=SC1091 . "$(dirname "$0")/../.env" fi API_URL=${GITEA_API_URL:-"https://gitea.elpatron.me/api/v1"} OWNER=${GITEA_OWNER:-"elpatron"} REPO=${GITEA_REPO:-"octo-funnel"} TOKEN=${GITEA_API_TOKEN:-""} TAG=${1:-v0.1.6.1} ASSET_PATH=${2:-"$(pwd)/octoprint_tailscale_funnel/dist/OctoPrint-Tailscale-Funnel-0.1.6.1.zip"} if [ -z "$TOKEN" ]; then echo "GITEA_API_TOKEN not set" >&2 exit 1 fi if [ ! -f "$ASSET_PATH" ]; then echo "Asset not found: $ASSET_PATH" >&2 exit 1 fi ASSET_NAME=$(basename "$ASSET_PATH") # Get release by tag REL_JSON=$(curl -sS -H "Authorization: token $TOKEN" "$API_URL/repos/$OWNER/$REPO/releases/tags/$TAG" || true) REL_ID=$(python3 - <<'PY' import sys, json s=sys.stdin.read().strip() try: d=json.loads(s) if s else {} print(d.get('id','')) except Exception: print('') PY <<<"$REL_JSON") # Fallback list search if [ -z "$REL_ID" ]; then LIST=$(curl -sS -H "Authorization: token $TOKEN" "$API_URL/repos/$OWNER/$REPO/releases?limit=100") REL_ID=$(TAG="$TAG" python3 - <<'PY' import os, sys, json tag=os.environ['TAG'] try: arr=json.loads(sys.stdin.read()) for r in arr: if r.get('tag_name')==tag: print(r.get('id','')) break except Exception: print('') PY <<<"$LIST") fi # Create release if still missing if [ -z "$REL_ID" ]; then PAYLOAD=$(TAG="$TAG" python3 - <<'PY' import json, os print(json.dumps({ 'tag_name': os.environ['TAG'], 'name': os.environ['TAG'], 'body': 'Automated release' })) PY ) CREATE=$(curl -sS -X POST -H 'Content-Type: application/json' -H "Authorization: token $TOKEN" -d "$PAYLOAD" "$API_URL/repos/$OWNER/$REPO/releases") REL_ID=$(python3 - <<'PY' import sys, json try: d=json.loads(sys.stdin.read()) print(d.get('id','')) except Exception: print('') PY <<<"$CREATE") fi if [ -z "$REL_ID" ]; then echo "Failed to resolve release id for tag $TAG" >&2 exit 1 fi # Check existing assets ASSETS=$(curl -sS -H "Authorization: token $TOKEN" "$API_URL/repos/$OWNER/$REPO/releases/$REL_ID/assets") HAS=$(NAME="$ASSET_NAME" python3 - <<'PY' import os, sys, json name=os.environ['NAME'] try: arr=json.loads(sys.stdin.read()) print(any(a.get('name')==name for a in arr)) except Exception: print(False) PY <<<"$ASSETS") if [ "$HAS" = "True" ]; then echo "Asset already exists: $ASSET_NAME" else echo "Uploading asset: $ASSET_NAME" curl -sS -X POST -H "Authorization: token $TOKEN" -H "Content-Type: application/octet-stream" \ --data-binary @"$ASSET_PATH" \ "$API_URL/repos/$OWNER/$REPO/releases/$REL_ID/assets?name=$ASSET_NAME" >/dev/null echo "Upload done." fi DL_URL="https://gitea.elpatron.me/$OWNER/$REPO/releases/download/$TAG/$ASSET_NAME" CODE=$(curl -s -o /dev/null -w '%{http_code}' "$DL_URL") echo "DOWNLOAD_URL=$DL_URL" echo "HTTP_STATUS=$CODE"