Skip to content

Commit

Permalink
Feat: add src_account_type and dst_account_type options to mirror dif…
Browse files Browse the repository at this point in the history
…ference 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
  • Loading branch information
snowyu authored Dec 13, 2021
1 parent d7e2547 commit 81f1a4c
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 10 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)找到完整用法。
Expand All @@ -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”。
Expand Down
8 changes: 6 additions & 2 deletions README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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”.
Expand Down Expand Up @@ -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).
Expand Down
8 changes: 8 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 }}
Expand Down
2 changes: 2 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}" \
Expand Down
30 changes: 24 additions & 6 deletions hub-mirror/hub.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import time
import functools
import json

Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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':
Expand All @@ -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)
Expand All @@ -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)

Expand Down
4 changes: 3 additions & 1 deletion hub-mirror/hubmirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('/')

Expand Down

0 comments on commit 81f1a4c

Please sign in to comment.