Skip to content

Instantly share code, notes, and snippets.

@svagionitis
Last active July 10, 2017 06:46
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 svagionitis/b9045dac399486eac0f1c47d4555706f to your computer and use it in GitHub Desktop.
Save svagionitis/b9045dac399486eac0f1c47d4555706f to your computer and use it in GitHub Desktop.
Clone a user repos from bitbucket using it's REST API
#!/bin/bash -ex
BITBUCKET_SERVER_API="https://api.bitbucket.org"
USER_REPOSITORIES="${BITBUCKET_SERVER_API}/1.0/user/repositories"
# Utilites to be used
CURL="`which curl` -s"
JQ="`which jq`"
GIT="`which git`"
usage()
{
echo "Usage: $0 <username> <password>"
}
if [ $# -lt 2 ]; then
usage
exit 1
fi
if [ -z ${1} ]; then
usage
exit 1
fi
USERNAME=${1}
if [ -z ${2} ]; then
usage
exit 1
fi
PASSWORD=${2}
START=$(date +%s)
GET_REPOSITORIES=$(${CURL} --user ${USERNAME}:${PASSWORD} ${USER_REPOSITORIES} | ${JQ} '.')
GET_NAME_REPOSITORIES=$(echo ${GET_REPOSITORIES} | ${JQ} '.[]."name"' | tr -d '"')
GET_RESOURCE_URI_REPOSITORIES=$(echo ${GET_REPOSITORIES} | ${JQ} '.[]."resource_uri"' | tr -d '"')
for repo in ${GET_RESOURCE_URI_REPOSITORIES}; do
GET_REPO=$(${CURL} --user ${USERNAME}:${PASSWORD} ${BITBUCKET_SERVER_API}${repo} | ${JQ} '.')
REPO_NAME=$(echo ${GET_REPO} | ${JQ} '."name"' | tr -d '"')
REPO_DIR="${REPO_NAME}.git"
# Check if directory already exists
# If already exists then update and
# continue to the next one
if [ -d ${REPO_DIR} ]; then
echo "Repo ${REPO_NAME} already exists, update it..."
cd ${REPO_DIR}
${GIT} fetch || true
cd -
continue
fi
echo "Cloning ${REPO_NAME}..."
${GIT} clone git@bitbucket.org:${USERNAME}/${REPO_NAME}.git ${REPO_DIR}
done
FINISH=$(date +%s)
echo "Total time: $(( ($FINISH-$START) / 60 )) minutes, $(( ($FINISH-$START) % 60 )) seconds"
@nepsdotin
Copy link

this scripts look great but cant be use in automation, because we are passing username and password, do you have a plan to write it using ssh key.

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