Last active
April 16, 2018 12:59
-
-
Save arciisine/e0cd830fe9a159faf17ceb20a47f7093 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
COWNER=eaiti | |
SOWNER=git.eaiti.com | |
SHOST=https://$SOWNER/rest/api/1.0 | |
SERVER_CREDS=$1 | |
CHOST=https://api.bitbucket.org/2.0 | |
CLOUD_CREDS=$2 | |
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] | @csv' |\ | |
sed -e 's|"||g' > $WORKSPACE/projects-server.csv | |
for LINE in `cat $WORKSPACE/projects-server.csv | tr ' ' '_'`; 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 }'` | |
PRIVATE=true | |
if [[ "$PUBLIC" == "true" ]]; then | |
PRIVATE=false | |
fi | |
PROJECT_BODY=$(cat << EOM | |
{ | |
"name": "$NAME", | |
"key": "$KEY", | |
"description": "$NAME", | |
"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' | |
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 | tr ' ' '_'`; 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 | |
RQUAL_NAME="${KEY}-${RNAME}" | |
if [[ "$RPUBLIC" == "true" ]]; then | |
RPRIVATE=false | |
fi | |
REPO_BODY=$(cat << EOM | |
{ | |
"scm": "git", | |
"name": "${RQUAL_NAME}", | |
"key": "$RKEY", | |
"description": "$RNAME", | |
"is_private": $RPRIVATE, | |
"fork_policy": "no_public_forks", | |
"has_issues": true, | |
"has_wiki": true, | |
"project": { | |
"key": "$KEY" | |
} | |
} | |
EOM | |
) | |
SLUG=${KEY}_${RKEY} | |
SLUG=`echo "$SLUG" | tr [:upper:] [:lower:] | tr '-' '_'` | |
echo -n " Creating Repository $SLUG..." | |
curl -s -u "$CLOUD_CREDS" -XPOST -H "$CJSON" -H "$AJSON" "$CHOST/repositories/$COWNER/$SLUG" -d "$REPO_BODY" 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 | |
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