Files
HiveGrid-Wireframes/hivegrid-cloud/.planning/plane-sync/dump_snapshot.py
T

66 lines
3.3 KiB
Python

#!/usr/bin/env python3
"""
dump_snapshot.py — zieht den vollständigen HiveGrid-Plane-Stand als lokalen Snapshot.
Schreibt .planning/plane-sync/plane-snapshot.json (Issues mit Text-Description, Labels,
Cycle/Module-Zuordnung, Assignees, Dates). Read-only. Arbeitsbasis für Scrub/Analyse.
"""
import json, os, re, urllib.request, urllib.error
TOK={}
for line in open(os.path.expanduser("~/.cli-tokens/plane")):
line=line.strip()
if "=" in line and not line.startswith("#"):
k,v=line.split("=",1); TOK[k.strip()]=v.strip()
BASE=TOK["PLANE_BASE_URL"].rstrip("/"); WS=TOK["PLANE_WORKSPACE"]; KEY=TOK["PLANE_API_KEY"]
PID="35d0d968-8ab3-40cc-97b8-b81d917dc5df" # HiveGrid (hardcoded, NICHT PowerLinked)
API=f"{BASE}/api/v1/workspaces/{WS}/projects/{PID}"
def req(p):
r=urllib.request.Request(API+p); r.add_header("X-API-Key",KEY)
try:
with urllib.request.urlopen(r,timeout=30) as resp:
raw=resp.read().decode(); return resp.status,(json.loads(raw) if raw else {})
except urllib.error.HTTPError as e: return e.code,{}
def get_all(p):
out,c=[],None
while True:
st,d=req(p+(("?per_page=100&cursor="+c) if c else "?per_page=100"))
if st>=400: break
if isinstance(d,list): out+=d; break
out+=d.get("results",[])
if d.get("next_page_results") and d.get("next_cursor"): c=d["next_cursor"]
else: break
return out
def html2txt(h):
if not h: return ""
h=re.sub(r'<a [^>]*href="([^"]+)"[^>]*>(.*?)</a>', r'\2 (\1)', h)
h=re.sub(r'<br\s*/?>', '\n', h); h=re.sub(r'</p>|</div>|</li>', '\n', h)
h=re.sub(r'<li>', '- ', h); h=re.sub(r'<[^>]+>', '', h)
import html as _h; h=_h.unescape(h)
return re.sub(r'\n{3,}', '\n\n', h).strip()
MEM={"8e34a88c-19c9-4565-b4f5-50415d623ce8":"Raffael","8bf43e86-dc8a-4963-b57a-aeb5f2cc450a":"Akeshan",
"6c1fc63f-133d-4f40-86e4-7f658a6edcc7":"David","a5883f5e-3038-453c-9401-39ab1f40be9e":"Dejan",
"48367c1f-20d2-44f1-8ae2-faf35aa2052a":"Haris"}
states={s["id"]:s["name"] for s in get_all("/states/")}
cycles=get_all("/cycles/"); modules=get_all("/modules/")
labels={l["id"]:l["name"] for l in get_all("/labels/")}
issues=get_all("/issues/")
i2cyc={};
for c in cycles:
for it in get_all(f"/cycles/{c['id']}/cycle-issues/"): i2cyc[it["id"]]=c["name"]
i2mod={}
for m in modules:
for it in get_all(f"/modules/{m['id']}/module-issues/"): i2mod[it["id"]]=m["name"]
snap=[]
for i in issues:
snap.append({"seq":i["sequence_id"],"id":i["id"],"name":i["name"],
"state":states.get(i.get("state")),"cycle":i2cyc.get(i["id"]),"module":i2mod.get(i["id"]),
"assignees":[MEM.get(a,a) for a in i.get("assignees",[])],
"labels":[labels.get(l,l) for l in i.get("labels",[])],
"priority":i.get("priority"),"start_date":i.get("start_date"),"target_date":i.get("target_date"),
"parent":i.get("parent"),"description":html2txt(i.get("description_html",""))})
snap.sort(key=lambda x:x["seq"])
out=".planning/plane-sync/plane-snapshot.json"
json.dump({"project":"HiveGrid","pid":PID,"count":len(snap),"labels":sorted(labels.values()),
"cycles":[c["name"] for c in cycles],"modules":[m["name"] for m in modules],"issues":snap},
open(out,"w"),ensure_ascii=False,indent=1)
print(f"Snapshot: {out} | {len(snap)} Issues | Labels: {sorted(labels.values())}")