Files
BT411/scratchpad/night7/gitea.py
T

60 lines
1.8 KiB
Python

"""Shared gitea helper for BT411 issue housekeeping."""
import base64, json, subprocess, urllib.request, urllib.error
REPO = r"C:\git\bt411"
BASE = "https://gitea.mysticmachines.com/api/v1/repos/VWE/BT411"
_out = subprocess.run(["git", "credential", "fill"],
input="protocol=https\nhost=gitea.mysticmachines.com\n\n",
capture_output=True, text=True, cwd=REPO)
_cred = dict(l.split("=", 1) for l in _out.stdout.strip().splitlines() if "=" in l)
AUTH = "Basic " + base64.b64encode(
(_cred["username"] + ":" + _cred["password"]).encode()).decode()
def call(path, method="GET", payload=None):
data = json.dumps(payload).encode() if payload is not None else None
r = urllib.request.Request(BASE + path, data=data, method=method)
r.add_header("Authorization", AUTH)
if data:
r.add_header("Content-Type", "application/json")
try:
return json.load(urllib.request.urlopen(r))
except urllib.error.HTTPError as e:
print("HTTP %s on %s %s: %s" % (e.code, method, path, e.read()[:400]))
raise
def all_issues(state="all"):
rows, page = [], 1
while True:
chunk = call("/issues?state=%s&limit=50&page=%d" % (state, page))
if not chunk:
break
rows += chunk
page += 1
if page > 12:
break
return rows
def create(title, body, labels=None):
p = {"title": title, "body": body}
if labels:
p["labels"] = labels
i = call("/issues", "POST", p)
print("CREATED #%s %s" % (i["number"], i["title"]))
return i
def comment(num, body):
call("/issues/%s/comments" % num, "POST", {"body": body})
print("COMMENTED #%s" % num)
def close(num, body=None):
if body:
comment(num, body)
call("/issues/%s" % num, "PATCH", {"state": "closed"})
print("CLOSED #%s" % num)