Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add src and dst repo list paging support #67

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 31 additions & 17 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,20 @@ FORCE_UPDATE="${INPUT_FORCE_UPDATE}"

DELAY_EXIT=false

TIME_OUT=600

function err_exit {
echo -e "\033[31m $1 \033[0m"
exit 1
}

FAILED_LIST=()

function delay_exit {
echo -e "\033[31m $1 \033[0m"
FAILED_LIST+=($2)
DELAY_EXIT=true
return 1
}

if [[ "$ACCOUNT_TYPE" == "org" ]]; then
Expand Down Expand Up @@ -78,10 +84,12 @@ fi
function get_all_repo_names
{
PAGE_NUM=100
if [[ "$SRC_TYPE" == "github" ]]; then
total=`curl -sI "$SRC_REPO_LIST_API?page=1&per_page=$PAGE_NUM" | sed -nr "s/^[lL]ink:.*page=([0-9]+)&per_page=$PAGE_NUM.*/\1/p"`
elif [[ "$SRC_TYPE" == "gitee" ]]; then
total=`curl -sI "$SRC_REPO_LIST_API?page=1&per_page=$PAGE_NUM" | grep total_page: |cut -d ' ' -f2 |tr -d '\r'`
URL=$1
HUB_TYPE=$2
if [[ "$HUB_TYPE" == "github" ]]; then
total=`curl -sI "$URL?page=1&per_page=$PAGE_NUM" | sed -nr "s/^[lL]ink:.*page=([0-9]+)&per_page=$PAGE_NUM.*/\1/p"`
elif [[ "$HUB_TYPE" == "gitee" ]]; then
total=`curl -sI "$URL?page=1&per_page=$PAGE_NUM" | grep total_page: |cut -d ' ' -f2 |tr -d '\r'`
fi

# use pagination?
Expand All @@ -92,14 +100,14 @@ function get_all_repo_names

p=1
while [ "$p" -le "$total" ]; do
x=`curl -s "$SRC_REPO_LIST_API?page=$p&per_page=$PAGE_NUM" | jq '.[] | .name' | sed 's/"//g'`
x=`curl -s "$URL?page=$p&per_page=$PAGE_NUM" | jq '.[] | .name' | sed 's/"//g'`
echo $x
p=$(($p + 1))
done
}

if [[ -z $STATIC_LIST ]]; then
SRC_REPOS=`get_all_repo_names`
SRC_REPOS=`get_all_repo_names $SRC_REPO_LIST_API $SRC_TYPE`
else
SRC_REPOS=`echo $STATIC_LIST | tr ',' ' '`
fi
Expand All @@ -114,19 +122,21 @@ else
err_exit "Unknown dst args, the `dst` should be `[github|gittee]/account`"
fi

DST_REPOS=`get_all_repo_names $DST_REPO_LIST_API $DST_TYPE`

function clone_repo
{
echo -e "\033[31m(0/3)\033[0m" "Downloading..."
if [ ! -d "$1" ]; then
git clone $SRC_REPO_BASE_URL$SRC_ACCOUNT/$1.git
timeout $TIME_OUT git clone $SRC_REPO_BASE_URL$SRC_ACCOUNT/$1.git
fi
cd $1
}

function create_repo
{
# Auto create non-existing repo
has_repo=`curl $DST_REPO_LIST_API | jq '.[] | select(.full_name=="'$DST_ACCOUNT'/'$1'").name' | wc -l`
has_repo=`echo $DST_REPOS | grep $1 | wc -l`
if [ $has_repo == 0 ]; then
echo "Create non-exist repo..."
if [[ "$DST_TYPE" == "github" ]]; then
Expand All @@ -135,23 +145,23 @@ function create_repo
curl -X POST --header 'Content-Type: application/json;charset=UTF-8' $DST_REPO_CREATE_API -d '{"name": "'$1'","access_token": "'$2'"}'
fi
fi
git remote add $DST_TYPE git@$DST_TYPE.com:$DST_ACCOUNT/$1.git
git remote add $DST_TYPE git@$DST_TYPE.com:$DST_ACCOUNT/$1.git || echo "Remote already exists."
}

function update_repo
{
echo -e "\033[31m(1/3)\033[0m" "Updating..."
git pull -p
timeout $TIME_OUT git pull -p
}

function import_repo
{
echo -e "\033[31m(2/3)\033[0m" "Importing..."
git remote set-head origin -d
if [[ "$FORCE_UPDATE" == "true" ]]; then
git push -f $DST_TYPE refs/remotes/origin/*:refs/heads/* --tags --prune
timeout $TIME_OUT git push -f $DST_TYPE refs/remotes/origin/*:refs/heads/* --tags --prune
else
git push $DST_TYPE refs/remotes/origin/*:refs/heads/* --tags --prune
timeout $TIME_OUT git push $DST_TYPE refs/remotes/origin/*:refs/heads/* --tags --prune
fi
}

Expand Down Expand Up @@ -196,23 +206,27 @@ for repo in $SRC_REPOS
if test_black_white_list $repo ; then
echo -e "\n\033[31mBackup $repo ...\033[0m"

clone_repo $repo || echo "clone and cd failed"
cd $CACHE_PATH

create_repo $repo $DST_TOKEN || echo "create failed"
clone_repo $repo || delay_exit "clone and cd failed" $repo || continue

update_repo || echo "Update failed"
create_repo $repo $DST_TOKEN || delay_exit "create failed" $repo || continue

import_repo && success=$(($success + 1)) || delay_exit "Push failed"
update_repo || delay_exit "Update failed" $repo || continue

cd ..
import_repo && success=$(($success + 1)) || delay_exit "Push failed" $repo || continue
else
skip=$(($skip + 1))
fi
}

failed=$(($all - $skip - $success))
echo "SRC: "$SRC_REPOS
echo "DST: "$DST_REPOS
echo "Failed: "$FAILED_LIST
echo "Total: $all, skip: $skip, successed: $success, failed: $failed."


if [[ "$DELAY_EXIT" == "true" ]]; then
exit 1
fi