Last active
September 18, 2024 07:56
-
-
Save Tobi-De/29e8f76cd03a95e2ae150db91ca6e903 to your computer and use it in GitHub Desktop.
backup github repos to gitea
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# requirements: httpx, python-dotenv | |
import os | |
import httpx | |
from dotenv import load_dotenv | |
from datetime import datetime as dt | |
import pytz | |
from dateutil import parser | |
load_dotenv() | |
gitea_api_key = os.getenv("GITEA_API_KEY") | |
gitea_server_url = os.getenv("GITEA_SERVER_URL") | |
github_token = os.getenv("GITHUB_TOKEN") | |
github_username = "tobi-de" | |
# repos_api_path = f"https://api.github.com/users/{github_username}/repos?type=owner&visibility=all" | |
repos_api_path = f"https://api.github.com/user/repos?type=owner" | |
def get_next_link(link): | |
links = link.split(',') | |
for l in links: | |
if 'rel="next"' in l: | |
return l.split(";")[0].replace("<", "").replace(">", "").strip() | |
def retrieve_all_gh_repos(): | |
repos = [] | |
with httpx.Client(headers={"Authorization": f"token {github_token}"}) as client: | |
url = repos_api_path | |
while True: | |
resp = client.get(url) | |
data = resp.json() | |
repos.extend({"clone_addr": d["clone_url"], "description": d["description"], "private": d["private"], | |
"repo_name": d["name"], "pushed_at": d["pushed_at"]} for d in data) | |
link = resp.headers.get("link") | |
has_next = link and 'rel="next"' in link | |
if not has_next: | |
break | |
url = get_next_link(link) | |
return repos | |
def main(): | |
repos = retrieve_all_gh_repos() | |
print(len(repos)) | |
current_datetime_utc = dt.now(pytz.timezone('UTC')) | |
with httpx.Client( | |
base_url=f"{gitea_server_url}/api/v1", | |
headers={"Authorization": f"token {gitea_api_key}"}, | |
) as client: | |
for repo in repos: | |
print(repo["repo_name"]) | |
last_pushed = parser.parse(repo["pushed_at"]) | |
mirror_interval = "24h" if (current_datetime_utc - last_pushed).days < 15 else f"{7 * 24}h" | |
resp = client.post("repos/migrate", json={ | |
"auth_password": github_token, | |
"auth_username": github_username, | |
"clone_addr": repo["clone_addr"], | |
"description": repo["description"], | |
"private": repo["private"], | |
"repo_name": repo["repo_name"], | |
"service": "github", | |
"issues": True, | |
"labels": True, | |
"mirror": True, | |
"releases": True, | |
"wiki": True, | |
"pull_requests": True, | |
"mirror_interval": mirror_interval | |
}) | |
print(resp) | |
print(len(repos)) | |
if __name__ == "__main__": | |
main() |
Author
Tobi-De
commented
Sep 18, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment