Last active
July 14, 2022 15:17
-
-
Save ehowardtillit/b8d806280c36bac998841060dd22fa8e to your computer and use it in GitHub Desktop.
A qucik script to migrate repos from Bitbucket to Github
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
#!/usr/bin/env bash | |
################################################################################ | |
# bitbucket2github.sh | |
# Small script to migrate repos. | |
# Uses GitHub Personal Token to authenticate against api.github.com | |
# Takes as parameter an organization name (optional) | |
# Takes as parameter the name of the URL of the Bitbucket repo to migrate | |
# Assumes you have ssh/keybased connections established : | |
# eval "$(ssh-agent -s)" | |
# ssh-add ~/.ssh/my_bitbucket_key && ssh -T [email protected] | |
# ssh-add ~/.ssh/my_github_key && ssh -T [email protected] | |
################################################################################ | |
# Credentias to Github API, should come from previous setted (and not empty) ENV variables. | |
GITHUB_USERNAME=${ENV_GITHUB_USERNAME:-} | |
if [ -z "$GITHUB_USERNAME" ]; then | |
echo "Please set (with a non empty value) the GITHUB_USERNAME environment variable" | |
exit 1 | |
fi | |
GITHUB_PERSONAL_TOKEN=${ENV_GITHUB_PERSONAL_TOKEN:-} | |
if [ -z "$GITHUB_PERSONAL_TOKEN" ]; then | |
echo "Please set (with a non empty value) the GITHUB_PERSONAL_TOKEN environment variable" | |
exit 1 | |
fi | |
# Parse the input | |
while getopts o:r: flag | |
do | |
case "${flag}" in | |
o) organization=${OPTARG};; | |
r) bitbucket_repository_url=${OPTARG};; | |
esac | |
done | |
# Split the the bitbucket repo | |
repository_name=`grep -Po '\w\K/\w+[^?]+' <<< "${bitbucket_repository_url}" | cut -d'/' -f3- | sed 's-/$--'` | |
repository_owner=`grep -Po '\w\K/\w+[^?]+' <<< "${bitbucket_repository_url}" | cut -d'/' -f2 | sed 's-/$--'` | |
if [ -z "$repository_name" ]; then | |
echo "Could not extract the repository name from the URL: $bitbucket_repository_url" | |
exit 1 | |
fi | |
# Clone the bitbucket repo | |
echo "Cloning the Bitbucket repository: $bitbucket_repository_url" | |
[email protected]:${repository_owner}/${repository_name}.git | |
echo $ssh_bitbucket_repository_url | |
git clone --mirror ${ssh_bitbucket_repository_url} ${repository_name} | |
if [ $? -ne 0 ]; then | |
echo "Error cloning the bitbucket repository" | |
exit 1 | |
fi | |
# Create the repo (org) | |
GITHUB_API_URL="https://api.github.com/orgs/${organization}/repos" | |
if [ -z "${organization}" ]; then # Non organization URL | |
GITHUB_API_URL='https://api.github.com/user/repos' | |
fi | |
echo $GITHUB_API_URL | |
echo $GITHUB_PERSONAL_TOKEN | |
echo $repository_name | |
curl_http_response_code=$(curl -s -o /dev/null -i -w "%{http_code}" -X POST -H "Accept: application/vnd.github.v3+json" -H "Authorization: token ${GITHUB_PERSONAL_TOKEN}" ${GITHUB_API_URL} -d "{\"name\":\"$repository_name\", \"private\":\"true\", \"description\":\"bitbucket2github.sh migrated the ${bitbucket_repository_url} repo.\" }") | |
if [ $curl_http_response_code -ne 201 ]; then | |
echo "Error creating the github repository" | |
exit 1 | |
fi | |
# rename existing repo to avoid conflicts | |
git remote rename origin bitbucket | |
if [ $? -ne 0 ]; then | |
echo "Error renaming origin" | |
exit 1 | |
fi | |
# set new Github repo as git remote | |
GITHUB_REMOTE_URL="[email protected]:${organization}/${repository_name}.git" | |
if [ -z "${organization}" ]; then # Non organization URL | |
GITHUB_REMOTE_URL="[email protected]:${username}/${repository_name}.git" | |
fi | |
git remote add origin ${GITHUB_REMOTE_URL} | |
if [ $?0 -ne 0 ]; then | |
echo "Error setting new origin" | |
exit 1 | |
fi | |
# push to new git repo | |
git push --mirror | |
if [ $? -ne 0 ]; then | |
echo "Error pushing to new git" | |
exit 1 | |
fi | |
# remove old bitbucket | |
git remote rm bitbucket | |
if [ $? -ne 0 ]; then | |
echo "Error removing old bitbucket" | |
exit 1 | |
fi | |
# Escaped back to parent folder and remove the cloned repo. | |
cd .. && rm -rf ${repository_name} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment