Skip to content

Improve error handling in git-ai-commit and add comprehensive tests#579

Merged
knoepfel merged 3 commits into
mainfrom
maintenance/fix-codeql-git-ai-commit
May 12, 2026
Merged

Improve error handling in git-ai-commit and add comprehensive tests#579
knoepfel merged 3 commits into
mainfrom
maintenance/fix-codeql-git-ai-commit

Conversation

@greenc-FNAL
Copy link
Copy Markdown
Contributor

@greenc-FNAL greenc-FNAL commented May 12, 2026

Primary goal: resolve 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.

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.
@greenc-FNAL
Copy link
Copy Markdown
Contributor Author

greenc-FNAL commented May 12, 2026

✅ 1 CodeQL alert resolved compared to main

  • Warning # 177 py/empty-except at scripts/git-ai-commit:214:5 — 'except' clause does nothing but pass and there is no explanatory comment.

Review the full CodeQL report for details.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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: pass patterns with explicit return/continue behavior in instruction/context gathering.
  • Refactor _kilo_auth_token, _gh_cli_token, and _edit to 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.

Comment thread scripts/test/test_git_ai_commit.py Outdated
Comment thread scripts/git-ai-commit Outdated
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).
@knoepfel knoepfel merged commit 66a32e3 into main May 12, 2026
36 checks passed
@knoepfel knoepfel deleted the maintenance/fix-codeql-git-ai-commit branch May 12, 2026 20:48
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.

3 participants