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

Separation of concerns #2

Merged
merged 6 commits into from
Jul 25, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor: deduplicate type information from messages
Use match/case when possible.
  • Loading branch information
fbessou committed Jul 24, 2024
commit 7eaefd0f63be7dc0989019802683f60225cdcd53
26 changes: 13 additions & 13 deletions git_hg_sync/repo_synchronizer.py
Original file line number Diff line number Diff line change
@@ -13,7 +13,6 @@ class EntityTypeError(Exception):

@dataclass
class Push:
type: str
repo_url: str
heads: list[str]
commits: list[str]
@@ -25,7 +24,6 @@ class Push:

@dataclass
class Tag:
type: str
repo_url: str
tag: str
commit: str
@@ -42,13 +40,14 @@ def __init__(self, repos_config):

def parse_entity(self, raw_entity):
logger.debug(f"parse_entity: {raw_entity}")
if raw_entity["type"] == "push":
entity = Push(**raw_entity)
elif raw_entity["type"] == "tag":
entity = Tag(**raw_entity)
else:
raise EntityTypeError(f"unsupported type {raw_entity['type']}")
return entity
message_type = raw_entity.pop("type")
match message_type:
case "push":
return Push(**raw_entity)
case "tag":
return Tag(**raw_entity)
case _:
raise EntityTypeError(f"unsupported type {message_type}")

def get_remote(self, repo, remote_url):
"""
@@ -70,10 +69,11 @@ def handle_commits(self, entity, clone_dir, remote_url, remote_target):
remote = self.get_remote(repo, remote_url)
# fetch new commits
remote.fetch()
if entity.type == "push":
remote.pull("branches/default/tip")
elif entity.type == "tag":
pass # TODO
match entity:
case Push():
remote.pull("branches/default/tip")
case _:
pass # TODO
# push on good repo/branch
remote = repo.remote(remote_target)
remote.push()
50 changes: 26 additions & 24 deletions tests/test_repo_synchronizer.py
Original file line number Diff line number Diff line change
@@ -31,28 +31,30 @@ def repos_config():
}
}

def raw_push_entity():
return {
"type": "push",
"repo_url": "repo_url",
"heads": ["head"],
"commits": ["commit"],
"time": 0,
"pushid": 0,
"user": "user",
"push_json_url": "push_json_url",
}


raw_push_entity = {
"type": "push",
"repo_url": "repo_url",
"heads": ["head"],
"commits": ["commit"],
"time": 0,
"pushid": 0,
"user": "user",
"push_json_url": "push_json_url",
}

raw_tag_entity = {
"type": "tag",
"repo_url": "repo_url",
"tag": "tag",
"commit": "commit",
"time": 0,
"pushid": 0,
"user": "user",
"push_json_url": "push_json_url",
}
def raw_tag_entity():
return {
"type": "tag",
"repo_url": "repo_url",
"tag": "tag",
"commit": "commit",
"time": 0,
"pushid": 0,
"user": "user",
"push_json_url": "push_json_url",
}


def setup_module():
@@ -62,9 +64,9 @@ def setup_module():

def test_parse_entity():
syncrepos = repo_synchronizer.RepoSynchronyzer(None)
push_entity = syncrepos.parse_entity(raw_push_entity)
push_entity = syncrepos.parse_entity(raw_push_entity())
assert isinstance(push_entity, repo_synchronizer.Push)
tag_entity = syncrepos.parse_entity(raw_tag_entity)
tag_entity = syncrepos.parse_entity(raw_tag_entity())
assert isinstance(tag_entity, repo_synchronizer.Tag)


@@ -77,7 +79,7 @@ def test_sync_process_with_bad_type():
def test_sync_process_with_bad_repo(repos_config):
syncrepos = repo_synchronizer.RepoSynchronyzer(repos_config=repos_config)
with pytest.raises(AssertionError) as e:
syncrepos.sync(raw_push_entity)
syncrepos.sync(raw_push_entity())
assert str(e.value) == f"clone {repos_config['repo_url']['clone']} doesn't exists"