68 lines
1.8 KiB
Python
68 lines
1.8 KiB
Python
|
|
|
|
import giteapy
|
|
import logging
|
|
import sys
|
|
|
|
|
|
class Migrator:
|
|
|
|
__DEFAULT_API_PATH = "/api/v1"
|
|
|
|
def __init__(
|
|
self,
|
|
source_host, source_port, source_token,
|
|
destination_host, destination_port, destination_token,
|
|
):
|
|
|
|
# noinspection PyTypeChecker
|
|
self.__logger: logging.Logger = None
|
|
self._init_logger()
|
|
|
|
self.__source_host = source_host
|
|
self.__source_port = source_port
|
|
self.__source_token = source_token
|
|
|
|
self.__destination_host = destination_host
|
|
self.__destination_port = destination_port
|
|
self.__destination_token = destination_token
|
|
|
|
def _init_logger(self):
|
|
|
|
logger = logging.Logger(name=f"{type(self).__name__}", level=logging.INFO)
|
|
|
|
stdout_handler = logging.StreamHandler(stream=sys.stdout)
|
|
logger.addHandler(stdout_handler)
|
|
|
|
self.__logger = logger
|
|
|
|
def _get_org_apis(self):
|
|
|
|
conf_source = giteapy.Configuration()
|
|
conf_source.api_key['access_token'] = self.__source_token
|
|
conf_source.host = self._make_api_base(host=self.__source_host, port=self.__source_port)
|
|
api_source = giteapy.OrganizationApi(giteapy.ApiClient(conf_source))
|
|
|
|
conf_destination = giteapy.Configuration()
|
|
conf_destination.api_key['access_token'] = self.__destination_token
|
|
conf_destination.host = self._make_api_base(host=self.__destination_host, port=self.__destination_port)
|
|
api_destination = giteapy.OrganizationApi(giteapy.ApiClient(conf_destination))
|
|
|
|
return api_source, api_destination
|
|
|
|
def _make_api_base(self, host, port):
|
|
|
|
base = f"https://{host}"
|
|
if port is not None:
|
|
base += f":{port}"
|
|
base += self.__DEFAULT_API_PATH
|
|
|
|
return base
|
|
|
|
def migrate_entire_org(self, source_org, destination_org):
|
|
|
|
api_source, api_destination = self._get_org_apis()
|
|
|
|
source_repos = api_source.org_list_repos(source_org)
|
|
self.__logger.info(f"Found {len(source_repos)} repos on source.")
|