refactor(devgen): enhance AI commit message sanitization
The `sanitize_ai_commit_message` function has been significantly improved to better handle AI-generated commit messages, including support for bolded headers, more robust header detection, and a fallback mechanism for non-conventional messages. **Enhancements & Refinements:** - Added comprehensive docstring explaining the function's purpose and behavior - Improved regex pattern to match conventional commit headers with optional bolding (e.g., **feat: ...**) - Added case-insensitive matching for commit types - Implemented a state-based approach with `found_header` flag for clearer logic - Added automatic removal of bold markers (**) from headers - Introduced fallback behavior to return the first non-empty line if no conventional header is found - Improved code structure with better variable naming and comments - Added early return for empty input strings **Bug Fixes:** - Fixed potential issue where the function could return empty strings for non-conventional messages - Resolved edge case handling for messages without proper headers ```
This commit is contained in:
3 files changed
+124
-37
No files matched your search
+38
-10
@@ -31,25 +31,53 @@ def is_file_recent(file_path: Path | str, max_age_minutes: int = 120) -> bool:
|
||||
|
||||
|
||||
def sanitize_ai_commit_message(raw_text: str) -> str:
|
||||
"""
|
||||
Cleans up the AI-generated commit message.
|
||||
It looks for a conventional commit header and extracts everything from there.
|
||||
It handles bolded headers and common AI prefixes.
|
||||
"""
|
||||
if not raw_text:
|
||||
return ""
|
||||
|
||||
lines = raw_text.strip().split("\n")
|
||||
cleaned_lines = []
|
||||
in_block = False
|
||||
# Regex for conventional commit header
|
||||
|
||||
# Regex for conventional commit header (including optional bolding)
|
||||
# Examples:
|
||||
# - feat(root): summary
|
||||
# - **fix: bug fix**
|
||||
# - chore(deps)!: breaking change
|
||||
header_pattern = re.compile(
|
||||
r"^(feat|fix|chore|refactor|docs|style|test|build|ci)(\(.*\))?!?: .*"
|
||||
r"^(\*\*)?(feat|fix|chore|refactor|docs|style|test|build|ci)(\(.*\))?!?: .*",
|
||||
re.IGNORECASE
|
||||
)
|
||||
|
||||
cleaned_lines = []
|
||||
found_header = False
|
||||
|
||||
for line in lines:
|
||||
stripped = line.strip()
|
||||
if in_block:
|
||||
if header_pattern.match(stripped) or "**Sponsor**" in line:
|
||||
if not found_header:
|
||||
if header_pattern.match(stripped):
|
||||
found_header = True
|
||||
# Remove bolding if present
|
||||
clean_line = stripped.replace("**", "").strip()
|
||||
cleaned_lines.append(clean_line)
|
||||
else:
|
||||
# If we hit another header or a known separator, we stop (optional, but keep for now)
|
||||
if "**Sponsor**" in line:
|
||||
break
|
||||
cleaned_lines.append(line)
|
||||
elif header_pattern.match(stripped):
|
||||
in_block = True
|
||||
cleaned_lines.append(line)
|
||||
|
||||
return "\n".join(cleaned_lines).strip() if cleaned_lines else ""
|
||||
if cleaned_lines:
|
||||
return "\n".join(cleaned_lines).strip()
|
||||
|
||||
# Fallback: if no conventional commit header found, just take the first non-empty line
|
||||
# to avoid failing completely, but log a warning if possible.
|
||||
for line in lines:
|
||||
if line.strip():
|
||||
return line.strip()
|
||||
|
||||
return ""
|
||||
|
||||
|
||||
def parse_markdown_sections(
|
||||
|
||||
Reference in new issue
Block a user