github-to-gitea-mirror/mirror.py

49 lines
1.5 KiB
Python
Raw Normal View History

2023-09-15 00:28:41 +02:00
import requests
from dotenv import load_dotenv
import os
2023-09-15 00:51:01 +02:00
# Load the .env file
2023-09-15 00:28:41 +02:00
load_dotenv()
GITHUB_USER = os.getenv("GITHUB_USER")
GITEA_USER = os.getenv("GITEA_USER")
GITEA_TOKEN = os.getenv("GITEA_TOKEN")
GITHUB_URL = f"https://api.github.com/users/{GITHUB_USER}/repos"
GITEA_URL = f"https://git.veen.world/api/v1/users/{GITEA_USER}/repos"
HEADERS = {
"Authorization": f"token {GITEA_TOKEN}"
}
2023-09-15 00:51:01 +02:00
# Fetch repos from GitHub
2023-09-15 00:28:41 +02:00
response = requests.get(GITHUB_URL)
response.raise_for_status()
github_repos = {repo['name']: repo['clone_url'] for repo in response.json()}
2023-09-15 00:51:01 +02:00
# Fetch repos from Gitea
2023-09-15 00:28:41 +02:00
response = requests.get(GITEA_URL, headers=HEADERS)
response.raise_for_status()
gitea_repos = set(repo['name'] for repo in response.json())
2023-09-15 00:51:01 +02:00
# Identify repos not yet mirrored on Gitea
2023-09-15 00:28:41 +02:00
to_mirror = {name: url for name, url in github_repos.items() if name not in gitea_repos}
2023-09-15 01:26:45 +02:00
# Fetch the Gitea User ID
response = requests.get(f"https://git.veen.world/api/v1/users/{GITEA_USER}", headers=HEADERS)
response.raise_for_status()
gitea_user_id = response.json().get("id")
2023-09-15 00:28:41 +02:00
for name, url in to_mirror.items():
data = {
2023-09-15 01:26:45 +02:00
"clone_addr": url,
2023-09-15 00:28:41 +02:00
"mirror": True,
"private": True,
"repo_name": name,
2023-09-15 01:26:45 +02:00
"uid": gitea_user_id
2023-09-15 00:28:41 +02:00
}
response = requests.post(f"https://git.veen.world/api/v1/repos/migrate", headers=HEADERS, json=data)
if response.status_code == 201:
2023-09-15 00:51:01 +02:00
print(f"{name} was successfully mirrored.")
2023-09-15 00:28:41 +02:00
else:
2023-09-15 00:51:01 +02:00
print(f"Error mirroring {name}. Status Code: {response.status_code}, Response: {response.text}")