#!/usr/bin/env bash # Create Gitea organization(s) and repositories via API. # Usage: GITEA_TOKEN=xxx bash scripts/dev-vm/gitea-create-orgs-and-repos.sh [--dry-run] # Create token at: https://gitea.d-bis.org/user/settings/applications (scopes: write:organization, write:repository) # Optional: GITEA_ORG=myorg REPO_NAMES="proxmox dbis_core ..." (default org: d-bis; repos from list or discover) set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" source "$PROJECT_ROOT/config/ip-addresses.conf" 2>/dev/null || true [ -f "$PROJECT_ROOT/.env" ] && set +u && source "$PROJECT_ROOT/.env" 2>/dev/null || true && set -u GITEA_URL="${GITEA_URL:-https://gitea.d-bis.org}" # GITEA_TOKEN from .env or environment # Token from Gitea: Settings → Applications → Generate New Token (write:organization, write:repository) # Or set GITEA_USER + GITEA_PASSWORD to create a token automatically (scopes: write:organization, write:repository) GITEA_TOKEN="${GITEA_TOKEN:-}" GITEA_USER="${GITEA_USER:-}" GITEA_PASSWORD="${GITEA_PASSWORD:-}" DRY_RUN=false [[ "${1:-}" == "--dry-run" ]] && DRY_RUN=true # Default org and repos (override with env) GITEA_ORG="${GITEA_ORG:-d-bis}" REPO_NAMES="${REPO_NAMES:-proxmox dbis_core explorer-monorepo virtual-banker alltra-lifi-settlement smom-dbis-138 unifi-api metamask-integration fireblocks-integration mcp-omada mcp-proxmox mcp-unifi the-order miracles_in_motion rpc-translator-138 token-lists forge-verification-proxy site-manager-api multi-chain-execution}" # Create token from username/password if needed if [ -z "$GITEA_TOKEN" ] && [ -n "$GITEA_USER" ] && [ -n "$GITEA_PASSWORD" ]; then echo "Creating token for $GITEA_USER..." TOKEN_RESP=$(curl -s -X POST "${GITEA_URL%/}/api/v1/users/${GITEA_USER}/tokens" \ -H "Content-Type: application/json" -u "$GITEA_USER:$GITEA_PASSWORD" \ -d '{"name":"org-repo-setup","scopes":["write:organization","write:repository"]}') GITEA_TOKEN=$(echo "$TOKEN_RESP" | jq -r '.sha1 // .token // empty') if [ -z "$GITEA_TOKEN" ] || [ "$GITEA_TOKEN" = "null" ]; then echo "Failed to create token: $TOKEN_RESP" exit 1 fi echo "Token created." fi if [ -z "$GITEA_TOKEN" ]; then echo "Set GITEA_TOKEN, or GITEA_USER + GITEA_PASSWORD. Create token at: $GITEA_URL/user/settings/applications (scopes: write:organization, write:repository)" exit 1 fi API="${GITEA_URL%/}/api/v1" AUTH="Authorization: token $GITEA_TOKEN" # Create organization create_org() { local org="$1" local full_name="${2:-$org}" if $DRY_RUN; then echo "[DRY-RUN] Would create org: $org ($full_name)" return 0 fi local resp resp=$(curl -s -w "\n%{http_code}" -X POST "$API/orgs" \ -H "Content-Type: application/json" -H "$AUTH" \ -d "{\"username\":\"$org\",\"full_name\":\"$full_name\",\"visibility\":\"public\"}") local code code=$(echo "$resp" | tail -1) local body body=$(echo "$resp" | sed '$d') if [ "$code" = "201" ]; then echo " Created org: $org" return 0 fi if echo "$body" | grep -q "already exists\|Username .* is already"; then echo " Org $org already exists" return 0 fi echo " Failed to create org $org: HTTP $code — $body" return 1 } # Create repository in org create_repo() { local org="$1" local name="$2" local desc="${3:-}" if $DRY_RUN; then echo "[DRY-RUN] Would create repo: $org/$name" return 0 fi local payload="{\"name\":\"$name\",\"private\":false}" [ -n "$desc" ] && payload=$(echo "$payload" | jq -c --arg d "$desc" '. + {description:$d}') local resp resp=$(curl -s -w "\n%{http_code}" -X POST "$API/orgs/$org/repos" \ -H "Content-Type: application/json" -H "$AUTH" -d "$payload") local code code=$(echo "$resp" | tail -1) local body body=$(echo "$resp" | sed '$d') if [ "$code" = "201" ]; then echo " Created repo: $org/$name" return 0 fi if echo "$body" | grep -q "already exists\|Repository .* already"; then echo " Repo $org/$name already exists" return 0 fi echo " Failed to create repo $org/$name: HTTP $code — $body" return 1 } echo "Gitea: $GITEA_URL | Org: $GITEA_ORG | DRY_RUN=$DRY_RUN" echo "Creating org: $GITEA_ORG" create_org "$GITEA_ORG" "D-BIS" || true echo "Creating repos in $GITEA_ORG:" for r in $REPO_NAMES; do create_repo "$GITEA_ORG" "$r" "" || true done echo "Done. Add remote and push: git remote add gitea $GITEA_URL/$GITEA_ORG/.git && git push -u gitea main"