added fallback to github api

This commit is contained in:
Jonas8272 committed 2026-11-20 19:37:12 +00:00
1 parent 8b235d49e8
commit 65819cb2e1
1 file changed
+35 -6
+35 -6
View File
@@ -4,7 +4,16 @@ set -euo pipefail
M=bucket/mornye.json
REPO=mornyeapp/Mornye-Desktop
tag=$(curl -fsSL "https://api.github.com/repos/$REPO/releases/latest" | jq -r .tag_name)
# Optional GitHub token (read-only, public repos) to lift the 60 requests/hour
# unauthenticated limit. Named GH_API_TOKEN on purpose: CI systems often set
# GITHUB_TOKEN to their own token, which api.github.com would reject.
auth=()
if [ -n "${GH_API_TOKEN:-}" ]; then
auth=(-H "Authorization: Bearer $GH_API_TOKEN")
fi
rel=$(curl -fsSL ${auth[@]+"${auth[@]}"} "https://api.github.com/repos/$REPO/releases/latest")
tag=$(jq -r .tag_name <<<"$rel")
ver=${tag#v}
cur=$(jq -r .version "$M")
@@ -14,17 +23,37 @@ if [ "$ver" = "$cur" ]; then
fi
base="https://github.com/$REPO/releases/download/$tag"
sums=$(curl -fsSL "$base/SHA256SUMS.txt" | tr -d '\r')
# Primary source: SHA256SUMS.txt, normalised to "<hash> <filename>" lines.
# A missing file is not fatal; we fall back to the API digests below.
sums=$(curl -fsSL "$base/SHA256SUMS.txt" | tr -d '\r' \
| awk '{n=$2; sub(/^\*/,"",n); print $1, n}') || sums=""
# Fallback source: the SHA-256 digest GitHub records for each release asset,
# taken from the release JSON we already fetched (no extra API call).
api_sums=$(jq -r '.assets[] | select(.digest != null)
| "\(.digest | sub("^sha256:"; "")) \(.name)"' <<<"$rel")
pick() { printf '%s\n' "$2" | grep -E "$1" || true; }
# Print "<hash> <filename>" for the one file matching the pattern, else fail.
# Tries SHA256SUMS.txt first and only falls back if it has no match at all.
find_asset() {
local m
m=$(echo "$sums" | awk '{n=$2; sub(/^\*/,"",n); print $1, n}' | grep -E "$1" || true)
if [ "$(echo "$m" | grep -c .)" -ne 1 ]; then
echo "expected exactly one match for /$1/ in SHA256SUMS.txt for $tag, got:" >&2
local pat=$1 m
m=$(pick "$pat" "$sums")
if [ -z "$m" ]; then
echo "no /$pat/ in SHA256SUMS.txt for $tag, trying GitHub API digests" >&2
m=$(pick "$pat" "$api_sums")
fi
if [ "$(printf '%s\n' "$m" | grep -c .)" -ne 1 ]; then
echo "expected exactly one match for /$pat/ for $tag, got:" >&2
echo "$m" >&2
exit 1
fi
if ! [[ ${m%% *} =~ ^[0-9a-fA-F]{64}$ ]]; then
echo "not a valid SHA-256 for /$pat/: $m" >&2
exit 1
fi
echo "$m"
}