Skip to content

Commit

Permalink
add status validation
Browse files Browse the repository at this point in the history
  • Loading branch information
tobru committed Jan 13, 2025
1 parent c33e479 commit f429753
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 65 deletions.
11 changes: 10 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
repos:
- repo: local
hooks:
- id: adr-status-valid
name: Validate ADR status
entry: hack/adr-tool.py
args:
- validate
language: python
files: ^docs/modules/ROOT/pages/adr/
- id: adr-index
name: Generate ADR Index
entry: hack/generate-adr-index.py
entry: hack/adr-tool.py
args:
- generate
language: python
files: ^docs/modules/ROOT/pages/adr/
121 changes: 121 additions & 0 deletions hack/adr-tool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#!/usr/bin/env python3

import os
import re
import sys
import argparse
from pathlib import Path


def validate_adr_status(status):
valid_statuses = [
"speculative",
"draft",
"accepted",
"rejected",
"implemented",
"obsolete",
]

if not status or status.lower() not in valid_statuses:
return False
return True


def extract_variables(file_path):
variables = {}
try:
with open(file_path, "r") as f:
content = f.read()
pattern = r":(\w+):\s*(.*)"
matches = re.finditer(pattern, content)
for match in matches:
variables[match.group(1)] = match.group(2).strip()

title_match = re.search(r"^=\s(.+)$", content, re.MULTILINE)
if title_match:
variables["title"] = title_match.group(1)
except Exception as e:
print(f"Error processing {file_path}: {e}")
return variables


def generate_index(adr_dir, output_file):
adr_files = sorted(
[f for f in Path(adr_dir).glob("*.adoc") if f.name != "index.adoc"]
)

content = """= ADR Index
:navtitle: ADRs
[cols="1,2,1,1,1,1"]
|===
|Number |Title |Author |Owner |Status |Date
"""

for adr_file in adr_files:
vars = extract_variables(adr_file)
if vars:
number = adr_file.stem
title = vars.get("title", "Untitled")
author = vars.get("adr_author", "")
owner = vars.get("adr_owner", "")
status = vars.get("adr_status", "")
date = vars.get("adr_date", "")

content += f"|xref:adr/{number}.adoc[{number}] |{title} |{author} |{owner} |{status} |{date}\n"

content += "|===\n"

with open(output_file, "w") as f:
f.write(content)


def validate_adrs(adr_dir):
adr_files = sorted(
[f for f in Path(adr_dir).glob("*.adoc") if f.name != "index.adoc"]
)
has_errors = False

for adr_file in adr_files:
vars = extract_variables(adr_file)
status = vars.get("adr_status", "")

if not validate_adr_status(status):
print(f"Error: Invalid status '{status}' in {adr_file}")
has_errors = True

return has_errors


def main():
parser = argparse.ArgumentParser(description="ADR Index Generator and Validator")
parser.add_argument(
"command",
choices=["generate", "validate"],
help="Command to execute (generate index or validate ADRs)",
)
# Add optional files argument for pre-commit compatibility
parser.add_argument(
"files",
nargs="*",
help="Files to process (ignored, for pre-commit hook compatibility)",
)
args = parser.parse_args()

base_dir = "docs/modules/ROOT/pages/adr"
output_file = f"{base_dir}/index.adoc"

if args.command == "generate":
generate_index(base_dir, output_file)
print(f"Generated index at {output_file}")
elif args.command == "validate":
has_errors = validate_adrs(base_dir)
if has_errors:
sys.exit(1)
print("All ADR statuses are valid")


if __name__ == "__main__":
main()
64 changes: 0 additions & 64 deletions hack/generate-adr-index.py

This file was deleted.

0 comments on commit f429753

Please sign in to comment.