1
0
mirror of https://github.com/balkian/gitea-github-mirrorer.git synced 2024-11-21 15:52:30 +00:00

Improvements

This commit is contained in:
J. Fernando Sánchez 2022-06-03 18:08:10 +02:00
parent bd24d6a72f
commit c1123f4e6a

View File

@ -13,11 +13,27 @@ repo_map = {}
TOKEN_FOLDER = os.environ.get("TOKEN_FOLDER, ") TOKEN_FOLDER = os.environ.get("TOKEN_FOLDER, ")
gitea_url = os.environ.get("GITEA_API", "https://git.sinpapel.es/api/v1") gitea_url = os.environ.get("GITEA_API", "https://git.sinpapel.es/api/v1")
gitea_user = os.environ.get("GITEA_USER", "balkian") gitea_user = os.environ.get("GITEA_USER", "balkian")
gitea_token = os.environ.get("GITEA_TOKEN") or open(os.path.expanduser("~/.gitea-token")).read().strip() gitea_token = os.environ.get("GITEA_TOKEN") or open(os.path.expanduser("~/.gitea-token")).read().strip()
print(gitea_user, gitea_token, gitea_url)
def env_list(name):
return list(filter(None, os.environ.get(name, "").split(",")))
# The following users and orgs will be mirrored
users_and_orgs = env_list("GITHUB_USERS_ORGS") + [gitea_user]
# And this list of projects as well
repo_list = env_list("GITHUB_REPO_LIST")
print(f"Cloning to {gitea_url}. Default user: {gitea_user}")
print(f'\tUsers and orgs: {" ".join(users_and_orgs)}')
if repo_list:
print(f'\tHandpicked repos: {" ".join(repo_list)} ({len(repo_list)})')
print(f'"{repo_list[0]}"')
session = requests.Session() # Gitea session = requests.Session() # Gitea
session.headers.update({ session.headers.update({
@ -25,9 +41,6 @@ session.headers.update({
"Authorization" : "token {0}".format(gitea_token), "Authorization" : "token {0}".format(gitea_token),
}) })
# The following users and orgs will be mirrorered
users_and_orgs = [gitea_user]
# Ask to remove repositories that "should not be mirrored" but are # Ask to remove repositories that "should not be mirrored" but are
prune = False prune = False
@ -35,56 +48,68 @@ github_username = os.environ.get("GITHUB_USER", "balkian")
github_token = os.environ.get("GITHUB_TOKEN") or open(os.path.expanduser(".github-token")).read().strip() github_token = os.environ.get("GITHUB_TOKEN") or open(os.path.expanduser(".github-token")).read().strip()
gh = Github(github_token) gh = Github(github_token)
uids = {}
def get_uid(user):
if user in uids:
return uids[user]
r = session.get(f"{gitea_url}/users/{gitea_dest_user}")
if r.status_code != 200:
msg = f"Cannot get user id for '{gitea_dest_user}'"
print(msg, file=sys.stderr)
raise Exception(msg)
uid = json.loads(r.text)["id"]
uids[user] = uid
return uid
for repo in gh.get_user().get_repos(): for repo in gh.get_user().get_repos():
# Mirror to Gitea if I haven't forked this repository from elsewhere # Mirror to Gitea if I haven't forked this repository from elsewhere
if not repo.fork: if not repo.fork:
user_or_org, real_repo = repo.full_name.split('/')[:2] user_or_org, repo_name = repo.full_name.split('/')[:2]
if real_repo in repo_map: # By default, mirror to the default user
gitea_dest_user = gitea_user
if repo_name in repo_map:
# We're creating the repo in another account (most likely an organization) # We're creating the repo in another account (most likely an organization)
gitea_dest_user = repo_map[real_repo] gitea_dest_user = repo_map[repo_name]
elif user_or_org in users_and_orgs: elif user_or_org in users_and_orgs or repo_name in repo_list or f"{user_or_org}/{repo_name}" in repo_list:
gitea_dest_user = gitea_user pass
else: else:
print('Not cloning', repo.full_name) print(f'... Skipping {repo.full_name}')
r = session.get("{0}/repos/{1}/{2}".format(gitea_url, gitea_user, real_repo)) r = session.get(f"{gitea_url}/repos/{gitea_user}/{repo_name}")
if r.status_code == 200: if r.status_code == 200:
print('*** repo exists: {0}/{2}'.format(gitea_user, real_repo)) print(f'*** repo exists: {gitea_user}/{repo_name}')
if not prune: if not prune:
continue continue
resp = input('\tRemove? [y/N]') resp = input('\tRemove? [y/N]')
if resp.lower() in ['y', 'yes']: if resp.lower() in ['y', 'yes']:
r = session.delete("{0}/repos/{1}/{2}".format(gitea_url, gitea_user, real_repo)) r = session.delete(f"{gitea_url}/repos/{gitea_user}/{repo_name}")
if r.status_code not in [200, 204]: if r.status_code not in [200, 204]:
print('Error', r.status_code, r.text) print('Error', r.status_code, r.text)
continue continue
r = session.get("{0}/users/{1}".format(gitea_url, gitea_dest_user))
if r.status_code != 200:
print("Cannot get user id for '{0}'".format(gitea_dest_user), file=sys.stderr)
exit(1)
gitea_uid = json.loads(r.text)["id"]
m = { m = {
"repo_name" : "{0}".format(real_repo), "repo_name" : repo_name,
"description" : repo.description or "not really known", "description" : repo.description or "not really known",
"clone_addr" : repo.clone_url, "clone_addr" : repo.clone_url,
"mirror" : True, "mirror" : True,
"private" : repo.private, "private" : repo.private,
"uid" : gitea_uid, "uid" : get_uid(gitea_dest_user),
} }
if repo.private: if repo.private:
m["auth_username"] = github_username m["auth_username"] = github_username
m["auth_password"] = "{0}".format(github_token) m["auth_password"] = f"{github_token}"
jsonstring = json.dumps(m) jsonstring = json.dumps(m)
r = session.post("{0}/repos/migrate".format(gitea_url), data=jsonstring) r = session.post(f"{gitea_url}/repos/migrate", data=jsonstring)
if r.status_code != 201: # if not CREATED if r.status_code == 201: # if CREATED
if r.status_code == 409: # repository exists print(f'\u2713 project created: {repo_name}')
print('{0} already exists'.format(real_repo)) elif r.status_code == 409: # repository exists
continue print(f'... already exists {repo_name}')
else:
print(r.status_code, r.text, jsonstring) print(r.status_code, r.text, jsonstring)
print(m) print(m)