diff --git a/scripts/fix-gitea-remotes.sh b/scripts/fix-gitea-remotes.sh new file mode 100644 index 0000000..a181a7d --- /dev/null +++ b/scripts/fix-gitea-remotes.sh @@ -0,0 +1,67 @@ +#!/usr/bin/env bash +set -euo pipefail + +CONFIG_FILE="/opt/appfactory/config/appfactory.env" + +if [ -f "$CONFIG_FILE" ]; then + set -a + # shellcheck disable=SC1090 + source "$CONFIG_FILE" + set +a +else + echo "ERROR: Missing config file: $CONFIG_FILE" + exit 1 +fi + +APPFACTORY_DIR="${APPFACTORY_DIR:-/opt/appfactory}" +WORKSPACE_DIR="${WORKSPACE_DIR:-$APPFACTORY_DIR/workspace}" +GITEA_ORG="${GITEA_ORG:-appfactory}" +GITEA_PUBLIC_URL="${GITEA_PUBLIC_URL:-${GITEA_URL:-}}" + +if [ -z "$GITEA_PUBLIC_URL" ]; then + echo "ERROR: GITEA_URL or GITEA_PUBLIC_URL is not configured" + exit 1 +fi + +GITEA_PUBLIC_URL="${GITEA_PUBLIC_URL%/}" + +echo "Fixing Gitea remotes" +echo "Workspace: $WORKSPACE_DIR" +echo "Target Gitea URL: $GITEA_PUBLIC_URL" +echo "Org: $GITEA_ORG" +echo + +if [ ! -d "$WORKSPACE_DIR" ]; then + echo "ERROR: Workspace directory not found: $WORKSPACE_DIR" + exit 1 +fi + +find "$WORKSPACE_DIR" -mindepth 2 -maxdepth 2 -type d -name ".git" | sort | while read -r git_dir; do + repo_dir="$(dirname "$git_dir")" + repo_name="$(basename "$repo_dir")" + + git config --global --add safe.directory "$repo_dir" >/dev/null 2>&1 || true + + current_url="$(git -C "$repo_dir" remote get-url origin 2>/dev/null || true)" + + if [ -z "$current_url" ]; then + echo "SKIP: $repo_name has no origin remote" + continue + fi + + new_url="$GITEA_PUBLIC_URL/$GITEA_ORG/$repo_name.git" + + if [ "$current_url" = "$new_url" ]; then + echo "OK: $repo_name -> $current_url" + continue + fi + + echo "FIX: $repo_name" + echo " old: $current_url" + echo " new: $new_url" + + git -C "$repo_dir" remote set-url origin "$new_url" +done + +echo +echo "DONE" \ No newline at end of file