86 lines
3.8 KiB
Python
86 lines
3.8 KiB
Python
"""Push viktor-autoreg to GitHub via Git Data API (no git-remote-https on this machine)."""
|
|
import json, base64, os, glob, urllib.request, urllib.error
|
|
|
|
TOK = open(os.path.expanduser("~/tmp/viktor_reg/ghtok.txt")).read().strip()
|
|
OWNER = "gbonnet65923"
|
|
REPO = "viktor-autoreg"
|
|
API = "https://api.github.com"
|
|
|
|
def req(method, path, data=None, raw=False):
|
|
r = urllib.request.Request(API + path, method=method,
|
|
data=json.dumps(data).encode() if data else None,
|
|
headers={"Authorization": f"token {TOK}", "User-Agent": "eni",
|
|
"Accept": "application/vnd.github+json",
|
|
**({"Content-Type": "application/json"} if data else {})})
|
|
try:
|
|
resp = urllib.request.urlopen(r, timeout=60)
|
|
body = resp.read()
|
|
return json.loads(body) if not raw else body
|
|
except urllib.error.HTTPError as e:
|
|
detail = e.read().decode()[:300]
|
|
print(f"HTTP {e.code} on {method} {path}: {detail}")
|
|
if e.code in (409, 422) and "already exists" in detail:
|
|
return {"full_name": f"{OWNER}/{REPO}", "exists": True}
|
|
raise
|
|
|
|
# 1. create repo
|
|
try:
|
|
repo = req("POST", "/user/repos", {"name": REPO, "description": "Viktor.com reverse API + signup door analysis + autoreg tooling", "private": False, "auto_init": False})
|
|
print("repo created:", repo["full_name"])
|
|
except urllib.error.HTTPError as e:
|
|
err = e.read().decode()
|
|
if "already exists" in err:
|
|
print("repo exists, continuing")
|
|
else:
|
|
raise
|
|
BR = "main"
|
|
# 2a. seed empty repo with initial commit (blob API requires non-empty repo)
|
|
readme_b64 = base64.b64encode(open(os.path.expanduser("~/tmp/viktor_reg/README.md"), "rb").read()).decode()
|
|
try:
|
|
c = req("PUT", f"/repos/{OWNER}/{REPO}/contents/README.md", {"message": "init", "content": readme_b64, "branch": BR})
|
|
print("seeded README:", c["commit"]["sha"][:10])
|
|
except urllib.error.HTTPError as e:
|
|
detail = e.read().decode()[:200]
|
|
if "already exists" in detail:
|
|
print("README already there")
|
|
else:
|
|
print("seed err:", e.code, detail); raise
|
|
|
|
# get base tree/commit for update
|
|
head = req("GET", f"/repos/{OWNER}/{REPO}/git/refs/heads/{BR}")
|
|
base_commit = head["object"]["sha"]
|
|
base_tree = req("GET", f"/repos/{OWNER}/{REPO}/git/commits/{base_commit}")["tree"]["sha"]
|
|
print("base:", base_commit[:10], "tree:", base_tree[:10])
|
|
|
|
# 2. create blobs on top of seeded repo
|
|
files = []
|
|
for f in glob.glob(os.path.expanduser("~/tmp/viktor_reg/**/*"), recursive=True):
|
|
if os.path.isfile(f) and not f.endswith("ghtok.txt"):
|
|
files.append(f)
|
|
print("files:", [f.split("viktor_reg")[-1].replace(os.sep, "/") for f in files])
|
|
|
|
sha_cache = {}
|
|
for f in files:
|
|
rel = f.split("viktor_reg")[-1].replace(os.sep, "/").lstrip("/")
|
|
content = base64.b64encode(open(f, "rb").read()).decode()
|
|
blob = req("POST", f"/repos/{OWNER}/{REPO}/git/blobs", {"content": content, "encoding": "base64"})
|
|
sha_cache[rel] = blob["sha"]
|
|
print("blob:", rel, blob["sha"][:10])
|
|
|
|
tree = req("POST", f"/repos/{OWNER}/{REPO}/git/trees", {
|
|
"base_tree": base_tree,
|
|
"tree": [{"path": p, "mode": "100644", "type": "blob", "sha": s} for p, s in sha_cache.items()]})
|
|
print("tree:", tree["sha"][:10])
|
|
|
|
commit = req("POST", f"/repos/{OWNER}/{REPO}/git/commits", {
|
|
"message": "viktor.com reverse API + signup doors + autoreg tools", "tree": tree["sha"], "parents": [base_commit]})
|
|
print("commit:", commit["sha"][:10])
|
|
|
|
try:
|
|
ref = req("POST", f"/repos/{OWNER}/{REPO}/git/refs", {"ref": f"refs/heads/{BR}", "sha": commit["sha"]})
|
|
print("ref created:", ref["object"]["sha"][:10])
|
|
except urllib.error.HTTPError:
|
|
ref = req("PATCH", f"/repos/{OWNER}/{REPO}/git/refs/heads/{BR}", {"sha": commit["sha"], "force": True})
|
|
print("ref updated:", ref["object"]["sha"][:10])
|
|
print("URL: https://github.com/%s/%s" % (OWNER, REPO))
|