Bitbucket is a web-based version control repository hosting service owned by Atlassian, for source code and development projects that use Git revision control system.

1 Set up an SSH key

1.1 Setup SSH on macOS/Linux/Windows

  1. Copy
    $ ssh-keygen
    Generating public/private rsa key pair.
    ...
  2. Press the Enter or Return key to accept the default location.
  3. Enter and re-enter a passphrase when prompted. The command creates your default identity with its public and private keys.
  4. List the contents of ~/.ssh to view the key files.
    Copy
    $ ls ~/.ssh
    id_rsa id_rsa.pub

1.2 Add the public key to your Bitbucket settings

  1. From Bitbucket, choose Bitbucket settings from your avatar in the lower left. The Account settings page opens.
  2. Click SSH keys. If you've already added keys, you'll see them on this page.
  3. In your terminal window, copy the contents of your public key file. If you renamed the key, replace id_rsa.pub with the public key file name. On Linux, you can cat the contents:
    Copy
    $ cat ~/.ssh/id_rsa.pub
    On macOS, the following command copies the output to the clipboard:
    Copy
    $ pbcopy < ~/.ssh/id_rsa.pub
  4. Select and copy the key output in the clipboard.
  5. From Bitbucket, goto Personal Settings and click the SSH Keys options



  6. Click Add key.
  7. Enter a Label for your new key, for example, Default public key.
  8. Paste the copied public key into the SSH Key field. You may see an email address on the last line when you paste. It doesn't matter whether or not you include the email address in the Key.
  9. Click Save. Bitbucket sends you an email to confirm the addition of the key.

1.3 Verify SSH is working

Return to the terminal window and verify your configuration and username by entering the following command:

Copy
$ ssh -T git@bitbucket.org
logged in as yourid

You can use git or hg to connect to Bitbucket. Shell access is disabled

2 Bitbucket API

You can interact with Bitbucket using it's REST api.

2.1 List teams

Copy
curl -u username:password  https://api.bitbucket.org/2.0/teams?role=admin | jq -r '.values[].username'
team1
team2

2.2 List repositores

You can list repositories under your account name

Copy
curl -u username:password https://api.bitbucket.org/2.0/repositories/${USERNAME}?pagelen=100 | grep -o '"ssh:[^ ,]\+'

Or you can list repositories under a given team name

Copy
curl -u username:password https://api.bitbucket.org/2.0/repositories/${TEAMNAME}?pagelen=100 | grep -o '"ssh:[^ ,]\+'

2.3 Automatic clone all repositories

Some times can be usefull to have a full copy of all repositores. The following shell will perform:

  • Query to get teams under your bitbucket account.
  • Ask to clone an specific team or all teams (*).
  • For each team will show team members an projects. If (*) not specified for team, the shell will ask to clone individual projects.

Copy
USERNAME=username
PASSWORD=password


BAPI=https://api.bitbucket.org/2.0
TEAM_0=$USERNAME
TEAM_1=`curl -s -u $USERNAME:$PASSWORD "$BAPI/teams?role=admin" | jq -r '.values[].username'`
TEAM_2=`curl -s -u $USERNAME:$PASSWORD "$BAPI/teams?role=admin&page=2" | jq -r '.values[].username'`
TEAMS="$TEAM_0 $TEAM_1 $TEAM_2"

if ! [ -x "`command -v jq`" ]; then
   echo "Error: jq not installed"
   exit 1
fi

# ============================================================
# Show TEAMS
# ============================================================
echo
echo "TEAMS"
echo
for TEAM in $TEAMS; do
    echo "   # $TEAM"
done
echo

echo "Enter a TEAM to clone or (*) to clone all teams and repositories"
read NAME

if [ "$NAME" = "*" ]; then
    echo "Cloning all teams and repositories"
else
    TEAMS=$NAME
fi


for TEAM in $TEAMS; do  
    # ============================================================
    # Show team and members
    # ============================================================
    echo
    echo "TEAM $TEAM"
    echo

    # When team==account ther are no members and no projects

    if [ "$TEAM" = "$TEAM_0" ]; then
	    # We ara at team==acount so we have only repositories, no teams and no projects
	    NAME="*"
    else
        # ============================================================
        # Show members of team
        # ============================================================
        echo "   MEMBERS"
        echo
        old_IFS=$IFS
        IFS=$'\n' 
        for MEMBER in `curl -s -u $USERNAME:$PASSWORD $BAPI/teams/$TEAM/members | jq -r '.values[].display_name'`; do
            echo " + $MEMBER"
        done
        IFS=$old_IFS
        echo

        # ============================================================
        # Show projects
        # ============================================================
        echo "   PROJECTS"
        echo
        for PROJECT in `curl -s -u $USERNAME:$PASSWORD https://api.bitbucket.org/2.0/teams/$TEAM/projects/ |  jq -r '.values[].name'`; do
            echo " * $PROJECT"
        done
        echo
    fi

    if [ "$NAME" != "*" ]; then
        echo "Enter a project to clone or RETURN to clone all"
        read PROJECT
    else
        PROJECT=""
    fi

    # ============================================================
    # Get alll repositories from TEAM
    # ============================================================
    NEXT="https://api.bitbucket.org/2.0/repositories/$TEAM"
    while [ -n "`echo $NEXT | grep https`" ] ; do
        echo "curl -s -u $USERNAME:$PASSWORD "$NEXT""
        DATA=`curl -s -u $USERNAME:$PASSWORD "$NEXT"`
        NEXT=`echo $DATA | jq -r '.next'`
        PAGE=`echo $DATA | jq -r '.page'`
        # filter for clone with either hg or git
        # ssh://hg@bitbucket.org:repo
        # git@bitbucket.org:repo
        if [ -n "$PROJECT" ]; then
            REPOS=`echo $DATA | jq -r ".values[] | select (.project.name==\"$PROJECT\")" | jq -r '.links.clone[].href' | egrep -h "^git|ssh"`
        else 
            REPOS=`echo $DATA | jq -r '.values[].links.clone[].href' | egrep -h "^git|ssh"`
        fi
        for REPO in $REPOS; do
            if [ -d `basename $REPO .git` ]; then
                echo "Repository $REPO already exists"
            else
                if [ `echo $REPO | grep -c "ssh://hg"` -gt 0 ]; then
                    echo hg clone $REPO
                    hg clone $REPO
                else
                    echo git clone $REPO
                    git clone $REPO
                fi
                if [ $? != 0 ]; then
                    echo "ERROR: clone $REPO failed!"
                fi
            fi
        done
    done
done

2.3.1 Install jq utility

To run previous shell you need jq. To install jq utility you can execute on MacOS:

Copy
brew install jq

If there are some problems with the directories permissions you must create the folders and change the folders owners (administrator permissions required). For example:

Problems:

Copy
$ brew install jq
mkdir: /usr/local/var/homebrew/locks: Permission denied
Error: Can't create update lock in /usr/local/var/homebrew/locks!
Fix permissions by running:
  sudo chown -R $(whoami) /usr/local/var/homebrew
Error: The following directories are not writable by your user:
/usr/local/share/doc

You should change the ownership of these directories to your user.
  sudo chown -R $(whoami) /usr/local/share/doc

And make sure that your user has write permission.
  chmod u+w /usr/local/share/doc

Solutions:

Copy
sudo mkdir /usr/local/var/homebrew
sudo chown -R $(whoami) /usr/local/var/homebrew
sudo chown -R $(whoami) /usr/local/share/doc