mirror of
https://github.com/SonarSource/sonarqube-quality-gate-action.git
synced 2026-09-23 13:38:32 +00:00
SQQGGHA-12: surface SonarQube error message on background task … (#71)
* feat(SQQGGHA-12): surface SonarQube error message on background task failure Report the analysis error message when the background task status is not SUCCESS, instead of only failing at the quality gate lookup step. * fix: surface CANCELED SonarQube task status with clear message Co-authored-by: Amaury Wyart <285676107+amaurywyart-sq@users.noreply.github.com> * fix: use octal ANSI escape codes for bash 3.2 compatibility \e is not interpreted by echo -e on bash 3.2 (e.g. macOS/self-hosted runners), which printed raw escape sequences instead of colored output. \033 works on both bash 3.2 and modern bash. --------- Co-authored-by: Gitar <noreply@gitar.ai> Co-authored-by: Amaury Wyart <285676107+amaurywyart-sq@users.noreply.github.com>
This commit is contained in:
co-authored by
Gitar
Amaury Wyart
parent
8e9b0ca0a7
commit
7a5fffe8e5
@@ -59,6 +59,15 @@ if [[ ${status} == "PENDING" || ${status} == "IN_PROGRESS" ]] && [[ ${SECONDS} -
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ ${status} == "CANCELED" ]]; then
|
||||
fail "The SonarQube background task was CANCELED."
|
||||
fi
|
||||
|
||||
if [[ ${status} == "FAILED" ]]; then
|
||||
errorMessage="$(jq -r '.task.errorMessage // "No error message provided."' <<< "${task}")"
|
||||
fail "The SonarQube background task ${status}.${reset}\n\n${errorMessage}"
|
||||
fi
|
||||
|
||||
analysisId="$(jq -r '.task.analysisId' <<< "${task}")"
|
||||
qualityGateUrl="${serverUrl}/api/qualitygates/project_status?analysisId=${analysisId}"
|
||||
qualityGateStatus="$(curl --location --location-trusted --max-redirs 10 --silent --fail --show-error --user "${SONAR_TOKEN}": "${qualityGateUrl}" | jq -r '.projectStatus.status')"
|
||||
|
||||
+6
-6
@@ -4,12 +4,12 @@
|
||||
set -e
|
||||
set -o pipefail
|
||||
|
||||
gray="\\e[37m"
|
||||
blue="\\e[36m"
|
||||
red="\\e[31m"
|
||||
yellow="\\e[33m"
|
||||
green="\\e[32m"
|
||||
reset="\\e[0m"
|
||||
gray="\\033[37m"
|
||||
blue="\\033[36m"
|
||||
red="\\033[31m"
|
||||
yellow="\\033[33m"
|
||||
green="\\033[32m"
|
||||
reset="\\033[0m"
|
||||
|
||||
info() { echo -e "${blue}INFO: $*${reset}"; }
|
||||
error() { echo -e "${red}ERROR: $*${reset}"; }
|
||||
|
||||
@@ -105,6 +105,43 @@ teardown() {
|
||||
[[ "$output" = *"Quality Gate not set for the project. Please configure the Quality Gate in SonarQube or remove sonarqube-quality-gate action from the workflow."* ]]
|
||||
}
|
||||
|
||||
@test "fail when Sonar background task failed with an error message" {
|
||||
export SONAR_TOKEN="test"
|
||||
echo "serverUrl=http://localhost:9000" >> metadata_tmp
|
||||
echo "ceTaskUrl=http://localhost:9000/api/ce/task?id=AXlCe3jz9LkwR9Gs0pBY" >> metadata_tmp
|
||||
|
||||
#mock curl
|
||||
function curl() {
|
||||
echo '{"task":{"status":"FAILED","errorMessage":"Fact of life: analysis cannot be processed"}}'
|
||||
}
|
||||
export -f curl
|
||||
|
||||
run script/check-quality-gate.sh metadata_tmp 300
|
||||
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" = *"The SonarQube background task FAILED."* ]]
|
||||
[[ "$output" = *"Fact of life: analysis cannot be processed"* ]]
|
||||
}
|
||||
|
||||
@test "fail when Sonar background task canceled" {
|
||||
export SONAR_TOKEN="test"
|
||||
echo "serverUrl=http://localhost:9000" >> metadata_tmp
|
||||
echo "ceTaskUrl=http://localhost:9000/api/ce/task?id=AXlCe3jz9LkwR9Gs0pBY" >> metadata_tmp
|
||||
|
||||
#mock curl
|
||||
function curl() {
|
||||
echo '{"task":{"status":"CANCELED","errorMessage":"Analysis was canceled"}}'
|
||||
}
|
||||
export -f curl
|
||||
|
||||
run script/check-quality-gate.sh metadata_tmp 300
|
||||
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" = *"The SonarQube background task was CANCELED."* ]]
|
||||
[[ "$output" != *"Analysis was canceled"* ]]
|
||||
[[ "$output" != *"No error message provided."* ]]
|
||||
}
|
||||
|
||||
@test "fail when polling timeout is reached" {
|
||||
export SONAR_TOKEN="test"
|
||||
echo "serverUrl=http://localhost:9000" >> metadata_tmp
|
||||
|
||||
Reference in New Issue
Block a user