104 lines
3.1 KiB
Bash
104 lines
3.1 KiB
Bash
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
: "${FILE:?FILE is required}"
|
|
: "${LATEST_RELEASE_URL:?LATEST_RELEASE_URL is required}"
|
|
: "${DOWNLOAD_URL_TEMPLATE:?DOWNLOAD_URL_TEMPLATE is required}"
|
|
: "${RELEASE_API_REPO:?RELEASE_API_REPO is required}"
|
|
|
|
if command -v python >/dev/null 2>&1; then
|
|
PYTHON_BIN=python
|
|
elif command -v python3 >/dev/null 2>&1; then
|
|
PYTHON_BIN=python3
|
|
else
|
|
echo "python is required but was not found"
|
|
exit 1
|
|
fi
|
|
|
|
version_strip_prefix="${LATEST_VERSION_STRIP_PREFIX:-v}"
|
|
release_tag_template="${RELEASE_TAG_TEMPLATE:-{version}}"
|
|
release_tag_template="${release_tag_template//$'\r'/}"
|
|
|
|
current_version=$($PYTHON_BIN - <<'PY'
|
|
import re
|
|
import os
|
|
p=os.environ['FILE']
|
|
s=open(p).read()
|
|
m=re.search(r'version\s*=\s*"([^"]+)";', s)
|
|
print(m.group(1) if m else "")
|
|
PY
|
|
)
|
|
|
|
latest_version=$(curl -fsSLI -o /dev/null -w '%{url_effective}' "$LATEST_RELEASE_URL" \
|
|
| sed -E 's#.*/##')
|
|
|
|
if [ -n "$version_strip_prefix" ]; then
|
|
latest_version="${latest_version#${version_strip_prefix}}"
|
|
fi
|
|
|
|
echo "current=$current_version"
|
|
echo "latest=$latest_version"
|
|
|
|
if [ -z "$latest_version" ] || [ "$latest_version" = "$current_version" ]; then
|
|
echo "updated=false" >> "$GITHUB_OUTPUT"
|
|
exit 0
|
|
fi
|
|
|
|
download_url="${DOWNLOAD_URL_TEMPLATE//\{version\}/$latest_version}"
|
|
new_hash=$(nix store prefetch-file --json "$download_url" | "$PYTHON_BIN" -c 'import json,sys; print(json.load(sys.stdin)["hash"])')
|
|
|
|
export LATEST_VERSION="$latest_version"
|
|
export NEW_HASH="$new_hash"
|
|
|
|
"$PYTHON_BIN" - <<'PY'
|
|
import os
|
|
import re
|
|
p=os.environ['FILE']
|
|
s=open(p).read()
|
|
s=re.sub(r'version\s*=\s*"[^"]+"', f'version = "{os.environ["LATEST_VERSION"]}"', s, count=1)
|
|
s=re.sub(r'hash\s*=\s*"[^"]+"', f'hash = "{os.environ["NEW_HASH"]}"', s, count=1)
|
|
open(p,'w').write(s)
|
|
PY
|
|
|
|
echo "updated=true" >> "$GITHUB_OUTPUT"
|
|
echo "version=$latest_version" >> "$GITHUB_OUTPUT"
|
|
echo "previous_version=$current_version" >> "$GITHUB_OUTPUT"
|
|
|
|
release_tag="${release_tag_template//\{version\}/$latest_version}"
|
|
release_tag="${release_tag#\{}"
|
|
release_tag="${release_tag%\}}"
|
|
release_tag="${release_tag#\'}"
|
|
release_tag="${release_tag%\'}"
|
|
|
|
api_url="https://api.github.com/repos/${RELEASE_API_REPO}/releases/tags/${release_tag}"
|
|
|
|
curl_headers=(
|
|
-H "Accept: application/vnd.github+json"
|
|
-H "X-GitHub-Api-Version: 2022-11-28"
|
|
)
|
|
if [ -n "${GITHUB_TOKEN:-}" ]; then
|
|
curl_headers+=( -H "Authorization: Bearer ${GITHUB_TOKEN}" )
|
|
fi
|
|
|
|
api_response=$(curl -sS -w '\n%{http_code}' "${curl_headers[@]}" "$api_url" || true)
|
|
api_body=$(printf '%s\n' "$api_response" | sed '$d')
|
|
api_code=$(printf '%s\n' "$api_response" | tail -n1)
|
|
|
|
release_notes=""
|
|
if [ "$api_code" = "200" ]; then
|
|
release_notes=$(printf '%s' "$api_body" | "$PYTHON_BIN" -c 'import json,sys; d=json.load(sys.stdin); print((d.get("body") or "").strip())' || true)
|
|
else
|
|
echo "warning: failed to fetch release notes from GitHub API (status=$api_code, url=$api_url)"
|
|
fi
|
|
|
|
if [ -z "$release_notes" ]; then
|
|
release_notes="_No changelog found in upstream release notes. Check ${LATEST_RELEASE_URL%/latest}/tag/${release_tag}._"
|
|
fi
|
|
|
|
delimiter="CHANGELOG_$(date +%s%N)"
|
|
{
|
|
echo "changelog<<${delimiter}"
|
|
printf '%s\n' "$release_notes"
|
|
echo "${delimiter}"
|
|
} >> "$GITHUB_OUTPUT"
|