refactor(devtools): improve changelog generation to handle existing files and duplicate headers
This commit is contained in:
2 files changed
+51
-21
No files matched your search
+8
-5
@@ -1,7 +1,10 @@
|
|||||||
## 0.1.1
|
# Changelog
|
||||||
|
|
||||||
## v0.1.1
|
## v0.1.1
|
||||||
|
|
||||||
|
```
|
||||||
|
## v0.1.1
|
||||||
|
|
||||||
✨ Added
|
✨ Added
|
||||||
- Added git hooks management and auto-fix on pre-commit
|
- Added git hooks management and auto-fix on pre-commit
|
||||||
- Added git hooks management CLI
|
- Added git hooks management CLI
|
||||||
@@ -10,8 +13,10 @@
|
|||||||
📝 Documentation
|
📝 Documentation
|
||||||
- Recommended installation using pipx and uv
|
- Recommended installation using pipx and uv
|
||||||
|
|
||||||
|
🔧 Maintenance
|
||||||
# Changelog
|
- Removed duplicate changelog entries
|
||||||
|
- Updated changelog for v0.1.1 release
|
||||||
|
```
|
||||||
|
|
||||||
## v0.1.0
|
## v0.1.0
|
||||||
|
|
||||||
@@ -48,7 +53,6 @@
|
|||||||
|
|
||||||
### 🔧 Maintenance
|
### 🔧 Maintenance
|
||||||
|
|
||||||
- ## v0.1.0
|
|
||||||
- ### 📝 Documentation
|
- ### 📝 Documentation
|
||||||
- - 📝 Documentation:
|
- - 📝 Documentation:
|
||||||
- ### 🔧 Maintenance
|
- ### 🔧 Maintenance
|
||||||
@@ -73,4 +77,3 @@
|
|||||||
|
|
||||||
- ### 🔒 Security
|
- ### 🔒 Security
|
||||||
- - ### Security
|
- - ### Security
|
||||||
|
|
||||||
@@ -119,28 +119,55 @@ class ChangelogGenerator(AIService):
|
|||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
existing_content = ""
|
existing_content = ""
|
||||||
|
|
||||||
# Create new version entry
|
# Normalize and prepare new entry
|
||||||
new_version_entry = f"## {version}\n\n{changelog_content}\n\n"
|
normalized_version = version.lstrip("vV")
|
||||||
|
header_line = f"## v{normalized_version}"
|
||||||
|
new_version_entry = f"{header_line}\n\n{changelog_content}\n\n"
|
||||||
|
|
||||||
if existing_content:
|
content = existing_content or ""
|
||||||
# Check if version already exists
|
|
||||||
version_pattern = f"## {version}\n"
|
# Ensure a single top-level title
|
||||||
if version_pattern in existing_content:
|
if not content.strip():
|
||||||
# Replace existing version content
|
content = f"# Changelog\n\n{new_version_entry}"
|
||||||
pattern = f"{version_pattern}.*?(?=## |$)"
|
else:
|
||||||
new_content = re.sub(
|
# Remove duplicate top-level titles that might exist mid-file
|
||||||
pattern, new_version_entry, existing_content, flags=re.DOTALL
|
lines = content.splitlines()
|
||||||
|
cleaned_lines = []
|
||||||
|
for i, line in enumerate(lines):
|
||||||
|
if i != 0 and line.strip() == "# Changelog":
|
||||||
|
# skip duplicate title
|
||||||
|
continue
|
||||||
|
cleaned_lines.append(line)
|
||||||
|
content = "\n".join(cleaned_lines)
|
||||||
|
|
||||||
|
# Ensure the file starts with a single title
|
||||||
|
if not content.lstrip().startswith(
|
||||||
|
"# Changelog\n"
|
||||||
|
) and not content.strip().startswith("# Changelog"):
|
||||||
|
content = f"# Changelog\n\n{content.strip()}\n"
|
||||||
|
|
||||||
|
# Replace existing section for this version (match both with and without leading 'v')
|
||||||
|
# Find a header line matching the version
|
||||||
|
pattern = (
|
||||||
|
rf"^##\s+v?{re.escape(normalized_version)}\s*$[\s\S]*?(?=^##\s+|\Z)"
|
||||||
|
)
|
||||||
|
if re.search(pattern, content, flags=re.MULTILINE):
|
||||||
|
content = re.sub(
|
||||||
|
pattern, new_version_entry, content, flags=re.MULTILINE
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
# Add new version at the top
|
# Insert new entry after the title
|
||||||
new_content = new_version_entry + existing_content
|
if content.startswith("# Changelog\n\n"):
|
||||||
else:
|
content = content.replace(
|
||||||
# Create new changelog
|
"# Changelog\n\n", f"# Changelog\n\n{new_version_entry}", 1
|
||||||
new_content = f"# Changelog\n\n{new_version_entry}"
|
)
|
||||||
|
else:
|
||||||
|
# Fallback: prepend
|
||||||
|
content = f"# Changelog\n\n{new_version_entry}{content}"
|
||||||
|
|
||||||
# Write updated changelog
|
# Write updated changelog
|
||||||
with open(output_file, "w") as f:
|
with open(output_file, "w") as f:
|
||||||
f.write(new_content)
|
f.write(content)
|
||||||
|
|
||||||
def parse_changes_from_text(self, text: str) -> List[Dict[str, str]]:
|
def parse_changes_from_text(self, text: str) -> List[Dict[str, str]]:
|
||||||
"""Parse changes from text input.
|
"""Parse changes from text input.
|
||||||
|
|||||||
Reference in new issue
Block a user