flat table with action column in dry-run PR comment

This commit is contained in:
Prox 2026-03-06 19:20:29 +02:00
parent a93eebdf18
commit bb7fc8dc16

View File

@ -73,22 +73,14 @@ jobs:
print(f"[{env}] Reconciler call failed: {e}") print(f"[{env}] Reconciler call failed: {e}")
continue continue
# Format as markdown grouped by action # Format as markdown table
ops = data.get("operations", []) ops = data.get("operations", [])
summary = data.get("summary", {}) summary = data.get("summary", {})
lines = [f"## Reconciliation Plan: `{env}`\n"] lines = [f"## Reconciliation Plan: `{env}`\n"]
if not ops: if not ops:
lines.append("No changes detected.\n") lines.append("No changes detected.\n")
else: else:
groups = {"Create": [], "Update": [], "Delete": []}
for op in ops:
t = op["type"]
if t.startswith("create"): groups["Create"].append(op)
elif t.startswith("delete"): groups["Delete"].append(op)
else: groups["Update"].append(op)
def fmt_val(v): def fmt_val(v):
"""Format a value for display in markdown."""
if isinstance(v, bool): if isinstance(v, bool):
return str(v).lower() return str(v).lower()
if isinstance(v, list): if isinstance(v, list):
@ -97,14 +89,33 @@ jobs:
return "(none)" return "(none)"
return str(v) return str(v)
def fmt_create_details(op): def get_action(op):
"""Summarize key fields for a create operation.""" """Derive a human-readable action from the operation."""
t = op["type"]
changes = op.get("changes", [])
# For updates, check if it's just an enable/disable toggle
if not t.startswith("create") and not t.startswith("delete"):
if len(changes) == 1 and changes[0]["field"] == "enabled":
return "disable" if changes[0]["to"] is False else "enable"
return "update"
return t.split("_", 1)[0]
def fmt_details(op, action):
"""Format details based on action type."""
if action == "delete":
return ""
if action in ("enable", "disable"):
return ""
changes = op.get("changes", [])
if changes:
parts = []
for c in changes:
parts.append(f"`{c['field']}`: {fmt_val(c['from'])} → {fmt_val(c['to'])}")
return "; ".join(parts)
d = op.get("details", {}) d = op.get("details", {})
if not d: if not d:
return "" return ""
parts = [] parts = []
if "enabled" in d:
parts.append(f"enabled={fmt_val(d['enabled'])}")
if "sources" in d and "destinations" in d: if "sources" in d and "destinations" in d:
parts.append(f"{fmt_val(d['sources'])} → {fmt_val(d['destinations'])}") parts.append(f"{fmt_val(d['sources'])} → {fmt_val(d['destinations'])}")
if "auto_groups" in d: if "auto_groups" in d:
@ -115,31 +126,13 @@ jobs:
parts.append(f"peers: {fmt_val(d['peers'])}") parts.append(f"peers: {fmt_val(d['peers'])}")
return "; ".join(parts) if parts else "" return "; ".join(parts) if parts else ""
def fmt_changes(op): lines.append("| Action | Resource | Name | Details |")
"""Format field changes as 'field: old → new'.""" lines.append("|--------|----------|------|---------|")
changes = op.get("changes", []) for op in ops:
if not changes:
return ""
parts = []
for c in changes:
parts.append(f"`{c['field']}`: {fmt_val(c['from'])} → {fmt_val(c['to'])}")
return "; ".join(parts)
for action, items in groups.items():
if not items:
continue
lines.append(f"### {action} ({len(items)})\n")
lines.append("| Resource | Name | Details |")
lines.append("|----------|------|---------|")
for op in items:
resource = op["type"].split("_", 1)[1] if "_" in op["type"] else op["type"] resource = op["type"].split("_", 1)[1] if "_" in op["type"] else op["type"]
if action == "Update": action = get_action(op)
detail = fmt_changes(op) detail = fmt_details(op, action)
elif action == "Create": lines.append(f"| {action} | {resource} | {op['name']} | {detail} |")
detail = fmt_create_details(op)
else:
detail = ""
lines.append(f"| {resource} | {op['name']} | {detail} |")
lines.append("") lines.append("")
c, u, d = summary.get("created", 0), summary.get("updated", 0), summary.get("deleted", 0) c, u, d = summary.get("created", 0), summary.get("updated", 0), summary.get("deleted", 0)