Files
2026-09-21 10:57:25 +01:00

198 lines
3.7 KiB
Bash
Executable File

#!/bin/bash
set -euo pipefail
SONAR_URL="http://192.168.1.2:9001"
PROJECT="Warehouse-Management-System"
# The directory scan.sh was invoked from is the project root.
PROJECT_DIR="$PWD"
OUTPUT="$PROJECT_DIR/sonarqube-issues.csv"
REPORT_TASK="$PROJECT_DIR/.scannerwork/report-task.txt"
TMP=$(mktemp)
trap 'rm -f "$TMP" "$TMP.new"' EXIT
if [ -z "${SONAR_TOKEN:-}" ]; then
echo "ERROR: SONAR_TOKEN is not set"
exit 1
fi
if [ ! -d "$PROJECT_DIR" ]; then
echo "ERROR: Project directory does not exist: $PROJECT_DIR"
exit 1
fi
# Delete previous report FIRST.
echo "Removing previous SonarQube report..."
rm -f "$OUTPUT"
echo
echo "Project directory: $PROJECT_DIR"
echo "Running SonarQube scan..."
cd "$PROJECT_DIR"
JAVA_HOME="/opt/homebrew/opt/openjdk" \
PATH="/opt/homebrew/opt/openjdk/bin:$PATH" \
sonar-scanner \
-Dsonar.projectKey="$PROJECT" \
-Dsonar.sources=. \
-Dsonar.host.url="$SONAR_URL" \
-Dsonar.token="$SONAR_TOKEN" \
-Dsonar.scm.disabled=true
#
# Wait for SonarQube server-side analysis to complete.
#
echo
echo "Scanner finished."
echo "Waiting for SonarQube analysis to complete..."
if [ ! -f "$REPORT_TASK" ]; then
echo "ERROR: SonarQube report task file not found:"
echo "$REPORT_TASK"
exit 1
fi
TASK_ID=$(grep '^ceTaskId=' "$REPORT_TASK" | cut -d= -f2)
if [ -z "$TASK_ID" ]; then
echo "ERROR: Could not determine SonarQube Compute Engine task ID."
exit 1
fi
echo "Compute Engine task: $TASK_ID"
while true; do
RESPONSE=$(curl \
--connect-timeout 5 \
--max-time 30 \
-sS \
-u "$SONAR_TOKEN:" \
"$SONAR_URL/api/ce/task?id=$TASK_ID")
STATUS=$(printf '%s' "$RESPONSE" | jq -r '.task.status')
case "$STATUS" in
SUCCESS)
echo "SonarQube analysis complete."
break
;;
PENDING)
echo " Analysis pending..."
;;
IN_PROGRESS)
echo " Analysis in progress..."
;;
FAILED)
echo "ERROR: SonarQube analysis failed."
printf '%s\n' "$RESPONSE" | jq
exit 1
;;
CANCELED)
echo "ERROR: SonarQube analysis was canceled."
exit 1
;;
*)
echo "ERROR: Unexpected SonarQube task status: $STATUS"
printf '%s\n' "$RESPONSE" | jq
exit 1
;;
esac
sleep 2
done
#
# Analysis is now complete.
# Download the issues.
#
echo
echo "Downloading SonarQube issues..."
echo '[]' > "$TMP"
PAGE=1
PAGE_SIZE=500
while true; do
echo "Fetching page $PAGE..."
RESPONSE=$(curl \
--connect-timeout 5 \
--max-time 60 \
-sS \
-u "$SONAR_TOKEN:" \
"$SONAR_URL/api/issues/search?componentKeys=$PROJECT&ps=$PAGE_SIZE&p=$PAGE")
COUNT=$(echo "$RESPONSE" | jq '.issues | length')
TOTAL=$(echo "$RESPONSE" | jq '.total')
echo " $COUNT issues received ($TOTAL total)"
jq -s '.[0] + .[1].issues' \
"$TMP" \
<(printf '%s' "$RESPONSE") > "$TMP.new"
mv "$TMP.new" "$TMP"
[ "$COUNT" -lt "$PAGE_SIZE" ] && break
PAGE=$((PAGE + 1))
done
#
# Generate CSV.
#
jq -r --arg project "$PROJECT" '
[
"Severity",
"Type",
"Status",
"Rule",
"File",
"Line",
"Message",
"Effort",
"Assignee",
"Author",
"Created",
"Updated"
],
(.[] | [
.severity,
.type,
.status,
.rule,
(.component | sub("^" + $project + ":";"")),
(.line // ""),
.message,
(.effort // ""),
(.assignee // ""),
(.author // ""),
.creationDate,
.updateDate
]) | @csv
' "$TMP" > "$OUTPUT"
ISSUE_COUNT=$(($(wc -l < "$OUTPUT") - 1))
echo
echo "Complete."
echo "Report: $OUTPUT"
echo "Issues exported: $ISSUE_COUNT"