list_all_subtasks.py 1.74 KB
#!/usr/bin/env python3
"""List all subtasks for a given main task record_id.

Usage: python3 scripts/list_all_subtasks.py <main_task_id> [r|b]
  r = 需求表 (default), b = Bug表

Output: JSON list of subtasks with id, title, status.
"""
import sys, json, os

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "references"))
from pipeline_api import load_config, fetch_all


def list_subtasks(main_id, table="r"):
    AH, AT, RT, BT = load_config()
    table_id = RT if table == "r" else BT

    all_records = fetch_all(table_id)
    subtasks = []

    for r in all_records:
        fields = r.get("fields", {})
        parent = fields.get("父记录", [])

        if not parent or not isinstance(parent, list):
            continue

        for p in parent:
            if isinstance(p, dict) and p.get("record_ids"):
                pids = p["record_ids"]
                if isinstance(pids, list) and main_id in pids:
                    title = fields.get("需求标题") or fields.get("Bug标题") or ""
                    subtasks.append({
                        "id": r.get("record_id"),
                        "title": title,
                        "status": fields.get("状态", ""),
                    })
                    break

    # Sort by title (S1, S2, ...)
    subtasks.sort(key=lambda s: s["title"])
    return subtasks


if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: list_all_subtasks.py <main_task_id> [r|b]", file=sys.stderr)
        sys.exit(1)

    main_id = sys.argv[1]
    table = sys.argv[2] if len(sys.argv) > 2 else "r"

    subtasks = list_subtasks(main_id, table)
    print(json.dumps(subtasks, ensure_ascii=False, indent=2))
    print(f"\nTotal: {len(subtasks)} subtask(s)", file=sys.stderr)