From 95b494e547ef9fbd2baadbc4039719afd18299d1 Mon Sep 17 00:00:00 2001 From: Mike Date: Tue, 17 Jan 2023 23:46:36 -0800 Subject: [PATCH] Add support for specifying the destination name pattern --- domain/Migrator.py | 38 +++++++++++++++++++++++++++++++++----- main.py | 16 ++++++++++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/domain/Migrator.py b/domain/Migrator.py index 5c89285..359b614 100644 --- a/domain/Migrator.py +++ b/domain/Migrator.py @@ -8,6 +8,7 @@ import sys class Migrator: __DEFAULT_API_PATH = "/api/v1" + __REPO_ORIGINAL_NAME_TOKEN = "%N%" def __init__( self, @@ -80,11 +81,21 @@ class Migrator: return base + def _make_destination_repo_name(self, pattern: str, repo: giteapy.Repository): + + repo_name = pattern.replace(self.__REPO_ORIGINAL_NAME_TOKEN, repo.name) + + return repo_name + def set_verify_ssl(self, b: bool): self.__verify_ssl = b - def migrate_entire_org(self, source_org, destination_org): + def migrate_entire_org( + self, + source_org: str, + destination_org: str, destination_repo_name: str, destination_topics: list + ): api_source, api_destination = self._get_org_apis() @@ -137,7 +148,8 @@ class Migrator: self.__logger.info("Repos to migrate:") for repo in repos_migrate: repo: giteapy.Repository - self.__logger.info(f"#{repo.id} \"{repo.name}\"") + destination_name = self._make_destination_repo_name(pattern=destination_repo_name, repo=repo) + self.__logger.info(f"#{repo.id} \"{repo.name}\" ==> \"{destination_name}\"") else: self.__logger.info("No repos marked to migrate") @@ -154,11 +166,22 @@ class Migrator: confirmation = input("Do you confirm the above selections? Enter CONFIRM ==> ") if confirmation == "CONFIRM": self.__logger.info("Confirmation received; Processing ... ") - self._migrate_repos(destination_org_name=destination_org, repos=repos_migrate) + self._migrate_repos( + destination_org_name=destination_org, + destination_repo_name=destination_repo_name, + destination_topics=destination_topics, + repos=repos_migrate + ) else: self.__logger.info("Confirmation not received; Won't do anything.") - def _migrate_repos(self, destination_org_name: str, repos: list): + def _migrate_repos( + self, + destination_org_name: str, + destination_repo_name: str, + destination_topics: list, + repos: list + ): api_source, api_destination = self._get_org_apis() @@ -171,12 +194,14 @@ class Migrator: repo: giteapy.Repository + this_destination_repo_name = destination_repo_name.replace("%N%", repo.name) + migrate_body = giteapy.MigrateRepoForm( mirror=False, clone_addr=repo.clone_url, uid=destination_org.id, private=repo.private, - repo_name=repo.name, + repo_name=this_destination_repo_name, description=repo.description, labels=True, issues=True, pull_requests=True, releases=True, milestones=True, wiki=True ) @@ -194,3 +219,6 @@ class Migrator: ) migration_result = destination_api.repo_migrate(body=migrate_body) self.__logger.info(f"Migration result: {migration_result}") + + for t in destination_topics: + self.__logger.info(f"Want to append topic: {t}") diff --git a/main.py b/main.py index 9acebd9..cab890b 100644 --- a/main.py +++ b/main.py @@ -64,6 +64,20 @@ def main(): help="Name of the destination organization" ) + parser.add_argument( + "--destination-repo-name", "--destination-name", "--dest-repo-name", "--dest-name", + dest="destination_repo_name", + default="%N%", + help="Specify the destination repository name(s). Use wildcard %N% anywhere to denote the original name" + ) + parser.add_argument( + "--destination-add-topic", "--destination-topic", "-dest-add-topic", "--dest-topic", + dest="destination_topics", + default=[], + action="append", + help="Specify zero or more topics to add to each destination repository" + ) + parser.add_argument( "--no-verify-ssl", dest="verify_ssl", @@ -85,6 +99,8 @@ def main(): mig.migrate_entire_org( source_org=args.source_org, destination_org=args.destination_org, + destination_repo_name=args.destination_repo_name, + destination_topics=args.destination_topics )