Skip to content

Instantly share code, notes, and snippets.

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 nilpix/eb6098fce3f9e01eb963f6eb96f44f65 to your computer and use it in GitHub Desktop.
Save nilpix/eb6098fce3f9e01eb963f6eb96f44f65 to your computer and use it in GitHub Desktop.
#!/bin/bash
COWNER=teamOwner
SHOSTNAME=my.server.com
SHOST=https://$SHOSTNAME/rest/api/1.0
SERVER_CREDS=myuser:mypass
# the port that git clone ssh://host:PORT
SPORT=9999
CHOST=https://api.bitbucket.org/2.0
CLOUD_CREDS=myuser:mypass
CJSON='Content-Type: application/json'
AJSON='Accepts: application/json'
WORKSPACE=`mktemp -d`
function create_project_repos() {
curl -s -u $SERVER_CREDS -H $CJSON "$SHOST/projects?pageSize=150" |\
jq '.values' |\
jq 'del(.[].links,.[].type)' |\
jq -r '.[] | [.id, .key, .name, .public, .description] | @csv' |\
sed -e 's|"||g' > $WORKSPACE/projects-server.csv
for LINE in `cat $WORKSPACE/projects-server.csv`; do
ID=`echo $LINE | awk -F ',' '{ print $1 }'`
KEY=`echo $LINE | awk -F ',' '{ print $2 }'`
NAME=`echo $LINE | awk -F ',' '{ print $3 }'`
PUBLIC=`echo $LINE | awk -F ',' '{ print $4 }'`
DESCRIPTION=`echo $LINE | awk -F ',' '{ print $5 }'`
PRIVATE=true
if [[ "$PUBLIC" == "true" ]]; then
PRIVATE=false
fi
PROJECT_BODY=$(cat << EOM
{
"name": "$NAME",
"key": "$KEY",
"description": "$DESCRIPTION",
"is_private": $PRIVATE
}
EOM
)
echo -n "Creating Project $KEY..."
curl -s -u "$CLOUD_CREDS" -XPOST -H "$CJSON" -H "$AJSON" "$CHOST/teams/$COWNER/projects/" -d "$PROJECT_BODY" 2>&1 > $WORKSPACE/response.json
if [[ $? -eq 0 ]] && ! [[ `jq -r '.type' $WORKSPACE/response.json` == "error" ]]; then
echo 'done'
read -p "Press any key to continue... " -n1 -s
else
echo 'failed'
jq '.' $WORKSPACE/response.json
fi
curl -s -u $SERVER_CREDS -H $CJSON "$SHOST/projects/$KEY/repos" |\
jq '.values' |\
jq 'del(.[].links,.[].project,.[].scmId, .[].state, .[].statusMessage, .[].forkable)' |\
jq -r '.[] | [.id, .name, .slug, .public] | @csv' |\
sed -e 's|"||g' > $WORKSPACE/repos.csv
#Looping through repos
for RLINE in `cat $WORKSPACE/repos.csv`; do
RID=`echo $RLINE | awk -F ',' '{ print $1 }'`
RNAME=`echo $RLINE | awk -F ',' '{ print $2 }'`
RKEY=`echo $RLINE | awk -F ',' '{ print $3 }'`
RPUBLIC=`echo $RLINE | awk -F ',' '{ print $4 }'`
RPRIVATE=true
if [[ "$RPUBLIC" == "true" ]]; then
RPRIVATE=false
fi
REPO_BODY=$(cat << EOM
{
"scm": "git",
"name": "${RNAME}",
"key": "$RKEY",
"description": "$RNAME",
"is_private": $RPRIVATE,
"fork_policy": "no_public_forks",
"has_issues": true,
"has_wiki": true,
"project": {
"key": "$KEY"
}
}
EOM
)
echo -n " Creating Repository $RKEY..."
curl -s -u "$CLOUD_CREDS" -XPOST -H "$CJSON" -H "$AJSON" "$CHOST/repositories/$COWNER/$RKEY" -d "$REPO_BODY" 2>&1 > $WORKSPACE/response.json
if [[ $? -eq 0 ]] && ! [[ `jq -r '.type' $WORKSPACE/response.json` == "error" ]]; then
echo 'done'
echo "###"
echo "Processing $RKEY"
git clone --bare ssh://git@$SHOSTNAME:$SPORT/$KEY/$RKEY
echo ""
echo "Pushing mirror to bitbucket CLOUD"
cd $RKEY.git
git push --mirror git@bitbucket.org:$COWNER/$RKEY.git
cd ..
echo "Removing $RKEY.git"
rm -rf "$RKEY.git"
read -p "Press any key to continue... " -n1 -s
else
echo 'failed'
jq '.' $WORKSPACE/response.json
fi
done
done
}
function remove_repos() {
curl -s -u "$CLOUD_CREDS" -XGET -H "$CJSON" -H "$AJSON" "$CHOST/repositories/$COWNER" 2>&1 > $WORKSPACE/delete_repos.json
for SLUG in `cat $WORKSPACE/delete_repos.json | jq -r '.values[].slug'`; do
echo -n "Removing Repository $SLUG..."
curl -s -u "$CLOUD_CREDS" -XDELETE -H "$CJSON" -H "$AJSON" "$CHOST/repositories/$COWNER/$SLUG" 2>&1 > $WORKSPACE/response.json
if [[ $? -eq 0 ]] && ! [[ `jq -r '.type' $WORKSPACE/response.json` == "error" ]]; then
echo 'done'
else
echo 'failed'
jq '.' $WORKSPACE/response.json
fi
done
NEXT=`cat $WORKSPACE/delete_repos.json | jq -r '.next' | sed -e 's/^null$//'`
if [[ -n "$NEXT" ]]; then
remove_repos
fi
}
function remove_projects() {
curl -s -u "$CLOUD_CREDS" -XGET -H "$CJSON" -H "$AJSON" "$CHOST/teams/$COWNER/projects/" 2>&1 > $WORKSPACE/delete_projects.json
for KEY in `cat $WORKSPACE/delete_projects.json | jq -r '.values[].key'`; do
echo -n "Removing Project $KEY..."
curl -s -u "$CLOUD_CREDS" -XDELETE -H "$CJSON" -H "$AJSON" "$CHOST/teams/$COWNER/projects/$KEY" 2>&1 > $WORKSPACE/response.json
if [[ $? -eq 0 ]] && ! [[ `jq -r '.type' $WORKSPACE/response.json` == "error" ]]; then
echo 'done'
else
echo 'failed'
jq '.' $WORKSPACE/response.json
fi
done
NEXT=`cat $WORKSPACE/delete_projects.json | jq -r '.next' | sed -e 's/^null$//'`
if [[ -n "$NEXT" ]]; then
remove_projects
fi
}
function remove_project_repos() {
remove_repos
remove_projects
}
remove_project_repos
create_project_repos
rm -rf $WORKSPACE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment