#!/usr/bin/env python3 """ backup_review.py — zieht den vollen ROHEN Plane-Stand (description_html + alle IDs) als Arbeitsbasis + Rollback-Backup für die Review-Plane-Operation. READ-ONLY. Schreibt: plane-backup-review.json (raw issues + meta: states/cycles/modules/labels/members mit IDs). """ import json, os, 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 states = {s["name"]: s["id"] for s in get_all("/states/")} cycles = get_all("/cycles/"); modules = get_all("/modules/") labels = {l["name"]: l["id"] for l in get_all("/labels/")} issues = get_all("/issues/") # Zuordnung issue -> cycle / module (über cycle-issues/module-issues) i2cyc, i2mod = {}, {} cyc_name = {c["id"]: c["name"] for c in cycles} mod_name = {m["id"]: m["name"] for m in modules} for c in cycles: for it in get_all(f"/cycles/{c['id']}/cycle-issues/"): i2cyc[it["id"]] = c["id"] for m in modules: for it in get_all(f"/modules/{m['id']}/module-issues/"): i2mod.setdefault(it["id"], []).append(m["id"]) raw = [] for i in issues: raw.append({ "id": i["id"], "seq": i["sequence_id"], "name": i["name"], "state": i.get("state"), "assignees": i.get("assignees", []), "labels": i.get("labels", []), "priority": i.get("priority"), "start_date": i.get("start_date"), "target_date": i.get("target_date"), "parent": i.get("parent"), "cycle": i2cyc.get(i["id"]), "modules": i2mod.get(i["id"], []), "description_html": i.get("description_html", ""), }) raw.sort(key=lambda x: x["seq"]) out = { "pid": PID, "count": len(raw), "meta": { "states": states, "labels": labels, "cycles": {c["name"]: c["id"] for c in cycles}, "modules": {m["name"]: m["id"] for m in modules}, }, "issues": raw, } path = ".planning/plane-sync/plane-backup-review.json" json.dump(out, open(path, "w"), ensure_ascii=False, indent=1) print(f"Backup: {path} | {len(raw)} Issues") print("States:", list(states.keys())) print("Cycles:", list(out['meta']['cycles'].keys())) print("Modules:", list(out['meta']['modules'].keys()))