-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
191 lines (151 loc) · 6.53 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import json
import requests
from github import Github as GitHub
from src.settings import SETTINGS
from src.secrets import SECRETS
# Secrets
gt_token = SECRETS["GITEA_TOKEN"]
gt_url = SECRETS["GITEA_URL"]
gh_url = SECRETS["GITHUB_URL"]
gh_token = SECRETS["GITHUB_TOKEN"]
# Settings
ignore_list = SETTINGS["ignore_list"]
skip_forks = SETTINGS["skip_forks"]
gh = GitHub(gh_token)
# Create a session
session = requests.Session()
session.headers.update({
"Content-Type": "application/json",
"Authorization": f"token {gt_token}"})
# GitHub
def gh_get_username():
return gh.get_user().login
def gh_get_user_repos():
return [repo.name for repo in gh.get_user().get_repos()]
def gh_get_starred():
#repo_id = [i.id for i in gh.get_user().get_starred()]
repo_name = [starred.name for starred in gh.get_user().get_starred()]
# return list(zip(repo_id, repo_fn))
return repo_name
#! Gitea
def gt_migrate(full_name):
""" Migrate a repo from GitHub to Gitea """
get_repo = gh.get_repo(full_name)
data = {
"repo_name": get_repo.name,
"repo_id:": get_repo.id,
"mirror": True,
"issues": True,
"labels": True,
"lfs": True,
"milestones": True,
"pull_requests": True,
"releases": True,
"clone_addr": get_repo.clone_url,
"description": get_repo.description}
if get_repo.private:
data["auth_username"] = get_repo.owner.login
data["auth_password"] = gh_token
data["private"] = True
jsonstring = json.dumps(data)
session.post(gt_url + "/repos/migrate", data=jsonstring)
for topic in get_repo.get_topics():
gt_put_repo_topics(gt_get_username(), get_repo.name, topic)
print(f"[4/5] Added {topic} topic to {get_repo.name}")
return True
def check_gitea():
""" Check if Gitea is online """
r = requests.get(f"{gt_url}/version")
if r.status_code != 200:
print(f"Can't reach Gitea. {r.status_code}")
return False
else:
print("[1/5] Gitea is online.")
return True
def gt_get_username():
""" Get Gitea username """
return session.get(f"{gt_url}/user").json()["username"]
def gt_get_user_repos():
""" Get all repos of user from Gitea """
payload = {"page": 1, "limit": 1000}
response = session.get(f"{gt_url}/user/repos", params=payload).json()
return [i["name"] for i in response]
def gt_get_repo_topics(owner, repo):
""" Get topics of a repo from Gitea """
payload = {"page": 1, "limit": 1000}
return session.get(f"{gt_url}/repos/{owner}/{repo}/topics", params=payload).json()
def gt_put_repo_topics(owner, repo, topic):
""" Add topic to a repository on Gitea """
return session.put(f"{gt_url}/repos/{owner}/{repo}/topics/{topic}")
def main():
""" Main function """
if check_gitea():
print(f"[2/5] Logged in as {gt_get_username()} on Gitea.")
print(f"[3/5] Logged in as {gh_get_username()} on GitHub.")
# * Check the difference between user repos on Gitea and GitHub and then get the repos from GitHub
repo_difference = list(set(gh_get_user_repos()) - set(gt_get_user_repos()))
repo_count = len(repo_difference)
for user_repos in [i for i in gh.get_user().get_repos()]:
if skip_forks and user_repos.fork:
repo_count -= 1
elif user_repos.name in ignore_list:
repo_count -= 1
while repo_count > 0:
print(f"[4/5] {len(repo_difference)} repos from {gh_get_username()} to migrate.")
print(f"{repo_difference}")
# - Migrate the user repositories
for user_repo in [repo for repo in gh.get_user().get_repos()]:
# - Skip forks
if skip_forks and user_repo.fork:
print(f"[4/5] Skipping fork {user_repo.full_name}")
continue
# - Ignore the repos in the ignore list
elif user_repo.name in ignore_list:
print(f"[4/5] Ignoring {user_repo.full_name}")
continue
elif user_repo.name in repo_difference:
print(f"[4/5] Migrating {user_repo.full_name}")
gt_migrate(user_repo.full_name)
print(f"[4/5] Migrated {user_repo.full_name}")
repo_count -= 1
# - Add topics to repository
print(f"[4/5] Adding topics to {user_repo.full_name}")
print(f"[4/5] Added all topics. Moving on to next repo.")
else:
print(f"[4/5] {user_repo.full_name} already migrated.")
else:
print(f"[4/5] No new user repos to migrate. Checking starred repos.")
# * If there is no difference between Gitea and GitHub, then check starred repos from GitHub
star_difference = list(set(gh_get_starred()) - set(gt_get_user_repos()))
star_count = len(star_difference)
for user_repos in [i for i in gh.get_user().get_starred()]:
if skip_forks and user_repos.fork:
star_count -= 1
elif user_repos.name in ignore_list:
star_count -= 1
while star_count > 0:
print(f"[5/5] {len(star_difference)} starred repos to migrate.")
# - Migrate the starred repos
for starred_repo in [repo for repo in gh.get_user().get_starred()]:
if skip_forks and starred_repo.fork:
print(f"[5/5] Skipping fork {starred_repo.full_name}")
continue
elif starred_repo.name in ignore_list:
print(f"[5/5] Ignoring {starred_repo.full_name}")
continue
elif starred_repo.name in star_difference:
print(f"[5/5] Migrating {starred_repo.full_name}")
gt_migrate(starred_repo.full_name)
print(f"[5/5] Migrated {starred_repo.full_name}")
star_count -= 1
# - Add topics to repository
print(f"[5/5] Adding topics to {starred_repo.full_name}")
print(f"[5/5] Added all topics. Moving on to next repo.")
# else:
# print(f"[5/5] {starred_repo.full_name} already migrated.")
else:
print(f"[5/5] No new starred repos to migrate. You are all set.")
else:
quit
if __name__ == "__main__":
main()