Skip to content

Commit

Permalink
Add black and white list support (#20)
Browse files Browse the repository at this point in the history
This patch adds the black and white list support for hub
mirror action.

The back list has **high priority**, that mean if the repo
in the back list will never mirror, even the repo is also
in the white list.

And the white list can be used as single repo mirror, you
can set white_list as specific repo to only sync this single
repo.

Closes: #19, #14
Related: #18
  • Loading branch information
Yikun authored Mar 20, 2020
1 parent c4dc341 commit d7f65e2
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/verify-on-ubuntu-org.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ jobs:
dst_key: ${{ secrets.GITEE_PRIVATE_KEY }}
dst_token: ${{ secrets.GITEE_TOKEN }}
account_type: org
# Only sync Kunpeng
black_list: 'KAE'
white_list: 'KAE,Kunpeng'
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 @@ -19,14 +19,15 @@ jobs:
if: steps.cache.outputs.cache-hit == 'true'
run: echo "Cached successfully."

- name: Mirror Github to Gitee
- name: Mirror Github to Gitee with white list and cache
uses: ./.
with:
src: github/Yikun
dst: gitee/yikunkero
dst_key: ${{ secrets.GITEE_PRIVATE_KEY }}
dst_token: ${{ secrets.GITEE_TOKEN }}
cache_path: /github/workspace/hub-mirror-cache
white_list: 'hub-mirror-action, hexo-migrator-github-issue'

- name: Print cache path
run: |
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/verify-on-ubuntu-user.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ jobs:
steps:
- name: Checkout source codes
uses: actions/checkout@v1
- name: Mirror Github to Gitee
- name: Mirror Github to Gitee with white list
uses: ./.
with:
src: github/Yikun
dst: gitee/yikunkero
dst_key: ${{ secrets.GITEE_PRIVATE_KEY }}
dst_token: ${{ secrets.GITEE_TOKEN }}
white_list: 'hub-mirror-action'
8 changes: 8 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ inputs:
cache_path:
description: "The path to cache the source repos code."
default: '/github/workspace/hub-mirror-cache'
black_list:
description: "Hight priority, the back list of mirror repo. like 'repo1,repo2,repo3'"
default: ''
white_list:
description: "Low priority, the white list of mirror repo. like 'repo1,repo2,repo3'"
default: ''
runs:
using: "docker"
image: "Dockerfile"
Expand All @@ -37,3 +43,5 @@ runs:
- ${{ inputs.account_type }}
- ${{ inputs.clone_style }}
- ${{ inputs.cache_path }}
- ${{ inputs.black_list }}
- ${{ inputs.white_list }}
53 changes: 42 additions & 11 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ CLONE_STYLE="${INPUT_CLONE_STYLE}"

CACHE_PATH="${INPUT_CACHE_PATH}"

WHITE_LIST="${INPUT_WHITE_LIST}"
BLACK_LIST="${INPUT_BLACK_LIST}"

if [[ "$ACCOUNT_TYPE" == "org" ]]; then
SRC_LIST_URL_SUFFIX=orgs/$SRC_ACCOUNT/repos
DST_LIST_URL_SUFFIX=orgs/$DST_ACCOUNT/repos
Expand Down Expand Up @@ -104,27 +107,55 @@ function import_repo
git push $DST_TYPE refs/remotes/origin/*:refs/heads/* --tags --prune
}

function _check_in_list () {
local e match="$1"
shift
for e; do [[ "$e" == "$match" ]] && return 0; done
return 1
}

function test_black_white_list
{
WHITE_ARR=(`echo $WHITE_LIST | tr ',' ' '`)
BLACK_ARR=(`echo $BLACK_LIST | tr ',' ' '`)
_check_in_list $1 "${WHITE_ARR[@]}";in_white_list=$?
_check_in_list $1 "${BLACK_ARR[@]}";in_back_list=$?

if [[ $in_back_list -ne 0 ]] ; then
if [[ -z $WHITE_LIST ]] || [[ $in_white_list -eq 0 ]] ; then
return 0
else
echo "Skip, "$1" not in non-empty white list"$WHITE_LIST
return 1
fi
else
echo "Skip, "$1 "in black list: "$BLACK_LIST
return 1
fi
}

if [ ! -d "$CACHE_PATH" ]; then
mkdir -p $CACHE_PATH
fi
cd $CACHE_PATH

for repo in $SRC_REPOS
{
echo -e "\n\033[31mBackup $repo ...\033[0m"
if test_black_white_list $repo ; then
echo -e "\n\033[31mBackup $repo ...\033[0m"

cd_src_repo $repo
cd_src_repo $repo

add_remote_repo $repo $DST_TOKEN
add_remote_repo $repo $DST_TOKEN

update_repo
update_repo

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

cd ..
cd ..
fi
}

0 comments on commit d7f65e2

Please sign in to comment.