CI: use jq for Forgejo release API (no python3)
Some checks failed
Forgejo Release Builder / release (push) Failing after 17m13s
Some checks failed
Forgejo Release Builder / release (push) Failing after 17m13s
This commit is contained in:
parent
4b1b0e8cc4
commit
52036476a2
1 changed files with 126 additions and 61 deletions
|
|
@ -273,72 +273,97 @@ jobs:
|
||||||
run: |
|
run: |
|
||||||
set -euo pipefail
|
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"
|
api_base="${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/releases"
|
||||||
payload_file="$(mktemp)"
|
payload_file="$(mktemp)"
|
||||||
response_file="$(mktemp)"
|
response_file="$(mktemp)"
|
||||||
|
|
||||||
python3 - <<'PY' > "$payload_file"
|
commit_logs="${COMMIT_LOGS:-}"
|
||||||
import json
|
commit_logs="$(printf '%s' "$commit_logs" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')"
|
||||||
import os
|
if [ -z "$commit_logs" ]; then
|
||||||
import textwrap
|
commit_logs="- Initial release"
|
||||||
|
fi
|
||||||
|
|
||||||
version_tag = os.environ["VERSION_TAG"]
|
full_changelog=""
|
||||||
version_display = os.environ.get("VERSION_DISPLAY", version_tag)
|
if [ -n "${PREV_TAG_NAME:-}" ]; then
|
||||||
previous_tag = os.environ.get("PREV_TAG_NAME", "").strip()
|
full_changelog="**Full Changelog**: [${REPOSITORY_NAME}@${PREV_TAG_NAME}...${VERSION_TAG}](${REPO_URL}/compare/${PREV_TAG_NAME}...${VERSION_TAG})"
|
||||||
commit_logs = os.environ.get("COMMIT_LOGS", "").strip() or "- Initial release"
|
fi
|
||||||
repo_url = os.environ["REPO_URL"]
|
|
||||||
repository_name = os.environ["REPOSITORY_NAME"]
|
|
||||||
|
|
||||||
if previous_tag:
|
release_body="$(printf '%s\n' \
|
||||||
full_changelog = (
|
"#### What's Changed" \
|
||||||
f"**Full Changelog**: "
|
"" \
|
||||||
f"[{repository_name}@{previous_tag}...{version_tag}]"
|
"##### New" \
|
||||||
f"({repo_url}/compare/{previous_tag}...{version_tag})"
|
"" \
|
||||||
)
|
"##### Improve" \
|
||||||
else:
|
"" \
|
||||||
full_changelog = ""
|
"##### 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(
|
"${JQ_BIN}" -n \
|
||||||
f"""\
|
--arg tag "${VERSION_TAG}" \
|
||||||
#### What's Changed
|
--arg title "Komikku ${VERSION_DISPLAY}" \
|
||||||
##### New
|
--arg body "${release_body}" \
|
||||||
|
'{tag_name: $tag, name: $title, body: $body, draft: true, prerelease: false}' > "${payload_file}"
|
||||||
##### 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
|
|
||||||
|
|
||||||
status="$(curl -sS -o "$response_file" -w '%{http_code}' \
|
status="$(curl -sS -o "$response_file" -w '%{http_code}' \
|
||||||
-H "Authorization: token ${FORGEJO_TOKEN}" \
|
-H "Authorization: token ${FORGEJO_TOKEN}" \
|
||||||
"${api_base}/tags/${VERSION_TAG}")"
|
"${api_base}/tags/${VERSION_TAG}")"
|
||||||
|
|
||||||
if [ "$status" = "200" ]; then
|
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 \
|
curl -fsS \
|
||||||
-X PATCH \
|
-X PATCH \
|
||||||
-H "Authorization: token ${FORGEJO_TOKEN}" \
|
-H "Authorization: token ${FORGEJO_TOKEN}" \
|
||||||
|
|
@ -354,7 +379,7 @@ jobs:
|
||||||
--data @"$payload_file" \
|
--data @"$payload_file" \
|
||||||
"${api_base}" \
|
"${api_base}" \
|
||||||
> "$response_file"
|
> "$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
|
else
|
||||||
cat "$response_file"
|
cat "$response_file"
|
||||||
exit 1
|
exit 1
|
||||||
|
|
@ -370,6 +395,50 @@ jobs:
|
||||||
run: |
|
run: |
|
||||||
set -euo pipefail
|
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}"
|
release_api="${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/releases/${RELEASE_ID}"
|
||||||
assets_file="$(mktemp)"
|
assets_file="$(mktemp)"
|
||||||
|
|
||||||
|
|
@ -389,13 +458,9 @@ jobs:
|
||||||
fi
|
fi
|
||||||
|
|
||||||
asset_name="$(basename "$file")"
|
asset_name="$(basename "$file")"
|
||||||
asset_id="$(
|
asset_id="$("${JQ_BIN}" -r --arg n "${asset_name}" '(.[] | select(.name == $n) | .id) // empty' "$assets_file")"
|
||||||
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"
|
|
||||||
)"
|
|
||||||
|
|
||||||
if [ -n "$asset_id" ]; then
|
if [ -n "$asset_id" ] && [ "$asset_id" != "null" ]; then
|
||||||
curl -fsS \
|
curl -fsS \
|
||||||
-X DELETE \
|
-X DELETE \
|
||||||
-H "Authorization: token ${FORGEJO_TOKEN}" \
|
-H "Authorization: token ${FORGEJO_TOKEN}" \
|
||||||
|
|
@ -403,7 +468,7 @@ jobs:
|
||||||
> /dev/null
|
> /dev/null
|
||||||
fi
|
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 \
|
curl -fsS \
|
||||||
-X POST \
|
-X POST \
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue