Implement AI commit message core functionality plus improved error handling and config management#10
Open
ilgaur wants to merge 25 commits intotechbend:mainfrom
Open
Conversation
…messages to the generate_commit_msg()
…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()
…mmit message generation
…r empty diff data
…ml to the gitignore
…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.
tavallaie
requested changes
Jun 8, 2025
| description = "Create commit messages using AI" | ||
| readme = "README.md" | ||
| requires-python = ">=3.12" | ||
| dependencies = ["openai"] |
Comment on lines
+113
to
+114
| if __name__ == '__main__': | ||
| sys.exit(main()) |
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 |
Member
There was a problem hiding this comment.
try not returning unnecessary variable or 1
Comment on lines
+92
to
+93
| if len(sys.argv) < 2: | ||
| return 1 |
Member
There was a problem hiding this comment.
one idea makes it a function for more readability or use break and continue
| raise Exception("API Key is missing") | ||
| if not env_vars["base_url"]: | ||
| raise Exception("Base URL is missing") | ||
|
|
Member
There was a problem hiding this comment.
please make config object and return that varible
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}") |
Member
There was a problem hiding this comment.
please clean this function to not have extra try
| def get_diff(): | ||
| #returns diff data | ||
| result = subprocess.run(['git', 'diff', '--staged'], capture_output=True, text=True).stdout | ||
| return result |
- 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
type(scope): descriptionTechnical 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 :
Closes some issues from #3 and #8