114 lines
3.1 KiB
Bash
114 lines
3.1 KiB
Bash
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
archive_path="${1:-relaytv.zip}"
|
|
notes_path="${2:-RELEASE_NOTES.md}"
|
|
|
|
for variable_name in GITEA_SERVER_URL GITEA_REPOSITORY GITEA_REF_NAME GITEA_TOKEN; do
|
|
if [[ -z "${!variable_name:-}" ]]; then
|
|
echo "Required environment variable is not set: ${variable_name}" >&2
|
|
exit 1
|
|
fi
|
|
done
|
|
|
|
if [[ ! -f "$archive_path" ]]; then
|
|
echo "Release archive does not exist: $archive_path" >&2
|
|
exit 1
|
|
fi
|
|
|
|
if [[ ! -f "$notes_path" ]]; then
|
|
echo "Release notes do not exist: $notes_path" >&2
|
|
exit 1
|
|
fi
|
|
|
|
api_base="${GITEA_SERVER_URL%/}/api/v1/repos/${GITEA_REPOSITORY}/releases"
|
|
asset_name="${archive_path##*/}"
|
|
authorization_header="Authorization: token ${GITEA_TOKEN}"
|
|
|
|
release_payload="$({
|
|
RELEASE_NOTES_PATH="$notes_path" python3 - <<'PY'
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
|
|
tag = os.environ["GITEA_REF_NAME"]
|
|
notes = Path(os.environ["RELEASE_NOTES_PATH"]).read_text(encoding="utf-8")
|
|
print(json.dumps({
|
|
"body": notes,
|
|
"draft": False,
|
|
"name": tag,
|
|
"prerelease": False,
|
|
"tag_name": tag,
|
|
}))
|
|
PY
|
|
})"
|
|
|
|
lookup_response="$(
|
|
curl --silent --show-error \
|
|
--header "$authorization_header" \
|
|
--output - \
|
|
--write-out $'\n%{http_code}' \
|
|
"$api_base/tags/$GITEA_REF_NAME"
|
|
)"
|
|
lookup_status="${lookup_response##*$'\n'}"
|
|
lookup_body="${lookup_response%$'\n'*}"
|
|
|
|
case "$lookup_status" in
|
|
200)
|
|
release_id="$(python3 -c 'import json, sys; print(json.load(sys.stdin)["id"])' <<<"$lookup_body")"
|
|
curl --fail-with-body --silent --show-error \
|
|
--request PATCH \
|
|
--header "$authorization_header" \
|
|
--header "Content-Type: application/json" \
|
|
--data "$release_payload" \
|
|
"$api_base/$release_id" >/dev/null
|
|
;;
|
|
404)
|
|
release_response="$(
|
|
curl --fail-with-body --silent --show-error \
|
|
--request POST \
|
|
--header "$authorization_header" \
|
|
--header "Content-Type: application/json" \
|
|
--data "$release_payload" \
|
|
"$api_base"
|
|
)"
|
|
release_id="$(python3 -c 'import json, sys; print(json.load(sys.stdin)["id"])' <<<"$release_response")"
|
|
;;
|
|
*)
|
|
echo "Could not look up Gitea release for $GITEA_REF_NAME (HTTP $lookup_status)." >&2
|
|
echo "$lookup_body" >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
asset_id="$({
|
|
ASSET_NAME="$asset_name" python3 -c '
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
assets = json.load(sys.stdin)
|
|
match = next((asset for asset in assets if asset["name"] == os.environ["ASSET_NAME"]), None)
|
|
print(match["id"] if match else "")
|
|
' < <(
|
|
curl --fail-with-body --silent --show-error \
|
|
--header "$authorization_header" \
|
|
"$api_base/$release_id/assets"
|
|
)
|
|
})"
|
|
|
|
if [[ -n "$asset_id" ]]; then
|
|
curl --fail-with-body --silent --show-error \
|
|
--request DELETE \
|
|
--header "$authorization_header" \
|
|
"$api_base/$release_id/assets/$asset_id" >/dev/null
|
|
fi
|
|
|
|
curl --fail-with-body --silent --show-error \
|
|
--request POST \
|
|
--header "$authorization_header" \
|
|
--form "attachment=@${archive_path};filename=${asset_name}" \
|
|
"$api_base/$release_id/assets?name=$asset_name" >/dev/null
|
|
|
|
echo "Published $asset_name to Gitea release $GITEA_REF_NAME."
|