Files
doc-exports/.gitea/workflows/helpers/class-comment.py

72 lines
2.0 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Generate PR comment for CLASS.TXT.json duplicate title violations.
This script reads violations.json (created by class-txt-check.py) and generates
a formatted markdown comment to be posted on the PR. The comment includes:
- File path where violations were found
- Parent document title and code
- The duplicate title
- Document codes that share the duplicate title
Usage:
Run after class-txt-check.py fails. Reads violations.json and outputs JSON
with 'body' field containing the markdown comment text.
"""
import json
import sys
def main():
try:
with open("violations.json", "r") as f:
violations = json.load(f)
except Exception:
violations = []
if not violations:
print(json.dumps({"body": "No violations to report"}))
sys.exit(0)
# Group violations by file
by_file = {}
for v in violations:
key = v["file"]
if key not in by_file:
by_file[key] = []
by_file[key].append(v)
# Build message
lines = [
"❌ **Duplicate title detected in CLASS.TXT.json**",
"",
"Found child documents with duplicate titles under the same parent:",
"",
]
for filepath, file_violations in by_file.items():
lines.append(f"**{filepath}:**")
for v in file_violations:
parent_code = v["parent_code"]
parent_title = v["parent_title"]
duplicate_title = v["duplicate_title"]
codes = v["codes"]
lines.append(f" - Parent: `{parent_title}` (code: `{parent_code}`)")
lines.append(f" Duplicate title: `{duplicate_title}`")
for code in codes:
lines.append(f" - Document code: `{code}`")
lines.append("")
lines.append(
"**Please ensure all child documents under the same parent have unique titles.**"
)
message = "\n".join(lines)
print(json.dumps({"body": message}))
if __name__ == "__main__":
main()