From 81f1a4c8d18ec3b443cc7e7980f1a067e327ce10 Mon Sep 17 00:00:00 2001 From: Riceball LEE Date: Mon, 13 Dec 2021 14:44:39 +0800 Subject: [PATCH] Feat: add src_account_type and dst_account_type options to mirror difference account type. (#129) * feat: add src_account_type and dst_account_type options to mirror difference account type * Fix: Add a 2 second delay after the repo is created to prevent errors in reporting that the repo does not exist --- README.md | 6 +++++- README_en.md | 8 ++++++-- action.yml | 8 ++++++++ entrypoint.sh | 2 ++ hub-mirror/hub.py | 30 ++++++++++++++++++++++++------ hub-mirror/hubmirror.py | 4 +++- 6 files changed, 48 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index fbab4238..48ba6d48 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,8 @@ steps: dst_key: ${{ secrets.GITEE_PRIVATE_KEY }} dst_token: ${{ secrets.GITEE_TOKEN }} account_type: org + # src_account_type: org + # dst_account_type: org ``` 上面的配置完成了kunpencompute组织从github到gitee的同步,你可以在[测试和demo](https://github.com/Yikun/hub-mirror-action/tree/master/.github/workflows)找到完整用法。 @@ -36,7 +38,9 @@ steps: - `dst_token` 创建仓库的API tokens, 用于自动创建不存在的仓库,Github可以在[这里](https://github.com/settings/tokens)找到,Gitee可以在[这里](https://gitee.com/profile/personal_access_tokens)找到。 #### 可选参数 -- `account_type` 默认为user,源和目的的账户类型,可以设置为org(组织)或者user(用户),目前仅支持**同类型账户**(即组织到组织,或用户到用户)的同步。 +- `account_type` 默认为user,源和目的的账户类型,可以设置为org(组织)或者user(用户),该参数支持**同类型账户**(即组织到组织,或用户到用户)的同步。如果源目的仓库是不同类型,请单独使用`src_account_type`和`dst_account_type`配置。 +- `src_account_type` 默认为`account_type`,源账户类型,可以设置为org(组织)或者user(用户)。 +- `dst_account_type` 默认为`account_type`,目的账户类型,可以设置为org(组织)或者user(用户)。 - `clone_style` 默认为https,可以设置为ssh或者https。 - `cache_path` 默认为'', 将代码缓存在指定目录,用于与actions/cache配合以加速镜像过程。 - `black_list` 默认为'', 配置后,黑名单中的repos将不会被同步,如“repo1,repo2,repo3”。 diff --git a/README_en.md b/README_en.md index 229bf7e0..d348da58 100644 --- a/README_en.md +++ b/README_en.md @@ -16,6 +16,8 @@ steps: dst_key: ${{ secrets.GITEE_PRIVATE_KEY }} dst_token: ${{ secrets.GITEE_TOKEN }} account_type: org + # src_account_type: org + # dst_account_type: org ``` Here is a workflow to mirror the kunpengcompute org repos from Github to Gitee, see more complete workflows in [here](https://github.com/Yikun/hub-mirror-action/tree/master/.github/workflows). @@ -34,7 +36,9 @@ More than [100+](https://github.com/search?p=2&q=hub-mirror-action+%22account_ty - `dst_token` the API token to create non-existent repo, You can get Github token in [here](https://github.com/settings/tokens), and the Gitee in [here](https://gitee.com/profile/personal_access_tokens). #### Optional -- `account_type` (optional) default is `user`, the account type of src and dst account, can be set to `org` or `user`,only support mirror between same account type (that is "org to org" or "user to user"). +- `account_type` (optional) default is `user`, the account type of src and dst account, can be set to `org` or `user`,only support mirror between same account type (that is "org to org" or "user to user"). if u wanna mirror difference account type, use the `src_account_type` and `dst_account_type` please. +- `src_account_type` (optional) default is `account_type`, the account type of src account, can be set to `org` or `user`. +- `dst_account_type` (optional) default is `account_type`, the account type of dst account, can be set to `org` or `user`. - `clone_style` (optional) default is `https`, can be set to `ssh` or `https`. - `cache_path` (optional) let code clone in specific path, can be used with actions/cache to speed up mirror. - `black_list` (optional) the black list, such as “repo1,repo2,repo3”. @@ -148,7 +152,7 @@ More than [100+](https://github.com/search?p=2&q=hub-mirror-action+%22account_ty ## FAQ - How to use `secrets` to add token and key? - + You can use below steps to add secrets, you can also see more in [Secrets](https://help.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets). 1. **Get Token and Key**,you can get them in [ssh key](https://gitee.com/profile/sshkeys) and [token](https://gitee.com/profile/personal_access_tokens). diff --git a/action.yml b/action.yml index b51b6c6b..a7d49ea9 100644 --- a/action.yml +++ b/action.yml @@ -20,6 +20,12 @@ inputs: account_type: description: "The account type. Such as org, user." default: 'user' + src_account_type: + description: "The src account type. Such as org, user." + default: '' + dst_account_type: + description: "The dst account type. Such as org, user." + default: '' clone_style: description: "The git clone style, https or ssh." default: 'https' @@ -56,6 +62,8 @@ runs: - ${{ inputs.src }} - ${{ inputs.dst }} - ${{ inputs.account_type }} + - ${{ inputs.src_account_type }} + - ${{ inputs.dst_account_type }} - ${{ inputs.clone_style }} - ${{ inputs.cache_path }} - ${{ inputs.black_list }} diff --git a/entrypoint.sh b/entrypoint.sh index fd8603a3..b6566fde 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -15,6 +15,8 @@ pip3 install -r /hub-mirror/requirements.txt python3 /hub-mirror/hubmirror.py --src "${INPUT_SRC}" --dst "${INPUT_DST}" \ --dst-token "${INPUT_DST_TOKEN}" \ --account-type "${INPUT_ACCOUNT_TYPE}" \ +--src-account-type "${INPUT_SRC_ACCOUNT_TYPE}" \ +--dst-account-type "${INPUT_DST_ACCOUNT_TYPE}" \ --clone-style "${INPUT_CLONE_STYLE}" \ --cache-path "${INPUT_CACHE_PATH}" \ --black-list "${INPUT_BLACK_LIST}" \ diff --git a/hub-mirror/hub.py b/hub-mirror/hub.py index c58b54c6..b88bad3c 100644 --- a/hub-mirror/hub.py +++ b/hub-mirror/hub.py @@ -1,3 +1,4 @@ +import time import functools import json @@ -7,10 +8,14 @@ class Hub(object): def __init__( self, src, dst, dst_token, account_type="user", - clone_style="https" + clone_style="https", + src_account_type=None, + dst_account_type=None, ): # TODO: check invalid type self.account_type = account_type + self.src_account_type = src_account_type or account_type + self.dst_account_type = dst_account_type or account_type self.src_type, self.src_account = src.split('/') self.dst_type, self.dst_account = dst.split('/') self.dst_token = dst_token @@ -35,7 +40,10 @@ def __init__( def has_dst_repo(self, repo_name): url = '/'.join( - [self.dst_base, self.account_type+'s', self.dst_account, 'repos'] + [ + self.dst_base, self.dst_account_type+'s', self.dst_account, + 'repos' + ] ) repo_names = self._get_all_repo_names(url) if not repo_names: @@ -45,11 +53,12 @@ def has_dst_repo(self, repo_name): def create_dst_repo(self, repo_name): suffix = 'user/repos' - if self.account_type == "org": + if self.dst_account_type == "org": suffix = 'orgs/%s/repos' % self.dst_account url = '/'.join( [self.dst_base, suffix] ) + result = None if self.dst_type == 'gitee': data = {'name': repo_name} elif self.dst_type == 'github': @@ -62,7 +71,8 @@ def create_dst_repo(self, repo_name): data=data, headers={'Authorization': 'token ' + self.dst_token} ) - if response.status_code == 201: + result = response.status_code == 201 + if result: print("Destination repo creating accepted.") else: print("Destination repo creating failed: " + response.text) @@ -72,16 +82,24 @@ def create_dst_repo(self, repo_name): headers={'Content-Type': 'application/json;charset=UTF-8'}, params={"name": repo_name, "access_token": self.dst_token} ) - if response.status_code == 201: + result = response.status_code == 201 + if result: print("Destination repo creating accepted.") else: print("Destination repo creating failed: " + response.text) else: print(repo_name + " repo exist, skip creating...") + # TODO(snowyu): Cleanup 2s sleep + if result: + time.sleep(2) + return result def dynamic_list(self): url = '/'.join( - [self.src_base, self.account_type+'s', self.src_account, 'repos'] + [ + self.src_base, self.src_account_type+'s', self.src_account, + 'repos', + ] ) return self._get_all_repo_names(url) diff --git a/hub-mirror/hubmirror.py b/hub-mirror/hubmirror.py index 2f5a3c69..e87eab4c 100644 --- a/hub-mirror/hubmirror.py +++ b/hub-mirror/hubmirror.py @@ -55,7 +55,9 @@ def run(self): self.args.dst, self.args.dst_token, account_type=self.args.account_type, - clone_style=self.args.clone_style + clone_style=self.args.clone_style, + src_account_type=self.args.src_account_type, + dst_account_type=self.args.dst_account_type, ) src_type, src_account = self.args.src.split('/')