#!/usr/bin/env bash # ============================================================================= # Pubblica una release su Forgejo tramite API REST e carica i file di release/. # Alternativa manuale (o step di riserva) all'action forgejo-release del workflow. # # Uso: # FORGEJO_URL=https://forgejo.example.it \ # FORGEJO_REPO=proprietario/repository \ # FORGEJO_TOKEN= \ # scripts/release-upload.sh v1.0.0 [cartella] # cartella predefinita: release # # Prima eseguire `npm run package` con RELEASE_VERSION=v1.0.0. Il tag deve già esistere # sul server (git push origin v1.0.0). Le note della release sono prese da CHANGELOG.md. # Se la release esiste già, i file con lo stesso nome vengono sostituiti. # ============================================================================= set -euo pipefail TAG="${1:-${RELEASE_VERSION:-}}" DIR="${2:-release}" die() { echo "✘ $*" >&2; exit 1; } [ -n "$TAG" ] || die "indicare il tag, es.: scripts/release-upload.sh v1.0.0" [ -n "${FORGEJO_URL:-}" ] || die "variabile FORGEJO_URL mancante (es. https://forgejo.example.it)" [ -n "${FORGEJO_REPO:-}" ] || die "variabile FORGEJO_REPO mancante (es. proprietario/repository)" [ -n "${FORGEJO_TOKEN:-}" ] || die "variabile FORGEJO_TOKEN mancante" [ -d "$DIR" ] || die "cartella $DIR assente: eseguire prima npm run package" command -v curl >/dev/null || die "curl non installato" command -v node >/dev/null || die "node non installato" API="${FORGEJO_URL%/}/api/v1/repos/${FORGEJO_REPO}" AUTH="Authorization: token ${FORGEJO_TOKEN}" json_field() { node -e 'let s="";process.stdin.on("data",d=>s+=d).on("end",()=>{const v=JSON.parse(s)[process.argv[1]];if(v===undefined)process.exit(1);console.log(typeof v==="object"?JSON.stringify(v):v)})' "$1"; } NOTES="$(node scripts/changelog-notes.mjs "$TAG")" PAYLOAD="$(node -e 'console.log(JSON.stringify({tag_name:process.argv[1],name:process.argv[1],body:process.argv[2],draft:false,prerelease:/-/.test(process.argv[1])}))' "$TAG" "$NOTES")" echo "→ Release $TAG su $FORGEJO_REPO" if EXISTING="$(curl -fsS -H "$AUTH" "$API/releases/tags/$TAG" 2>/dev/null)"; then RELEASE_ID="$(printf '%s' "$EXISTING" | json_field id)" echo " release già esistente (id $RELEASE_ID): aggiornamento dei file" else CREATED="$(curl -fsS -X POST -H "$AUTH" -H 'Content-Type: application/json' -d "$PAYLOAD" "$API/releases")" \ || die "creazione della release non riuscita (permessi del token? il tag esiste sul server?)" RELEASE_ID="$(printf '%s' "$CREATED" | json_field id)" echo " release creata (id $RELEASE_ID)" fi ASSETS="$(curl -fsS -H "$AUTH" "$API/releases/$RELEASE_ID/assets")" for FILE in "$DIR"/*; do [ -f "$FILE" ] || continue NAME="$(basename "$FILE")" OLD_ID="$(printf '%s' "$ASSETS" | node -e 'let s="";process.stdin.on("data",d=>s+=d).on("end",()=>{const a=JSON.parse(s).find(x=>x.name===process.argv[1]);if(a)console.log(a.id)})' "$NAME")" if [ -n "$OLD_ID" ]; then curl -fsS -X DELETE -H "$AUTH" "$API/releases/$RELEASE_ID/assets/$OLD_ID" >/dev/null fi curl -fsS -X POST -H "$AUTH" -F "attachment=@${FILE}" \ "$API/releases/$RELEASE_ID/assets?name=$(node -e 'console.log(encodeURIComponent(process.argv[1]))' "$NAME")" >/dev/null \ || die "caricamento di $NAME non riuscito" echo " ✔ $NAME" done echo "✔ Release $TAG pubblicata: ${FORGEJO_URL%/}/${FORGEJO_REPO}/releases/tag/$TAG"