Skip to content

Commit 998b450

Browse files
authored
Merge pull request #334 from contentstack/fix/snyk
Adding a Pre-Commit Hook
2 parents 5c9606d + 548a0a9 commit 998b450

39 files changed

+6257
-3455
lines changed

.babelrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: ['@babel/preset-env']
3+
};

.eslintrc.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
module.exports = {
2+
env: {
3+
"es2020": true,
4+
"node": true,
5+
"browser": true,
6+
"jest": true
7+
},
8+
extends: 'standard',
9+
// "globals": {
10+
// "Atomics": "readonly",
11+
// "SharedArrayBuffer": "readonly"
12+
// },
13+
// "parserOptions": {
14+
// "ecmaFeatures": {
15+
// "jsx": true
16+
// },
17+
// "ecmaVersion": 2015,
18+
// "sourceType": "module"
19+
// },
20+
parser: "@babel/eslint-parser", // Use Babel parser to handle modern JS syntax
21+
plugins: [
22+
'standard',
23+
'promise'
24+
],
25+
rules: {
26+
'semi': ['error', 'always'],
27+
'semi-spacing': ['error', { before: false, after: true }],
28+
'camelcase': 'off',
29+
'no-tabs': 'off',
30+
'eqeqeq': 'off',
31+
'no-unused-vars': 'warn',
32+
'no-undef': 'warn',
33+
'no-prototype-builtins': 'off',
34+
'no-extend-native': 'off',
35+
'no-fallthrough': 'off',
36+
'prefer-promise-reject-errors': 'off',
37+
'prefer-regex-literals': 'off',
38+
'no-useless-escape': 'off',
39+
'n/handle-callback-err': 'off',
40+
'n/no-callback-literal': 'off',
41+
'no-async-promise-executor': 'off'
42+
}
43+
};

.github/workflows/link-check.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Lint Check on PR
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened]
6+
7+
jobs:
8+
lint:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- name: Checkout the repository
13+
uses: actions/checkout@v4
14+
15+
- name: Set up Node.js
16+
uses: actions/setup-node@v2
17+
with:
18+
node-version: '22.x'
19+
registry-url: 'https://registry.npmjs.org'
20+
21+
- name: Install dependencies
22+
run: npm install
23+
24+
- name: Run ESLint
25+
run: npm run lint

.husky/pre-commit

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/usr/bin/env sh
2+
# Pre-commit hook to run lint, Snyk and Talisman scans, completing all before deciding to commit
3+
4+
# Function to check if a command exists
5+
command_exists() {
6+
command -v "$1" >/dev/null 2>&1
7+
}
8+
9+
# Allow bypassing the hook with an environment variable
10+
if [ "$SKIP_HOOK" = "1" ]; then
11+
echo "Skipping lint, Snyk and Talisman scans (SKIP_HOOK=1)."
12+
exit 0
13+
fi
14+
15+
# Run ESLint check first
16+
echo "Running ESLint check..."
17+
npm run lint
18+
lint_exit_code=$?
19+
20+
if [ $lint_exit_code -ne 0 ]; then
21+
echo "ESLint check failed. Please fix the linting issues and try again."
22+
echo "You can run 'npm run format' to auto-fix most issues."
23+
exit 1
24+
fi
25+
26+
echo "ESLint check passed."
27+
28+
# Check if Snyk is installed
29+
if ! command_exists snyk; then
30+
echo "Error: Snyk is not installed. Please install it and try again."
31+
exit 1
32+
fi
33+
34+
# Check if Talisman is installed
35+
if ! command_exists talisman; then
36+
echo "Error: Talisman is not installed. Please install it and try again."
37+
exit 1
38+
fi
39+
40+
# Initialize variables to track scan results
41+
snyk_failed=false
42+
talisman_failed=false
43+
44+
# Run Snyk vulnerability scan
45+
echo "Running Snyk vulnerability scan..."
46+
snyk test --all-projects > snyk_output.log 2>&1
47+
snyk_exit_code=$?
48+
49+
if [ $snyk_exit_code -eq 0 ]; then
50+
echo "Snyk scan passed: No vulnerabilities found."
51+
elif [ $snyk_exit_code -eq 1 ]; then
52+
echo "Snyk found vulnerabilities. See snyk_output.log for details."
53+
snyk_failed=true
54+
else
55+
echo "Snyk scan failed with error (exit code $snyk_exit_code). See snyk_output.log for details."
56+
snyk_failed=true
57+
fi
58+
59+
# Run Talisman secret scan (continues even if Snyk failed)
60+
echo "Running Talisman secret scan..."
61+
talisman --githook pre-commit > talisman_output.log 2>&1
62+
talisman_exit_code=$?
63+
64+
if [ $talisman_exit_code -eq 0 ]; then
65+
echo "Talisman scan passed: No secrets found."
66+
else
67+
echo "Talisman scan failed (exit code $talisman_exit_code). See talisman_output.log for details."
68+
talisman_failed=true
69+
fi
70+
71+
# Evaluate results after both scans
72+
if [ "$snyk_failed" = true ] || [ "$talisman_failed" = true ]; then
73+
echo "Commit aborted due to issues found in one or both scans."
74+
[ "$snyk_failed" = true ] && echo "- Snyk issues: Check snyk_output.log"
75+
[ "$talisman_failed" = true ] && echo "- Talisman issues: Check talisman_output.log"
76+
exit 1
77+
fi
78+
79+
# If all checks pass, allow the commit
80+
echo "All checks passed (ESLint, Snyk, Talisman). Proceeding with commit."
81+
rm -f snyk_output.log talisman_output.log
82+
exit 0

0 commit comments

Comments
 (0)