From a6dadccf839f147a8896910ae1d7d681bbbd1fb5 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Mon, 10 Aug 2026 13:55:10 +0200 Subject: sonarqube.sh: Neue Struktur --- nvim/scripts/sonarqube.sh | 85 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 66 insertions(+), 19 deletions(-) mode change 100755 => 100644 nvim/scripts/sonarqube.sh (limited to 'nvim/scripts') diff --git a/nvim/scripts/sonarqube.sh b/nvim/scripts/sonarqube.sh old mode 100755 new mode 100644 index ee2148e..0f64524 --- a/nvim/scripts/sonarqube.sh +++ b/nvim/scripts/sonarqube.sh @@ -2,31 +2,78 @@ set -eu -errexit() { - echo "$@" >&2 - exit "${2:-1}" +die() { + printf 'Fehler: %s\n' "$*" >&2 + exit 1 } -command -v curl >/dev/null || errexit "curl nicht installiert" -command -v jq >/dev/null || errexit "jq nicht installiert" +have() { + command -v "$1" >/dev/null 2>&1 +} + +check_prerequisites() { + if ! have curl; then + die "curl ist nicht installiert." + fi + + if ! have jq; then + die "jq ist nicht installiert." + fi +} -SONAR_CONFIGURATION_FILE="/opt/homebrew/etc/sonar-scanner.properties" -[ -f "$SONAR_CONFIGURATION_FILE" ] || errexit "Keine globale sonarcube Konfiguration gefunden!" +get_property() { + key="$1" + file="$2" -PROJECT_FILE=$(find . -maxdepth 1 -name "sonar-project*.properties" -print -quit) -[ -n "$PROJECT_FILE" ] || errexit "Keine sonar-project*.properties Datei gefunden!" + awk -F= -v key="$key" '$1 == key { gsub(/^[ \t]+|[ \t]+$/, "", $2); print $2; exit }' "$file" +} + +find_project_file() { + find . -maxdepth 1 -name "sonar-project*.properties" -print -quit +} -SONAR_TOKEN=$(awk -F= '/^sonar.token=/ {gsub(/^[ \t]+|[ \t]+$/,"",$2); print $2}' "$SONAR_CONFIGURATION_FILE") -[ -n "$SONAR_TOKEN" ] || errexit "Kein 'sonar.token' in $SONAR_CONFIGURATION_FILE vorhanden" +load_sonar_config() { + config_file="/opt/homebrew/etc/sonar-scanner.properties" -SONAR_HOST_URL=$(awk -F= '/^sonar.host.url=/ {gsub(/^[ \t]+|[ \t]+$/,"",$2); print $2}' "$SONAR_CONFIGURATION_FILE") -[ -n "$SONAR_HOST_URL" ] || errexit "Kein 'sonar.host.url' in $SONAR_CONFIGURATION_FILE vorhanden" + if [ ! -f "$config_file" ]; then + die "Keine globale Sonarqube-Konfiguration gefunden: $config_file" + fi -PROJECT_KEY=$(awk -F= '/^sonar.projectKey=/ {gsub(/^[ \t]+|[ \t]+$/,"",$2); print $2}' "$PROJECT_FILE") -[ -n "$PROJECT_KEY" ] || errexit "Kein 'sonar.projectKey' in $PROJECT_FILE vorhanden" + sonar_token=$(get_property "sonar.token" "$config_file") + if [ -z "$sonar_token" ]; then + die "Kein 'sonar.token' in $config_file vorhanden." + fi -curl -s \ - -u "$SONAR_TOKEN:" \ - "$SONAR_HOST_URL/api/issues/search?componentKeys=$PROJECT_KEY&statuses=OPEN&ps=500" | - jq -r '.issues[] | "\(.component | sub("^[^:]+:"; "")):\(.line // 1): [\(.severity)] \(.message | gsub("\n"; " ")) (\(.author))"' + sonar_host_url=$(get_property "sonar.host.url" "$config_file") + if [ -z "$sonar_host_url" ]; then + die "Kein 'sonar.host.url' in $config_file vorhanden." + fi +} + +load_project_key() { + project_file=$(find_project_file) + if [ -z "$project_file" ]; then + die "Keine sonar-project*.properties Datei gefunden." + fi + + project_key=$(get_property "sonar.projectKey" "$project_file") + if [ -z "$project_key" ]; then + die "Kein 'sonar.projectKey' in $project_file vorhanden." + fi +} + +fetch_issues() { + curl -s \ + -u "$sonar_token:" \ + "$sonar_host_url/api/issues/search?componentKeys=$project_key&statuses=OPEN&ps=500" | + jq -r '.issues[] | "\(.component | sub("^[^:]+:"; "")):\(.line // 1): [\(.severity)] \(.message | gsub("\n"; " ")) (\(.author))"' +} + +main() { + check_prerequisites + load_sonar_config + load_project_key + fetch_issues +} +main "$@" -- cgit v1.3