Created
April 2, 2021 22:41
-
-
Save perfectra1n/3d0202525d0cb06cf363dd7ebc940bcb to your computer and use it in GitHub Desktop.
Migrate Gitlab projects to Gitea repos.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import gitlab | |
import sys | |
import log | |
import requests | |
import json | |
""" | |
MAIN FUNCTIONS | |
""" | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='This script will migrate all repos from a Gitlab, to a Gitea, to their corresponding user.') | |
parser.add_argument("--gitlab-url", nargs=1, help="The gitlab URL that you wish to clone to. Please include the 'https://' if the URL includes it.") | |
parser.add_argument("--gitlab-access-token", nargs=1, help="Please provide the gitlab access token you would like to use.") | |
parser.add_argument("--gitea-url", nargs=1, help="The gitea URL that you wish to clone to. Please include the 'https://' if the URL includes it.") | |
parser.add_argument("--gitea-access-token", nargs=1, help="Please provide the gitea access token you would like to use.") | |
parser.add_argument("--debug", action='store_true', help="Use this flag if you want to debug the output.") | |
# Parse the list of arguments from argparse | |
args = parser.parse_args() | |
logger = log.get_logger("migrate.py", debug=args.debug) | |
if not args.gitlab_url: | |
print("Please enter the URL to the Gitlab (e.g. https://gitlab.com): ") | |
args.gitlab_url = input("Gitlab URL: ") | |
if not args.gitlab_access_token: | |
print(f"Please enter the Gitlab access token for your account at {args.gitlab_url}: ") | |
args.github_access_token = input("Gitlab Token: ") | |
if not args.gitea_url: | |
print("Please enter the URL to the Gitea (e.g. https://gitlab.com): ") | |
args.gitea_url = input("Gitea URL: ") | |
if not args.gitea_access_token: | |
print(f"Please enter the Gitea access token for your account at {args.gitea_url}: ") | |
args.gitea_access_token = input("Gitea Token: ") | |
## Gitlab work | |
gitlab_object = gitlab.Gitlab(args.gitlab_url[0], private_token=args.gitlab_access_token[0]) | |
# Get the user incase we need to amend any repo names further down the line. | |
gitlab_object.auth() | |
gitlab_user = gitlab_object.user | |
logger.debug("Current user is: ") | |
logger.debug(gitlab_user.name) | |
if not gitlab_user.can_create_project: | |
logger.error("This user is not allowed to create projects, exiting...") | |
sys.exit(1) | |
headers = { | |
"Content-Type":"application/json" | |
} | |
params = { | |
"access_token":args.gitea_access_token | |
} | |
checked = 0 | |
for project in gitlab_object.projects.list(all=True): | |
logger.debug("Gitlab repo name: " + project.name) | |
logger.debug("Added: " + project.name) | |
# Check to see if it's owned by a group instead | |
try: | |
project.owner["username"] | |
projowner = project.owner["username"] | |
except AttributeError as e: | |
logger.warning(f"The project {project.name} does not have an owner, please enter the Username that you would like to attribute this repo to in Gitea") | |
projowner = input("Username to attribute repo to: ") | |
pass | |
payload = { | |
# Because the args.gitlab_access_token is a list, grab the 0th element | |
"auth_token":args.gitlab_access_token[0], | |
"clone_addr":project.http_url_to_repo, | |
"repo_name":project.name, | |
"repo_owner":projowner | |
} | |
# Because the args.gitea_url is a list, grab the 0th element | |
response = requests.request("POST", url=args.gitea_url[0]+"/api/v1/repos/migrate", data=json.dumps(payload), headers=headers, params=params) | |
logger.info(f"Response code from Gitea: {str(response.status_code)} for project {project.name}.") | |
logger.info() | |
if response.status_code != 201: | |
if response.status_code == 409: | |
logger.warn(f"A repository with the same name '{project.name}' already exists, continuing.") | |
else: | |
logger.error(response.text) | |
checked = checked + 1 | |
logger.info("Checked, and attempted, to bring " + str(checked) + " projects over from gitlab to gitea.") | |
logger.info("Completed migration.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment