Skip to content

Instantly share code, notes, and snippets.

@bhaireshm
Created October 28, 2024 18:13
Show Gist options
  • Save bhaireshm/15044e7f00857b241f166c7bf36a3a85 to your computer and use it in GitHub Desktop.
Save bhaireshm/15044e7f00857b241f166c7bf36a3a85 to your computer and use it in GitHub Desktop.
This script facilitates the seamless migration of repositories from AWS CodeCommit to GitHub. It ensures that migrations are not duplicated, verifies the existence of GitHub repositories, and creates new ones if needed. The script logs all activities and maintains a status file to track which migrations have been completed successfully.
#!/bin/bash
# ------------------------------------------------------------------------------------ #
# AWS CodeCommit to GitHub Migration Script
# Author: bhaireshm
# Date: 2024-10-28
# Version: 1.0.0
# Description:
# This script facilitates the seamless migration of repositories from AWS CodeCommit to GitHub.
# It ensures that migrations are not duplicated, verifies the existence of GitHub repositories,
# and creates new ones if needed. The script logs all activities and maintains a status file
# to track which migrations have been completed successfully.
# Usage:
# 1. Ensure you have Git installed on your system.
# 2. Run the script and provide your AWS and GitHub credentials when prompted.
# 3. Enter the list of CodeCommit repository names you wish to migrate.
# 4. The script will handle the migration process and log the details in 'info.log'.
# ------------------------------------------------------------------------------------ #
# Check if all required tools are installed
command -v git >/dev/null 2>&1 || {
echo >&2 "Git is required but not installed. Aborting."
exit 1
}
# Function to check if the migration has already been done
check_migration_status() {
local repo_name=$1
local migration_status_file="migration_status.txt"
if grep -q "^$repo_name$" "$migration_status_file"; then
echo "Migration for $repo_name has already been completed. Skipping..."
return 1
fi
return 0
}
# Function to check if a GitHub repository exists
check_github_repo_exists() {
local repo_name=$1
local response_code
response_code=$(curl -s -o /dev/null -w "%{http_code}" -u "$github_username:$github_token" "https://api.github.com/repos/$github_username/$repo_name")
echo "$response_code"
}
# Function to create a GitHub repository
create_github_repo() {
local repo_name=$1
local response_code
response_code=$(curl -s -o /dev/null -w "%{http_code}" -u "$github_username:$github_token" -X POST -d "{\"name\":\"$repo_name\"}" "https://api.github.com/user/repos")
if [ "$response_code" -eq 201 ]; then
echo "Repository $repo_name created successfully on GitHub." | tee -a info.log
elif [ "$response_code" -eq 401 ]; then
echo "Invalid GitHub credentials. Aborting." | tee -a info.log
exit 1
else
echo "Failed to create repository $repo_name on GitHub. Response code: $response_code" | tee -a info.log
exit 1
fi
}
# Function to migrate a single repository
migrate_repo() {
local repo_name=$1
local codecommit_url=$2
local github_url=$3
# Check if migration has already been done
check_migration_status "$repo_name"
if [ $? -eq 1 ]; then
return
fi
# Check if GitHub repository exists, if not create it
if [ "$(check_github_repo_exists "$repo_name")" -ne 200 ]; then
echo "GitHub repository $repo_name does not exist. Creating..." | tee -a info.log
create_github_repo "$repo_name"
else
echo "GitHub repository $repo_name already exists." | tee -a info.log
fi
echo "Migrating repository: $repo_name" | tee -a info.log
echo "Cloning CodeCommit repository..." | tee -a info.log
if ! git clone --mirror "$codecommit_url" "$repo_name"; then # >>info.log 2>&1; then
echo "Failed to clone CodeCommit repository $repo_name. Skipping..." | tee -a info.log
exit 1
fi
echo "Pushing all branches, tags, PRs, and releases to GitHub repository..." | tee -a info.log
cd "$repo_name" || {
echo "Failed to enter directory $repo_name. Aborting."
exit 1
}
git remote add github "https://$github_username:$github_token@$github_url" >>info.log 2>&1
echo "Remote -> $(git remote get-url github)"
if ! git push github --all; then # >>info.log 2>&1; then
echo "Failed to push branches to GitHub repository $repo_name. Aborting." | tee -a info.log
exit 1
fi
if ! git push github --tags; then # >>info.log 2>&1; then
echo "Failed to push tags to GitHub repository $repo_name. Aborting." | tee -a info.log
exit 1
fi
echo "Migration completed for $repo_name" | tee -a info.log
# Cleanup repository
echo "Cleaning up local repository: $repo_name" | tee -a info.log
cd ..
rm -rf "$repo_name"
}
# Create required files if they do not exist, Add permission to read and write files if not already set
[ ! -f info.log ] && touch info.log
[ ! -x info.log ] && chmod +x info.log
[ ! -f "$migration_status_file" ] && touch "$migration_status_file"
[ ! -r "$migration_status_file" ] && chmod +r "$migration_status_file"
[ ! -w "$migration_status_file" ] && chmod +w "$migration_status_file"
# Main script
echo "AWS CodeCommit to GitHub Migration Script" | tee -a info.log
# Initialize missing variables
github_username=""
github_token=""
repo_names=""
username=$(git config user.name)
email=$(git config user.email)
echo "User: $username, Email: $email"
# Check GitHub credentials
echo "Checking GitHub credentials..." | tee -a info.log
if [ -z "$github_username" ]; then
read -p "GitHub Username not found. Please provide your GitHub Username: " github_username
fi
if [ -z "$github_token" ]; then
read -p "GitHub Personal Access Token not found. Please provide your GitHub Personal Access Token: " github_token
fi
# Get the list of repositories to migrate from AWS CodeCommit
echo "Fetching repository names from AWS CodeCommit..." | tee -a info.log
repo_names=$(aws codecommit list-repositories --query 'repositories[].repositoryName' --output text)
# Check if any repositories were found
if [ -z "$repo_names" ]; then
echo "No repositories starting with 'apf-' found in AWS CodeCommit. Aborting." | tee -a info.log
exit 1
fi
# Migrate each repository
for repo_name in $repo_names; do
echo "Migrating repository: $repo_name" | tee -a info.log
codecommit_url="https://git-codecommit.us-east-1.amazonaws.com/v1/repos/$repo_name"
github_url="github.com/$github_username/$repo_name.git"
migrate_repo "$repo_name" "$codecommit_url" "$github_url"
echo "------------------------------------------------------------------------------------" | tee -a info.log
done
echo "Migration script completed." | tee -a info.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment