#!/usr/bin/env python3 import os import requests from base64 import b64decode, b64encode from dotenv import load_dotenv # Load credentials from .env load_dotenv() GITHUB_USER = os.getenv("GITHUB_USERNAME") TOKEN = os.getenv("GITHUB_TOKEN") FORGEJO_USER = os.getenv("FORGEJO_USERNAME") FORGEJO_URL = os.getenv("FORGEJO_URL") headers = {"Authorization": f"token {TOKEN}"} # Get list of repos repos_url = f"https://api.github.com/users/{GITHUB_USER}/repos?per_page=100" repos = requests.get(repos_url, headers=headers).json() for repo in repos: repo_name = repo["name"] print(f"Processing {repo_name}...") # Skip forks if desired if repo["fork"]: print(" Skipping fork") continue # 1. Update or create README.md readme_url = f"https://api.github.com/repos/{GITHUB_USER}/{repo_name}/contents/README.md" r = requests.get(readme_url, headers=headers) notice = f"⚠️ This repository has moved to {FORGEJO_URL}/{FORGEJO_USER}/{repo_name}\n\n" if r.status_code == 200: readme_info = r.json() sha = readme_info["sha"] content = b64decode(readme_info["content"]).decode("utf-8") if not content.startswith(notice): updated_content = notice + content data = { "message": "Add notice about migration to Forgejo", "content": b64encode(updated_content.encode("utf-8")).decode("utf-8"), "sha": sha } r = requests.put(readme_url, headers=headers, json=data) print(" README.md updated" if r.status_code == 200 else f" Failed to update README.md: {r.text}") else: print(" README.md already contains notice") elif r.status_code == 404: data = { "message": "Create README with migration notice", "content": b64encode(notice.encode("utf-8")).decode("utf-8") } r = requests.put(readme_url, headers=headers, json=data) print(" README.md created" if r.status_code == 201 else f" Failed to create README.md: {r.text}") else: print(f" Failed to get README.md: {r.text}") # 2. Archive the repo archive_url = f"https://api.github.com/repos/{GITHUB_USER}/{repo_name}" data = {"archived": True} r = requests.patch(archive_url, headers=headers, json=data) print(" Repository archived" if r.status_code == 200 else f" Failed to archive repo: {r.text}")