#!/usr/bin/env bash # vendored from sow-platform/deploy/scripts/promote-gitea.sh — keep in sync # Channel pointer for sow-tools binary releases (service tier). # channels.json values are version tags (vX.Y.Z), not SHAs. set -euo pipefail : "${GITEA_SERVER:?set GITEA_SERVER}" : "${GITEA_TOKEN:?set GITEA_TOKEN}" : "${GITEA_OWNER:?set GITEA_OWNER}" : "${GITEA_REPO:?set GITEA_REPO}" STREAM_NAME=sow-tools STREAM_TIER=service DRY_RUN="${DRY_RUN:-0}" need() { command -v "$1" >/dev/null 2>&1 || { printf 'need: %s not found\n' "$1" >&2; exit 1; }; } need jq curl die() { printf '[channels] ERROR: %s\n' "$*" >&2; exit 1; } log() { printf '[channels] %s\n' "$*" >&2; } require_release() { local tag="$1" status status="$(curl -fsS -o /dev/null -w "%{http_code}" \ -H "Authorization: token ${GITEA_TOKEN}" \ "${GITEA_SERVER}/api/v1/repos/${GITEA_REPO}/releases/tags/${tag}" \ 2>/dev/null || echo "000")" [[ "$status" == "200" ]] || die "no Gitea release for tag ${tag} (HTTP $status)" } _auth="Authorization: token ${GITEA_TOKEN}" _pkg_url="${GITEA_SERVER}/api/packages/${GITEA_OWNER}/generic/deployment-channels/${STREAM_NAME}/channels.json" _pkg_del="${GITEA_SERVER}/api/v1/packages/${GITEA_OWNER}/generic/deployment-channels/${STREAM_NAME}" valid_channel() { case "$1" in current|previous) return 0 ;; *) return 1 ;; esac; } read_channels() { local tmp status tmp="$(mktemp)" status="$(curl -fsS -o "$tmp" -w "%{http_code}" -H "$_auth" "$_pkg_url" 2>/dev/null || echo "000")" case "$status" in 200) jq -e . "$tmp" >/dev/null 2>&1 || die "invalid channels.json"; cat "$tmp" ;; 404|000) printf '{}\n' ;; *) die "read channels.json: HTTP $status" ;; esac rm -f "$tmp" } write_channels() { local json="$1" tmp jq -e . <<<"$json" >/dev/null 2>&1 || die "refusing to write invalid channels.json" tmp="$(mktemp)"; jq -S . <<<"$json" >"$tmp" if [[ "$DRY_RUN" == "1" ]]; then log "DRY_RUN: would write:"; jq -S . "$tmp" >&2; rm -f "$tmp"; return 0 fi curl -fsS -X DELETE -H "$_auth" "$_pkg_del" >/dev/null 2>&1 || true curl -fsS -X PUT -H "$_auth" -H "Content-Type: application/octet-stream" \ --data-binary "@$tmp" "$_pkg_url" >/dev/null || die "write failed" rm -f "$tmp" log "deployment-channels/sow-tools updated"; jq -S . <<<"$json" >&2 } cmd="${1:-show}" case "$cmd" in show) read_channels | jq -S . ;; set) channel="${2:?}"; tag="${3:?}" valid_channel "$channel" || die "unknown channel: $channel" require_release "$tag" write_channels "$(read_channels | jq --arg c "$channel" --arg t "$tag" '.[$c]=$t')" ;; release) tag="${2:?}" require_release "$tag" write_channels "$(read_channels | jq --arg t "$tag" ' (if .current then .previous = .current else . end) | .current = $t')" ;; *) die "usage: promote show | set | release " ;; esac