Files
gitea-cli/scripts/meta.sh
timmy faee15bc78 Initial commit
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 13:04:09 +08:00

73 lines
2.3 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=lib/common.sh
source "${SCRIPT_DIR}/lib/common.sh"
usage() {
cat >&2 <<'EOF'
Usage: meta.sh <repo> [--desc "description"] [--topics "a,b,c"]
--desc 設定 repo description
--topics 覆蓋式設定 topic list逗號分隔
EOF
exit 1
}
[[ $# -ge 1 ]] || usage
REPO=""
DESC=""
HAS_DESC=0
TOPICS_CSV=""
HAS_TOPICS=0
while [[ $# -gt 0 ]]; do
case "$1" in
--desc)
[[ $# -ge 2 ]] || { log_err "--desc 需要一個值"; exit 1; }
DESC="$2"; HAS_DESC=1; shift 2 ;;
--topics)
[[ $# -ge 2 ]] || { log_err "--topics 需要一個值"; exit 1; }
TOPICS_CSV="$2"; HAS_TOPICS=1; shift 2 ;;
-h|--help) usage ;;
-*) log_err "未知參數:$1"; usage ;;
*)
if [[ -z "$REPO" ]]; then REPO="$1"; else log_err "多餘參數:$1"; usage; fi
shift ;;
esac
done
[[ -n "$REPO" ]] || usage
[[ $HAS_DESC -eq 1 || $HAS_TOPICS -eq 1 ]] || { log_err "必須至少提供 --desc 或 --topics"; usage; }
load_config
require_cmd jq
if [[ $HAS_DESC -eq 1 ]]; then
ESCAPED_DESC="$(printf '%s' "$DESC" | jq -Rs .)"
log_info "更新 description"
gitea_api PATCH "/repos/${GITEA_USER}/${REPO}" "{\"description\":${ESCAPED_DESC}}"
case "$HTTP_CODE" in
200|201) log_ok "description 已更新" ;;
404) log_err "找不到 repo${GITEA_USER}/${REPO}"; exit 1 ;;
*) log_err "更新 description 失敗 (HTTP ${HTTP_CODE})"; { cat "$GITEA_API_RESP_FILE"; echo ""; } >&2; exit 1 ;;
esac
fi
if [[ $HAS_TOPICS -eq 1 ]]; then
TOPICS_JSON="$(printf '%s' "$TOPICS_CSV" | jq -R 'split(",") | map(select(length > 0))')"
log_info "覆蓋 topics${TOPICS_JSON}"
gitea_api PUT "/repos/${GITEA_USER}/${REPO}/topics" "{\"topics\":${TOPICS_JSON}}"
case "$HTTP_CODE" in
200|204) log_ok "topics 已更新" ;;
404) log_err "找不到 repo${GITEA_USER}/${REPO}"; exit 1 ;;
422) log_err "topics 格式錯誤"; { cat "$GITEA_API_RESP_FILE"; echo ""; } >&2; exit 1 ;;
*) log_err "更新 topics 失敗 (HTTP ${HTTP_CODE})"; { cat "$GITEA_API_RESP_FILE"; echo ""; } >&2; exit 1 ;;
esac
fi
log_done "完成"
echo "${GITEA_URL}/${GITEA_USER}/${REPO}"