feat: Trivy Pre-Commit Hook with Security Scanning#4
Merged
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR introduces a comprehensive pre-commit hook for Trivy security scanning with extensive configuration options, automated vulnerability detection, and high-quality test coverage. The implementation provides a production-ready tool for integrating security scanning into development workflows.
Key Changes
- Implemented Python-based Trivy pre-commit hook with configurable severity levels, output formats, and scanner types
- Added comprehensive test suite achieving 94% code coverage with 28 tests
- Configured CI/CD pipeline for automated testing and code quality checks across Python 3.9-3.12
Reviewed Changes
Copilot reviewed 12 out of 14 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| pre_commit_hooks/trivy_scan.py | Core hook implementation with argument parsing and Trivy scan execution |
| pre_commit_hooks/init.py | Package initialization with version metadata |
| tests/test_trivy_scan.py | Comprehensive test suite covering all hook functionality |
| tests/conftest.py | Pytest fixtures for mocking Trivy installation and subprocess calls |
| tests/init.py | Test package initialization |
| .pre-commit-hooks.yaml | Pre-commit framework integration configuration |
| .github/workflows/ci.yml | GitHub Actions workflow for CI/CD automation |
| pyproject.toml | Project metadata, dependencies, and tool configurations |
| .pylintrc | Pylint code quality configuration |
| trivy.yaml.example | Example Trivy configuration file |
| .trivyignore.example | Example vulnerability ignore file |
| README.md | Complete documentation with usage examples and development guide |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
…mmit' The pre-commit framework deprecated the 'commit' stage name in version 3.2.0, replacing it with 'pre-commit' to better align with actual git hook names. This change eliminates the deprecation warning and ensures compatibility with future versions of the pre-commit framework. No functional changes - the hook runs at the exact same point in the git workflow.
…tree Add optional --dependency-tree argument that allows users to visualize the dependency tree showing how vulnerabilities are introduced through the dependency chain. This flag is disabled by default and can be enabled when users need more detailed context about vulnerability sources. Changes: - Add --dependency-tree argument to parse_arguments() in trivy_scan.py - Pass --dependency-tree flag to Trivy command when enabled - Add comprehensive tests for the new flag - Update README.md with configuration documentation and usage example - All tests pass (28/28) with 94% code coverage
Signed-off-by: Mehdi Bechiri <cebidhem@pm.me>
47fa432 to
0305052
Compare
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.
Summary
This PR introduces a comprehensive pre-commit hook that integrates Trivy security scanning to automatically detect vulnerabilities before commits are finalized. The implementation includes a Python-based hook with extensive configuration options, comprehensive test coverage, and CI/CD automation.
Key Features
🔒 Core Functionality
--dependency-treeflag to show vulnerability chains🛠️ Configuration Options
All options are customizable via command-line arguments:
--severity: Severity filtering (default: HIGH,CRITICAL)--format: Output format (default: table)--scanners: Scanner types (default: vuln)--dependency-tree: Show dependency tree with vulnerabilities--skip-db-update: Skip database updates for faster scans--timeout: Scan timeout configuration--ignore-unfixed: Ignore unfixed vulnerabilities--trivyignore: Custom ignore file support--config: Custom Trivy configuration file✅ Quality Assurance
📦 Implementation Details
Files Added:
pre_commit_hooks/trivy_scan.py- Main hook implementation (212 lines)tests/test_trivy_scan.py- Comprehensive test suite (283 lines)tests/conftest.py- Pytest fixtures and configuration.pre-commit-hooks.yaml- Pre-commit framework integration.github/workflows/ci.yml- CI/CD automationpyproject.toml- Project metadata and tool configurationuv.lock- Locked dependencies for reproducibility.pylintrc- Linting configuration.trivyignore.example- Example vulnerability ignore filetrivy.yaml.example- Example Trivy configurationREADME.md- Comprehensive documentationRecent Fixes:
committopre-commit(fixes compatibility warning).gitignoreentries for cache and IDE filesUsage
As a Pre-commit Hook
Add to
.pre-commit-config.yaml:Standalone
Testing
All validation commands pass successfully:
CI/CD
GitHub Actions workflow runs on every push and PR:
Breaking Changes
None - this is the initial release.
Dependencies
Documentation
Complete documentation includes:
Commits Included
Related Issues
Closes #1 - Implement Trivy pre-commit hook
Closes #3 - Fix deprecated pre-commit stage name
Ready for Review: All tests passing, documentation complete, CI/CD configured.