Skip to content

Commit c8d089e

Browse files
committed
README and small corrections for Gitea/GitLab scripts
1 parent 2106c6e commit c8d089e

6 files changed

+29
-24
lines changed
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Here are Python scripts that can be used when migrating from Gitea to GitLab.
2+
3+
This repo is intended to be used as a playground for a corresponding article:
4+
5+
* Russian version: «[Опыт миграции из Gitea в GitLab. Сложно, но успешно](https://habr.com/ru/company/flant/blog/577808/)».

2021/09-gitea-gitlab-migration/add_collaborator.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import random
1010
import string
1111

12-
# gitea connection
12+
# Gitea connection
1313
configuration = giteapy.Configuration()
1414
configuration.api_key['access_token'] = 'superSecret'
1515
configuration.host = 'https://gitea.example.com/api/v1'
@@ -18,7 +18,7 @@
1818
org_api_instance = giteapy.OrganizationApi(giteapy.ApiClient(configuration))
1919
repo_api_instance = giteapy.RepositoryApi(giteapy.ApiClient(configuration))
2020

21-
# Adding RO user for migrations in any repo
21+
# adding RO user for migrations in any repo
2222
all_orgs = admin_api_instance.admin_get_all_orgs()
2323
for org in all_orgs:
2424
print(org.username)

2021/09-gitea-gitlab-migration/gitea_gitlab_import_repo.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
driver = webdriver.Firefox(os.getcwd()+os.path.sep)
1616
driver.get(GITLAB_URL)
1717

18-
# Gitlab login
18+
# GitLab login
1919
user = driver.find_element_by_id("user_login")
2020
user.send_keys(GITLAB_USER)
2121
pas = driver.find_element_by_id("user_password")
2222
pas.send_keys(GITLAB_PASSWORD)
2323
login = driver.find_element_by_name("commit").click()
2424

25-
# Starting import process
25+
# starting import process
2626
driver.get(GITLAB_URL+"/import/gitea/new")
2727
gitea_host = driver.find_element_by_name("gitea_host_url")
2828
gitea_host.send_keys(GITED_URL)
@@ -38,15 +38,15 @@
3838
# clicking select button to show dropdown menu and activate buttons
3939
row.find_element_by_class_name("gl-dropdown-toggle").click()
4040
time.sleep(1)
41-
# Finding project group
41+
# finding project group
4242
for btn in row.find_elements_by_class_name("dropdown-item"):
4343
if btn.get_attribute("data-qa-group-name") == group:
4444
btn.click()
4545
time.sleep(1)
4646
# starting import
4747
import_button = row.find_element(By.XPATH, "//button[@data-qa-selector='import_button']")
4848
import_button.click()
49-
# Wait until import is ready
49+
# wait until import is ready
5050
while True:
5151
time.sleep(10)
5252
status = row.find_elements_by_class_name("gl-p-4")[-1].text

2021/09-gitea-gitlab-migration/gitea_gitlab_user_copier.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ def get_random_string(length):
1616
result_str = ''.join(random.choice(letters) for i in range(length))
1717
return result_str
1818

19-
# gitea connection
19+
# Gitea connection
2020
configuration = giteapy.Configuration()
2121
configuration.api_key['access_token'] = 'superSecret'
2222
configuration.host = 'https://gitea.example.com/api/v1'
2323
admin_api_instance = giteapy.AdminApi(giteapy.ApiClient(configuration))
2424
user_api_instance = giteapy.UserApi(giteapy.ApiClient(configuration))
2525
org_api_instance = giteapy.OrganizationApi(giteapy.ApiClient(configuration))
2626

27-
# gitlab connection
27+
# GitLab connection
2828
gl = gitlab.Gitlab('https://gitlab.example.com', private_token='superSecret')
2929

30-
# inspect gitlab
30+
# inspect GitLab
3131
dict_gl_users = dict()
32-
# get gitea users
32+
# get Gitea users
3333
gt_users = admin_api_instance.admin_get_all_users()
3434
#pprint(api_response)
3535
lg_gt_map = dict()

2021/09-gitea-gitlab-migration/gitlab_fix_keys.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,27 @@ def get_random_string(length):
2323
def get_hash(key):
2424
return hashlib.md5(key.encode('utf-8')).hexdigest()
2525

26-
# gitea connection
26+
# Gitea connection
2727
configuration = giteapy.Configuration()
2828
configuration.api_key['access_token'] = 'superSecret'
2929
configuration.host = 'https://gitea.example.com/api/v1'
3030
admin_api_instance = giteapy.AdminApi(giteapy.ApiClient(configuration))
3131
user_api_instance = giteapy.UserApi(giteapy.ApiClient(configuration))
3232
org_api_instance = giteapy.OrganizationApi(giteapy.ApiClient(configuration))
3333

34-
# gitlab connection
34+
# GitLab connection
3535
gl = gitlab.Gitlab('https://gitlab.example.com', private_token='superSecret')
3636

3737
# clean blocked users keys
3838
for block_gl_user in gl.users.list(blocked=True, page=1, per_page=10000):
3939
print("Blocked user", block_gl_user.username)
4040
for block_gl_user_key in block_gl_user.keys.list():
41-
print("Found key", block_gl_user_key.title)
41+
print("Found a key", block_gl_user_key.title)
4242
block_gl_user_key.delete()
4343

44-
# inspect gitlab
44+
# inspect GitLab
4545
dict_gl_users = dict()
46-
# get gitea users
46+
# get Gitea users
4747
gt_users = admin_api_instance.admin_get_all_users()
4848
pattern = re.compile("^id_.+$")
4949

@@ -61,7 +61,7 @@ def get_hash(key):
6161
res = gl.users.list(username=gt_user.login)
6262
if len(res) > 0:
6363
if res[0].attributes['state'] == 'blocked':
64-
print("Skip blocked user", gt_user.login)
64+
print("Skip the blocked user", gt_user.login)
6565
continue
6666
dict_gl_users[res[0].username] = res[0]
6767
gl_keys_dict = dict()
@@ -72,7 +72,7 @@ def get_hash(key):
7272
keys_to_delete = list()
7373
for raw_gl_key in gl_keys:
7474
if pattern.match(raw_gl_key.title):
75-
print(gt_user.login, "delete key", raw_gl_key.title)
75+
print(gt_user.login, "delete the key", raw_gl_key.title)
7676
raw_gl_key.delete()
7777
else:
7878
gl_key_hash = get_hash(raw_gl_key.key.strip().split(' ')[1])
@@ -86,23 +86,23 @@ def get_hash(key):
8686
if gt_key in gl_keys_dict:
8787
keys_to_delete.remove(gt_key)
8888
else:
89-
print(gt_user.login, "missing key", gt_keys_dict[gt_key].title)
89+
print(gt_user.login, "missing a key", gt_keys_dict[gt_key].title)
9090
try:
9191
res[0].keys.create({'title': gt_keys_dict[gt_key].title, 'key': gt_keys_dict[gt_key].key})
9292
res[0].save()
9393
except:
9494
all_problem_keys_dict.append(gt_key)
95-
print(gt_user.login, "can not add key", gt_keys_dict[gt_key].title)
95+
print(gt_user.login, "can not add the key", gt_keys_dict[gt_key].title)
9696
for dkey in keys_to_delete:
9797
if pattern.match(gl_keys_dict[dkey].title):
98-
print(gt_user.login, "has additional key", gl_keys_dict[dkey].title)
98+
print(gt_user.login, "has an additional key", gl_keys_dict[dkey].title)
9999
#gl_keys_dict[key].delete()
100100
else:
101101
print("New user", gt_user.login)
102102

103-
print("Get problems key", len(all_problem_keys_dict))
103+
print("Get problematic key", len(all_problem_keys_dict))
104104
for pkey in all_problem_keys_dict:
105105
if pkey in all_gl_keys_dict:
106-
print("Key", pkey, "has user", all_gl_keys_dict[pkey])
106+
print("This key", pkey, "has user", all_gl_keys_dict[pkey])
107107
else:
108108
print("Can not find user for key", pkey)

2021/09-gitea-gitlab-migration/gitlab_fix_refs.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
import subprocess
99
import shutil
1010

11-
# gitlab connection
11+
# GitLab connection
1212
gl = gitlab.Gitlab('https://gitlab.example.com', private_token='superSecret')
1313

1414
shutil.rmtree('code',ignore_errors=True)
1515

16-
# Adding empty commit everywhere
16+
# adding an empty commit everywhere
1717
all_orgs = gl.groups.list()
1818
skip_orgs = ['someorg']
1919
for org in all_orgs:

0 commit comments

Comments
 (0)