Files
gitea-migration/phase5_teardown.sh

127 lines
3.1 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# =============================================================================
# phase5_teardown.sh — Remove .gitea/workflows/ from all repos
# Clones each repo, removes the .gitea/workflows/ directory, commits + pushes.
# Safe to run if .gitea/workflows/ doesn't exist (skips with info message).
# =============================================================================
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
source "${SCRIPT_DIR}/lib/common.sh"
# Parse arguments
AUTO_YES=false
for arg in "$@"; do
case "$arg" in
--yes|-y) AUTO_YES=true ;;
--help|-h)
cat <<EOF
Usage: $(basename "$0") [options]
Options:
--yes, -y Skip all confirmation prompts
--help, -h Show this help
EOF
exit 0
;;
*)
log_error "Unknown argument: $arg"
exit 1
;;
esac
done
confirm_action() {
local prompt="$1"
if [[ "$AUTO_YES" == "true" ]]; then
log_info "Auto-confirmed (--yes): ${prompt}"
return 0
fi
printf '%s' "$prompt"
read -r confirm
[[ "$confirm" =~ ^[Yy]$ ]]
}
load_env
require_vars GITEA_ADMIN_TOKEN GITEA_INTERNAL_URL GITEA_ORG_NAME \
GITEA_ADMIN_USER \
REPO_NAMES
log_warn "=== Phase 5 Teardown: Remove Gitea Workflows ==="
read -ra REPOS <<< "$REPO_NAMES"
TEMP_BASE="/tmp/gitea-migration-teardown"
GITEA_BASE_URL="${GITEA_INTERNAL_URL%/}"
ASKPASS_SCRIPT=""
cleanup() {
rm -rf "$TEMP_BASE"
if [[ -n "$ASKPASS_SCRIPT" ]]; then
rm -f "$ASKPASS_SCRIPT"
fi
}
trap cleanup EXIT
setup_git_auth() {
ASKPASS_SCRIPT=$(mktemp)
cat > "$ASKPASS_SCRIPT" <<'EOF'
#!/usr/bin/env sh
case "$1" in
*sername*) printf '%s\n' "$GITEA_GIT_USERNAME" ;;
*assword*) printf '%s\n' "$GITEA_GIT_TOKEN" ;;
*) printf '\n' ;;
esac
EOF
chmod 700 "$ASKPASS_SCRIPT"
}
git_with_auth() {
GIT_TERMINAL_PROMPT=0 \
GIT_ASKPASS="$ASKPASS_SCRIPT" \
GITEA_GIT_USERNAME="$GITEA_ADMIN_USER" \
GITEA_GIT_TOKEN="$GITEA_ADMIN_TOKEN" \
"$@"
}
if ! confirm_action 'This will remove .gitea/workflows/ from all repos. Continue? [y/N] '; then
log_info "Teardown cancelled"
exit 0
fi
setup_git_auth
for repo in "${REPOS[@]}"; do
log_info "--- Processing: ${repo} ---"
# Check if .gitea/workflows/ exists before cloning (avoid unnecessary work)
if ! gitea_api GET "/repos/${GITEA_ORG_NAME}/${repo}/contents/.gitea/workflows" >/dev/null 2>&1; then
log_info "No .gitea/workflows/ in ${repo} — already clean"
continue
fi
# Clone, remove, commit, push
CLONE_DIR="${TEMP_BASE}/${repo}"
rm -rf "$CLONE_DIR"
REPO_URL="${GITEA_BASE_URL}/${GITEA_ORG_NAME}/${repo}.git"
git_with_auth git clone -q "$REPO_URL" "$CLONE_DIR"
if [[ -d "${CLONE_DIR}/.gitea/workflows" ]]; then
rm -rf "${CLONE_DIR}/.gitea/workflows"
(
cd "$CLONE_DIR"
git config user.name "Gitea Migration"
git config user.email "migration@gitea.local"
git add -A
git commit -q -m "Remove Gitea Actions workflows (teardown)"
git_with_auth git push -q origin HEAD
)
log_success "Removed .gitea/workflows/ from ${repo}"
else
log_info ".gitea/workflows/ not found in clone — already clean"
fi
done
log_success "Phase 5 teardown complete"