From 7a5fffe8e523c40e0c740b6bc2712ab503e52efa Mon Sep 17 00:00:00 2001 From: Amaury Wyart Date: Tue, 4 Aug 2026 10:49:44 +0200 Subject: [PATCH] =?UTF-8?q?SQQGGHA-12:=20surface=20SonarQube=20error=20mes?= =?UTF-8?q?sage=20on=20background=20task=20=E2=80=A6=20(#71)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 Co-authored-by: Amaury Wyart <285676107+amaurywyart-sq@users.noreply.github.com> --- script/check-quality-gate.sh | 9 ++++++++ script/common.sh | 12 +++++----- test/check-quality-gate-test.bats | 37 +++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/script/check-quality-gate.sh b/script/check-quality-gate.sh index 5c51963..1576db0 100755 --- a/script/check-quality-gate.sh +++ b/script/check-quality-gate.sh @@ -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')" diff --git a/script/common.sh b/script/common.sh index b890acf..794358f 100755 --- a/script/common.sh +++ b/script/common.sh @@ -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}"; } diff --git a/test/check-quality-gate-test.bats b/test/check-quality-gate-test.bats index 49bf4f3..4e44dff 100755 --- a/test/check-quality-gate-test.bats +++ b/test/check-quality-gate-test.bats @@ -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