Also add ability to copy source topics

This commit is contained in:
Mike 2023-02-11 01:25:21 -08:00
parent 9c0640f2d1
commit 7fd124118f
2 changed files with 45 additions and 7 deletions

View File

@ -105,12 +105,14 @@ class Migrator:
self, self,
interactive: bool = True, interactive: bool = True,
source_org: str = None, source_topics: list[str] = None, source_org: str = None, source_topics: list[str] = None,
destination_org: str = None, destination_repo_name: str = None, destination_topics: list = None destination_org: str = None, destination_repo_name: str = None, destination_topics: list = None,
do_destination_copy_topics: bool = True
): ):
assert source_org is not None, "Source org must be specified" assert source_org is not None, "Source org must be specified"
assert destination_org is not None, "Destination org must be specified" assert destination_org is not None, "Destination org must be specified"
assert destination_repo_name is not None, "Destination repo name must be specified" assert destination_repo_name is not None, "Destination repo name must be specified"
assert destination_topics is not None, "Destination topics must be specified" assert destination_topics is not None, "Destination topics must be specified"
assert do_destination_copy_topics is not None, "Destination directive to copy source topics should be specified"
# api_source, api_destination = self._get_org_apis() # api_source, api_destination = self._get_org_apis()
api_source: giteapy.OrganizationApi api_source: giteapy.OrganizationApi
@ -198,6 +200,7 @@ class Migrator:
destination_org_name=destination_org, destination_org_name=destination_org,
destination_repo_name=destination_repo_name, destination_repo_name=destination_repo_name,
destination_topics=destination_topics, destination_topics=destination_topics,
do_destination_copy_topics=do_destination_copy_topics,
repos=repos_migrate repos=repos_migrate
) )
self.__logger.info(f"{len(source_repos_successful)} of {len(repos_migrate)} repos successfully migrated.") self.__logger.info(f"{len(source_repos_successful)} of {len(repos_migrate)} repos successfully migrated.")
@ -288,6 +291,7 @@ class Migrator:
destination_org_name: str, destination_org_name: str,
destination_repo_name: str, destination_repo_name: str,
destination_topics: list, destination_topics: list,
do_destination_copy_topics: bool,
repos: list repos: list
): ):
@ -295,9 +299,13 @@ class Migrator:
destination_org = api_destination.org_get(org=destination_org_name) destination_org = api_destination.org_get(org=destination_org_name)
destination_org: giteapy.Organization destination_org: giteapy.Organization
self.__logger.info(f"Destination organization: {destination_org.full_name}") self.__logger.info(f"Destination organization: {destination_org.full_name}")
api_source_repos = self._get_repo_api(
hostname=self.__source_host, port=self.__source_port,
token=self.__source_token
)
source_repos_successful = [] source_repos_successful = []
for source_repo in repos: for source_repo in repos:
@ -305,6 +313,9 @@ class Migrator:
this_destination_repo_name = destination_repo_name.replace("%N%", source_repo.name) this_destination_repo_name = destination_repo_name.replace("%N%", source_repo.name)
source_repo_topics = api_source_repos.repo_list_topics(owner=source_repo.owner.login, repo=source_repo.name)
source_repo_topics = source_repo_topics.topics
migrate_body = giteapy.MigrateRepoForm( migrate_body = giteapy.MigrateRepoForm(
mirror=False, mirror=False,
clone_addr=source_repo.clone_url, clone_addr=source_repo.clone_url,
@ -334,6 +345,17 @@ class Migrator:
assert repo_new.name == this_destination_repo_name,\ assert repo_new.name == this_destination_repo_name,\
"New repository didn't end up with the correct name. Failure?" "New repository didn't end up with the correct name. Failure?"
# Copy source topics?
if do_destination_copy_topics:
for topic in source_repo_topics:
self.__logger.debug(f"Appending source topic to new repo: {topic}")
destination_api.repo_add_topc(
owner=destination_org.username,
repo=repo_new.name,
topic=topic,
)
# Add specified topics
for topic in destination_topics: for topic in destination_topics:
self.__logger.debug(f"Appending topic to new repo: {topic}") self.__logger.debug(f"Appending topic to new repo: {topic}")
destination_api.repo_add_topc( destination_api.repo_add_topc(

20
main.py
View File

@ -79,13 +79,28 @@ def main():
help="Specify the destination repository name(s). Use wildcard %%N%% anywhere to denote the original name" help="Specify the destination repository name(s). Use wildcard %%N%% anywhere to denote the original name"
) )
parser.add_argument( parser.add_argument(
"--destination-add-topic", "--destination-topic", "-dest-add-topic", "--dest-topic", "--destination-add-topic", "--destination-topic", "--dest-add-topic", "--dest-topic",
dest="destination_topics", dest="destination_topics",
default=[], default=[],
action="append", action="append",
help="Specify zero or more topics to add to each destination repository" help="Specify zero or more topics to add to each destination repository"
) )
parser.add_argument(
"--destination-copy-topics", "--dest-copy-topics",
dest="do_destination_copy_topics",
default=True,
action="store_true",
help="Destination repos should copy topics from their source."
)
parser.add_argument(
"--no-destination-copy-topics", "--no-dest-copy-topics",
dest="do_destination_copy_topics",
default=True,
action="store_false",
help="Destination repos should NOT copy topics from their source."
)
parser.add_argument( parser.add_argument(
"--interactive", "--interactive",
dest="interactive", dest="interactive",
@ -125,7 +140,8 @@ def main():
source_topics=args.source_topics, source_topics=args.source_topics,
destination_org=args.destination_org, destination_org=args.destination_org,
destination_repo_name=args.destination_repo_name, destination_repo_name=args.destination_repo_name,
destination_topics=args.destination_topics destination_topics=args.destination_topics,
do_destination_copy_topics=args.do_destination_copy_topics
) )