|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +echo "🔹 Enter the source organization (e.g., org-x):" |
| 4 | +read ORIGIN_ORG |
| 5 | + |
| 6 | +echo "🔹 Enter the target organization (e.g., org-y):" |
| 7 | +read TARGET_ORG |
| 8 | + |
| 9 | +echo "🔹 Use SSH or HTTPS? (Enter 'ssh' or 'https'):" |
| 10 | +read CLONE_METHOD |
| 11 | + |
| 12 | +if [[ "$CLONE_METHOD" != "ssh" && "$CLONE_METHOD" != "https" ]]; then |
| 13 | + echo "❌ Invalid clone method. Use 'ssh' or 'https'." |
| 14 | + exit 1 |
| 15 | +fi |
| 16 | + |
| 17 | +echo "🔹 Do you want to clone all repos or selected repos only? (Enter 'all' or 'selected'):" |
| 18 | +read REPO_CHOICE |
| 19 | + |
| 20 | +REPOS=() |
| 21 | + |
| 22 | +if [[ "$REPO_CHOICE" == "all" ]]; then |
| 23 | + echo "📦 Fetching all repositories from $ORIGIN_ORG..." |
| 24 | + mapfile -t REPOS < <(gh repo list "$ORIGIN_ORG" --limit 1000 --json name --jq '.[].name') |
| 25 | +elif [[ "$REPO_CHOICE" == "selected" ]]; then |
| 26 | + echo "🔹 Enter full GitHub repo URLs (one per line). Press Ctrl+D when done:" |
| 27 | + while IFS= read -r repo_url; do |
| 28 | + repo_name=$(basename "$repo_url" .git) |
| 29 | + REPOS+=("$repo_name") |
| 30 | + done |
| 31 | +else |
| 32 | + echo "❌ Invalid choice. Use 'all' or 'selected'." |
| 33 | + exit 1 |
| 34 | +fi |
| 35 | + |
| 36 | +TEMP_DIR="temp_repos" |
| 37 | +echo "📁 Creating temporary directory: $TEMP_DIR" |
| 38 | +mkdir -p "$TEMP_DIR" |
| 39 | +cd "$TEMP_DIR" || exit 1 |
| 40 | + |
| 41 | +for repo in "${REPOS[@]}"; do |
| 42 | + echo "" |
| 43 | + echo "==========================================" |
| 44 | + echo "🔄 Processing repository: $repo" |
| 45 | + echo "==========================================" |
| 46 | + |
| 47 | + if [[ "$CLONE_METHOD" == "ssh" ]]; then |
| 48 | + CLONE_URL="git@github.com:$ORIGIN_ORG/$repo.git" |
| 49 | + PUSH_URL="git@github.com:$TARGET_ORG/$repo.git" |
| 50 | + else |
| 51 | + CLONE_URL="https://github.com/$ORIGIN_ORG/$repo.git" |
| 52 | + PUSH_URL="https://github.com/$TARGET_ORG/$repo.git" |
| 53 | + fi |
| 54 | + |
| 55 | + echo "🔁 Cloning $CLONE_URL..." |
| 56 | + git clone --mirror "$CLONE_URL" |
| 57 | + |
| 58 | + cd "$repo.git" || continue |
| 59 | + |
| 60 | + echo "📦 Creating repository $TARGET_ORG/$repo..." |
| 61 | + gh repo create "$TARGET_ORG/$repo" --private --confirm |
| 62 | + |
| 63 | + echo "🚀 Pushing to $TARGET_ORG..." |
| 64 | + git push --mirror "$PUSH_URL" |
| 65 | + |
| 66 | + cd .. |
| 67 | +done |
| 68 | + |
| 69 | +cd .. |
| 70 | + |
| 71 | +echo "" |
| 72 | +echo "🧼 Do you want to delete the temporary cloned repositories in '$TEMP_DIR'? (yes/no):" |
| 73 | +read CLEANUP_CONFIRM |
| 74 | + |
| 75 | +if [[ "$CLEANUP_CONFIRM" == "yes" || "$CLEANUP_CONFIRM" == "y" ]]; then |
| 76 | + echo "🗑️ Deleting temporary directory '$TEMP_DIR'..." |
| 77 | + rm -rf "$TEMP_DIR" |
| 78 | + echo "✅ Cleanup completed." |
| 79 | +else |
| 80 | + echo "📁 Temporary directory '$TEMP_DIR' retained for your reference." |
| 81 | +fi |
| 82 | + |
| 83 | +echo "🎉 All repositories have been successfully migrated from $ORIGIN_ORG to $TARGET_ORG!" |
0 commit comments