@@ -79,21 +79,42 @@ jobs:
7979 SONAR_TOKEN : ${{ secrets.SONAR_TOKEN }}
8080 SONAR_HOST : https://sonarcloud.io
8181 QG_STATUS : ${{ steps.sonar-qg.outputs.quality-gate-status }}
82+ PR_NUMBER : ${{ github.event.pull_request.number }}
8283 run : |
8384 set +e
8485
8586 PROJECT_KEY=$(grep -E '^\s*sonar.projectKey\s*=' project/sonar-project.properties \
8687 | head -1 | cut -d'=' -f2 | tr -d ' \r\n')
8788
89+ # PR-Kontext erkennen: dann Daten der PR-Analyse holen, nicht der Main-Branch
90+ PR_PARAM=""
91+ if [ -n "${PR_NUMBER}" ]; then
92+ PR_PARAM="&pullRequest=${PR_NUMBER}"
93+ echo "PR-Modus aktiv – Daten werden für PR #${PR_NUMBER} geholt"
94+ else
95+ echo "Branch-Modus aktiv – Daten werden für den Default-Branch geholt"
96+ fi
97+
8898 METRICS="alert_status,bugs,vulnerabilities,code_smells,coverage,line_coverage,branch_coverage,duplicated_lines_density,duplicated_blocks,duplicated_files,complexity,cognitive_complexity,ncloc,sqale_rating,reliability_rating,security_rating,sqale_index"
8999
90- API_URL="${SONAR_HOST}/api/measures/component?component=${PROJECT_KEY}&metricKeys=${METRICS}"
100+ API_URL="${SONAR_HOST}/api/measures/component?component=${PROJECT_KEY}&metricKeys=${METRICS}${PR_PARAM} "
91101 HTTP_CODE=$(curl -s -o /tmp/sonar.json -w "%{http_code}" \
92102 -u "${SONAR_TOKEN}:" "${API_URL}")
93103 RESP=$(cat /tmp/sonar.json)
94104
95105 HAS_MEASURES=$(echo "${RESP}" | jq -r 'if (.component? and .component.measures?) then "yes" else "no" end' 2>/dev/null)
96106
107+ # DIAGNOSE – wir wollen sehen, was der Script konkret sieht
108+ echo "===== DEBUG ====="
109+ echo "PROJECT_KEY = '${PROJECT_KEY}'"
110+ echo "API_URL = ${API_URL}"
111+ echo "HTTP_CODE = ${HTTP_CODE}"
112+ echo "HAS_MEASURES= ${HAS_MEASURES}"
113+ echo "RESP first 800 chars:"
114+ echo "${RESP}" | head -c 800
115+ echo ""
116+ echo "===== /DEBUG ====="
117+
97118 SUMMARY_FILE="${GITHUB_STEP_SUMMARY}"
98119
99120 if [ "${HTTP_CODE}" != "200" ] || [ "${HAS_MEASURES}" != "yes" ]; then
@@ -117,11 +138,14 @@ jobs:
117138 }
118139
119140 get_sub() {
120- local sub_resp
121- sub_resp=$(curl -s -u "${SONAR_TOKEN}:" -G \
122- --data-urlencode "component=${PROJECT_KEY}:$1" \
123- --data-urlencode "metricKeys=$2" \
124- "${SONAR_HOST}/api/measures/component")
141+ local sub_resp curl_args
142+ curl_args=(-s -u "${SONAR_TOKEN}:" -G
143+ --data-urlencode "component=${PROJECT_KEY}:$1"
144+ --data-urlencode "metricKeys=$2")
145+ if [ -n "${PR_NUMBER}" ]; then
146+ curl_args+=(--data-urlencode "pullRequest=${PR_NUMBER}")
147+ fi
148+ sub_resp=$(curl "${curl_args[@]}" "${SONAR_HOST}/api/measures/component")
125149 echo "${sub_resp}" | jq -r --arg k "$2" \
126150 'if (.component? and .component.measures?) then
127151 ((.component.measures[] | select(.metric==$k) | .value) // "-")
@@ -155,6 +179,17 @@ jobs:
155179 echo "**Quality Gate:** ${QG_EMOJI} **${QG_ICON}**"
156180 echo "**Dashboard:** [${SONAR_HOST}/project/overview?id=${PROJECT_KEY}](${SONAR_HOST}/project/overview?id=${PROJECT_KEY})"
157181 echo ""
182+ echo "<details><summary>Debug-Info (kann später raus)</summary>"
183+ echo ""
184+ echo "- PROJECT_KEY: ${PROJECT_KEY}"
185+ echo "- HTTP_CODE: ${HTTP_CODE}"
186+ echo "- HAS_MEASURES: ${HAS_MEASURES}"
187+ echo "- coverage value direkt aus get: '$(get coverage)'"
188+ echo "- ncloc direkt aus get: '$(get ncloc)'"
189+ echo "- duplicated_lines_density direkt aus get: '$(get duplicated_lines_density)'"
190+ echo ""
191+ echo "</details>"
192+ echo ""
158193 echo "### Coverage"
159194 echo ""
160195 echo "| Komponente | Coverage | Line Coverage | Branch Coverage | LoC |"
@@ -178,7 +213,7 @@ jobs:
178213 echo ""
179214 } >> "$SUMMARY_FILE"
180215
181- TREE_URL="${SONAR_HOST}/api/measures/component_tree?component=${PROJECT_KEY}&metricKeys=duplicated_blocks,duplicated_lines,duplicated_lines_density,ncloc&qualifiers=FIL&ps=500&s=metric&metricSort=duplicated_blocks&asc=false"
216+ TREE_URL="${SONAR_HOST}/api/measures/component_tree?component=${PROJECT_KEY}&metricKeys=duplicated_blocks,duplicated_lines,duplicated_lines_density,ncloc&qualifiers=FIL&ps=500&s=metric&metricSort=duplicated_blocks&asc=false${PR_PARAM} "
182217 TREE_RESP=$(curl -s -u "${SONAR_TOKEN}:" "${TREE_URL}")
183218
184219 FILES_WITH_DUPS=$(echo "${TREE_RESP}" | jq '[.components[]? | select((.measures[]? | select(.metric=="duplicated_blocks") | .value | tonumber) > 0)] | length' 2>/dev/null || echo "0")
0 commit comments