Changelog: - Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
118 lines
4.9 KiB
Bash
Executable File
118 lines
4.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Mirror the public releases of git.rfa.cz/tth-pub/meshcore-tth to the GitHub
|
|
# fork's Releases (TeTeHacko/MeshCore-TTH), called by pub-release.yml after
|
|
# each release. git.rfa.cz stays the primary source: auto-OTA, the docs and
|
|
# the release notes' links point there, and integrity rests on the signed
|
|
# SHA256SUMS, not on the host.
|
|
#
|
|
# Converges instead of copying "the one new release": every run brings the
|
|
# newest $KEEP releases of Gitea over -- creates what is missing, uploads
|
|
# assets a failed run left out -- so a GitHub outage is mended by the next
|
|
# run. GitHub is the ARCHIVE: Gitea keeps $KEEP, GitHub the newest $GH_KEEP
|
|
# (older ones are deleted, their tags stay), so an older build stays
|
|
# downloadable for a rollback over USB/BLE. Their notes' links to Gitea
|
|
# release pages die with Gitea's pruning; the mirror header links the list.
|
|
#
|
|
# A release is created only once GitHub has its commit (the export pushes it
|
|
# as branch `tth` in tth-upstream-sync.yml); until then it is skipped with a
|
|
# warning and the next run picks it up.
|
|
#
|
|
# GH_TOKEN=... .github/gh-release-mirror.sh # mirror
|
|
# .github/gh-release-mirror.sh --dry-run # print the plan only (no token needed)
|
|
#
|
|
# env: GH_TOKEN (fine-grained, Contents: write on GH_REPO), GITEA (default
|
|
# https://git.rfa.cz), SRC_REPO (tth-pub/meshcore-tth), GH_REPO
|
|
# (TeTeHacko/MeshCore-TTH), KEEP (5, as pub-release.yml), GH_KEEP (30).
|
|
set -euo pipefail
|
|
|
|
case "${1:-}" in
|
|
-h | --help)
|
|
sed -n '2,/^set -euo/p' "$0" | sed '$d; s/^# \{0,1\}//'
|
|
exit 0
|
|
;;
|
|
--dry-run) DRY=1 ;;
|
|
"") DRY=0 ;;
|
|
*)
|
|
echo "usage: $0 [--dry-run]" >&2
|
|
exit 2
|
|
;;
|
|
esac
|
|
|
|
GITEA=${GITEA:-https://git.rfa.cz}
|
|
SRC_REPO=${SRC_REPO:-tth-pub/meshcore-tth}
|
|
GH_REPO=${GH_REPO:-TeTeHacko/MeshCore-TTH}
|
|
KEEP=${KEEP:-5}
|
|
GH_KEEP=${GH_KEEP:-30}
|
|
src="$GITEA/api/v1/repos/$SRC_REPO"
|
|
gh="https://api.github.com/repos/$GH_REPO"
|
|
up="https://uploads.github.com/repos/$GH_REPO"
|
|
if [ "$DRY" = 0 ] && [ -z "${GH_TOKEN:-}" ]; then
|
|
echo "GH_TOKEN not set: nothing mirrored" >&2
|
|
exit 0
|
|
fi
|
|
|
|
# GitHub API call; the token goes through --config on stdin, never argv
|
|
ghapi() {
|
|
local auth=""
|
|
[ -n "${GH_TOKEN:-}" ] && auth="header = \"Authorization: Bearer $GH_TOKEN\""
|
|
curl -sS --retry 3 --config - -H 'Accept: application/vnd.github+json' \
|
|
-H 'X-GitHub-Api-Version: 2022-11-28' "$@" <<<"$auth"
|
|
}
|
|
enc() { jq -rn --arg s "$1" '$s|@uri'; }
|
|
|
|
tmp=$(mktemp -d)
|
|
trap 'rm -rf "$tmp"' EXIT
|
|
|
|
# newest $KEEP of ours on Gitea, oldest first (GitHub orders by creation)
|
|
curl -sSf --retry 3 "$src/releases?limit=50" |
|
|
jq -c --argjson k "$KEEP" 'map(select(.tag_name | startswith("tth-") or test("[+]tth[.]")))
|
|
| sort_by(.created_at) | reverse | .[:$k] | reverse | .[]' >"$tmp/src.jsonl"
|
|
newest=$(tail -1 "$tmp/src.jsonl" | jq -r .tag_name)
|
|
ghapi -f "$gh/releases?per_page=100" | jq -c '.[]' >"$tmp/gh.jsonl"
|
|
|
|
while read -r rel; do
|
|
tag=$(jq -r .tag_name <<<"$rel")
|
|
ghrel=$(jq -c --arg t "$tag" 'select(.tag_name == $t)' "$tmp/gh.jsonl")
|
|
if [ -z "$ghrel" ]; then
|
|
sha=$(curl -sSf --retry 3 "$src/tags/$(enc "$tag")" | jq -r .commit.sha)
|
|
if [ "$(ghapi -o /dev/null -w '%{http_code}' "$gh/commits/$sha")" != 200 ]; then
|
|
echo "::warning::$tag: commit $sha not on GitHub yet, next run"
|
|
continue
|
|
fi
|
|
echo "create $tag ($sha)"
|
|
[ "$DRY" = 1 ] && continue
|
|
body=$(
|
|
echo "> **Zrcadlo** · mirror of $GITEA/$SRC_REPO/releases -- primární zdroj (auto-OTA,"
|
|
echo "> podpisy \`SHA256SUMS\`) je git.rfa.cz; tam drží posledních $KEEP vydání, tady $GH_KEEP."
|
|
echo
|
|
jq -r .body <<<"$rel"
|
|
)
|
|
latest=false
|
|
[ "$tag" = "$newest" ] && latest=true
|
|
jq -n --arg t "$tag" --arg c "$sha" --arg n "$(jq -r .name <<<"$rel")" --arg b "$body" --arg l "$latest" \
|
|
'{tag_name: $t, target_commitish: $c, name: $n, body: $b, make_latest: $l}' >"$tmp/new.json"
|
|
ghrel=$(ghapi -f -X POST -H 'Content-Type: application/json' --data-binary "@$tmp/new.json" "$gh/releases")
|
|
fi
|
|
id=$(jq -r .id <<<"$ghrel")
|
|
have=$(jq -r '.assets[]? | (.label // "" | select(. != "")) // .name' <<<"$ghrel" | sort)
|
|
n=0
|
|
while read -r name url; do
|
|
grep -qxF "$name" <<<"$have" && continue
|
|
n=$((n + 1))
|
|
[ "$DRY" = 1 ] && continue
|
|
curl -sSfL --retry 3 -o "$tmp/asset" "$url"
|
|
ghapi -f -X POST -H 'Content-Type: application/octet-stream' --data-binary "@$tmp/asset" \
|
|
"$up/releases/$id/assets?name=$(enc "$name")&label=$(enc "$name")" >/dev/null
|
|
done < <(jq -r '.assets[] | "\(.name) \(.browser_download_url)"' <<<"$rel")
|
|
[ "$n" = 0 ] || echo "$tag: $n asset(s) $([ "$DRY" = 1 ] && echo "to upload" || echo uploaded)"
|
|
done <"$tmp/src.jsonl"
|
|
|
|
# the archive keeps the newest $GH_KEEP (the tags stay, like the export's)
|
|
[ "$DRY" = 1 ] || ghapi -f "$gh/releases?per_page=100" | jq -c '.[]' >"$tmp/gh.jsonl"
|
|
jq -sr --argjson k "$GH_KEEP" 'map(select(.tag_name | startswith("tth-") or test("[+]tth[.]")))
|
|
| sort_by(.created_at) | reverse | .[$k:][] | "\(.id) \(.tag_name)"' "$tmp/gh.jsonl" |
|
|
while read -r id tag; do
|
|
echo "prune $tag"
|
|
[ "$DRY" = 1 ] || ghapi -f -X DELETE "$gh/releases/$id" >/dev/null
|
|
done
|