Skip to content

Commit 6cf3c2e

Browse files
authored
Snyk Code / Test / IAC scan will pass if valid files are not found. (#44)
<!-- markdownlint-disable-file MD041 --> ## Pull request checklist Please check if your PR fulfills the following requirements: - [x] I have read the [CONTRIBUTING](https://github.com/fabasoad/pre-commit-snyk/blob/main/CONTRIBUTING.md) doc. - [ ] Tests for the changes have been added (for bug fixes / features). - [ ] Docs have been reviewed and added / updated if needed (for bug fixes / features). ## Pull request type <!-- Please do not submit updates to dependencies unless it fixes an issue. --> <!-- Please try to limit your pull request to one type, submit multiple pull requests if needed. --> Please check the type of change your PR introduces: - [ ] Bugfix - [ ] Feature - [ ] Code style update (formatting, renaming) - [ ] Refactoring (no functional changes, no api changes) - [ ] Build related changes - [ ] Documentation content changes - [x] Other (please describe): Modified the IAC, Code & Test hooks to pass if a valid file is not found. Hooks to capture the exit codes and pass if either exit code 2 or 3 is given, echoing the error. **Snyk CLI Exit codes** Possible exit codes and their meaning: 0: success (scan completed), no vulnerabilities found 1: action_needed (scan completed), vulnerabilities found 2: failure, try to re-run the command. Use -d to output the debug logs. 3: failure, no supported projects detected ## What is the current behavior <!-- Please describe the current behavior that you are modifying, or link to a relevant issue. --> Currently, if a valid file is not found then the test fails which stops the commit. Ideally, this should pass as no vulnerability has been detected, the valid file is simply not present. Currently the only way around this is to remove the relevant test from the .pre-commit-config.yaml to pass. ## What is the new behavior <!-- Please describe the behavior or changes that are being added by this PR. --> - Code / Test / IAC tests pass if valid file is not found. - Hooks to capture the exit code and pass if either exit code 2 or 3 is given, echoing the error. - Commit no longer fails if valid file is not present. ## Does this introduce a breaking change - [ ] Yes - [x] No <!-- If this introduces a breaking change, please describe the impact and migration path for existing applications below. --> ## Other information <!-- Any other information that is important to this PR such as screenshots of how the component looks before and after the change. --> <!-- This document was adapted from the open-source [appium/appium](https://github.com/appium/appium/blob/master/.github/PULL_REQUEST_TEMPLATE.md) repository. --> --- Closes #{IssueNumber}
1 parent 4491005 commit 6cf3c2e

5 files changed

Lines changed: 52 additions & 4 deletions

File tree

.pre-commit-hooks.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,28 @@
1414
entry: hooks/snyk-iac.sh
1515
language: script
1616
pass_filenames: false
17+
verbose: true
1718

1819
- id: snyk-test
1920
name: Snyk Test
2021
description: Runs 'snyk test' command
2122
entry: hooks/snyk-test.sh
2223
language: script
2324
pass_filenames: false
25+
verbose: true
2426

2527
- id: snyk-code
2628
name: Snyk Code
2729
description: Runs 'snyk code test' command
2830
entry: hooks/snyk-code.sh
2931
language: script
3032
pass_filenames: false
33+
verbose: true
3134

3235
- id: snyk-log4shell
3336
name: Snyk log4shell
3437
description: Runs 'snyk log4shell' command
3538
entry: hooks/snyk-log4shell.sh
3639
language: script
3740
pass_filenames: false
41+
verbose: true

hooks/snyk-code.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
#!/usr/bin/env sh
2-
set -eu
2+
set -u
33

44
SCRIPT_PATH=$(realpath "$0")
55
HOOKS_FOLDER_PATH=$(dirname "${SCRIPT_PATH}")
66
INSTALLATION_FOLDER_PATH="${HOOKS_FOLDER_PATH}/installation"
77

88
sh "${INSTALLATION_FOLDER_PATH}/main.sh"
99

10+
# Capture exit code of Snyk Test hook
11+
set +e
1012
snyk code test "$@"
13+
snyk_exit_code=$?
14+
set -e
15+
16+
# Check if the exit code is 3
17+
if [ "$snyk_exit_code" = 3 ]; then
18+
exit 0
19+
fi
20+
21+
exit "$snyk_exit_code"

hooks/snyk-iac.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
#!/usr/bin/env sh
2-
set -eu
2+
set -u
33

44
SCRIPT_PATH=$(realpath "$0")
55
HOOKS_FOLDER_PATH=$(dirname "${SCRIPT_PATH}")
66
INSTALLATION_FOLDER_PATH="${HOOKS_FOLDER_PATH}/installation"
77

88
sh "${INSTALLATION_FOLDER_PATH}/main.sh"
99

10+
# Capture exit code of Snyk Test hook
11+
set +e
1012
snyk iac test "$@"
13+
snyk_exit_code=$?
14+
set -e
15+
16+
# Check if the exit code is 3
17+
if [ "$snyk_exit_code" = 3 ]; then
18+
exit 0
19+
fi
20+
21+
exit "$snyk_exit_code"

hooks/snyk-log4shell.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
#!/usr/bin/env sh
2-
set -eu
2+
set -u
33

44
SCRIPT_PATH=$(realpath "$0")
55
HOOKS_FOLDER_PATH=$(dirname "${SCRIPT_PATH}")
66
INSTALLATION_FOLDER_PATH="${HOOKS_FOLDER_PATH}/installation"
77

88
sh "${INSTALLATION_FOLDER_PATH}/main.sh"
99

10+
# Capture exit code of Snyk Test hook
11+
set +e
1012
snyk log4shell "$@"
13+
snyk_exit_code=$?
14+
set -e
15+
16+
# Check if the exit code is 3
17+
if [ "$snyk_exit_code" = 3 ]; then
18+
exit 0
19+
fi
20+
21+
exit "$snyk_exit_code"

hooks/snyk-test.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
#!/usr/bin/env sh
2-
set -eu
2+
set -u
33

44
SCRIPT_PATH=$(realpath "$0")
55
HOOKS_FOLDER_PATH=$(dirname "${SCRIPT_PATH}")
66
INSTALLATION_FOLDER_PATH="${HOOKS_FOLDER_PATH}/installation"
77

88
sh "${INSTALLATION_FOLDER_PATH}/main.sh"
99

10+
# Capture exit code of Snyk Test hook
11+
set +e
1012
snyk test "$@"
13+
snyk_exit_code=$?
14+
set -e
15+
16+
# Check if the exit code is 3
17+
if [ "$snyk_exit_code" = 3 ]; then
18+
exit 0
19+
fi
20+
21+
exit "$snyk_exit_code"

0 commit comments

Comments
 (0)