♻️ refactor(generator): update commit message and changelog generation using AI

This commit is contained in:
sankalp committed 2025-06-04 13:22:27 +05:45
1 parent 13d0353fa9
commit 6f04e5a4b6
1 file changed
+89 -51
+89 -51
View File
@@ -1,67 +1,104 @@
""" """
Commit message and changelog generation using AI. Commit message and 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
class CommitGenerator(AIService): class CommitGenerator(AIService):
"""AI-powered commit message and changelog generator.""" """AI-powered commit message and changelog generator."""
def __init__(self, config: Config): def __init__(self, config: Config):
"""Initialize commit generator.""" """Initialize commit generator."""
super().__init__(config) super().__init__(config)
def generate_commit_message(self, diff: str, temperature: Optional[float] = None) -> str: def generate_commit_message(
self, diff: str, temperature: Optional[float] = None
) -> str:
"""Generate a commit message from a diff.""" """Generate a commit message from a diff."""
system_prompt = """You are a helpful AI that generates git commit messages. Keep responses concise and follow the conventional commit format. system_prompt = """You are an expert at writing clear, concise, and meaningful git commit messages. Follow these rules strictly:
Format: type(scope): description 1. Format: type(scope): description
Types and their emojis: - type: One of feat, fix, docs, style, refactor, test, chore
- feat: ✨ (new feature) - scope: Optional, describes the section of the codebase affected
- fix: 🐛 (bug fix) - description: Clear, concise explanation of the change
- docs: 📚 (documentation)
- style: 💅 (formatting, missing semi colons, etc) 2. Types and their emojis:
- refactor: ♻️ (refactoring code) - feat: ✨ (new feature or enhancement)
- test: ✅ (adding tests) - fix: 🐛 (bug fix)
- chore: 🔧 (maintenance) - docs: 📚 (documentation changes)
- style: 💅 (formatting, missing semi colons, etc; no code change)
The message should be a single line and follow the conventional commit format strictly. Include the appropriate emoji at the start of the message.""" - refactor: ♻️ (code restructuring, no functional changes)
- test: ✅ (adding or modifying tests)
user_prompt = f"Write a conventional commit message for these changes:\n\n{diff}" - chore: 🔧 (maintenance tasks, dependencies, etc)
3. Guidelines:
- Keep the description under 72 characters
- Use imperative mood ("add" not "added")
- Don't end with a period
- Focus on the "why" not the "what"
- Be specific about the change
- Use present tense
- Start with a verb
4. Examples:
- ✨ feat(auth): add OAuth2 authentication
- 🐛 fix(api): handle null response from server
- 📚 docs(readme): update installation instructions
- 💅 style(ui): format button component
- ♻️ refactor(db): optimize database queries
- ✅ test(api): add unit tests for user endpoints
- 🔧 chore(deps): update dependencies
Analyze the changes carefully and write a commit message that accurately reflects the modifications."""
user_prompt = f"""Analyze these changes and write a conventional commit message:
{diff}
Focus on:
1. What type of change this is (feat, fix, etc.)
2. What part of the codebase is affected (scope)
3. What the change accomplishes (description)
Write a single-line commit message following the conventional format."""
message = self.generate_completion( message = self.generate_completion(
system_prompt, system_prompt, user_prompt, temperature=temperature
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() # Take only the first line message = message.split("\n")[0].strip()
# Ensure the message starts with an emoji # Ensure the message starts with an emoji
if not any(emoji in message for emoji in ['✨', '🐛', '📚', '💅', '♻️', '✅', '🔧']): if not any(
emoji in message for emoji in ["✨", "🐛", "📚", "💅", "♻️", "✅", "🔧"]
):
# Add appropriate emoji based on the commit type # Add appropriate emoji based on the commit type
if message.startswith('feat'): if message.startswith("feat"):
message = '✨ ' + message message = "✨ " + message
elif message.startswith('fix'): elif message.startswith("fix"):
message = '🐛 ' + message message = "🐛 " + message
elif message.startswith('docs'): elif message.startswith("docs"):
message = '📚 ' + message message = "📚 " + message
elif message.startswith('style'): elif message.startswith("style"):
message = '💅 ' + message message = "💅 " + message
elif message.startswith('refactor'): elif message.startswith("refactor"):
message = '♻️ ' + message message = "♻️ " + message
elif message.startswith('test'): elif message.startswith("test"):
message = '✅ ' + message message = "✅ " + message
elif message.startswith('chore'): elif message.startswith("chore"):
message = '🔧 ' + message message = "🔧 " + message
else: else:
message = '🔧 ' + message # Default to chore message = "🔧 " + message # Default to chore
return message return message
def generate_changelog(self, commits: List[str], version: str, temperature: Optional[float] = None) -> str: def generate_changelog(
self, commits: List[str], version: str, temperature: Optional[float] = None
) -> str:
"""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:
@@ -69,11 +106,12 @@ class CommitGenerator(AIService):
2. Keep descriptions concise but informative 2. Keep descriptions concise but informative
3. Use past tense 3. Use past tense
4. Start each entry with a verb""" 4. Start each entry with a verb"""
user_prompt = f"Generate a changelog for version {version} with these commits:\n\n" + "\n".join(commits) user_prompt = (
f"Generate a changelog for version {version} with these commits:\n\n"
+ "\n".join(commits)
)
return self.generate_completion( return self.generate_completion(
system_prompt, system_prompt, user_prompt, temperature=temperature
user_prompt, )
temperature=temperature
)