Skip to content

Commit

Permalink
basic changes for sonarqube hook
Browse files Browse the repository at this point in the history
  • Loading branch information
raj-kapil committed Oct 15, 2024
1 parent 952bf90 commit 0307ffd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- id: sonarqube-precommit-hook
name: SonarQube Pre-commit Hook
description: Run SonarQube scan before committing.
entry: ./run-sonarqube-scan.sh
language: script
files: \.java$
24 changes: 24 additions & 0 deletions run-sonarqube-scan.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh

# Use environment variables if available, otherwise fallback to defaults
SONAR_PROJECT_KEY=${SONAR_PROJECT_KEY:-"default-project-key"}
SONAR_ORGANIZATION=${SONAR_ORGANIZATION:-"default-organization"}
SONAR_HOST_URL=${SONAR_HOST_URL:-"https://sonarcloud.io"}
SONAR_LOGIN=${SONAR_LOGIN:-"<your-sonarcloud-token>"}

# Run SonarQube scan
echo "Running SonarQube scan before commit..."

mvn clean verify sonar:sonar \
-Dsonar.projectKey=$SONAR_PROJECT_KEY \
-Dsonar.organization=$SONAR_ORGANIZATION \
-Dsonar.host.url=$SONAR_HOST_URL \
-Dsonar.login=$SONAR_LOGIN

# Exit with non-zero status if scan fails
if [ $? -ne 0 ]; then
echo "SonarQube scan failed. Aborting commit."
exit 1
else
echo "SonarQube scan passed. Proceeding with commit."
fi

0 comments on commit 0307ffd

Please sign in to comment.