import json import os import requests import sys TOKEN = os.getenv("GITEA_TOKEN") URL = os.getenv("GITEA_URL") REPO = os.getenv("REPO_NAME") HEADERS = {"Authorization": f"token {TOKEN}", "Content-Type": "application/json"} def sync(): # Your ls shows 'issues.jsonl' exists beads_path = ".beads/issues.jsonl" if not os.path.exists(beads_path): print(f"❌ ERROR: {beads_path} not found.") # Print directory to debug what the Action user sees print(f"Directory Contents: {os.listdir('.beads/') if os.path.exists('.beads') else 'None'}") sys.exit(1) print(f"🔍 Reading Beads from: {beads_path}") try: # Fetch existing Gitea issues resp = requests.get(f"{URL}/api/v1/repos/{REPO}/issues?state=all", headers=HEADERS) resp.raise_for_status() existing = {i['title'].split(']')[0][1:]: i for i in resp.json() if ']' in i['title']} with open(beads_path, "r") as f: for line in f: if not line.strip(): continue data = json.loads(line) bid, title = data.get("id"), data.get("title") payload = { "title": f"[{bid}] {title}", "body": f"{data.get('description', 'No description')}\n\n---\n**Beads ID:** `{bid}`", "state": "closed" if data.get("status") in ["closed", "done"] else "open" } if bid in existing: print(f"✅ Updating: {bid}") requests.patch(f"{URL}/api/v1/repos/{REPO}/issues/{existing[bid]['number']}", headers=HEADERS, json=payload) else: print(f"➕ Creating: {bid}") requests.post(f"{URL}/api/v1/repos/{REPO}/issues", headers=HEADERS, json=payload) except PermissionError: print(f"❌ PERMISSION DENIED: Action user cannot read {beads_path}. Run 'chmod 644 {beads_path}' locally.") sys.exit(1) except Exception as e: print(f"❌ ERROR: {e}") sys.exit(1) if __name__ == "__main__": sync()