#!/usr/bin/env python3
"""
sync_blocked_by.py — macht blocks/blocked-by bidirektional konsistent.
Für jedes konkrete Paar "A blocks B" wird A in B's blocked-by-Feld ergänzt (falls fehlt).
IDEMPOTENT (berechnet Fehlende aus Ist-Stand), default DRY-RUN, scharf: --apply
"""
import json, os, re, sys, time, urllib.request, urllib.error
APPLY = "--apply" in sys.argv
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"
API = f"{BASE}/api/v1/workspaces/{WS}/projects/{PID}"
IDRE = re.compile(r"\b(VEXT|VR|VA|VD|VH|VJ|VN|EXT|AK|R|D|H|J|N)-[A-Za-z0-9]+")
def ids(s): return IDRE.findall(s) and [m.group(0) for m in IDRE.finditer(s)] or []
def http(m, p, b=None):
data = json.dumps(b).encode() if b is not None else None
r = urllib.request.Request(API + p, data=data, method=m); r.add_header("X-API-Key", KEY)
if data: r.add_header("Content-Type", "application/json")
for _ in range(4):
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:
if e.code == 429: time.sleep(5); continue
return e.code, {}
return 999, {}
def get_all(p):
out, c = [], None
while True:
st, d = http("GET", 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
issues = get_all("/issues/")
W = {} # wid -> issue
for i in issues:
if " · " in i["name"]: W[i["name"].split(" · ")[0].strip()] = i
def field(html, label):
m = re.search(label + r":(?:b|strong)>\s*(.*?)
\s*)(.*?)(