Setup
bash
1export GITEA_TOKEN="xxxxxxxxxxxxxxxxxxxxxxxx"2export GITEA_URL="http://192.168.0.100:3000"bash
1python3 -m venv .venv2source .venv/bin/activate3pip install requestsbash
1nano github-to-gitea.pypython
1#!/usr/bin/env python32
3import os4import sys5import requests6
7
8# ============================================================9# Configuration10# ============================================================11
12GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")13GITEA_URL = os.getenv("GITEA_URL", "http://192.168.0.100:3000")14GITEA_TOKEN = os.getenv("GITEA_TOKEN")15GITEA_OWNER = os.getenv("GITEA_OWNER", "ded")16
17# Mirror interval accepted by Gitea.18# Examples: "8h", "24h", "168h"19MIRROR_INTERVAL = os.getenv("MIRROR_INTERVAL", "24h")20
21# Set to "true" if you want to mirror only private repos.22PRIVATE_ONLY = os.getenv("PRIVATE_ONLY", "false").lower() == "true"23
24
25# ============================================================26# Validation27# ============================================================28
29if not GITHUB_TOKEN:30 print("ERROR: GITHUB_TOKEN is not set")31 sys.exit(1)32
33if not GITEA_TOKEN:34 print("ERROR: GITEA_TOKEN is not set")35 sys.exit(1)36
37
38GITEA_URL = GITEA_URL.rstrip("/")39
40
41github_headers = {42 "Accept": "application/vnd.github+json",43 "Authorization": f"Bearer {GITHUB_TOKEN}",44 "X-GitHub-Api-Version": "2026-03-10",45}46
47gitea_headers = {48 "Authorization": f"token {GITEA_TOKEN}",49 "Content-Type": "application/json",50}51
52
53# ============================================================54# GitHub55# ============================================================56
57def get_github_repos():58 """59 Get all repositories accessible to the authenticated user,60 but keep only repositories owned by that user.61 """62
63 repos = []64 page = 165
66 while True:67 response = requests.get(68 "https://api.github.com/user/repos",69 headers=github_headers,70 params={71 "per_page": 100,72 "page": page,73 "affiliation": "owner",74 "sort": "full_name",75 "direction": "asc",76 },77 timeout=30,78 )79
80 response.raise_for_status()81
82 batch = response.json()83
84 if not batch:85 break86
87 repos.extend(batch)88 page += 189
90 return repos91
92
93# ============================================================94# Gitea95# ============================================================96
97def gitea_repo_exists(owner, repo):98 response = requests.get(99 f"{GITEA_URL}/api/v1/repos/{owner}/{repo}",100 headers=gitea_headers,101 timeout=30,102 )103
104 if response.status_code == 200:105 return True106
107 if response.status_code == 404:108 return False109
110 response.raise_for_status()111
112
113def create_mirror(repo):114 name = repo["name"]115
116 payload = {117 "clone_addr": repo["clone_url"],118
119 # GitHub authentication used by Gitea120 "auth_token": GITHUB_TOKEN,121
122 # Gitea destination123 "repo_owner": GITEA_OWNER,124 "repo_name": name,125
126 # Tell Gitea this is a GitHub repository127 "service": "github",128
129 # Create pull mirror130 "mirror": True,131 "mirror_interval": MIRROR_INTERVAL,132
133 # Preserve metadata where possible134 "private": repo["private"],135 "description": repo.get("description") or "",136
137 "issues": repo.get("has_issues", False),138 "pull_requests": True,139 "releases": True,140 "wiki": repo.get("has_wiki", False),141 "labels": True,142 "milestones": True,143 "lfs": True,144 }145
146 response = requests.post(147 f"{GITEA_URL}/api/v1/repos/migrate",148 headers=gitea_headers,149 json=payload,150 timeout=120,151 )152
153 return response154
155
156# ============================================================157# Main158# ============================================================159
160def main():161
162 print("=" * 60)163 print("GitHub → Gitea mirror")164 print("=" * 60)165
166 print(f"GitHub: https://github.com")167 print(f"Gitea: {GITEA_URL}")168 print(f"Owner: {GITEA_OWNER}")169 print(f"Interval: {MIRROR_INTERVAL}")170 print()171
172 print("Fetching GitHub repositories...")173
174 try:175 repos = get_github_repos()176 except requests.RequestException as e:177 print(f"ERROR: Cannot access GitHub API: {e}")178 sys.exit(1)179
180 print(f"Found {len(repos)} repositories.")181 print()182
183 created = 0184 skipped = 0185 failed = 0186
187 for repo in repos:188
189 name = repo["name"]190 full_name = repo["full_name"]191
192 if PRIVATE_ONLY and not repo["private"]:193 print(f"[SKIP] {full_name} (public)")194 skipped += 1195 continue196
197 print(f"[CHECK] {full_name}")198
199 try:200 if gitea_repo_exists(GITEA_OWNER, name):201 print(f"[EXISTS] {GITEA_OWNER}/{name}")202 skipped += 1203 continue204
205 print(f"[CREATE] {GITEA_OWNER}/{name}")206
207 response = create_mirror(repo)208
209 if response.status_code == 201:210 print(f"[OK] {GITEA_OWNER}/{name}")211 created += 1212
213 else:214 print(215 f"[ERROR] {GITEA_OWNER}/{name}: "216 f"{response.status_code} {response.text}"217 )218 failed += 1219
220 except requests.RequestException as e:221 print(f"[ERROR] {full_name}: {e}")222 failed += 1223
224 print()225
226 print("=" * 60)227 print("Finished")228 print("=" * 60)229
230 print(f"Found: {len(repos)}")231 print(f"Created: {created}")232 print(f"Skipped: {skipped}")233 print(f"Failed: {failed}")234
235 if failed:236 sys.exit(1)237
238
239if __name__ == "__main__":240 main()