gitlab-to-gitea/gitlab-to-gitea.py

97 lines
3 KiB
Python
Raw Permalink Normal View History

2022-07-07 23:19:20 +02:00
import gitea
import gitlab
import yaml
def getGiteaClient():
with open("/home/dorian/.config/tea/config.yml", "r") as stream:
teaConfig = yaml.safe_load(stream)
login = teaConfig['logins'][0]
conf = gitea.Configuration()
conf.host = f"{login['url']}/api/v1"
#conf.username = login['user']
conf.api_key = {"access_token": login['token']}
return gitea.ApiClient(conf)
def getGitlabClient():
with open("./gitlab-config.yml", "r") as stream:
gitlabConfig = yaml.safe_load(stream)
return gitlab.Gitlab(url=gitlabConfig['url'], private_token=gitlabConfig['private_token'])
giteaClient = getGiteaClient()
repositoryApi = gitea.api.RepositoryApi(giteaClient)
userApi = gitea.api.UserApi(giteaClient)
organizationApi = gitea.api.OrganizationApi(giteaClient)
currentUser = userApi.user_get_current()
gitlabClient = getGitlabClient()
# migrate repos
reposToMigrate = []
print("Loading repositories from Gitlab ...")
#projects = gitlabClient.projects.list(
# search_namespaces=True, search="max", iterator=True)
user = gitlabClient.users.list(username='fenoglio')[0]
projects = user.projects.list()
2022-07-07 23:19:20 +02:00
for project in projects:
#print(project)
#print(project.namespace['path'], project.name, project.visibility, project.http_url_to_repo)
namespace = project.namespace
if namespace['kind'] == 'group':
try:
organizationApi.org_get(namespace['path'])
except gitea.rest.ApiException as e:
if e.status == 404:
# create organization!
group = gitlabClient.groups.get(namespace['path'])
#print(group)
organizationApi.org_create({
"full_name": namespace['name'],
"username": namespace['path'],
"description": group.description,
"visibility": "public"
})
pass
else:
raise e
migration = {
'auth_token': gitlabClient.private_token,
'auth_username': "dorian",
'clone_addr': project.http_url_to_repo,
'issues': True,
'labels': True,
'milestones': True,
'private': project.visibility == "private",
'pull_requests': True,
'releases': True,
'repo_name': project.path,
'service': "gitlab",
'wiki': True,
'repo_owner': project.namespace['path']
}
reposToMigrate.append(migration)
for repo in reposToMigrate:
try:
existingRepo = repositoryApi.repo_get(repo["repo_owner"], repo['repo_name'])
skip = not existingRepo.empty
except gitea.rest.ApiException as e:
if e.status == 404:
skip = False
else:
raise e
print("Migrating", repo['clone_addr'], "to owner", repo["repo_owner"], "with name", repo['repo_name'], "[SKIPPED]" if skip else "")
if skip:
continue
2022-07-07 23:19:20 +02:00
repositoryApi.repo_migrate(body=repo)