From 6e82caecdbf2dca1e0f01d39f17431f37c025859 Mon Sep 17 00:00:00 2001 From: Yikun Jiang Date: Tue, 7 Jun 2022 10:27:07 +0800 Subject: [PATCH] Add dst_visibility support --- .github/workflows/verify-on-ubuntu-org.yml | 1 - .../workflows/verify-on-ubuntu-private.yml | 23 +++++++++++++++++++ .../workflows/verify-on-ubuntu-user-cache.yml | 1 - .github/workflows/verify-on-ubuntu-user.yml | 1 - README.md | 1 + README_en.md | 1 + action.yml | 4 ++++ entrypoint.sh | 3 ++- hub-mirror/hub.py | 20 +++++++++++++--- hub-mirror/hubmirror.py | 1 + 10 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/verify-on-ubuntu-private.yml diff --git a/.github/workflows/verify-on-ubuntu-org.yml b/.github/workflows/verify-on-ubuntu-org.yml index 88bed90c..83351461 100644 --- a/.github/workflows/verify-on-ubuntu-org.yml +++ b/.github/workflows/verify-on-ubuntu-org.yml @@ -1,5 +1,4 @@ on: - push: pull_request_target: branches: [master] name: Tests / test-org-mirror diff --git a/.github/workflows/verify-on-ubuntu-private.yml b/.github/workflows/verify-on-ubuntu-private.yml new file mode 100644 index 00000000..b17bc1c8 --- /dev/null +++ b/.github/workflows/verify-on-ubuntu-private.yml @@ -0,0 +1,23 @@ +on: + pull_request_target: + branches: [master] +name: Tests / test-user-mirror +jobs: + run: + name: Run + runs-on: ubuntu-latest + steps: + - name: Checkout source codes + uses: actions/checkout@v1 + - 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 }} + static_list: 'hashes,simple-spring' + force_update: true + debug: true + mappings: 'hashes=>private_hashes,simple-spring=>private_simple-spring' + dst_visibility: 'private' diff --git a/.github/workflows/verify-on-ubuntu-user-cache.yml b/.github/workflows/verify-on-ubuntu-user-cache.yml index 46c2bfa1..5eae9302 100644 --- a/.github/workflows/verify-on-ubuntu-user-cache.yml +++ b/.github/workflows/verify-on-ubuntu-user-cache.yml @@ -1,5 +1,4 @@ on: - push: pull_request_target: branches: [master] name: Tests / test-user-mirror (cached) diff --git a/.github/workflows/verify-on-ubuntu-user.yml b/.github/workflows/verify-on-ubuntu-user.yml index 2032cd40..db0f267a 100644 --- a/.github/workflows/verify-on-ubuntu-user.yml +++ b/.github/workflows/verify-on-ubuntu-user.yml @@ -1,5 +1,4 @@ on: - push: pull_request_target: branches: [master] name: Tests / test-user-mirror diff --git a/README.md b/README.md index be4390bf..a3404aa1 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ steps: - `debug` 默认为false, 配置后,启用debug开关,会显示所有执行命令。 - `timeout` 默认为'30m', 用于设置每个git命令的超时时间,'600'=>600s, '30m'=>30 mins, '1h'=>1 hours - `mappings` 源仓库映射规则,比如'A=>B, C=>CC', A会被映射为B,C会映射为CC,映射不具有传递性。主要用于源和目的仓库名不同的镜像。 +- `dst_visibility` 默认为public,当前配置为priavte,意味着新创建的仓将默认的设置为私有仓,已存在的仓库则只会进行同步。 ## 举些例子 diff --git a/README_en.md b/README_en.md index 1b39367c..414636fd 100644 --- a/README_en.md +++ b/README_en.md @@ -47,6 +47,7 @@ More than [100+](https://github.com/search?p=2&q=hub-mirror-action+%22account_ty - `force_update` (optional) Force to update the destination repo, use '-f' flag do 'git push' - `timeout` (optional) Default is '30m', set the timeout for every git command, like '600'=>600s, '30m'=>30 mins, '1h'=>1 hours - `mappings` (optional) Default is empty, the source repos mappings, such as 'A=>B, C=>CC', source repo name would be mapped follow the rule: A to B, C to CC. Mapping is not transitive. +- `dst_visibility` (optional) Default is public. If set to `private`, new creating repo visibility is `private`, but if destination repo have been created, the repo visibility will not be set, just only mirror the repo. ## Scenarios diff --git a/action.yml b/action.yml index a7d49ea9..ff195d6e 100644 --- a/action.yml +++ b/action.yml @@ -53,6 +53,9 @@ inputs: mappings: description: "The source repos mappings, such as 'A=>B, C=>CC', source repo name would be mapped follow the rule: A to B, C to CC. Mapping is not transitive." default: '' + dst_visibility: + description: "set destination repo visibility to public or private" + default: 'public' runs: using: "docker" image: "Dockerfile" @@ -73,3 +76,4 @@ runs: - ${{ inputs.debug}} - ${{ inputs.timeout}} - ${{ inputs.mappings}} + - ${{ inputs.dst_visibility }} diff --git a/entrypoint.sh b/entrypoint.sh index b6566fde..8f060402 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -25,7 +25,8 @@ python3 /hub-mirror/hubmirror.py --src "${INPUT_SRC}" --dst "${INPUT_DST}" \ --force-update "${INPUT_FORCE_UPDATE}" \ --debug "${INPUT_DEBUG}" \ --timeout "${INPUT_TIMEOUT}" \ ---mappings "${INPUT_MAPPINGS}" +--mappings "${INPUT_MAPPINGS}" \ +--dst-visibility "${INPUT_DST_VISIBILITY}" # Skip original code exit $? diff --git a/hub-mirror/hub.py b/hub-mirror/hub.py index b88bad3c..c6540bf7 100644 --- a/hub-mirror/hub.py +++ b/hub-mirror/hub.py @@ -11,6 +11,7 @@ def __init__( clone_style="https", src_account_type=None, dst_account_type=None, + dst_visibility='public' ): # TODO: check invalid type self.account_type = account_type @@ -37,6 +38,15 @@ def __init__( # TODO: toekn push support prefix = "git@" + self.dst_type + ".com:" self.dst_repo_base = prefix + self.dst_account + # Currently, if dst_visibility is not 'public', create the private + # repo when mirroring. + # We perhaps also support: + # - `auto`: dst visibility according to src visibility + # - `private`: dst visibility is force set to private + # - `public`: dst visibility is force set to public + # See also: + # https://github.com/Yikun/hub-mirror-action/issues/38#issuecomment-1094033841 # noqa: E501 + self.dst_private = False if dst_visibility == 'public' else True def has_dst_repo(self, repo_name): url = '/'.join( @@ -60,9 +70,13 @@ def create_dst_repo(self, repo_name): ) result = None if self.dst_type == 'gitee': - data = {'name': repo_name} + data = { + 'name': repo_name, + "access_token": self.dst_token, + 'private': self.dst_private + } elif self.dst_type == 'github': - data = json.dumps({'name': repo_name}) + data = json.dumps({'name': repo_name, 'private': self.dst_private}) if not self.has_dst_repo(repo_name): print(repo_name + " doesn't exist, create it...") if self.dst_type == "github": @@ -80,7 +94,7 @@ def create_dst_repo(self, repo_name): response = requests.post( url, headers={'Content-Type': 'application/json;charset=UTF-8'}, - params={"name": repo_name, "access_token": self.dst_token} + params=data ) result = response.status_code == 201 if result: diff --git a/hub-mirror/hubmirror.py b/hub-mirror/hubmirror.py index e87eab4c..50b3c0ce 100644 --- a/hub-mirror/hubmirror.py +++ b/hub-mirror/hubmirror.py @@ -58,6 +58,7 @@ def run(self): clone_style=self.args.clone_style, src_account_type=self.args.src_account_type, dst_account_type=self.args.dst_account_type, + dst_visibility=self.args.dst_visibility, ) src_type, src_account = self.args.src.split('/')