Skip to content

Commit

Permalink
Improve the exception handle
Browse files Browse the repository at this point in the history
- Added the debug arg to enable the detail log
- improve some exception warning
  • Loading branch information
Yikun committed Sep 1, 2020
1 parent e68b0fc commit 2ded29f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
1 change: 1 addition & 0 deletions .github/workflows/verify-on-ubuntu-org.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ jobs:
black_list: 'KAE'
white_list: 'KAE,Kunpeng,kunpengcompute.github.io'
force_update: true
debug: true
3 changes: 2 additions & 1 deletion .github/workflows/verify-on-ubuntu-user-cache.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ jobs:
dst_token: ${{ secrets.GITEE_TOKEN }}
cache_path: /github/workspace/hub-mirror-cache
white_list: 'hub-mirror-action, hexo-migrator-github-issue'
force_update: true

- name: Print cache path
run: |
ls -la /home/runner/work/hub-mirror-action/hub-mirror-action/hub-mirror-cache
ls -la /home/runner/work/hub-mirror-action/hub-mirror-action/hub-mirror-cache
2 changes: 2 additions & 0 deletions .github/workflows/verify-on-ubuntu-user.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ jobs:
dst_key: ${{ secrets.GITEE_PRIVATE_KEY }}
dst_token: ${{ secrets.GITEE_TOKEN }}
static_list: 'hub-mirror-action,hashes,simple-spring'
force_update: true
debug: true
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ inputs:
force_update:
description: "Force to update the destination repo, use '-f' flag do 'git push'"
default: false
debug:
description: "Enable the debug flag to show detail log"
default: false
runs:
using: "docker"
image: "Dockerfile"
Expand All @@ -53,3 +56,4 @@ runs:
- ${{ inputs.white_list }}
- ${{ inputs.static_list }}
- ${{ inputs.force_update}}
- ${{ inputs.debug}}
39 changes: 21 additions & 18 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#!/bin/bash

set -x
DEBUG="${INPUT_DEBUG}"

if [[ "$DEBUG" == "true" ]]; then
set -x
fi

mkdir -p /root/.ssh
echo "${INPUT_DST_KEY}" > /root/.ssh/id_rsa
chmod 600 /root/.ssh/id_rsa
Expand Down Expand Up @@ -28,6 +33,11 @@ STATIC_LIST="${INPUT_STATIC_LIST}"

FORCE_UPDATE="${INPUT_FORCE_UPDATE}"

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

if [[ "$ACCOUNT_TYPE" == "org" ]]; then
SRC_LIST_URL_SUFFIX=orgs/$SRC_ACCOUNT/repos
DST_LIST_URL_SUFFIX=orgs/$DST_ACCOUNT/repos
Expand All @@ -37,8 +47,7 @@ elif [[ "$ACCOUNT_TYPE" == "user" ]]; then
DST_LIST_URL_SUFFIX=users/$DST_ACCOUNT/repos
DST_CREATE_URL_SUFFIX=user/repos
else
echo "Unknown account type, the `account_type` should be `user` or `org`"
exit 1
err_exit "Unknown account type, the `account_type` should be `user` or `org`"
fi

if [[ "$SRC_TYPE" == "github" ]]; then
Expand All @@ -56,8 +65,7 @@ elif [[ "$SRC_TYPE" == "gitee" ]]; then
SRC_REPO_BASE_URL=https://gitee.com/
fi
else
echo "Unknown src args, the `src` should be `[github|gittee]/account`"
exit 1
err_exit "Unknown src args, the `src` should be `[github|gittee]/account`"
fi

if [[ -z $STATIC_LIST ]]; then
Expand All @@ -73,12 +81,10 @@ elif [[ "$DST_TYPE" == "gitee" ]]; then
DST_REPO_CREATE_API=https://gitee.com/api/v5/$DST_CREATE_URL_SUFFIX
DST_REPO_LIST_API=https://gitee.com/api/v5/$DST_LIST_URL_SUFFIX
else
echo "Unknown dst args, the `dst` should be `[github|gittee]/account`"
exit 1
err_exit "Unknown dst args, the `dst` should be `[github|gittee]/account`"
fi


function cd_src_repo
function clone_repo
{
echo -e "\033[31m(0/3)\033[0m" "Downloading..."
if [ ! -d "$1" ]; then
Expand All @@ -87,11 +93,12 @@ function cd_src_repo
cd $1
}

function add_remote_repo
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`
if [ $has_repo == 0 ]; then
echo "Create non-exist repo..."
if [[ "$DST_TYPE" == "github" ]]; then
curl -H "Authorization: token $2" --data '{"name":"'$1'"}' $DST_REPO_CREATE_API
elif [[ "$DST_TYPE" == "gitee" ]]; then
Expand Down Expand Up @@ -155,17 +162,13 @@ for repo in $SRC_REPOS
if test_black_white_list $repo ; then
echo -e "\n\033[31mBackup $repo ...\033[0m"

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

add_remote_repo $repo $DST_TOKEN
create_repo $repo $DST_TOKEN || echo "create failed"

update_repo
update_repo || echo "Update failed"

if [ $? -eq 0 ]; then
import_repo
else
echo -e "\033[31mUpdate failed.\033[0m" ""
fi
import_repo || err_exit "Push failed"

cd ..
fi
Expand Down

0 comments on commit 2ded29f

Please sign in to comment.