From 6e733d78c06b441a8e87f89b8257a5b426c45a1b Mon Sep 17 00:00:00 2001 From: Enrico Ludwig Date: Sun, 14 Jul 2024 13:31:34 +0200 Subject: [PATCH] Fixed logic bugs with --create* and --update* switches --- gitlab2gitea/gitlab2gitea.py | 154 ++++++++++++++++++++++++----------- 1 file changed, 108 insertions(+), 46 deletions(-) diff --git a/gitlab2gitea/gitlab2gitea.py b/gitlab2gitea/gitlab2gitea.py index 2dd966b..a02d1a1 100644 --- a/gitlab2gitea/gitlab2gitea.py +++ b/gitlab2gitea/gitlab2gitea.py @@ -15,9 +15,12 @@ # --gitea-url Gitea URL # --gitea-api-version Gitea API version (default: v1) # -# --create-missing-groups Create missing groups on Gitea (default: True) -# --create-missing-users Create missing users on Gitea (default: True) -# --create-missing-projects Create missing projects on Gitea (default: True) +# --no-create-missing-groups Do not create missing groups on Gitea (default: False) +# --no-create-missing-users Do not create missing users on Gitea (default: False) +# --no-create-missing-projects Do not create missing projects on Gitea (default: False) +# --no-update-existing-groups Do not update existing groups on Gitea (default: False) +# --no-update-existing-users Do not update existing users on Gitea (default: False) +# --no-update-existing-projects Do not update existing projects on Gitea (default: False) # # --quiet Enable quiet mode (default: False) # --debug Enable debug mode (default: False) @@ -48,9 +51,13 @@ GITEA_API_VERSION = 'v1' # Settings - General Repository -CREATE_MISSING_GROUPS = True -CREATE_MISSING_USERS = True -CREATE_MISSING_PROJECTS = True +NO_CREATE_MISSING_GROUPS = False +NO_CREATE_MISSING_USERS = False +NO_CREATE_MISSING_PROJECTS = False + +NO_UPDATE_EXISTING_GROUPS = False +NO_UPDATE_EXISTING_USERS = False +NO_UPDATE_EXISTING_PROJECTS = False # Settings - General @@ -92,14 +99,23 @@ if 'GITEA_TOKEN' in os.environ: if 'GITEA_API_VERSION' in os.environ: GITEA_API_VERSION = os.environ['GITEA_API_VERSION'] -if 'CREATE_MISSING_GROUPS' in os.environ: - CREATE_MISSING_GROUPS = bool(os.environ['CREATE_MISSING_GROUPS']) +if 'NO_CREATE_MISSING_GROUPS' in os.environ: + NO_CREATE_MISSING_GROUPS = False -if 'CREATE_MISSING_USERS' in os.environ: - CREATE_MISSING_USERS = bool(os.environ['CREATE_MISSING_USERS']) +if 'NO_CREATE_MISSING_USERS' in os.environ: + NO_CREATE_MISSING_USERS = False -if 'CREATE_MISSING_PROJECTS' in os.environ: - CREATE_MISSING_PROJECTS = bool(os.environ['CREATE_MISSING_PROJECTS']) +if 'NO_CREATE_MISSING_PROJECTS' in os.environ: + NO_CREATE_MISSING_PROJECTS = False + +if 'NO_UPDATE_EXISTING_GROUPS' in os.environ: + NO_UPDATE_EXISTING_GROUPS = False + +if 'NO_UPDATE_EXISTING_USERS' in os.environ: + NO_UPDATE_EXISTING_USERS = False + +if 'NO_UPDATE_EXISTING_PROJECTS' in os.environ: + NO_UPDATE_EXISTING_PROJECTS = False if 'QUIET' in os.environ: QUIET = bool(os.environ['QUIET']) @@ -148,17 +164,29 @@ if os.path.exists('.env'): if key == 'GITEA_API_VERSION': GITEA_API_VERSION = value - if key == 'CREATE_MISSING_GROUPS': + if key == 'NO_CREATE_MISSING_GROUPS': if value.lower() == 'true' or value == '1': - CREATE_MISSING_GROUPS = True + NO_CREATE_MISSING_GROUPS = True - if key == 'CREATE_MISSING_USERS': + if key == 'NO_CREATE_MISSING_USERS': if value.lower() == 'true' or value == '1': - CREATE_MISSING_USERS = True + NO_CREATE_MISSING_USERS = True - if key == 'CREATE_MISSING_PROJECTS': + if key == 'NO_CREATE_MISSING_PROJECTS': if value.lower() == 'true' or value == '1': - CREATE_MISSING_PROJECTS = True + NO_CREATE_MISSING_PROJECTS = True + + if key == 'NO_UPDATE_EXISTING_GROUPS': + if value.lower() == 'true' or value == '1': + NO_UPDATE_EXISTING_GROUPS = True + + if key == 'NO_UPDATE_EXISTING_USERS': + if value.lower() == 'true' or value == '1': + NO_UPDATE_EXISTING_USERS = True + + if key == 'NO_UPDATE_EXISTING_PROJECTS': + if value.lower() == 'true' or value == '1': + NO_UPDATE_EXISTING_PROJECTS = True if key == 'QUIET': QUIET = bool(value) @@ -191,9 +219,12 @@ parser.add_argument('--gitea-token', help='Gitea access token') parser.add_argument('--gitlab-url', help='GitLab URL') parser.add_argument('--gitea-url', help='Gitea URL') parser.add_argument('--gitea-api-version', help='Gitea API version', default='v1') -parser.add_argument('--create-missing-groups', help='Create missing groups on Gitea', action='store_true') -parser.add_argument('--create-missing-users', help='Create missing users on Gitea', action='store_true') -parser.add_argument('--create-missing-projects', help='Create missing projects on Gitea', action='store_true') +parser.add_argument('--no-create-missing-groups', help='Do not create missing groups on Gitea', action='store_true') +parser.add_argument('--no-create-missing-users', help='Do not create missing users on Gitea', action='store_true') +parser.add_argument('--no-create-missing-projects', help='Do not create missing projects on Gitea', action='store_true') +parser.add_argument('--no-update-existing-groups', help='Do not update existing groups on Gitea', action='store_true') +parser.add_argument('--no-update-existing-users', help='Do not update existing users on Gitea', action='store_true') +parser.add_argument('--no-update-existing-projects', help='Do not update existing projects on Gitea', action='store_true') parser.add_argument('--quiet', help='Enable quiet mode', action='store_true') parser.add_argument('--debug', help='Enable debug mode', action='store_true') parser.add_argument('--trace', help='Enable trace mode', action='store_true') @@ -221,14 +252,23 @@ if args.gitea_url: if args.gitea_api_version: GITEA_API_VERSION = args.gitea_api_version -if args.create_missing_groups: - CREATE_MISSING_GROUPS = True +if args.no_create_missing_groups: + NO_CREATE_MISSING_GROUPS = True -if args.create_missing_users: - CREATE_MISSING_USERS = True +if args.no_create_missing_users: + NO_CREATE_MISSING_USERS = True -if args.create_missing_projects: - CREATE_MISSING_PROJECTS = True +if args.no_create_missing_projects: + NO_CREATE_MISSING_PROJECTS = True + +if args.no_update_existing_groups: + NO_UPDATE_EXISTING_GROUPS = True + +if args.no_update_existing_users: + NO_UPDATE_EXISTING_USERS = True + +if args.no_update_existing_projects: + NO_UPDATE_EXISTING_PROJECTS = True if args.quiet: QUIET = True @@ -895,6 +935,9 @@ def update_existing_groups(gitlab_groups: list, gitea_groups: list): def update_existing_users(gitlab_users: list, gitea_users: list): pass +def update_existing_projects(gitlab_projects: list, gitea_projects: list): + pass + def migrate_groups(): gitlab_groups = get_gitlab_groups() gitea_groups = get_gitea_groups() @@ -913,7 +956,7 @@ def migrate_groups(): if missing_matches > 0: _warn(f'{missing_matches} groups are missing on Gitea!') - if missing_matches > 0 and CREATE_MISSING_GROUPS: + if missing_matches > 0 and not NO_CREATE_MISSING_GROUPS: _info('Creating missing groups on Gitea...') if not DRY_RUN: @@ -924,15 +967,18 @@ def migrate_groups(): else: _warn('Dry-run mode enabled, skipping creation of missing groups on Gitea...') - _info('Updating existing groups on Gitea...') + if not NO_UPDATE_EXISTING_GROUPS: + _info('Updating existing groups on Gitea...') - try: - if not DRY_RUN: - update_existing_groups(gitlab_groups, gitea_groups) - else: - _warn('Dry-run mode enabled, skipping update of existing groups on Gitea...') - except Exception as e: - _exception(f'Failed to update existing groups: {e}', e) + try: + if not DRY_RUN: + update_existing_groups(gitlab_groups, gitea_groups) + else: + _warn('Dry-run mode enabled, skipping update of existing groups on Gitea...') + except Exception as e: + _exception(f'Failed to update existing groups: {e}', e) + + _info('Group migration completed!') def migrate_users(): gitlab_users = get_gitlab_users() @@ -952,7 +998,7 @@ def migrate_users(): if missing_matches > 0: _warn(f'{missing_matches} users are missing on Gitea!') - if missing_matches > 0 and CREATE_MISSING_USERS: + if missing_matches > 0 and not NO_CREATE_MISSING_USERS: _info('Creating missing users on Gitea...') if not DRY_RUN: @@ -960,15 +1006,18 @@ def migrate_users(): else: _warn('Dry-run mode enabled, skipping creation of missing users on Gitea...') - _info('Updating existing users on Gitea...') + if not NO_UPDATE_EXISTING_USERS: + _info('Updating existing users on Gitea...') - try: - if not DRY_RUN: - update_existing_users(gitlab_users, gitea_users) - else: - _warn('Dry-run mode enabled, skipping update of existing users on Gitea...') - except Exception as e: - _exception(f'Failed to update existing users: {e}', e) + try: + if not DRY_RUN: + update_existing_users(gitlab_users, gitea_users) + else: + _warn('Dry-run mode enabled, skipping update of existing users on Gitea...') + except Exception as e: + _exception(f'Failed to update existing users: {e}', e) + + _info('User migration completed!') def migrate_projects(): @@ -989,13 +1038,26 @@ def migrate_projects(): if missing_matches > 0: _warn(f'{missing_matches} projects are missing on Gitea!') - if missing_matches > 0 and CREATE_MISSING_PROJECTS: + if missing_matches > 0 and not NO_CREATE_MISSING_PROJECTS: _info('Creating missing projects on Gitea...') if not DRY_RUN: create_missing_projects(gitlab_projects, gitea_projects) else: _warn('Dry-run mode enabled, skipping creation of missing projects on Gitea...') + + if not NO_UPDATE_EXISTING_PROJECTS: + _info('Updating existing projects on Gitea...') + + try: + if not DRY_RUN: + update_existing_projects(gitlab_projects, gitea_projects) + else: + _warn('Dry-run mode enabled, skipping update of existing projects on Gitea...') + except Exception as e: + _exception(f'Failed to update existing projects: {e}', e) + + _info('Project migration completed!') def run_migration():