Improve error handling in git-ai-commit and add comprehensive tests#579
Open
greenc-FNAL wants to merge 3 commits into
Open
Improve error handling in git-ai-commit and add comprehensive tests#579greenc-FNAL wants to merge 3 commits into
greenc-FNAL wants to merge 3 commits into
Conversation
Primary goal: `solve CodeQL `py/empty-except` violations. Additionally: refactor three functions to handle edge cases more robustly: - **_kilo_auth_token**: Replace lenient `.get()` chaining with explicit `isinstance()` checks at each level (dict, provider, key string). Validates JSON structure before attempting field access, preventing silent failures on malformed credentials files. - **_gh_cli_token**: Add explicit `return None` for non-zero exit codes and missing `gh` binary (FileNotFoundError). Previously relied on implicit falsy stdout check; now surfaces all failure modes uniformly. - **_edit**: Wrap subprocess call in try-except to catch FileNotFoundError (missing editor) and CalledProcessError (non-zero exit). Raise _Error with descriptive messages instead of letting exceptions propagate. Ensures temp file cleanup via finally block even on error. Finally, add comprehensive test suite (`scripts/test/test_git_ai_commit.py`) covering all three functions with 28 test cases: malformed JSON, missing files, type mismatches, subprocess failures, and happy paths. Tests use importlib to load the script as a module and pytest fixtures for isolation.
Contributor
Author
✅ 1 CodeQL alert resolved compared to main
Review the full CodeQL report for details. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses CodeQL py/empty-except findings in scripts/git-ai-commit by making exception handling explicit, while also hardening token discovery and editor invocation paths. It adds a new pytest suite to cover the updated behavior of the key helper functions.
Changes:
- Replace silent
except: passpatterns with explicitreturn/continuebehavior in instruction/context gathering. - Refactor
_kilo_auth_token,_gh_cli_token, and_editto validate structures and surface subprocess failures as_Error. - Add a new pytest module (
scripts/test/test_git_ai_commit.py) covering success and failure modes for the above helpers.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
scripts/git-ai-commit |
Makes error handling explicit in instruction loading; hardens token parsing; improves editor failure reporting and main’s handling of _edit() errors. |
scripts/test/test_git_ai_commit.py |
Adds pytest coverage for token resolution helpers and editor error/cleanup behavior. |
Add explicit UTF-8 encoding to file operations for consistency and clarity.
Simplify command construction in `main()` by using list concatenation instead
of filtering None values. Add pylint directive to allow hyphens in the script
name (required by git-subcommand convention).
In tests, introduce typed aliases for dynamically-loaded module members so
static analysis can reason about call sites. This eliminates `Unknown` type
inference and improves IDE support. Rename unused kwargs parameters to
`_kwargs` to satisfy linting rules. Update docstrings for consistency
("Ensure" prefix for test descriptions).
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.
Primary goal: resolve CodeQL
py/empty-exceptviolations.Additionally: refactor three functions to handle edge cases more robustly:
_kilo_auth_token: Replace lenient
.get()chaining with explicitisinstance()checks at each level (dict, provider, key string). ValidatesJSON structure before attempting field access, preventing silent failures on
malformed credentials files.
_gh_cli_token: Add explicit
return Nonefor non-zero exit codes andmissing
ghbinary (FileNotFoundError). Previously relied on implicitfalsy stdout check; now surfaces all failure modes uniformly.
_edit: Wrap subprocess call in try-except to catch FileNotFoundError
(missing editor) and CalledProcessError (non-zero exit). Raise _Error with
descriptive messages instead of letting exceptions propagate. Ensures temp
file cleanup via finally block even on error.
Finally, add comprehensive test suite
(
scripts/test/test_git_ai_commit.py) covering all three functions with28 test cases: malformed JSON, missing files, type mismatches,
subprocess failures, and happy paths. Tests use importlib to load the
script as a module and pytest fixtures for isolation.