source_code_dir="/Users/username/sourcecode/dev.tencent.com/"
token="your_coding_net_token"
gh_token="your_github_com_token"
current_dir=$(pwd)
response=$(curl -X GET "https://coding.net/api/user/projects?type=all&sort=&page=1&pageSize=100" -H "Authorization: token ${token}")
project_count=$(echo $response | jq '.data.list | length')
echo $project_count
for(( i = 0; i < $project_count; i = i + 1 ))
do
operation="clone"
item=$(echo $response | jq -r ".data.list[$i]")
name=$(echo $item | jq -r ".name")
description=$(echo $item | jq -r ".description")
owner_user_name=$(echo $item | jq -r ".owner_user_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