Skip to content

Implement AI commit message core functionality plus improved error handling and config management#10

Open
ilgaur wants to merge 25 commits intotechbend:mainfrom
ilgaur:config-management-improvements-and-refactors
Open

Implement AI commit message core functionality plus improved error handling and config management#10
ilgaur wants to merge 25 commits intotechbend:mainfrom
ilgaur:config-management-improvements-and-refactors

Conversation

@ilgaur
Copy link
Copy Markdown

@ilgaur ilgaur commented Jun 5, 2025

This PR implements the core functionality for ai-commiter with enhanced reliability and maintainability. after closing a bloated PR I made before for the initial development, I have created this one that sums up all the development and minor changes and refactors as they have been discussed.

Core stuff that I've done:

  • Pre-commit integration: Uses pre-commit framework for hook orchestration
  • AI-powered commit messages: Integrates with OpenRouter API
  • Staged diff processing: Captures and processes git staged changes via subprocess
  • Semantic commit format: Enforces conventional commit format type(scope): description
  • Flexible configuration: Environment-based config with sensible defaults

Technical Improvements and refactors that have been done after comments in #4, for some of the comments, I have created their respective issues and sub-issues, which are #3 and #8 :

  • Better error handling: Proper exception raising
  • Modular config management: Separated environment variable retrieval from config creation
  • Enhanced reliability: Better error handling throughout the commit generation flow
  • Better maintainability: Improved code structure and documentation

Closes some issues from #3 and #8

ilgaur added 24 commits May 12, 2025 23:29
…nc and include it in the function orchestration in the main
…api_config() inclusion in the generate_commit_msg() + minor syntax fix in the commit_messae validation in the main()
…te it with the appropriate env vars using os.environ.get + have some optional env vars and use defaults on them so users can override. Addresses: techbend#9
- Split get_api_config() into get_config_env_vars() and create_config_from_env_vars()
- Replace print statements with proper exception raising for better error propagation
- Add comprehensive try-catch blocks in main() function
- Pass config as parameter to generate_commit_msg() instead of creating it internally
- Improve error messages with more descriptive context
- Add comments for better code documentation

This refactoring makes the code more modular, testable, and provides better error handling throughout the commit message generation flow.
Comment thread pyproject.toml
description = "Create commit messages using AI"
readme = "README.md"
requires-python = ">=3.12"
dependencies = ["openai"]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need openai version

Comment thread ai_commiter/hook.py Outdated
Comment on lines +113 to +114
if __name__ == '__main__':
sys.exit(main())
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use main() directly?

Comment thread ai_commiter/hook.py Outdated
Comment on lines +90 to +111
def main():
#first arg is the script name and the second arg is the file which I'm validating to make sure it exists
if len(sys.argv) < 2:
return 1

try:
env_vars = get_config_env_vars()
config = create_config_from_env_vars(env_vars)

commit_msg_file = sys.argv[1]
diff_data = get_diff()
if not diff_data:
return 1

commit_message = generate_commit_msg(diff_data, config)
if commit_message is None:
return 1
result = write_commit_message(commit_msg_file, commit_message)
return result
except Exception as e:
print(f"Error: {e}")
return 1
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try not returning unnecessary variable or 1

Comment thread ai_commiter/hook.py Outdated
Comment on lines +92 to +93
if len(sys.argv) < 2:
return 1
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one idea makes it a function for more readability or use break and continue

Comment thread ai_commiter/hook.py
raise Exception("API Key is missing")
if not env_vars["base_url"]:
raise Exception("Base URL is missing")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please make config object and return that varible

Comment thread ai_commiter/hook.py
Comment on lines +24 to +34
def get_config_env_vars():
try:
return {
"api_key": os.environ.get("AI_COMMITER_API_KEY", ""),
"base_url": os.environ.get("AI_COMMITER_BASE_URL", ""),
"model": os.environ.get("AI_COMMITER_MODEL", DEFAULT_MODEL),
"system_prompt": os.environ.get("AI_COMMITER_SYSTEM_PROMPT", DEFAULT_SYSTEM_PROMPT),
"user_prompt": os.environ.get("AI_COMMITER_USER_PROMPT", DEFAULT_USER_PROMPT)
}
except Exception as e:
raise Exception(f"Error retrieving environment variables: {e}")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please clean this function to not have extra try

Comment thread ai_commiter/hook.py Outdated
def get_diff():
#returns diff data
result = subprocess.run(['git', 'diff', '--staged'], capture_output=True, text=True).stdout
return result
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use try here

  - Move DEFAULT_SYSTEM_PROMPT, DEFAULT_USER_PROMPT, and DEFAULT_MODEL to separate config module
  - Add proper exception handling with try-catch blocks in get_diff()
  - Change environment variable retrieval to return None instead of empty strings for missing values
  - Extract argument validation into separate validate_arguments() function
  - Simplify main() function by removing nested try-catch and using explicit error raising
  - Remove return value from write_commit_message() function
  - Update function comments and structure for better clarity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants