secure-husky-setup 1.0.14 → 1.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/hooks.js +24 -16
- package/package.json +1 -1
package/lib/hooks.js
CHANGED
|
@@ -11,9 +11,7 @@ exports.setupPreCommitHook = async (gitRoot) => {
|
|
|
11
11
|
return;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
// relative path from gitRoot to project e.g. "server" or "."
|
|
15
14
|
const projectDir = path.relative(gitRoot, process.cwd()) || '.';
|
|
16
|
-
|
|
17
15
|
const hookContent = buildHookScript(projectDir);
|
|
18
16
|
|
|
19
17
|
if (await fs.pathExists(hookPath)) {
|
|
@@ -33,23 +31,32 @@ exports.setupPreCommitHook = async (gitRoot) => {
|
|
|
33
31
|
};
|
|
34
32
|
|
|
35
33
|
function buildHookScript(projectDir) {
|
|
36
|
-
// All paths
|
|
37
|
-
//
|
|
34
|
+
// All paths relative to git root — NO cd at top level
|
|
35
|
+
// sonar-scanner runs in a subshell cd'd into project dir so it finds sonar-project.properties
|
|
38
36
|
const gitleaksBin = projectDir !== '.'
|
|
39
37
|
? `./${projectDir}/.tools/gitleaks/gitleaks`
|
|
40
38
|
: `./.tools/gitleaks/gitleaks`;
|
|
41
39
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
// subshell cd for sonar so properties file is found correctly
|
|
41
|
+
const sonarSubshell = projectDir !== '.'
|
|
42
|
+
? `(cd "./${projectDir}" && ./node_modules/.bin/sonar-scanner -Dsonar.qualitygate.wait=true)`
|
|
43
|
+
: `(./node_modules/.bin/sonar-scanner -Dsonar.qualitygate.wait=true)`;
|
|
45
44
|
|
|
46
|
-
const
|
|
45
|
+
const sonarPropsCheck = projectDir !== '.'
|
|
47
46
|
? `./${projectDir}/sonar-project.properties`
|
|
48
47
|
: `./sonar-project.properties`;
|
|
49
48
|
|
|
49
|
+
const sonarBinCheck = projectDir !== '.'
|
|
50
|
+
? `./${projectDir}/node_modules/.bin/sonar-scanner`
|
|
51
|
+
: `./node_modules/.bin/sonar-scanner`;
|
|
52
|
+
|
|
53
|
+
const sonarHostGrep = projectDir !== '.'
|
|
54
|
+
? `grep "^sonar.host.url=" "./${projectDir}/sonar-project.properties"`
|
|
55
|
+
: `grep "^sonar.host.url=" "./sonar-project.properties"`;
|
|
56
|
+
|
|
50
57
|
return `#!/bin/sh
|
|
51
58
|
|
|
52
|
-
# Hook runs from git root — all paths
|
|
59
|
+
# Hook runs from git root — all paths relative to git root
|
|
53
60
|
# projectDir: ${projectDir}
|
|
54
61
|
|
|
55
62
|
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM)
|
|
@@ -105,16 +112,16 @@ fi
|
|
|
105
112
|
echo ""
|
|
106
113
|
echo "[SonarQube] Scanning changed files..."
|
|
107
114
|
|
|
108
|
-
|
|
109
|
-
|
|
115
|
+
SONAR_BIN_CHECK="${sonarBinCheck}"
|
|
116
|
+
SONAR_PROPS_CHECK="${sonarPropsCheck}"
|
|
110
117
|
|
|
111
|
-
if [ ! -f "$
|
|
118
|
+
if [ ! -f "$SONAR_BIN_CHECK" ]; then
|
|
112
119
|
echo "[SonarQube] sonar-scanner not found. Skipping."
|
|
113
120
|
else
|
|
114
|
-
if [ ! -f "$
|
|
121
|
+
if [ ! -f "$SONAR_PROPS_CHECK" ]; then
|
|
115
122
|
echo "[SonarQube] sonar-project.properties not found. Skipping."
|
|
116
123
|
else
|
|
117
|
-
SONAR_HOST=$(
|
|
124
|
+
SONAR_HOST=$(${sonarHostGrep} | cut -d'=' -f2 | tr -d '[:space:]')
|
|
118
125
|
SONAR_DOMAIN=$(echo "$SONAR_HOST" | sed 's|https://||' | sed 's|http://||' | cut -d'/' -f1 | cut -d':' -f1)
|
|
119
126
|
SONAR_PORT=$(echo "$SONAR_HOST" | grep -o ':[0-9]*$' | tr -d ':')
|
|
120
127
|
SONAR_PORT=\${SONAR_PORT:-9000}
|
|
@@ -125,10 +132,11 @@ else
|
|
|
125
132
|
SONAR_INCLUSIONS=$(echo "$STAGED_FILES" | tr '\\n' ',' | sed 's/,$//')
|
|
126
133
|
echo "[SonarQube] Scanning: $SONAR_INCLUSIONS"
|
|
127
134
|
|
|
128
|
-
|
|
135
|
+
# Run sonar-scanner in subshell from project dir so it picks up sonar-project.properties
|
|
136
|
+
${sonarSubshell} -Dsonar.inclusions="\$SONAR_INCLUSIONS"
|
|
129
137
|
SONAR_EXIT=$?
|
|
130
138
|
|
|
131
|
-
if [
|
|
139
|
+
if [ \$SONAR_EXIT -ne 0 ]; then
|
|
132
140
|
echo "[SonarQube] Quality Gate FAILED. Commit blocked."
|
|
133
141
|
exit 1
|
|
134
142
|
fi
|