From e2e7fada03153b561aff2e5fc2246c167f1022a9 Mon Sep 17 00:00:00 2001 From: Vladimir Berezhnoy Date: Tue, 1 Oct 2024 21:56:48 +0000 Subject: [PATCH] allow rewriting repo and owner --- changes/00.feature | 1 + giturlparse/result.py | 21 +++++++++++++++++++++ giturlparse/tests/test_rewrite.py | 24 ++++++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 changes/00.feature diff --git a/changes/00.feature b/changes/00.feature new file mode 100644 index 0000000..c3fb47c --- /dev/null +++ b/changes/00.feature @@ -0,0 +1 @@ +Allow rewriting repo and owner diff --git a/giturlparse/result.py b/giturlparse/result.py index 4319437..c7c59ed 100644 --- a/giturlparse/result.py +++ b/giturlparse/result.py @@ -38,6 +38,12 @@ def valid(self): ## # Alias properties ## + def _update_url(self): + protocol = getattr(self, "protocol", None) + if protocol: + self.url = self.format(protocol) + self._parsed["url"] = self.format(protocol) + @property def host(self): return self.domain @@ -50,6 +56,21 @@ def resource(self): def name(self): return self.repo + @name.setter + def name(self, new_name): + self.repo = new_name + self._parsed["repo"] = new_name + self._update_url() + + @property + def owner(self): + return self._parsed["owner"] + + @owner.setter + def owner(self, new_owner): + self._parsed["owner"] = new_owner + self._update_url() + @property def user(self): if hasattr(self, "_user"): diff --git a/giturlparse/tests/test_rewrite.py b/giturlparse/tests/test_rewrite.py index 789c32f..3077983 100644 --- a/giturlparse/tests/test_rewrite.py +++ b/giturlparse/tests/test_rewrite.py @@ -71,6 +71,20 @@ ) +REWRITE_COMPONENTS = { + "name": { + "source": "git@github.com:Org/Repo.git", + "value": "New-repo", + "expected": "git@github.com:Org/New-repo.git", + }, + "owner": { + "source": "git@github.com:Org/Repo.git", + "value": "New-Org", + "expected": "git@github.com:New-Org/Repo.git", + }, +} + + class UrlRewriteTestCase(unittest.TestCase): def _test_rewrite(self, source, protocol, expected): parsed = parse(source) @@ -81,6 +95,16 @@ def test_rewrites(self): for data in REWRITE_URLS: self._test_rewrite(*data) + def _test_rewrite_components(self, field, data): + parsed = parse(data["source"]) + setattr(parsed, field, data["value"]) + self.assertTrue(parsed.valid, "Invalid Url: %s" % data["source"]) + return self.assertEqual(parsed.url, data["expected"]) + + def test_rewrite_components(self): + for field, data in REWRITE_COMPONENTS.items(): + self._test_rewrite_components(field, data) + # Test Suite suite = unittest.TestLoader().loadTestsFromTestCase(UrlRewriteTestCase)