Fix wiki report posting - handle root-level parent doc creation
This commit is contained in:
+20
-27
@@ -196,45 +196,38 @@ def post_report_to_wiki(report_md, project_name, collection_id):
|
||||
if not collection_id:
|
||||
return None
|
||||
|
||||
from shared import ensure_child_doc, find_child_doc
|
||||
|
||||
now = datetime.now(MT)
|
||||
headers = wiki_headers()
|
||||
|
||||
# Ensure "Project Status Reports" parent doc exists in this collection
|
||||
reports_parent_id = ensure_child_doc(
|
||||
None, "Project Status Reports",
|
||||
"Automated project status reports generated by the Project Monitor agent.",
|
||||
collection_id=collection_id,
|
||||
)
|
||||
# ensure_child_doc with parent_id=None won't find via parentDocumentId match.
|
||||
# Search manually for root-level doc in this collection.
|
||||
if not reports_parent_id:
|
||||
try:
|
||||
search = api_request(
|
||||
f"{WIKI_API}/documents.search",
|
||||
data={"query": "Project Status Reports", "collectionId": collection_id},
|
||||
headers=headers, method="POST",
|
||||
)
|
||||
for doc in search.get("data", []):
|
||||
d = doc.get("document", {})
|
||||
if d.get("title") == "Project Status Reports" and d.get("collectionId") == collection_id:
|
||||
reports_parent_id = d["id"]
|
||||
break
|
||||
except Exception:
|
||||
pass
|
||||
# Find or create "Project Status Reports" parent doc at root of this collection
|
||||
reports_parent_id = None
|
||||
try:
|
||||
search = api_request(
|
||||
f"{WIKI_API}/documents.search",
|
||||
data={"query": "Project Status Reports", "collectionId": collection_id},
|
||||
headers=headers, method="POST",
|
||||
)
|
||||
for doc in search.get("data", []):
|
||||
d = doc.get("document", {})
|
||||
if d.get("title") == "Project Status Reports" and not d.get("parentDocumentId"):
|
||||
reports_parent_id = d["id"]
|
||||
break
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if not reports_parent_id:
|
||||
try:
|
||||
result = api_request(
|
||||
f"{WIKI_API}/documents.create",
|
||||
data={"title": "Project Status Reports",
|
||||
"text": "Automated project status reports.",
|
||||
"text": "Automated project status reports generated by the Project Monitor agent.",
|
||||
"collectionId": collection_id, "publish": True},
|
||||
headers=headers, method="POST",
|
||||
)
|
||||
reports_parent_id = result["data"]["id"]
|
||||
except Exception:
|
||||
reports_parent_id = None
|
||||
print(f" Created Project Status Reports parent: {reports_parent_id}")
|
||||
except Exception as e:
|
||||
print(f" Warning: could not create reports parent: {e}", file=sys.stderr)
|
||||
|
||||
title = f"Project Status — {project_name} — {now.strftime('%Y-%m-%d')}"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user