-
Notifications
You must be signed in to change notification settings - Fork 1
/
import.sh
executable file
·95 lines (80 loc) · 2.03 KB
/
import.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env bash
##### CONFIG
source config.sh
user_g=${user_github}
user_b=${user_bitbucket}
token_g=${token_github}
token_b=$(curl https://bitbucket.org/site/oauth2/access_token \
-d grant_type=client_credentials \
-u ${key_bitbucket}:${secret_bitbucket} | \
python ./access.py)
##### LIST REPOSITORIES
get_repos () {
curl -H 'Authorization: Bearer '"$token_b" \
'https://api.bitbucket.org/2.0/repositories/'"$user_b"'?page='"$2" | \
python './'"$1"'.py'
}
per_page=10
total=$(bc <<< 'scale = 1; (('"$(get_repos total 1)"'/'"$per_page"')+0.5)/1')
total_pages=$(bc <<< '('"$total"'+0.5)/1')
repos=''
collect_all_repos () {
current_page=${total_pages}
while [ ${current_page} -gt 0 ]
do
repos+=$(get_repos names ${current_page})
((current_page--))
done
}
##### CREATE REPO
create_repo () {
curl -H 'Authorization: token '"$token_g" \
-d "{
\"name\": \"$1\",
\"private\": true
}" \
https://api.github.com/user/repos
}
##### DELETE REPO
delete_repo () {
curl -X DELETE -H 'Authorization: token '"$token_g" \
https://api.github.com/repos/${user_g}/$1
}
delete_all () {
for name in ${repositories_total[@]}
do
delete_repo ${name}
echo 'Successfully deleted '"$name"
done
echo Deleted all successfully.
}
##### IMPORT REPO & CLEAN UP
git_clone () {
git clone --bare 'https://x-token-auth:'"$token_b"'@bitbucket.org/'"$user_b"'/'"$1"'.git'
}
git_push () {
cd "$1"'.git' && git push --mirror '[email protected]:'"$user_g"'/'"$1"'.git'
}
repo_rm () {
cd .. && rm -rf "$1"'.git'
}
git_import () {
create_repo $1
git_clone $1
git_push $1
repo_rm $1
}
import_all () {
i=1
for name in ${repositories_total[@]}
do
git_import ${name}
echo 'Successfully imported #'"$i"': '"$name"
((i++))
done
echo "$i"' repositories have been successfully imported.'
}
##### EXECUTE
collect_all_repos
repositories_total=($repos)
import_all