From adacfe32a42b3fccd58f65183d7e76147000527c Mon Sep 17 00:00:00 2001 From: Yikun Jiang Date: Tue, 27 Jul 2021 21:35:23 +0800 Subject: [PATCH] Add mappings feature --- action.yml | 4 ++++ entrypoint.sh | 1 + hub-mirror/__init__.py | 0 hub-mirror/hubmirror.py | 6 ++++- hub-mirror/test_hubmirror.py | 40 ++++++++++++++++++++++++++++++++++ hub-mirror/tests/__init__.py | 0 hub-mirror/tests/test_utils.py | 15 +++++++++++++ hub-mirror/utils.py | 12 ++++++++++ 8 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 hub-mirror/__init__.py create mode 100644 hub-mirror/test_hubmirror.py create mode 100644 hub-mirror/tests/__init__.py create mode 100644 hub-mirror/tests/test_utils.py diff --git a/action.yml b/action.yml index 4db9575d..50af9875 100644 --- a/action.yml +++ b/action.yml @@ -44,6 +44,9 @@ inputs: timeout: description: "Set the timeout for every git command, like '600'=>600s, '30m'=>30 mins, '1h'=>1 hours" default: '30m' + 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: '' runs: using: "docker" image: "Dockerfile" @@ -60,3 +63,4 @@ runs: - ${{ inputs.static_list }} - ${{ inputs.force_update}} - ${{ inputs.debug}} + - ${{ inputs.mappings}} diff --git a/entrypoint.sh b/entrypoint.sh index 26ca7f62..8f123d77 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -23,6 +23,7 @@ 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}" # Skip original code exit $? diff --git a/hub-mirror/__init__.py b/hub-mirror/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/hub-mirror/hubmirror.py b/hub-mirror/hubmirror.py index 78e8821c..a4aa74ab 100644 --- a/hub-mirror/hubmirror.py +++ b/hub-mirror/hubmirror.py @@ -2,7 +2,7 @@ import sys import yaml -from utils import str2bool, str2list +from utils import str2bool, str2list, str2map from hub import Hub from mirror import Mirror @@ -14,6 +14,7 @@ def __init__(self): self.white_list = str2list(self.args.white_list) self.black_list = str2list(self.args.black_list) self.static_list = str2list(self.args.static_list) + self.mappings = str2map(self.args.mappings) def _create_parser(self): with open('/action.yml', 'r') as f: @@ -65,6 +66,9 @@ def run(self): total, success, skip = len(src_repos), 0, 0 failed_list = [] for repo in src_repos: + if repo in self.mappings: + print("Map %s to %s" % (repo, self.mappings[repo])) + repo = self.mappings[repo] if self.test_black_white_list(repo): print("Backup %s" % repo) try: diff --git a/hub-mirror/test_hubmirror.py b/hub-mirror/test_hubmirror.py new file mode 100644 index 00000000..7814b338 --- /dev/null +++ b/hub-mirror/test_hubmirror.py @@ -0,0 +1,40 @@ +import unittest +from hubmirror import Mirror +from hubmirror import Hub +from hubmirror import HubMirror +import shutil + +class TestMirror(unittest.TestCase): + + def test_download(self): + self.github2gitee = Hub( + "github/hub-mirror-action", + "gitee/hub-mirror-action", + "dd68f7a02fb01594b4d5de363fe0bf67", + "org", + "ssh" + ) + self.gitee2github = Hub( + "gitee/hub-mirror-action", + "github/hub-mirror-action", + "9d0a194733b7bbc6d8c042736b1250f5e6009ab1", + "org", + "ssh" + ) + self.m1 = Mirror(self.gitee2github, "normal") + # Download a repo from gitee + self.m1.download() + # Create a repo in github + # self.m1.create() + + # self.m2 = Mirror(self.github2gitee, "normal") + # # Download a repo from github + # self.m2.download() + # # Create a repo in gitee + # self.m2.create() + # self.m2.push(force=True) + + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/hub-mirror/tests/__init__.py b/hub-mirror/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/hub-mirror/tests/test_utils.py b/hub-mirror/tests/test_utils.py new file mode 100644 index 00000000..f50b1571 --- /dev/null +++ b/hub-mirror/tests/test_utils.py @@ -0,0 +1,15 @@ +import unittest +from utils import str2map + + +class TestUtils(unittest.TestCase): + + def test_str2map(self): + self.assertEqual(str2map("a=>b, c=>d"), {'a': 'b', 'c': 'd'}) + self.assertEqual(str2map("a=>b,c=>d"), {'a': 'b', 'c': 'd'}) + # No transitivity + self.assertEqual(str2map("cc=>c,c=>d"), {'cc': 'c', 'c': 'd'}) + + +if __name__ == '__main__': + unittest.main() diff --git a/hub-mirror/utils.py b/hub-mirror/utils.py index c505a414..87d6b0e2 100644 --- a/hub-mirror/utils.py +++ b/hub-mirror/utils.py @@ -35,3 +35,15 @@ def str2list(s): if not s: return [] return s.replace(' ', '').split(',') if s else [] + + +# "a=>b, c=>d" to {'a': 'b', 'c': 'd'} +def str2map(s): + if not s: + return {} + mappings = {} + mappings_list = str2list(s) + for maping in mappings_list: + old, new = maping.split("=>") + mappings[old] = new + return mappings