Skip to content

Instantly share code, notes, and snippets.

@nickcarenza
Created April 7, 2015 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickcarenza/7338522ccd3e41a8b522 to your computer and use it in GitHub Desktop.
Save nickcarenza/7338522ccd3e41a8b522 to your computer and use it in GitHub Desktop.
Migrate Bitbucket to Github
BITBUCKET_ORG_NAME=''
BITBUCKET_REPO_NAME=$1
if [ -n "$2" ]; then
GITHUB_REPO_NAME=`echo $2 | tr '[:upper:]' '[:lower:]'`
else
GITHUB_REPO_NAME=$BITBUCKET_REPO_NAME
fi
GITHUB_ORG_NAME=''
GITHUB_TOKEN='...'
# Clone mirror the BitBucket repo
git clone --mirror git@bitbucket.org:${BITBUCKET_ORG_NAME}/${BITBUCKET_REPO_NAME}.git
if [ $? -ne 0 ]; then
echo "There was an error cloning the bitbucket repository"
exit 1
fi
cd ${BITBUCKET_REPO_NAME}.git
# Set push/fetch urls on the bitbucket repo to the github url
# git remote set-url origin --push "git@github.com:${GITHUB_ORG_NAME}/${GITHUB_REPO_NAME}.git"
# git remote set-url origin "git@github.com:${GITHUB_ORG_NAME}/${GITHUB_REPO_NAME}.git"
READONLY_TEAM=1220658
# Create repository on github
RESPONSE=`curl -u ${GITHUB_TOKEN}:x-oauth-basic -X POST -d '{"name": "'$GITHUB_REPO_NAME'","private":true,"has_issues":true,"team_id":'$READONLY_TEAM'}' -s https://api.github.com/orgs/${GITHUB_ORG_NAME}/repos`
ERRORS=`echo $RESPONSE | jq '.errors | length'`
MESSAGE=`echo $RESPONSE | jq 'has("message")'`
if [ $ERRORS -gt 0 ] || [ $MESSAGE = 'true' ]; then
echo "There was an error creating the new repository on github"
echo $RESPONSE | jq .
exit 1
fi
git push --mirror git@github.com:${GITHUB_ORG_NAME}/${GITHUB_REPO_NAME}.git
# Update default branch to production if necessary
if [ -n "$3" ]; then
RESPONSE=`curl -u ${GITHUB_TOKEN}:x-oauth-basic -X PATCH -d '{"default_branch":"'$3'"}' -s https://api.github.com/orgs/${GITHUB_ORG_NAME}/${GITHUB_REPO_NAME}.git`
ERRORS=`echo $RESPONSE | jq '.errors | length'`
MESSAGE=`echo $RESPONSE | jq 'has("message")'`
if [ $ERRORS -gt 0 ] || [ $MESSAGE = 'true' ]; then
echo "There was an error updateing the default branch to ${3} on github"
echo $RESPONSE | jq .
exit 1
fi
fi
cd ..
echo 'Returned to '`pwd`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment