aboutsummaryrefslogtreecommitdiff
path: root/nvim/scripts/sonarqube.sh
blob: 0f6452487ddb930e5f426afe125b1f6c74f04fdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/sh

set -eu

die() {
  printf 'Fehler: %s\n' "$*" >&2
  exit 1
}

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
}

get_property() {
  key="$1"
  file="$2"

  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
}

load_sonar_config() {
  config_file="/opt/homebrew/etc/sonar-scanner.properties"

  if [ ! -f "$config_file" ]; then
    die "Keine globale Sonarqube-Konfiguration gefunden: $config_file"
  fi

  sonar_token=$(get_property "sonar.token" "$config_file")
  if [ -z "$sonar_token" ]; then
    die "Kein 'sonar.token' in $config_file vorhanden."
  fi

  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 "$@"