Created
June 9, 2024 09:36
-
-
Save adamlacombe/31b0ea4cfd6bc50359d856ffa2f40563 to your computer and use it in GitHub Desktop.
This Bash script automates the migration of repositories from a Bitbucket organization to a GitHub organization, ensuring all branches and tags are transferred. It clones repositories with full mirroring, creates private repositories on GitHub, and pushes the content.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Bitbucket settings | |
BITBUCKET_USER="" | |
BITBUCKET_ORG="" | |
BITBUCKET_APP_PASSWORD="" | |
# GitHub settings | |
GITHUB_USER="" | |
GITHUB_ORG="" | |
GITHUB_TOKEN="" | |
# Temporary directory for cloning repos | |
TEMP_DIR="./temp_repos" | |
mkdir -p $TEMP_DIR | |
# Get all repos from Bitbucket organization | |
BITBUCKET_REPOS=$(curl -s -u $BITBUCKET_USER:$BITBUCKET_APP_PASSWORD "https://api.bitbucket.org/2.0/repositories/$BITBUCKET_ORG?pagelen=100" | jq -r '.values[] | .full_name + " " + (.links.clone[] | select(.name == "https") .href)') | |
# Loop through each Bitbucket repo details | |
echo "$BITBUCKET_REPOS" | while read FULL_REPO_NAME REPO_URL | |
do | |
REPO_NAME="${FULL_REPO_NAME#$BITBUCKET_ORG/}" | |
# Modify REPO_URL to include authentication credentials | |
AUTHENTICATED_REPO_URL="${REPO_URL/https:\/\/$BITBUCKET_USER@/https:\/\/$BITBUCKET_USER:$BITBUCKET_APP_PASSWORD@}" | |
echo "Migrating: $AUTHENTICATED_REPO_URL" | |
# Clone Bitbucket repo with mirroring | |
git clone --mirror $AUTHENTICATED_REPO_URL $TEMP_DIR/$REPO_NAME | |
# Create new repo on GitHub | |
RESPONSE=$(curl -s -w "%{http_code}" -o /dev/null -u $GITHUB_USER:$GITHUB_TOKEN -X POST https://api.github.com/orgs/$GITHUB_ORG/repos -d "{\"name\":\"$REPO_NAME\", \"private\": true}") | |
# Check if repo creation was successful | |
if [ "$RESPONSE" -eq 201 ]; then | |
echo "GitHub repository $REPO_NAME created successfully." | |
# Change directory to cloned repo | |
cd $TEMP_DIR/$REPO_NAME | |
# Add GitHub repo as a new remote for mirror push | |
git remote add github "https://$GITHUB_USER:[email protected]/$GITHUB_ORG/$REPO_NAME.git" | |
# Push all branches and tags to GitHub | |
git push github --mirror | |
# Return to original directory | |
cd - | |
# Clean up local repo | |
rm -rf $TEMP_DIR/$REPO_NAME | |
echo "Migration of $REPO_NAME completed." | |
else | |
echo "Failed to create GitHub repository $REPO_NAME. HTTP status code: $RESPONSE" | |
fi | |
done | |
echo "All repositories have been processed." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment