Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dst_visibility support #160

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/verify-on-ubuntu-org.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
on:
push:
pull_request_target:
branches: [master]
name: Tests / test-org-mirror
Expand Down
23 changes: 23 additions & 0 deletions .github/workflows/verify-on-ubuntu-private.yml
Original file line number Diff line number Diff line change
@@ -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'
1 change: 0 additions & 1 deletion .github/workflows/verify-on-ubuntu-user-cache.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
on:
push:
pull_request_target:
branches: [master]
name: Tests / test-user-mirror (cached)
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/verify-on-ubuntu-user.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
on:
push:
pull_request_target:
branches: [master]
name: Tests / test-user-mirror
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,意味着新创建的仓将默认的设置为私有仓,已存在的仓库则只会进行同步。

## 举些例子

Expand Down
1 change: 1 addition & 0 deletions README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -73,3 +76,4 @@ runs:
- ${{ inputs.debug}}
- ${{ inputs.timeout}}
- ${{ inputs.mappings}}
- ${{ inputs.dst_visibility }}
3 changes: 2 additions & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 $?
20 changes: 17 additions & 3 deletions hub-mirror/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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":
Expand All @@ -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:
Expand Down
1 change: 1 addition & 0 deletions hub-mirror/hubmirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('/')

Expand Down