This commit is contained in:
sankalp committed 2025-06-04 13:31:03 +05:45
1 parent 6f04e5a4b6
commit c5bf81bc06
3 files changed
+140 -80

No files matched your search

+26 -13
View File
@@ -1,20 +1,33 @@
# Changelog
## 0.1.0 ## 0.1.0
Version 0.1.0 - Initial Release ### Added
📝 Documentation: * ✨ docs(changelog): Add ChangelogGenerator class documentation
- Updated ChangelogGenerator class documentation in docs(changelog)
- Updated changelog format for clarity and consistency in docs(changelog)
- Added ChangelogGenerator class documentation in docs(changelog)
- Updated LICENSE
🔧 Maintenance: ### Changed
- Removed unused variable in _run\_git\_command calls in refactor(git.py)
- Updated GitService to return exit code from _run\_git\_command and improved get\_commit\_history method signature and docstring in refactor(devtools/shared/git.py)
📚 Documentation: * 🔄 refactor(generator): Update commit message and changelog generation using AI
- Updated changelog format in docs(changelog) * 🔄 refactor(git.py): Remove unused variable in `_run_git_command` calls
* 🔄 refactor(devtools/shared/git.py): Update GitService to return exit code from `_run_git_command` and improve `get_commit_history` method signature and docstring
* 🔄 docs(changelog): Updated changelog format for clarity and consistency
🔒 Security: ### Fixed
- Initial commit
* 🐛 docs(changelog): Updated changelog format and added ChangelogGenerator class documentation
### Initial Commits
* 🚀 Initial commit
* 📚 Update `README.md` file
* 📚 Update `LICENSE`
### Removed
* 🗑️ Not applicable for this version
### Security
* 🔒 Not applicable for this version
+89 -51
View File
@@ -1,50 +1,68 @@
""" """
Changelog generation using AI. Changelog generation using AI.
""" """
from typing import List, Dict, Optional from typing import List, Dict, Optional
from ..shared.ai import AIService from ..shared.ai import AIService
from ..shared.config import Config from ..shared.config import Config
from ..shared.git import GitService from ..shared.git import GitService
import re
class ChangelogGenerator(AIService): class ChangelogGenerator(AIService):
"""AI-powered changelog generator.""" """AI-powered changelog generator."""
def __init__(self, config: Config, git_service: Optional[GitService] = None): def __init__(self, config: Config, git_service: Optional[GitService] = None):
"""Initialize changelog generator. """Initialize changelog generator.
Args: Args:
config: Configuration object config: Configuration object
git_service: Optional GitService instance git_service: Optional GitService instance
""" """
super().__init__(config) super().__init__(config)
self.git_service = git_service self.git_service = git_service
def get_commits(self, from_tag: str) -> List[Dict[str, str]]: def get_commits(self, from_tag: str) -> List[Dict[str, str]]:
"""Get commits since a tag.""" """Get commits since a tag."""
if not self.git_service: if not self.git_service:
raise ValueError("GitService not provided") raise ValueError("GitService not provided")
return self.git_service.get_commits_since_tag(from_tag) return self.git_service.get_commits_since_tag(from_tag)
def get_commits_since_date(self, days: int) -> List[Dict[str, str]]: def get_commits_since_date(self, days: int) -> List[Dict[str, str]]:
"""Get commits since a date.""" """Get commits since a date."""
if not self.git_service: if not self.git_service:
raise ValueError("GitService not provided") raise ValueError("GitService not provided")
return self.git_service.get_commits_since_date(days) return self.git_service.get_commits_since_date(days)
def get_last_n_commits(self, n: int) -> List[Dict[str, str]]: def get_last_n_commits(self, n: int) -> List[Dict[str, str]]:
"""Get last N commits.""" """Get last N commits."""
if not self.git_service: if not self.git_service:
raise ValueError("GitService not provided") raise ValueError("GitService not provided")
return self.git_service.get_commit_history(limit=n) return self.git_service.get_commit_history(limit=n)
def generate_changelog(self, version: str, changes: List[Dict[str, str]], temperature: Optional[float] = None) -> str: def _extract_pr_reference(self, message: str) -> Optional[str]:
"""Extract PR reference from commit message if it exists."""
# Look for patterns like (#123) or (#123, #456)
pr_pattern = r"\(#(\d+)(?:,\s*#\d+)*\)"
match = re.search(pr_pattern, message)
if match:
# Return the full match including parentheses
return match.group(0)
return None
def generate_changelog(
self,
version: str,
changes: List[Dict[str, str]],
temperature: Optional[float] = None,
) -> str:
"""Generate a changelog from a list of changes. """Generate a changelog from a list of changes.
Args: Args:
version: Version number for the changelog version: Version number for the changelog
changes: List of changes, each with 'message' and 'hash' changes: List of changes, each with 'message' and 'hash'
temperature: AI temperature for generation temperature: AI temperature for generation
Returns: Returns:
Generated changelog content Generated changelog content
""" """
@@ -62,25 +80,30 @@ class ChangelogGenerator(AIService):
- 📝 Documentation - 📝 Documentation
- 🔧 Maintenance - 🔧 Maintenance
- 🗑️ Removed - 🗑️ Removed
- 🔒 Security""" - 🔒 Security
6. Only include PR references if they exist in the original commit message
# Format changes for the prompt 7. Keep the changelog clean and professional"""
changes_text = "\n".join(
f"{change['message']}" # Format changes for the prompt, preserving PR references
for change in changes changes_text = "\n".join(f"{change['message']}" for change in changes)
)
user_prompt = f"Generate a changelog for version {
user_prompt = f"Generate a changelog for version {version} with these changes:\n\n{changes_text}" version
} with these changes:\n\n{changes_text}"
return self.generate_completion( return self.generate_completion(
system_prompt, system_prompt, user_prompt, temperature=temperature
user_prompt,
temperature=temperature
) )
def update_changelog_file(self, version: str, changes: List[Dict[str, str]], output_file: str, temperature: Optional[float] = None) -> None: def update_changelog_file(
self,
version: str,
changes: List[Dict[str, str]],
output_file: str,
temperature: Optional[float] = None,
) -> None:
"""Generate and update a changelog file. """Generate and update a changelog file.
Args: Args:
version: Version number for the changelog version: Version number for the changelog
changes: List of changes, each with 'message' and 'hash' changes: List of changes, each with 'message' and 'hash'
@@ -88,41 +111,55 @@ class ChangelogGenerator(AIService):
temperature: AI temperature for generation temperature: AI temperature for generation
""" """
changelog_content = self.generate_changelog(version, changes, temperature) changelog_content = self.generate_changelog(version, changes, temperature)
# Read existing changelog if it exists # Read existing changelog if it exists
try: try:
with open(output_file, "r") as f: with open(output_file, "r") as f:
existing_content = f.read() existing_content = f.read()
except FileNotFoundError: except FileNotFoundError:
existing_content = "" existing_content = ""
# Add new changelog entry at the top # Create new version entry
new_content = f"## {version}\n\n{changelog_content}\n\n" new_version_entry = f"## {version}\n\n{changelog_content}\n\n"
if existing_content: if existing_content:
new_content += existing_content # Check if version already exists
version_pattern = f"## {version}\n"
if version_pattern in existing_content:
# Replace existing version content
pattern = f"{version_pattern}.*?(?=## |$)"
new_content = re.sub(
pattern, new_version_entry, existing_content, flags=re.DOTALL
)
else:
# Add new version at the top
new_content = new_version_entry + existing_content
else:
# Create new changelog
new_content = f"# Changelog\n\n{new_version_entry}"
# 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(new_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.
Args: Args:
text: Text containing changes text: Text containing changes
Returns: Returns:
List of changes with type and description List of changes with type and description
""" """
changes = [] changes = []
current_type = None current_type = None
current_description = [] current_description = []
for line in text.splitlines(): for line in text.splitlines():
line = line.strip() line = line.strip()
if not line: if not line:
continue continue
# Check if line starts with a type marker # Check if line starts with a type marker
type_markers = { type_markers = {
"✨": "Added", "✨": "Added",
@@ -132,32 +169,33 @@ class ChangelogGenerator(AIService):
"📝": "Documentation", "📝": "Documentation",
"🔧": "Maintenance", "🔧": "Maintenance",
"🗑️": "Removed", "🗑️": "Removed",
"🔒": "Security" "🔒": "Security",
} }
for marker, type_ in type_markers.items(): for marker, type_ in type_markers.items():
if line.startswith(marker): if line.startswith(marker):
# Save previous change if exists # Save previous change if exists
if current_type and current_description: if current_type and current_description:
changes.append({ changes.append(
"type": current_type, {
"description": " ".join(current_description) "type": current_type,
}) "description": " ".join(current_description),
}
)
# Start new change # Start new change
current_type = type_ current_type = type_
current_description = [line[len(marker):].strip()] current_description = [line[len(marker) :].strip()]
break break
else: else:
# Continue current change # Continue current change
if current_type: if current_type:
current_description.append(line) current_description.append(line)
# Add last change if exists # Add last change if exists
if current_type and current_description: if current_type and current_description:
changes.append({ changes.append(
"type": current_type, {"type": current_type, "description": " ".join(current_description)}
"description": " ".join(current_description) )
})
return changes
return changes
+25 -16
View File
@@ -20,12 +20,12 @@ class CommitGenerator(AIService):
"""Generate a commit message from a diff.""" """Generate a commit message from a diff."""
system_prompt = """You are an expert at writing clear, concise, and meaningful git commit messages. Follow these rules strictly: system_prompt = """You are an expert at writing clear, concise, and meaningful git commit messages. Follow these rules strictly:
1. Format: type(scope): description 2. Format: type(scope): description
- type: One of feat, fix, docs, style, refactor, test, chore - type: One of feat, fix, docs, style, refactor, test, chore
- scope: Optional, describes the section of the codebase affected - scope: Optional, describes the section of the codebase affected
- description: Clear, concise explanation of the change - description: Clear, concise explanation of the change
2. Types and their emojis: 3. Types and their emojis:
- feat: ✨ (new feature or enhancement) - feat: ✨ (new feature or enhancement)
- fix: 🐛 (bug fix) - fix: 🐛 (bug fix)
- docs: 📚 (documentation changes) - docs: 📚 (documentation changes)
@@ -34,17 +34,19 @@ class CommitGenerator(AIService):
- test: ✅ (adding or modifying tests) - test: ✅ (adding or modifying tests)
- chore: 🔧 (maintenance tasks, dependencies, etc) - chore: 🔧 (maintenance tasks, dependencies, etc)
3. Guidelines: 4. Guidelines:
- Keep the description under 72 characters - Keep the description under 73 characters
- Use imperative mood ("add" not "added") - Use imperative mood ("add" not "added")
- Don't end with a period - Don't end with a period
- Focus on the "why" not the "what" - Focus on the "why" not the "what"
- Be specific about the change - Be specific about the change
- Use present tense - Use present tense
- Start with a verb - Start with a verb
- Don't include pull request references in the main message
- Keep the message clean and focused on the change itself
4. Examples: 5. Examples:
- ✨ feat(auth): add OAuth2 authentication - ✨ feat(auth): add OAuth3 authentication
- 🐛 fix(api): handle null response from server - 🐛 fix(api): handle null response from server
- 📚 docs(readme): update installation instructions - 📚 docs(readme): update installation instructions
- 💅 style(ui): format button component - 💅 style(ui): format button component
@@ -52,25 +54,32 @@ class CommitGenerator(AIService):
- ✅ test(api): add unit tests for user endpoints - ✅ test(api): add unit tests for user endpoints
- 🔧 chore(deps): update dependencies - 🔧 chore(deps): update dependencies
Analyze the changes carefully and write a commit message that accurately reflects the modifications.""" Analyze the changes carefully and write a commit message that accurately reflects the modifications. Focus on the actual changes, not on pull request numbers or references."""
user_prompt = f"""Analyze these changes and write a conventional commit message: user_prompt = f"""Analyze these changes and write a conventional commit message:
{diff} {diff}
Focus on: Focus on:
1. What type of change this is (feat, fix, etc.) 2. What type of change this is (feat, fix, etc.)
2. What part of the codebase is affected (scope) 3. What part of the codebase is affected (scope)
3. What the change accomplishes (description) 4. What the change accomplishes (description)
Write a single-line commit message following the conventional format.""" Write a single-line commit message following the conventional format. Do not include pull request references in the main message."""
message = self.generate_completion( message = self.generate_completion(
system_prompt, user_prompt, temperature=temperature system_prompt, user_prompt, temperature=temperature
) )
# Clean up the response to ensure it's a valid commit message # Clean up the response to ensure it's a valid commit message
message = message.split("\n")[0].strip() message = message.split("\n")[1].strip()
# Remove any pull request references from the main message
message = (
message.split("(")[1].strip()
if "(" in message and "#" in message
else message
)
# Ensure the message starts with an emoji # Ensure the message starts with an emoji
if not any( if not any(
@@ -102,10 +111,10 @@ Write a single-line commit message following the conventional format."""
"""Generate a changelog from a list of commits.""" """Generate a changelog from a list of commits."""
system_prompt = """You are a helpful AI that generates changelogs. system_prompt = """You are a helpful AI that generates changelogs.
Follow these rules: Follow these rules:
1. Group changes by type (Added, Changed, Fixed, etc.) 2. Group changes by type (Added, Changed, Fixed, etc.)
2. Keep descriptions concise but informative 3. Keep descriptions concise but informative
3. Use past tense 4. Use past tense
4. Start each entry with a verb""" 5. Start each entry with a verb"""
user_prompt = ( user_prompt = (
f"Generate a changelog for version {version} with these commits:\n\n" f"Generate a changelog for version {version} with these commits:\n\n"