Last active
May 18, 2021 11:59
-
-
Save liangzai-cool/2835f1b94fd24a9385354dd5120eff2b to your computer and use it in GitHub Desktop.
gitee.com所有仓库同步到github私有仓库
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
source_code_dir="~/sourcecode/gitee.com/" | |
token="your_gitee_com_token" | |
gh_token="your_github_com_token" | |
current_dir=$(pwd) | |
response=$(curl -X GET "https://gitee.com/api/v5/user/repos?access_token=${token}&sort=created&page=1&per_page=20") | |
project_count=$(echo $response | jq '. | length') | |
echo $project_count | |
for(( i = 0; i < $project_count; i = i + 1 )) | |
do | |
operation="clone" | |
item=$(echo $response | jq -r ".[$i]") | |
name=$(echo $item | jq -r ".name") | |
description=$(echo $item | jq -r ".description") | |
owner_user_name=$(echo $item | jq -r ".owner.name") | |
ssh_url=$(echo $item | jq -r ".ssh_url") | |
project_local_dir="$source_code_dir$owner_user_name/$name" | |
if [ -d "$project_local_dir/.git" ]; then | |
echo "$project_local_dir/.git 存在" | |
operation="pull" | |
else | |
echo "$project_local_dir/.git 不存在" | |
fi | |
echo "正在 $operation 项目:$ssh_url" | |
if [ $operation = "clone" ]; then | |
git clone $ssh_url "$project_local_dir" | |
else | |
cd "$project_local_dir" | |
git pull | |
cd $current_dir | |
fi | |
echo -e "完成 $operation 项目:$ssh_url\n" | |
create_repo_response=$(curl --request POST \ | |
--url https://api.github.com/user/repos \ | |
--header "Authorization: token $gh_token" \ | |
--header "cache-control: no-cache" \ | |
--data "{\"name\": \"$name\", \"description\": \"$description\", \"private\": true}") | |
errors_length=$(echo $create_repo_response | jq -r ".errors | length") | |
if [ $errors_length = 0 ]; then | |
echo "仓库创建成功:$name" | |
gh_ssh_url=$(echo $create_repo_response | jq -r ".ssh_url") | |
cd $project_local_dir | |
git remote add github $gh_ssh_url | |
git push github --all | |
echo "创库推送成功:$name" | |
else | |
echo "仓库创建失败:$name,$create_repo_response" | |
fi | |
echo -e "完成同步项目:$name\n" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment