Skip to content

Instantly share code, notes, and snippets.

@leosoto
Created April 2, 2018 13:49
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save leosoto/ebd8554635d0e5f4346d9b57da3c9f5d to your computer and use it in GitHub Desktop.
Save leosoto/ebd8554635d0e5f4346d9b57da3c9f5d to your computer and use it in GitHub Desktop.
Moves all your organization's bitbucket repositories to GitHub. Note that when a repository is correctly uploaded to GitHub it is *removed* from BitBucket. Requires https://bitbucket.org/zhemao/bitbucket-cli/. Set the variables BB_* with your bitbucket credentials and GH_* variables with your GitHub credentials.
#!/bin/bash
set -e
repos=$(bb list -u $BB_USERNAME -p $BB_PASSWORD --private | grep $BB_ORG | cut -d' ' -f3 | cut -d'/' -f2)
for repo in $repos; do
echo
echo "* Processing $repo..."
echo
git clone --bare git@bitbucket.org:$BB_ORG/$repo.git
cd $repo.git
echo
echo "* $repo cloned, now creating on github..."
echo
curl -u $GH_USERNAME:$GH_PASSWORD https://api.github.com/orgs/$GH_ORG/repos -d "{\"name\": \"$repo\", \"private\": true}"
echo
echo "* mirroring $repo to github..."
echo
git push --mirror git@github.com:$GH_ORG/$repo.git && \
bb delete -u $BB_USERNAME -p $BB_PASSWORD --owner $BB_ORG $repo
cd ..
done
@shanerk
Copy link

shanerk commented Dec 28, 2018

Hiya! First of all - THANK YOU!! This is saving me a lot of time. Secondly, I made some updates in my fork. Your clone and push statements seem to be malformed and the changes I made allowed the script to run. I added (very basic) error handling as well so the batch doesn't fail even if one of the repos has problems. Finally, your ENV variables should be BB_ORG and GH_ORG on your blog to match the code. Cheers!

@pdecarcer
Copy link

pdecarcer commented Jan 31, 2020

The code has one problem. It uses bitbucket-cli! That library uses bitbucket-api 1.0 (deprecated). I did some modifications using jq.

#!/bin/bash
rm -rf "$BB_ORG" && mkdir "$BB_ORG" && cd $BB_ORG

URL="https://api.bitbucket.org/2.0/repositories/$BB_ORG?pagelen=100"

while [ ! -z $URL ]
do
    curl -u $BB_USERNAME:$BB_PASSWORD $NEXT_URL > repoinfo.json
    jq -r '.values[] | .links.clone[1].href' repoinfo.json > ../repos.txt
    URL=`jq -r '.next' repoinfo.json`
    for repo in `cat ../repos.txt`
        do
        echo
        echo "* Processing $repo..."
        echo
        git clone --bare $repo 
        fullName="$(cut -d '/' -f2 <<<"$repo")"
        repoName="$(cut -d '.' -f1 <<<"$fullName")"
        echo
        echo "* $repoName cloned, now creating on github..."  
        echo
        cd $repoName.git
        curl -u $GH_USERNAME:$GH_PASSWORD https://api.github.com/orgs/$GH_ORG/repos -d "{\"name\": \"$repoName\", \"private\": true}"
        echo
        echo "* mirroring $repoName to github..."  
        echo
        git push --mirror git@github.com:$GH_ORG/$repo.git && \
            bb delete -u $BB_USERNAME -p $BB_PASSWORD --owner $BB_ORG $repo   
        cd ..
    done
done

@leosoto
Copy link
Author

leosoto commented Feb 1, 2020

@pdecarcer Awesome.

PS: So bb delete still works but bb list doesn't?

@pdecarcer
Copy link

@leosoto Thats right!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment