-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal_push.py
More file actions
140 lines (117 loc) · 3.95 KB
/
local_push.py
File metadata and controls
140 lines (117 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import urllib.request
import urllib.error
import json
import ssl
import os
import sys
import subprocess
# -----------------------------
# Umgebungsvariablen laden
# -----------------------------
GITEA_TOKEN = os.getenv("GITEA_TOKEN")
if not GITEA_TOKEN:
print("❌ Fehler: Umgebungsvariable GITEA_TOKEN prüfen!")
sys.exit(1)
# -----------------------------
# Konfiguration
# -----------------------------
GITEA_URL = "https://git.gmk.lan:3300"
GITEA_ORG_REF = "github"
GITEA_ORG = "local"
LOCAL_BASE = sys.argv[1] if len(sys.argv) > 1 else "/mnt/z/github"
ssl_ctx = ssl._create_unverified_context()
# -----------------------------
# HTTP Funktion
# -----------------------------
def http_request(url, data=None, headers=None, method="GET"):
all_headers = headers or {}
all_headers["Authorization"] = f"token {GITEA_TOKEN}"
body = json.dumps(data).encode("utf-8") if data else None
req = urllib.request.Request(url, data=body, headers=all_headers, method=method)
try:
with urllib.request.urlopen(req, context=ssl_ctx) as resp:
return resp.read().decode("utf-8"), resp.getcode()
except urllib.error.HTTPError as e:
return e.read().decode("utf-8"), e.code
except Exception as e:
return str(e), 500
# -----------------------------
# API Funktionen
# -----------------------------
def gitea_repo_exists(org, repo_name):
url = f"{GITEA_URL}/api/v1/repos/{org}/{repo_name}"
_, status = http_request(url)
return status == 200
def create_gitea_repo(repo_name):
url = f"{GITEA_URL}/api/v1/orgs/{GITEA_ORG}/repos"
payload = {
"name": repo_name,
"private": False,
"auto_init": False,
}
headers = {"Content-Type": "application/json"}
text, status = http_request(url, data=payload, headers=headers, method="POST")
if status == 201:
return True
print(f" ❌ Repo anlegen fehlgeschlagen: {status} {text}")
return False
def push_repo(local_path, repo_name):
remote_url = f"https://oauth2:{GITEA_TOKEN}@git.gmk.lan:3300/{GITEA_ORG}/{repo_name}.git"
env = {**os.environ, "GIT_SSL_NO_VERIFY": "true"}
# Temporären Remote setzen (überschreibt nicht dauerhaft)
result = subprocess.run(
["git", "push", remote_url, "--all", "--force"],
cwd=local_path,
capture_output=True,
text=True,
env=env,
)
if result.returncode != 0:
print(f" ❌ Push fehlgeschlagen:\n{result.stderr.strip()}")
return False
# Tags auch pushen
subprocess.run(
["git", "push", remote_url, "--tags"],
cwd=local_path,
capture_output=True,
text=True,
env=env,
)
return True
# -----------------------------
# Hauptprogramm
# -----------------------------
def main():
print(f"🚀 Lokale Repos aus {LOCAL_BASE} nach Gitea ({GITEA_ORG}) pushen...\n")
if not os.path.isdir(LOCAL_BASE):
print(f"❌ Verzeichnis nicht gefunden: {LOCAL_BASE}")
sys.exit(1)
entries = sorted(os.listdir(LOCAL_BASE))
pushed = 0
skipped = 0
errors = 0
for name in entries:
local_path = os.path.join(LOCAL_BASE, name)
# Nur echte Git-Repos
if not os.path.isdir(os.path.join(local_path, ".git")):
continue
if gitea_repo_exists(GITEA_ORG, name):
print(f"⏭ Überspringe {name} (existiert bereits in {GITEA_ORG})")
skipped += 1
continue
if gitea_repo_exists(GITEA_ORG_REF, name):
print(f"⏭ Überspringe {name} (existiert bereits in {GITEA_ORG_REF})")
skipped += 1
continue
print(f"➕ {name}")
if not create_gitea_repo(name):
errors += 1
continue
if push_repo(local_path, name):
print(f" ✔ Gepusht")
pushed += 1
else:
errors += 1
print(f"\n📊 Fertig: {pushed} gepusht, {skipped} übersprungen, {errors} Fehler")
if __name__ == "__main__":
main()