Skip to content

Commit d348054

Browse files
authored
feat: removed warnings and add --dependency-tree option (#4)
* fix: update pre-commit stage name from deprecated 'commit' to 'pre-commit' 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. * feat: add --dependency-tree flag to display vulnerability dependency 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 * add to .gitignore Signed-off-by: Mehdi Bechiri <cebidhem@pm.me> --------- Signed-off-by: Mehdi Bechiri <cebidhem@pm.me>
1 parent 4370d90 commit d348054

5 files changed

Lines changed: 39 additions & 1 deletion

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,6 @@ cython_debug/
205205
marimo/_static/
206206
marimo/_lsp/
207207
__marimo__/
208+
209+
.channels_cache_v2.json
210+
.users_cache.json

.pre-commit-hooks.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
language: python
66
pass_filenames: false
77
always_run: true
8-
stages: [commit]
8+
stages: [pre-commit]

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ The hook supports extensive configuration through command-line arguments:
105105
| `--timeout` | - | Timeout for the scan (e.g., 5m0s) |
106106
| `--ignore-unfixed` | `false` | Ignore unfixed vulnerabilities |
107107
| `--trivyignore` | - | Path to .trivyignore file |
108+
| `--dependency-tree` | `false` | Show dependency tree with vulnerabilities |
108109

109110
### Examples
110111

@@ -144,6 +145,12 @@ The hook supports extensive configuration through command-line arguments:
144145
args: ['--trivyignore', '.trivyignore']
145146
```
146147
148+
**Show dependency tree:**
149+
```yaml
150+
- id: trivy-scan
151+
args: ['--dependency-tree']
152+
```
153+
147154
**Comprehensive configuration:**
148155
```yaml
149156
- id: trivy-scan

pre_commit_hooks/trivy_scan.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ def parse_arguments(argv: Optional[Sequence[str]] = None) -> argparse.Namespace:
9292
help="Path to .trivyignore file",
9393
)
9494

95+
parser.add_argument(
96+
"--dependency-tree",
97+
action="store_true",
98+
help="Show dependency tree with vulnerabilities",
99+
)
100+
95101
parser.add_argument(
96102
"trivy_args",
97103
nargs="*",
@@ -143,6 +149,9 @@ def run_trivy_scan(args: argparse.Namespace, scan_path: str = ".") -> int:
143149
if args.trivyignore:
144150
cmd.extend(["--ignorefile", args.trivyignore])
145151

152+
if args.dependency_tree:
153+
cmd.append("--dependency-tree")
154+
146155
# Add any additional arguments
147156
if args.trivy_args:
148157
cmd.extend(args.trivy_args)

tests/test_trivy_scan.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def test_parse_arguments_defaults(self):
3737
assert args.scanners == "vuln"
3838
assert args.skip_db_update is False
3939
assert args.ignore_unfixed is False
40+
assert args.dependency_tree is False
4041
assert args.config is None
4142
assert args.timeout is None
4243
assert args.trivyignore is None
@@ -87,6 +88,11 @@ def test_parse_arguments_with_trivyignore(self):
8788
args = parse_arguments(["--trivyignore", ".trivyignore"])
8889
assert args.trivyignore == ".trivyignore"
8990

91+
def test_parse_arguments_dependency_tree(self):
92+
"""Test parsing with dependency-tree flag."""
93+
args = parse_arguments(["--dependency-tree"])
94+
assert args.dependency_tree is True
95+
9096
def test_parse_arguments_with_additional_args(self):
9197
"""Test parsing with additional Trivy arguments."""
9298
args = parse_arguments(["--", "--debug", "--quiet"])
@@ -181,6 +187,19 @@ def test_run_trivy_scan_with_scanners(self, monkeypatch):
181187
scanners_index = call_args.index("--scanners")
182188
assert call_args[scanners_index + 1] == "vuln,misconfig"
183189

190+
def test_run_trivy_scan_with_dependency_tree(self, monkeypatch):
191+
"""Test Trivy scan with dependency-tree flag."""
192+
mock_run = MagicMock()
193+
mock_run.return_value.returncode = 0
194+
monkeypatch.setattr("subprocess.run", mock_run)
195+
196+
args = parse_arguments(["--dependency-tree"])
197+
run_trivy_scan(args)
198+
199+
# Verify that subprocess.run was called with dependency-tree flag
200+
call_args = mock_run.call_args[0][0]
201+
assert "--dependency-tree" in call_args
202+
184203
def test_run_trivy_scan_subprocess_error(self, mock_subprocess_error):
185204
"""Test Trivy scan with subprocess error."""
186205
args = parse_arguments([])

0 commit comments

Comments
 (0)