Files
gitea-migration/phase6_post_check.sh

83 lines
2.4 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# shellcheck disable=SC2329
# =============================================================================
# phase6_post_check.sh — Verify Phase 6 (GitHub Push Mirrors) succeeded
# Checks for each repo:
# 1. Push mirror config exists in Gitea
# 2. Triggers sync and verifies GitHub has matching latest commit
# Exits 0 only if ALL checks pass.
# =============================================================================
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
source "${SCRIPT_DIR}/lib/common.sh"
load_env
require_vars GITEA_ADMIN_TOKEN GITEA_INTERNAL_URL GITEA_ORG_NAME \
GITHUB_USERNAME GITHUB_TOKEN \
REPO_1_NAME REPO_2_NAME REPO_3_NAME
log_info "=== Phase 6 Post-Check ==="
REPOS=("$REPO_1_NAME" "$REPO_2_NAME" "$REPO_3_NAME")
PASS=0
FAIL=0
run_check() {
local description="$1"; shift
if "$@" 2>/dev/null; then
log_success "$description"
PASS=$((PASS + 1))
else
log_error "FAIL: $description"
FAIL=$((FAIL + 1))
fi
}
for repo in "${REPOS[@]}"; do
log_info "--- Checking repo: ${repo} ---"
# Check 1: Push mirror exists
# shellcheck disable=SC2329
check_mirror_exists() {
local mirrors
mirrors=$(gitea_api GET "/repos/${GITEA_ORG_NAME}/$1/push_mirrors")
local count
count=$(printf '%s' "$mirrors" | jq 'length')
[[ "$count" -gt 0 ]]
}
run_check "Push mirror exists for ${repo}" check_mirror_exists "$repo"
# Check 2: Latest commit SHA matches between Gitea and GitHub
# Trigger a sync first, then compare HEAD commits
# shellcheck disable=SC2329
check_commit_sync() {
# Trigger sync
gitea_api POST "/repos/${GITEA_ORG_NAME}/$1/push_mirrors-sync" "" >/dev/null 2>&1 || true
# Brief wait for sync to propagate
sleep 3
# Get Gitea's latest commit
local gitea_sha github_sha
gitea_sha=$(gitea_api GET "/repos/${GITEA_ORG_NAME}/$1/commits?limit=1" | jq -r '.[0].sha')
# Get GitHub's latest commit
github_sha=$(github_api GET "/repos/${GITHUB_USERNAME}/$1/commits?per_page=1" | jq -r '.[0].sha')
[[ "$gitea_sha" == "$github_sha" ]]
}
run_check "Latest commit synced to GitHub for ${repo}" check_commit_sync "$repo"
done
# Summary
printf '\n'
log_info "Results: ${PASS} passed, ${FAIL} failed"
if [[ $FAIL -gt 0 ]]; then
log_error "Phase 6 post-check FAILED"
exit 1
else
log_success "Phase 6 post-check PASSED — push mirrors working"
exit 0
fi