Implemented new cli options

This commit is contained in:
Enrico Ludwig 2024-07-18 12:25:57 +02:00
parent 03514cd497
commit 298b3003cd

View File

@ -29,6 +29,10 @@
# --override-users Override existing users on Gitea (default: False) - not implemented yet # --override-users Override existing users on Gitea (default: False) - not implemented yet
# --override-projects Override existing projects on Gitea (default: False) - not implemented yet # --override-projects Override existing projects on Gitea (default: False) - not implemented yet
# #
# --only-groups Migrate only groups (default: False)
# --only-users Migrate only users (default: False)
# --only-projects Migrate only projects (default: False)
#
# --quiet Enable quiet mode (default: False) # --quiet Enable quiet mode (default: False)
# --debug Enable debug mode (default: False) # --debug Enable debug mode (default: False)
# --trace Enable trace mode (default: False) # --trace Enable trace mode (default: False)
@ -66,6 +70,14 @@ NO_UPDATE_EXISTING_GROUPS = False
NO_UPDATE_EXISTING_USERS = False NO_UPDATE_EXISTING_USERS = False
NO_UPDATE_EXISTING_PROJECTS = False NO_UPDATE_EXISTING_PROJECTS = False
OVERWRITE_EXISTING_GROUPS = False
OVERWRITE_EXISTING_USERS = False
OVERWRITE_EXISTING_PROJECTS = False
ONLY_GROUPS = False
ONLY_USERS = False
ONLY_PROJECTS = False
# Settings - General # Settings - General
DEBUG = False DEBUG = False
@ -111,22 +123,40 @@ if "GITEA_API_VERSION" in os.environ:
GITEA_API_VERSION = os.environ["GITEA_API_VERSION"] GITEA_API_VERSION = os.environ["GITEA_API_VERSION"]
if "NO_CREATE_MISSING_GROUPS" in os.environ: if "NO_CREATE_MISSING_GROUPS" in os.environ:
NO_CREATE_MISSING_GROUPS = False NO_CREATE_MISSING_GROUPS = bool(os.environ["NO_CREATE_MISSING_GROUPS"])
if "NO_CREATE_MISSING_USERS" in os.environ: if "NO_CREATE_MISSING_USERS" in os.environ:
NO_CREATE_MISSING_USERS = False NO_CREATE_MISSING_USERS = bool(os.environ["NO_CREATE_MISSING_USERS"])
if "NO_CREATE_MISSING_PROJECTS" in os.environ: if "NO_CREATE_MISSING_PROJECTS" in os.environ:
NO_CREATE_MISSING_PROJECTS = False NO_CREATE_MISSING_PROJECTS = bool(os.environ["NO_CREATE_MISSING_PROJECTS"])
if "NO_UPDATE_EXISTING_GROUPS" in os.environ: if "NO_UPDATE_EXISTING_GROUPS" in os.environ:
NO_UPDATE_EXISTING_GROUPS = False NO_UPDATE_EXISTING_GROUPS = bool(os.environ["NO_UPDATE_EXISTING_GROUPS"])
if "NO_UPDATE_EXISTING_USERS" in os.environ: if "NO_UPDATE_EXISTING_USERS" in os.environ:
NO_UPDATE_EXISTING_USERS = False NO_UPDATE_EXISTING_USERS = bool(os.environ["NO_UPDATE_EXISTING_USERS"])
if "NO_UPDATE_EXISTING_PROJECTS" in os.environ: if "NO_UPDATE_EXISTING_PROJECTS" in os.environ:
NO_UPDATE_EXISTING_PROJECTS = False NO_UPDATE_EXISTING_PROJECTS = bool(os.environ["NO_UPDATE_EXISTING_PROJECTS"])
if "OVERWRITE_EXISTING_GROUPS" in os.environ:
OVERWRITE_EXISTING_GROUPS = bool(os.environ["OVERWRITE_EXISTING_GROUPS"])
if "OVERWRITE_EXISTING_USERS" in os.environ:
OVERWRITE_EXISTING_USERS = bool(os.environ["OVERWRITE_EXISTING_USERS"])
if "OVERWRITE_EXISTING_PROJECTS" in os.environ:
OVERWRITE_EXISTING_PROJECTS = bool(os.environ["OVERWRITE_EXISTING_PROJECTS"])
if "ONLY_GROUPS" in os.environ:
ONLY_GROUPS = bool(os.environ["ONLY_GROUPS"])
if "ONLY_USERS" in os.environ:
ONLY_USERS = bool(os.environ["ONLY_USERS"])
if "ONLY_PROJECTS" in os.environ:
ONLY_PROJECTS = bool(os.environ["ONLY_PROJECTS"])
if "QUIET" in os.environ: if "QUIET" in os.environ:
QUIET = bool(os.environ["QUIET"]) QUIET = bool(os.environ["QUIET"])
@ -199,6 +229,30 @@ if os.path.exists(".env"):
if value.lower() == "true" or value == "1": if value.lower() == "true" or value == "1":
NO_UPDATE_EXISTING_PROJECTS = True NO_UPDATE_EXISTING_PROJECTS = True
if key == "OVERWRITE_EXISTING_GROUPS":
if value.lower() == "true" or value == "1":
OVERWRITE_EXISTING_GROUPS = True
if key == "OVERWRITE_EXISTING_USERS":
if value.lower() == "true" or value == "1":
OVERWRITE_EXISTING_USERS = True
if key == "OVERWRITE_EXISTING_PROJECTS":
if value.lower() == "true" or value == "1":
OVERWRITE_EXISTING_PROJECTS = True
if key == "ONLY_GROUPS":
if value.lower() == "true" or value == "1":
ONLY_GROUPS = True
if key == "ONLY_USERS":
if value.lower() == "true" or value == "1":
ONLY_USERS = True
if key == "ONLY_PROJECTS":
if value.lower() == "true" or value == "1":
ONLY_PROJECTS = True
if key == "QUIET": if key == "QUIET":
QUIET = bool(value) QUIET = bool(value)
@ -226,48 +280,32 @@ parser = argparse.ArgumentParser(
description="Script to migrate repositories from GitLab to Gitea" description="Script to migrate repositories from GitLab to Gitea"
) )
# fmt: off
parser.add_argument("--gitlab-api-version", help="GitLab API version", default="v4") parser.add_argument("--gitlab-api-version", help="GitLab API version", default="v4")
parser.add_argument("--gitlab-token", help="GitLab access token") parser.add_argument("--gitlab-token", help="GitLab access token")
parser.add_argument("--gitea-token", help="Gitea access token") parser.add_argument("--gitea-token", help="Gitea access token")
parser.add_argument("--gitlab-url", help="GitLab URL") parser.add_argument("--gitlab-url", help="GitLab URL")
parser.add_argument("--gitea-url", help="Gitea URL") parser.add_argument("--gitea-url", help="Gitea URL")
parser.add_argument("--gitea-api-version", help="Gitea API version", default="v1") parser.add_argument("--gitea-api-version", help="Gitea API version", default="v1")
parser.add_argument( parser.add_argument("--no-create-missing-groups", help="Do not create missing groups on Gitea", action="store_true")
"--no-create-missing-groups", parser.add_argument("--no-create-missing-users", help="Do not create missing users on Gitea", action="store_true")
help="Do not create missing groups on Gitea", parser.add_argument("--no-create-missing-projects", help="Do not create missing projects on Gitea", action="store_true")
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( parser.add_argument("--no-update-existing-projects", help="Do not update existing projects on Gitea", action="store_true")
"--no-create-missing-users", parser.add_argument("--override-groups", help="Override existing groups on Gitea", action="store_true")
help="Do not create missing users on Gitea", parser.add_argument("--override-users", help="Override existing users on Gitea", action="store_true")
action="store_true", parser.add_argument("--override-projects", help="Override existing projects on Gitea", action="store_true")
) parser.add_argument("--only-groups", help="Migrate only groups", action="store_true")
parser.add_argument( parser.add_argument("--only-users", help="Migrate only users", action="store_true")
"--no-create-missing-projects", parser.add_argument("--only-projects", help="Migrate only projects", action="store_true")
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("--quiet", help="Enable quiet mode", action="store_true")
parser.add_argument("--debug", help="Enable debug 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") parser.add_argument("--trace", help="Enable trace mode", action="store_true")
parser.add_argument("--dry-run", help="Enable dry-run mode", action="store_true") parser.add_argument("--dry-run", help="Enable dry-run mode", action="store_true")
parser.add_argument("--log-file", help="Log file", default="gitlab2gitea.log") parser.add_argument("--log-file", help="Log file", default="gitlab2gitea.log")
parser.add_argument("--append-log", help="Append log file", action="store_true") parser.add_argument("--append-log", help="Append log file", action="store_true")
# fmt: on
args = parser.parse_args() args = parser.parse_args()
@ -307,6 +345,24 @@ if args.no_update_existing_users:
if args.no_update_existing_projects: if args.no_update_existing_projects:
NO_UPDATE_EXISTING_PROJECTS = True NO_UPDATE_EXISTING_PROJECTS = True
if args.override_groups:
OVERWRITE_EXISTING_GROUPS = True
if args.override_users:
OVERWRITE_EXISTING_USERS = True
if args.override_projects:
OVERWRITE_EXISTING_PROJECTS = True
if args.only_groups:
ONLY_GROUPS = True
if args.only_users:
ONLY_USERS = True
if args.only_projects:
ONLY_PROJECTS = True
if args.quiet: if args.quiet:
QUIET = True QUIET = True
@ -325,6 +381,56 @@ if args.log_file:
if args.append_log: if args.append_log:
APPEND_LOG = True APPEND_LOG = True
# check for mutually exclusive options
if ONLY_GROUPS and ONLY_USERS:
_error("Options --only-groups and --only-users are mutually exclusive!")
sys.exit(1)
if ONLY_GROUPS and ONLY_PROJECTS:
_error("Options --only-groups and --only-projects are mutually exclusive!")
sys.exit(1)
if ONLY_USERS and ONLY_PROJECTS:
_error("Options --only-users and --only-projects are mutually exclusive!")
sys.exit(1)
if NO_CREATE_MISSING_GROUPS and OVERWRITE_EXISTING_GROUPS:
_error(
"Options --no-create-missing-groups and --override-groups are mutually exclusive!"
)
sys.exit(1)
if NO_CREATE_MISSING_USERS and OVERWRITE_EXISTING_USERS:
_error(
"Options --no-create-missing-users and --override-users are mutually exclusive!"
)
sys.exit(1)
if NO_CREATE_MISSING_PROJECTS and OVERWRITE_EXISTING_PROJECTS:
_error(
"Options --no-create-missing-projects and --override-projects are mutually exclusive!"
)
sys.exit(1)
if NO_UPDATE_EXISTING_GROUPS and OVERWRITE_EXISTING_GROUPS:
_error(
"Options --no-update-existing-groups and --override-groups are mutually exclusive!"
)
sys.exit(1)
if NO_UPDATE_EXISTING_USERS and OVERWRITE_EXISTING_USERS:
_error(
"Options --no-update-existing-users and --override-users are mutually exclusive!"
)
sys.exit(1)
if NO_UPDATE_EXISTING_PROJECTS and OVERWRITE_EXISTING_PROJECTS:
_error(
"Options --no-update-existing-projects and --override-projects are mutually exclusive!"
)
sys.exit(1)
# Remove trailing slashes from URLs # Remove trailing slashes from URLs
GITLAB_URL = GITLAB_URL.rstrip("/") GITLAB_URL = GITLAB_URL.rstrip("/")
@ -1226,6 +1332,15 @@ def migrate_projects():
gitlab_projects = get_gitlab_projects() gitlab_projects = get_gitlab_projects()
gitea_projects = get_gitea_projects() gitea_projects = get_gitea_projects()
# dump the projects to json files
import json
with open("gitlab_projects.json", "w") as f:
json.dump(gitlab_projects, f, indent=4)
with open("gitea_projects.json", "w") as f:
json.dump(gitea_projects, f, indent=4)
_info(f"Projects on GitLab: {len(gitlab_projects)}") _info(f"Projects on GitLab: {len(gitlab_projects)}")
_trace(f"Projects on GitLab: {gitlab_projects}") _trace(f"Projects on GitLab: {gitlab_projects}")
_info(f"Projects on Gitea: {len(gitea_projects)}") _info(f"Projects on Gitea: {len(gitea_projects)}")
@ -1268,6 +1383,25 @@ def migrate_projects():
def run_migration(): def run_migration():
if ONLY_GROUPS:
_info("Migrating GitLab groups...")
migrate_groups()
_info("Group migration completed!")
return
elif ONLY_USERS:
_info("Migrating GitLab users...")
migrate_users()
_info("User migration completed!")
return
elif ONLY_PROJECTS:
_info("Migrating GitLab projects...")
migrate_projects()
_info("Project migration completed!")
return
else:
_info("Migrating GitLab groups...") _info("Migrating GitLab groups...")
migrate_groups() migrate_groups()
_info("Group migration completed!") _info("Group migration completed!")