Skip to content

Commit

Permalink
Add mappings feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Yikun committed Jul 27, 2021
1 parent 132b206 commit adacfe3
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 1 deletion.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -60,3 +63,4 @@ runs:
- ${{ inputs.static_list }}
- ${{ inputs.force_update}}
- ${{ inputs.debug}}
- ${{ inputs.mappings}}
1 change: 1 addition & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 $?
Empty file added hub-mirror/__init__.py
Empty file.
6 changes: 5 additions & 1 deletion hub-mirror/hubmirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
40 changes: 40 additions & 0 deletions hub-mirror/test_hubmirror.py
Original file line number Diff line number Diff line change
@@ -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()
Empty file added hub-mirror/tests/__init__.py
Empty file.
15 changes: 15 additions & 0 deletions hub-mirror/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -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()
12 changes: 12 additions & 0 deletions hub-mirror/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit adacfe3

Please sign in to comment.