diff --git a/scan.sh b/scan.sh index 464d68a2..13080e12 100755 --- a/scan.sh +++ b/scan.sh @@ -1,6 +1,101 @@ +#!/bin/bash +set -euo pipefail + +SONAR_URL="http://192.168.1.2:9001" +PROJECT="Warehouse-Management-System" +OUTPUT="sonarqube-issues.csv" +TMP=$(mktemp) + +trap 'rm -f "$TMP" "$TMP.new"' EXIT + +if [ -z "${SONAR_TOKEN:-}" ]; then + echo "ERROR: SONAR_TOKEN is not set" + exit 1 +fi + +# Delete previous report FIRST +echo "Removing previous SonarQube report..." +rm -f "$OUTPUT" + +echo +echo "Running SonarQube scan..." + JAVA_HOME="/opt/homebrew/opt/openjdk" \ PATH="/opt/homebrew/opt/openjdk/bin:$PATH" \ sonar-scanner \ - -Dsonar.projectKey=Warehouse-Management-System \ - -Dsonar.sources=. \ - -Dsonar.host.url=http://192.168.1.2:9001 + -Dsonar.projectKey="$PROJECT" \ + -Dsonar.sources=. \ + -Dsonar.host.url="$SONAR_URL" \ + -Dsonar.token="$SONAR_TOKEN" + +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 + +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" diff --git a/sonar-export.sh b/sonar-export.sh deleted file mode 100755 index 32192858..00000000 --- a/sonar-export.sh +++ /dev/null @@ -1,77 +0,0 @@ -#!/bin/bash -set -euo pipefail - -SONAR_URL="http://192.168.1.2:9001" -PROJECT="Warehouse-Management-System" -OUTPUT="sonarqube-issues.csv" -TMP=$(mktemp) - -if [ -z "${SONAR_TOKEN:-}" ]; then - echo "SONAR_TOKEN is not set" - exit 1 -fi - -echo "Downloading SonarQube issues for $PROJECT..." - -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" <(echo "$RESPONSE") > "$TMP.new" - mv "$TMP.new" "$TMP" - - [ "$COUNT" -lt "$PAGE_SIZE" ] && break - - PAGE=$((PAGE + 1)) -done - -jq -r ' -[ - "Severity", - "Type", - "Status", - "Rule", - "File", - "Line", - "Message", - "Effort", - "Assignee", - "Author", - "Created", - "Updated" -], -(.[] | [ - .severity, - .type, - .status, - .rule, - (.component | sub("^Warehouse-Management-System:";"")), - (.line // ""), - .message, - (.effort // ""), - (.assignee // ""), - (.author // ""), - .creationDate, - .updateDate -]) | @csv -' "$TMP" > "$OUTPUT" - -rm "$TMP" - -echo -echo "Complete." -echo "Report: $OUTPUT" -echo "Issues exported: $(($(wc -l < "$OUTPUT") - 1))"