#!/usr/bin/env bash # Create Gitea repos for all projects under PROJECTS_DIR (if missing) and push each. # Usage: GITEA_TOKEN=xxx bash scripts/dev-vm/push-all-projects-to-gitea.sh [--dry-run] [--create-only] # Optional: PROJECTS_DIR=/path REPO_NAMES="a b c" to limit repos. # Requires: run from proxmox repo root; GITEA_TOKEN in env or root .env set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" [ -f "$PROJECT_ROOT/.env" ] && set +u && source "$PROJECT_ROOT/.env" 2>/dev/null || true && set -u GITEA_ORG="${GITEA_ORG:-d-bis}" GITEA_URL="${GITEA_URL:-https://gitea.d-bis.org}" PROJECTS_DIR="${PROJECTS_DIR:-/home/intlc/projects}" DRY_RUN=false CREATE_ONLY=false [[ "${1:-}" == "--dry-run" ]] && DRY_RUN=true [[ "${1:-}" == "--create-only" ]] && CREATE_ONLY=true if [ -z "${GITEA_TOKEN:-}" ] && [ "$DRY_RUN" = false ]; then echo "Set GITEA_TOKEN to create repos and push (e.g. from Gitea Settings → Applications)." exit 1 fi API="${GITEA_URL%/}/api/v1" AUTH="" [ -n "${GITEA_TOKEN:-}" ] && AUTH="Authorization: token $GITEA_TOKEN" # Repos to skip (e.g. upstream SDKs that are too large or not our projects) SKIP_REPOS=( js ) # Discover git repos: direct children of PROJECTS_DIR that have .git discover_repos() { for d in "$PROJECTS_DIR"/*/; do [ -d "${d}.git" ] || continue name=$(basename "$d") for skip in "${SKIP_REPOS[@]}"; do [[ "$name" == "$skip" ]] && continue 2 done echo "${d%/}" done 2>/dev/null | sort -u } # Ensure Gitea repo exists; create if missing ensure_repo() { local repo_name="$1" if $DRY_RUN && [ -z "${GITEA_TOKEN:-}" ]; then echo " [DRY-RUN] $repo_name: would ensure exists" return 0 fi if $DRY_RUN; then echo " [DRY-RUN] Would create repo if missing: $GITEA_ORG/$repo_name" return 0 fi local resp code body resp=$(curl -s -w "\n%{http_code}" -X GET "$API/repos/$GITEA_ORG/$repo_name" ${AUTH:+-H "$AUTH"}) code=$(echo "$resp" | tail -1) if [ "$code" = "200" ]; then echo " $repo_name: exists" return 0 fi body=$(curl -s -w "\n%{http_code}" -X POST "$API/orgs/$GITEA_ORG/repos" \ -H "Content-Type: application/json" ${AUTH:+-H "$AUTH"} \ -d "{\"name\":\"$repo_name\",\"private\":false}") code=$(echo "$body" | tail -1) if [ "$code" = "201" ]; then echo " $repo_name: created" return 0 fi if echo "$body" | grep -q "already exists\|already exists"; then echo " $repo_name: exists" return 0 fi echo " $repo_name: failed (HTTP $code)" >&2 return 1 } # Push one repo (branch = current branch) push_repo() { local dir="$1" local repo_name repo_name=$(basename "$dir") local branch branch=$(git -C "$dir" branch --show-current 2>/dev/null || echo "main") if $DRY_RUN; then echo " [DRY-RUN] Would push $repo_name (branch $branch)" return 0 fi [ -z "${GITEA_TOKEN:-}" ] && { echo " $repo_name: skip (no GITEA_TOKEN)" >&2; return 1; } if ! git -C "$dir" remote get-url gitea &>/dev/null; then git -C "$dir" remote add gitea "$GITEA_URL/$GITEA_ORG/$repo_name.git" fi git -C "$dir" push "https://${GITEA_TOKEN}@${GITEA_URL#https://}/$GITEA_ORG/$repo_name.git" "$branch" --set-upstream 2>&1 && echo " $repo_name: pushed ($branch)" || { echo " $repo_name: push failed" >&2; return 1; } } echo "Gitea: $GITEA_URL | Org: $GITEA_ORG | Projects dir: $PROJECTS_DIR | DRY_RUN=$DRY_RUN | CREATE_ONLY=$CREATE_ONLY" REPOS=() while read -r d; do [ -n "$d" ] && REPOS+=("$d") done < <(discover_repos) echo "Discovered ${#REPOS[@]} repos. Ensuring Gitea repos exist..." for d in "${REPOS[@]}"; do ensure_repo "$(basename "$d")" || true done if [ "$CREATE_ONLY" = true ]; then echo "Create-only: skipping push." exit 0 fi echo "Pushing..." for d in "${REPOS[@]}"; do push_repo "$d" || true done echo "Done."