Keeps the PAT out of the stored remote URL and out of every command line. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
62 lines
2.1 KiB
Python
62 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Create the Gitea repo and set the origin remote, without exposing the token.
|
|
|
|
The PAT is read from `pass` inside this process and used only as an HTTP header;
|
|
it is never placed in a command line, a URL, or the stored remote.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
import urllib.error
|
|
import urllib.request
|
|
|
|
API = "https://git.falkvinge.net/api/v1"
|
|
OWNER = "rick"
|
|
REPO = "forgeway"
|
|
DESC = "OpenAI-compatible LLM router in front of forge's Ollama gpt-oss:120b; one instance per cluster"
|
|
|
|
|
|
def token() -> str:
|
|
return subprocess.run(["pass", "git/falkvinge-pat-rwall"],
|
|
capture_output=True, text=True, check=True).stdout.strip()
|
|
|
|
|
|
def api(method: str, path: str, body: dict | None = None) -> tuple[int, str]:
|
|
req = urllib.request.Request(
|
|
f"{API}{path}",
|
|
data=json.dumps(body).encode() if body else None,
|
|
headers={"Content-Type": "application/json", "Authorization": f"token {token()}"},
|
|
method=method,
|
|
)
|
|
try:
|
|
with urllib.request.urlopen(req, timeout=30) as r:
|
|
return r.status, r.read().decode()[:200]
|
|
except urllib.error.HTTPError as e:
|
|
return e.code, e.read().decode()[:200]
|
|
|
|
|
|
def main() -> int:
|
|
status, _ = api("GET", f"/repos/{OWNER}/{REPO}")
|
|
if status == 200:
|
|
print(f"repo {OWNER}/{REPO} already exists")
|
|
else:
|
|
status, body = api("POST", "/user/repos",
|
|
{"name": REPO, "description": DESC, "private": False, "auto_init": False})
|
|
if status not in (200, 201):
|
|
print(f"create failed: {status} {body}", file=sys.stderr)
|
|
return 1
|
|
print(f"created {OWNER}/{REPO}")
|
|
|
|
# The remote is stored WITHOUT credentials; pushes use the askpass helper below.
|
|
subprocess.run(["git", "remote", "remove", "origin"], capture_output=True)
|
|
subprocess.run(["git", "remote", "add", "origin",
|
|
f"https://git.falkvinge.net/{OWNER}/{REPO}.git"], check=True)
|
|
print("origin set (no credentials stored in the remote URL)")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|